<div dir="ltr">I didn't know asprintf was standardized. Nice to hear it!<br><br>Reviewed-by: Jasper St. Pierre <<a href="mailto:jstpierre@mecheye.net">jstpierre@mecheye.net</a>><br></div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Wed, Feb 19, 2014 at 1:23 AM, Alan Coopersmith <span dir="ltr"><<a href="mailto:alan.coopersmith@oracle.com" target="_blank">alan.coopersmith@oracle.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Includes fallback asprintf() for pre-Unix08 platforms<br>
<br>
Signed-off-by: Alan Coopersmith <<a href="mailto:alan.coopersmith@oracle.com">alan.coopersmith@oracle.com</a>><br>
---<br>
<a href="http://configure.ac" target="_blank">configure.ac</a> | 7 +++++-<br>
mkcomposecache.c | 66 +++++++++++++++++++++++++++++++++++++++++++++---------<br>
2 files changed, 61 insertions(+), 12 deletions(-)<br>
<br>
diff --git a/<a href="http://configure.ac" target="_blank">configure.ac</a> b/<a href="http://configure.ac" target="_blank">configure.ac</a><br>
index 727f100..aff0a49 100644<br>
--- a/<a href="http://configure.ac" target="_blank">configure.ac</a><br>
+++ b/<a href="http://configure.ac" target="_blank">configure.ac</a><br>
@@ -8,6 +8,11 @@ AC_CONFIG_HEADERS([config.h])<br>
AM_INIT_AUTOMAKE([foreign dist-bzip2])<br>
AM_MAINTAINER_MODE<br>
<br>
+# Set common system defines for POSIX extensions, such as _GNU_SOURCE<br>
+# Must be called before any macros that run the compiler (like those in<br>
+# XORG_DEFAULT_OPTIONS) to avoid autoconf errors.<br>
+AC_USE_SYSTEM_EXTENSIONS<br>
+<br>
# Require X.Org macros 1.8 or later for MAN_SUBSTS set by XORG_MANPAGE_SECTIONS<br>
m4_ifndef([XORG_MACROS_VERSION],<br>
[m4_fatal([must install xorg-macros 1.8 or later before running autoconf/autogen])])<br>
@@ -18,7 +23,7 @@ PKG_CHECK_MODULES(XLIB, x11)<br>
<br>
AC_CHECK_HEADERS([stdio.h stdlib.h string.h unistd.h locale.h], , [AC_MSG_FAILURE("cannot find essential header")])<br>
AC_CHECK_FUNCS([setlocale], , [AC_MSG_FAILURE("cannot find essential function")])<br>
-AC_CHECK_FUNCS([unsetenv])<br>
+AC_CHECK_FUNCS([unsetenv asprintf])<br>
<br>
AC_CONFIG_FILES([<br>
Makefile<br>
diff --git a/mkcomposecache.c b/mkcomposecache.c<br>
index c65951c..3b94e77 100644<br>
--- a/mkcomposecache.c<br>
+++ b/mkcomposecache.c<br>
@@ -27,11 +27,55 @@<br>
#include <unistd.h><br>
#include <locale.h><br>
<br>
+#ifndef HAVE_ASPRINTF<br>
+#include <stdarg.h><br>
+<br>
+/* sprintf variant found in newer libc's which allocates string to print to */<br>
+static int _X_ATTRIBUTE_PRINTF(2,3)<br>
+asprintf(char ** ret, const char *format, ...)<br>
+{<br>
+ char buf[256];<br>
+ int len;<br>
+ va_list ap;<br>
+<br>
+ va_start(ap, format);<br>
+ len = vsnprintf(buf, sizeof(buf), format, ap);<br>
+ va_end(ap);<br>
+<br>
+ if (len < 0)<br>
+ return -1;<br>
+<br>
+ if (len < sizeof(buf))<br>
+ {<br>
+ *ret = strdup(buf);<br>
+ }<br>
+ else<br>
+ {<br>
+ *ret = malloc(len + 1); /* vsnprintf doesn't count trailing '\0' */<br>
+ if (*ret != NULL)<br>
+ {<br>
+ va_start(ap, format);<br>
+ len = vsnprintf(*ret, len + 1, format, ap);<br>
+ va_end(ap);<br>
+ if (len < 0) {<br>
+ free(*ret);<br>
+ *ret = NULL;<br>
+ }<br>
+ }<br>
+ }<br>
+<br>
+ if (*ret == NULL)<br>
+ return -1;<br>
+<br>
+ return len;<br>
+}<br>
+#endif /* HAVE_ASPRINTF */<br>
+<br>
int main (int argc, char *argv[]) {<br>
Display *disp;<br>
XIM im;<br>
char *src, *dest;<br>
- int len;<br>
+ int ret;<br>
<br>
if (argc != 4 && argc != 5) {<br>
fprintf (stderr, "Usage: %s <Locale> <ComposeFile> <CacheDir> [<InternalName>]\n", argv[0]);<br>
@@ -53,20 +97,20 @@ int main (int argc, char *argv[]) {<br>
return 1;<br>
}<br>
<br>
- src = malloc (strlen (argv[2]) + 14);<br>
- len = strlen (argv[3]) + 15;<br>
- if (argc == 5)<br>
- len += strlen (argv[4]) + 1;<br>
- dest = malloc (len);<br>
- if (! src || ! dest) {<br>
- perror ("* malloc");<br>
+ if (asprintf (&src, "XCOMPOSEFILE=%s", argv[2]) == -1) {<br>
+ perror ("* asprintf");<br>
return 1;<br>
}<br>
- sprintf (src, "XCOMPOSEFILE=%s", argv[2]);<br>
+<br>
if (argc == 4)<br>
- sprintf (dest, "XCOMPOSECACHE=%s", argv[3]);<br>
+ ret = asprintf (&dest, "XCOMPOSECACHE=%s", argv[3]);<br>
else<br>
- sprintf (dest, "XCOMPOSECACHE=%s=%s", argv[3], argv[4]);<br>
+ ret = asprintf (&dest, "XCOMPOSECACHE=%s=%s", argv[3], argv[4]);<br>
+ if (ret == -1) {<br>
+ perror ("* asprintf");<br>
+ return 1;<br>
+ }<br>
+<br>
putenv (src);<br>
putenv (dest);<br>
#if HAVE_UNSETENV<br>
<span class="HOEnZb"><font color="#888888">--<br>
1.7.9.2<br>
<br>
_______________________________________________<br>
<a href="mailto:xorg-devel@lists.x.org">xorg-devel@lists.x.org</a>: X.Org development<br>
Archives: <a href="http://lists.x.org/archives/xorg-devel" target="_blank">http://lists.x.org/archives/xorg-devel</a><br>
Info: <a href="http://lists.x.org/mailman/listinfo/xorg-devel" target="_blank">http://lists.x.org/mailman/listinfo/xorg-devel</a><br>
</font></span></blockquote></div><br><br clear="all"><br>-- <br> Jasper<br>
</div>