diff options
author | Alan Coopersmith <alan.coopersmith@oracle.com> | 2011-11-11 21:07:30 -0800 |
---|---|---|
committer | Alan Coopersmith <alan.coopersmith@oracle.com> | 2011-11-16 22:58:49 -0800 |
commit | d0dfd2128e6111b967c51136d97093d3b53ee6d6 (patch) | |
tree | 63c7c0abdb2a5d0fa52013abe9dee3f710839c73 /xmodmap.c | |
parent | fecb1f5a0c2121de6d9209fc9525ac7d9b9555ea (diff) |
Replace chk_malloc + sprintf with asprintf
Includes local private copy of asprintf for OS'es without it in libc.
Removes chk_malloc as no callers remain anymore.
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Reviewed-by: Jeremy Huddleston <jeremyhu@apple.com>
Diffstat (limited to 'xmodmap.c')
-rw-r--r-- | xmodmap.c | 69 |
1 files changed, 54 insertions, 15 deletions
@@ -31,6 +31,7 @@ from The Open Group. #include <stdio.h> #include <stdlib.h> #include <ctype.h> +#include <stdarg.h> #include "xmodmap.h" const char *ProgramName; @@ -50,16 +51,54 @@ Exit(int status) exit (status); } -void * -chk_malloc(size_t n_bytes) +static void _X_NORETURN +FatalError(const char *message) { - void *buf = malloc(n_bytes); - if (!buf) { - fprintf(stderr, "%s: Could not allocate %d bytes\n", ProgramName, (int)n_bytes); - Exit(-1); + fprintf(stderr, "%s: %s\n", ProgramName, message); + Exit(-1); +} + +#ifndef HAVE_ASPRINTF +/* sprintf variant found in newer libc's which allocates string to print to */ +static int _X_ATTRIBUTE_PRINTF(2,3) +asprintf(char ** ret, const char *format, ...) +{ + char buf[256]; + int len; + va_list ap; + + va_start(ap, format); + len = vsnprintf(buf, sizeof(buf), format, ap); + va_end(ap); + + if (len < 0) + return -1; + + if (len < sizeof(buf)) + { + *ret = strdup(buf); } - return buf; + else + { + *ret = malloc(len + 1); /* snprintf doesn't count trailing '\0' */ + if (*ret != NULL) + { + va_start(ap, format); + len = vsnprintf(*ret, len + 1, format, ap); + va_end(ap); + if (len < 0) { + free(*ret); + *ret = NULL; + } + } + } + + if (*ret == NULL) + return -1; + + return len; } +#endif /* HAVE_ASPRINTF */ static const char help_message[] = "\nwhere options include:\n" @@ -247,11 +286,11 @@ main(int argc, char *argv[]) char *cmd; didAnything = True; if (++i >= argc) usage (); - cmd = chk_malloc (strlen ("remove control = ") + strlen (argv[i]) + 1); - (void) sprintf (cmd, "remove %s = %s", + if (asprintf (&cmd, "remove %s = %s", ((arg[1] == 's') ? "shift" : ((arg[1] == 'l') ? "lock" : - "control")), argv[i]); + "control")), argv[i]) == -1) + FatalError("Could not allocate memory for remove cmd"); process_line (cmd); continue; } @@ -269,8 +308,8 @@ main(int argc, char *argv[]) char *cmd; didAnything = True; if (++i >= argc) usage (); - cmd = chk_malloc (strlen ("add modX = ") + strlen (argv[i]) + 1); - (void) sprintf (cmd, "add mod%c = %s", arg[1], argv[i]); + if (asprintf (&cmd, "add mod%c = %s", arg[1], argv[i]) == -1) + FatalError("Could not allocate memory for add cmd"); process_line (cmd); continue; } @@ -285,11 +324,11 @@ main(int argc, char *argv[]) char *cmd; didAnything = True; if (++i >= argc) usage (); - cmd = chk_malloc (strlen ("add control = ") + strlen (argv[i]) + 1); - (void) sprintf (cmd, "add %s = %s", + if (asprintf (&cmd, "add %s = %s", ((arg[1] == 's') ? "shift" : ((arg[1] == 'l') ? "lock" : - "control")), argv[i]); + "control")), argv[i]) == -1) + FatalError("Could not allocate memory for remove cmd"); process_line (cmd); continue; } |