[PATCH:smproxy 3/7] Add local copy of asprintf()

Alan Coopersmith alan.coopersmith at oracle.com
Sun Nov 24 09:35:23 PST 2013


Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 configure.ac |    7 ++++++-
 save.c       |   45 +++++++++++++++++++++++++++++++++++++++++++++
 smproxy.h    |    5 +++++
 3 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index ea49c18..26bd5f4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -31,13 +31,18 @@ AC_CONFIG_HEADERS([config.h])
 AM_INIT_AUTOMAKE([foreign dist-bzip2])
 AM_MAINTAINER_MODE
 
+# Set common system defines for POSIX extensions, such as _GNU_SOURCE
+# Must be called before any macros that run the compiler (like
+# AC_PROG_LIBTOOL or XORG_DEFAULT_OPTIONS) to avoid autoconf errors.
+AC_USE_SYSTEM_EXTENSIONS
+
 # Require X.Org macros 1.8 or later for MAN_SUBSTS set by XORG_MANPAGE_SECTIONS
 m4_ifndef([XORG_MACROS_VERSION],
 	  [m4_fatal([must install xorg-macros 1.8 or later before running autoconf/autogen])])
 XORG_MACROS_VERSION(1.8)
 XORG_DEFAULT_OPTIONS
 
-AC_CHECK_FUNCS([mkstemp mktemp])
+AC_CHECK_FUNCS([asprintf mkstemp mktemp])
 
 # Checks for pkg-config packages
 PKG_CHECK_MODULES(SMPROXY, [sm ice xt xmuu])
diff --git a/save.c b/save.c
index 5c2a750..5d6b4ce 100644
--- a/save.c
+++ b/save.c
@@ -45,6 +45,51 @@ static char * unique_filename ( char *path, char *prefix );
 static char * unique_filename ( char *path, char *prefix, int *pFd );
 #endif
 
+#ifndef HAVE_ASPRINTF
+# include <stdarg.h>
+
+/* sprintf variant found in newer libc's which allocates string to print to */
+_X_HIDDEN 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);
+    }
+    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
+
+
 
 static int
 write_byte (FILE *file, unsigned char b)
diff --git a/smproxy.h b/smproxy.h
index f40532b..8d39f86 100644
--- a/smproxy.h
+++ b/smproxy.h
@@ -107,3 +107,8 @@ extern char * LookupClientID ( WinInfo *theWindow );
 extern WinInfo *win_head;
 
 #define SAVEFILE_VERSION 1
+
+#ifndef HAVE_ASPRINTF
+_X_HIDDEN int _X_ATTRIBUTE_PRINTF(2,3) asprintf(char ** ret,
+                                                const char *format, ...);
+#endif
-- 
1.7.9.2



More information about the xorg-devel mailing list