[PATCH:libXt 5/5] makestrs: Use asprintf() if available

Alan Coopersmith alan.coopersmith at oracle.com
Mon Jan 19 10:41:19 PST 2015


Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 configure.ac    |    5 ++++-
 util/makestrs.c |   17 +++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index 34e6aab..3633ec6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -64,7 +64,10 @@ if test x"$CC_FOR_BUILD" = x; then
 	fi
 fi
 AC_SUBST([CC_FOR_BUILD])
-CPPFLAGS_FOR_BUILD=${CPPFLAGS_FOR_BUILD-${CPPFLAGS}}
+DEFAULT_CPPFLAGS_FOR_BUILD="${CPPFLAGS}"
+AC_CHECK_FUNC(asprintf,
+  [DEFAULT_CPPFLAGS_FOR_BUILD="${DEFAULT_CPPFLAGS_FOR_BUILD} -DHAVE_ASPRINTF"])
+CPPFLAGS_FOR_BUILD=${CPPFLAGS_FOR_BUILD-${DEFAULT_CPPFLAGS_FOR_BUILD}}
 AC_SUBST(CPPFLAGS_FOR_BUILD)
 DEFAULT_CFLAGS_FOR_BUILD="${CFLAGS} ${CWARNFLAGS}"
 CFLAGS_FOR_BUILD=${CFLAGS_FOR_BUILD-${DEFAULT_CFLAGS_FOR_BUILD}}
diff --git a/util/makestrs.c b/util/makestrs.c
index f872ec8..5764849 100644
--- a/util/makestrs.c
+++ b/util/makestrs.c
@@ -90,19 +90,26 @@ static int   solaris_abi_names = FALSE;
 static char* includedir = NULL;
 static FILE *ifopen(const char *file, const char *mode)
 {
+#ifndef HAVE_ASPRINTF
     size_t len;
+#endif
     char *buffer;
     FILE *ret;
 
     if (includedir == NULL)
         return fopen(file, mode);
 
+#ifdef HAVE_ASPRINTF
+    if (asprintf(&buffer, "%s/%s", includedir, file) == -1)
+        return NULL;
+#else
     len = strlen(file) + strlen(includedir) + 1;
     buffer = (char*)malloc(len + 1);
     if (buffer == NULL)
         return NULL;
 
     snprintf(buffer, len + 1, "%s/%s", includedir, file);
+#endif
 
     ret = fopen(buffer, mode);
 
@@ -269,13 +276,23 @@ static void WriteHeader (char *tagline, File *phile, int abi)
 
     /* do the right thing for Motif, i.e. avoid _XmXmStrDefs_h_ */
     if (strcmp (prefixstr, "Xm") == 0) {
+#ifdef HAVE_ASPRINTF
+	if (asprintf (&fileprotstr, "_%s_", phile->name) == -1)
+	    exit (1);
+#else
 	if ((fileprotstr = malloc (strlen (phile->name) + 3)) == NULL)
 	   exit (1);
 	(void) sprintf (fileprotstr, "_%s_", phile->name);
+#endif
     } else {
+#ifdef HAVE_ASPRINTF
+	if (asprintf (&fileprotstr, "_%s%s_", prefixstr, phile->name) == -1)
+	    exit (1);
+#else
 	if ((fileprotstr = malloc (strlen (phile->name) + strlen (prefixstr) +  3)) == NULL)
 	   exit (1);
 	(void) sprintf (fileprotstr, "_%s%s_", prefixstr, phile->name);
+#endif
     }
 
     for (tmp = fileprotstr; *tmp; tmp++) if (*tmp == '.') *tmp = '_';
-- 
1.7.9.2



More information about the xorg-devel mailing list