From 46d33884f2f1061ae1a77c26928f97d573e57c6c Mon Sep 17 00:00:00 2001 From: rcyeske Date: Fri, 2 Mar 2001 06:06:23 +0000 Subject: * ratpoison.h (xmalloc, xrealloc, fatal): Prototype. * main.c (xmalloc): Move here from sbuf.c. (xrealloc): Likewise. (fatal): Likewise. * number.c (find_empty_cell): Use xrealloc, remove error check. (init_numbers): Likewise. * manage.c (get_window_name): Likewise. * main.c (main): Likewise. * list.c (add_to_window_list): Likewise. (add_to_window_list): Likewise. * events.c (handle_key): Likewise. * input.c (keysym_to_string): Likewise. (get_more_input): Use xrealloc, remove error check. * main.c (read_rc_file): Take a file pointer rather than a filename. (read_startup_files): If ~/.ratpoisonrc is not readable try /etc/ratpoisonrc. --- src/main.c | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 802d37f..1ccebe8 100644 --- a/src/main.c +++ b/src/main.c @@ -73,6 +73,32 @@ static struct option ratpoison_longopts[] = { {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; static char ratpoison_opts[] = "hvrk"; +void +fatal (const char *msg) +{ + fprintf (stderr, "ratpoison: %s", msg); + abort (); +} + +void * +xmalloc (size_t size) +{ + register void *value = malloc (size); + if (value == 0) + fatal ("Virtual memory exhausted"); + return value; +} + +void * +xrealloc (void *ptr, size_t size) +{ + register void *value = realloc (ptr, size); + if (value == 0) + fatal ("Virtual memory exhausted"); + PRINT_DEBUG("realloc: %d\n", size); + return value; +} + void sighandler (int signum) { @@ -192,8 +218,8 @@ read_rc_file (FILE *file) char *line; int linesize = n; - partial = (char*)malloc(n); - line = (char*)malloc(linesize); + partial = (char*)xmalloc(n); + line = (char*)xmalloc(linesize); *line = '\0'; while (fgets (partial, n, file) != NULL) @@ -201,7 +227,7 @@ read_rc_file (FILE *file) if ((strlen (line) + strlen (partial)) <= linesize) { linesize *= 2; - line = (char*) realloc (line, linesize); + line = (char*) xrealloc (line, linesize); } strcat (line, partial); @@ -245,7 +271,7 @@ read_startup_files () fprintf (stderr, "ratpoison: $HOME not set!?\n"); else { - char *filename = (char*)malloc (strlen (homedir) + strlen ("/.ratpoisonrc") + 1); + char *filename = (char*)xmalloc (strlen (homedir) + strlen ("/.ratpoisonrc") + 1); sprintf (filename, "%s/.ratpoisonrc", homedir); if ((fileptr = fopen (filename, "r")) == NULL) @@ -359,11 +385,7 @@ main (int argc, char *argv[]) } num_screens = ScreenCount (dpy); - if ((screens = (screen_info *)malloc (sizeof (screen_info) * num_screens)) == NULL) - { - PRINT_ERROR ("Out of memory!\n"); - exit (EXIT_FAILURE); - } + screens = (screen_info *)xmalloc (sizeof (screen_info) * num_screens); PRINT_DEBUG ("%d screens.\n", num_screens); -- cgit v1.2.3