diff options
author | Alan Coopersmith <alan.coopersmith@oracle.com> | 2014-05-27 20:55:33 -0700 |
---|---|---|
committer | Alan Coopersmith <alan.coopersmith@oracle.com> | 2014-05-27 20:55:37 -0700 |
commit | bfb6044479e8e974e848f47969f75b84865fea13 (patch) | |
tree | d8631e32a64744389b9c6e2291ff901f771c0745 /utils.c | |
parent | 7848ba1d292f55553058c1f17596baa1094cdae5 (diff) |
Replace several malloc+sprintf pairs with asprintf() calls
Includes fallback uAsprintf() for systems without asprintf yet
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Diffstat (limited to 'utils.c')
-rw-r--r-- | utils.c | 47 |
1 files changed, 47 insertions, 0 deletions
@@ -27,6 +27,10 @@ \*/ /* $XFree86: xc/programs/xkbprint/utils.c,v 3.4 2001/01/17 23:46:11 dawes Exp $ */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include "utils.h" #include <ctype.h> #include <stdlib.h> @@ -144,3 +148,46 @@ uInternalError(const char *s, ...) va_end(ap); return; } + +/***====================================================================***/ + +#ifndef HAVE_ASPRINTF +int +uAsprintf(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); + } + 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 */ |