diff options
-rw-r--r-- | configure.ac | 4 | ||||
-rw-r--r-- | psgeom.c | 12 | ||||
-rw-r--r-- | utils.c | 47 | ||||
-rw-r--r-- | utils.h | 6 | ||||
-rw-r--r-- | xkbprint.c | 7 |
5 files changed, 68 insertions, 8 deletions
diff --git a/configure.ac b/configure.ac index 66db16a..c1ad70f 100644 --- a/configure.ac +++ b/configure.ac @@ -27,6 +27,8 @@ AC_INIT([xkbprint], [1.0.3], [https://bugs.freedesktop.org/enter_bug.cgi?product=xorg], [xkbprint]) AC_CONFIG_SRCDIR([Makefile.am]) AC_CONFIG_HEADERS([config.h]) +# Needed for asprintf on GNU libc +AC_USE_SYSTEM_EXTENSIONS # Initialize Automake AM_INIT_AUTOMAKE([foreign dist-bzip2]) @@ -38,6 +40,8 @@ m4_ifndef([XORG_MACROS_VERSION], XORG_MACROS_VERSION(1.8) XORG_DEFAULT_OPTIONS +AC_CHECK_FUNCS([asprintf]) + # Checks for pkg-config packages PKG_CHECK_MODULES(XKBPRINT, [xkbfile x11 xproto >= 7.0.17]) @@ -26,6 +26,10 @@ ********************************************************/ /* $XFree86: xc/programs/xkbprint/psgeom.c,v 1.5 2001/07/25 15:05:25 dawes Exp $ */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #define XK_TECHNICAL #define XK_PUBLISHING #define XK_KATAKANA @@ -869,11 +873,9 @@ PSPageTrailer(FILE *out, PSState *state) if (sName == NULL) sName = "(unknown)"; - lbuf = malloc(10 + strlen(sName)); - if (!lbuf) { + if (asprintf(&lbuf, "Layout: %s", sName) == -1) { uFatalError("Can't allocate memory for string\n"); } - sprintf(lbuf, "Layout: %s", sName); fprintf(out, "kbx kbdscalewidth 0 (%s) centeroffset pop add\n", lbuf); fprintf(out, " kby kbdscaleheight add %d add\n", baseline); @@ -904,11 +906,9 @@ PSPageTrailer(FILE *out, PSState *state) if (sName == NULL) sName = "(unknown)"; - lbuf = malloc(12 + strlen(sName)); - if (!lbuf) { + if (asprintf(&lbuf, "Keycodes: %s", sName) == -1) { uFatalError("Can't allocate memory for string\n"); } - sprintf(lbuf, "Keycodes: %s", sName); fprintf(out, "kbx kbdscalewidth 0 (%s) centeroffset pop add\n", lbuf); fprintf(out, " kby kbdscaleheight add %d add\n", baseline); @@ -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 */ @@ -95,6 +95,12 @@ extern void uInternalError(const char *s, ...) _X_ATTRIBUTE_PRINTF(1, 2); #define uStringCompare(s1,s2) (strcmp(s1,s2)) #define uStrCaseEqual(s1,s2) (strcasecmp(s1,s2)==0) +#ifndef HAVE_ASPRINTF +extern _X_HIDDEN int _X_ATTRIBUTE_PRINTF(2,3) + uAsprintf(char ** ret, const char *format, ...); +#define asprintf uAsprintf +#endif + /***====================================================================***/ _XFUNCPROTOEND @@ -26,6 +26,10 @@ ********************************************************/ /* $XFree86: xc/programs/xkbprint/xkbprint.c,v 3.10 2003/05/27 22:27:07 tsi Exp $ */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include <stdio.h> #include <ctype.h> #include <X11/Xlocale.h> @@ -421,8 +425,7 @@ parseArgs(int argc, char *argv[]) FILE *file = NULL; if (outputFile == NULL) { - outputFile = malloc(strlen(outputFont) + 5); - sprintf(outputFile, "%s.pfa", outputFont); + asprintf(&outputFile, "%s.pfa", outputFont); } else if (uStringEqual(outputFile, "-")) file = stdout; |