diff options
author | Jérémie Courrèges-Anglas <jca@wxcvbn.org> | 2017-09-17 19:32:29 +0200 |
---|---|---|
committer | Jérémie Courrèges-Anglas <jca@wxcvbn.org> | 2017-09-17 19:46:06 +0200 |
commit | 218e68c7327c94bd90b2c5b708203be84413db18 (patch) | |
tree | f531af4e1dae664a09c319da61fed07920ab3c8d /src/globals.c | |
parent | 9b7cfb7c8d8d337554ad7c5995ce75fac52c7d54 (diff) | |
download | ratpoison-218e68c7327c94bd90b2c5b708203be84413db18.zip |
Move fatal() and x* helpers to their own file
Should help use them from within tests.
Diffstat (limited to 'src/globals.c')
-rw-r--r-- | src/globals.c | 143 |
1 files changed, 0 insertions, 143 deletions
diff --git a/src/globals.c b/src/globals.c index 5f4350d..55f97b9 100644 --- a/src/globals.c +++ b/src/globals.c @@ -362,149 +362,6 @@ rp_text_width (rp_screen *s, char *string, int count) #endif } -void -fatal (const char *msg) -{ - fprintf (stderr, "ratpoison: %s", msg); - abort (); -} - -void * -xmalloc (size_t size) -{ - void *value; - - value = malloc (size); - if (value == NULL) - fatal ("Virtual memory exhausted"); - return value; -} - -void * -xrealloc (void *ptr, size_t size) -{ - void *value; - - value = realloc (ptr, size); - if (value == NULL) - fatal ("Virtual memory exhausted"); - return value; -} - -char * -xstrdup (const char *s) -{ - char *value; - value = strdup (s); - if (value == NULL) - fatal ("Virtual memory exhausted"); - return value; -} - -/* Return a new string based on fmt. */ -char * -xvsprintf (char *fmt, va_list ap) -{ - int size, nchars; - char *buffer; - va_list ap_copy; - - /* A reasonable starting value. */ - size = strlen (fmt) + 1; - buffer = xmalloc (size); - - while (1) - { -#if defined(va_copy) - va_copy (ap_copy, ap); -#elif defined(__va_copy) - __va_copy (ap_copy, ap); -#else - /* If there is no copy macro then this MAY work. On some systems - this could fail because va_list is a pointer so assigning one - to the other as below wouldn't make a copy of the data, but - just the pointer to the data. */ - ap_copy = ap; -#endif - nchars = vsnprintf (buffer, size, fmt, ap_copy); -#if defined(va_copy) || defined(__va_copy) - va_end (ap_copy); -#endif - - if (nchars > -1 && nchars < size) - return buffer; - else if (nchars > -1) - size = nchars + 1; - /* c99 says -1 is an error other than truncation, - * which thus will not go away with a larger buffer. - * To support older system but not making errors fatal - * (ratpoison will abort when trying to get too much memory otherwise), - * try to increase a bit but not too much: */ - else if (size < MAX_LEGACY_SNPRINTF_SIZE) - size *= 2; - else - { - free(buffer); - break; - } - - /* Resize the buffer and try again. */ - buffer = xrealloc (buffer, size); - } - - return xstrdup("<FAILURE>"); -} - -/* Return a new string based on fmt. */ -char * -xsprintf (char *fmt, ...) -{ - char *buffer; - va_list ap; - - va_start (ap, fmt); - buffer = xvsprintf (fmt, ap); - va_end (ap); - - return buffer; -} - -/* strtok but do it for whitespace and be locale compliant. */ -char * -strtok_ws (char *s) -{ - char *nonws; - static char *last = NULL; - - if (s != NULL) - last = s; - else if (last == NULL) - { - PRINT_ERROR (("strtok_ws() called but not initalized, this is a *BUG*\n")); - abort(); - } - - /* skip to first non-whitespace char. */ - while (*last && isspace ((unsigned char)*last)) - last++; - - /* If we reached the end of the string here then there is no more - data. */ - if (*last == '\0') - return NULL; - - /* Now skip to the end of the data. */ - nonws = last; - while (*last && !isspace ((unsigned char)*last)) - last++; - if (*last) - { - *last = '\0'; - last++; - } - return nonws; -} - /* A case insensitive strncmp. */ int str_comp (char *s1, char *s2, size_t len) |