]> arthur.barton.de Git - netatalk.git/commitdiff
Add vasprintf compatibility function
authorRalph Boehme <sloowfranklin@gmail.com>
Tue, 22 Oct 2013 17:34:01 +0000 (19:34 +0200)
committerRalph Boehme <sloowfranklin@gmail.com>
Wed, 23 Oct 2013 08:52:24 +0000 (10:52 +0200)
configure.ac
include/atalk/compat.h
libatalk/compat/misc.c

index 12a0ce2f263d9bc2096c1accd9d729bbf28a5df0..b3dba04365e976a00745791d43f00b47b8d68761 100644 (file)
@@ -76,7 +76,7 @@ AC_CHECK_MEMBERS(struct tm.tm_gmtoff,,, [#include <time.h>])
 
 dnl these tests have been comfirmed to be needed in 2011
 AC_CHECK_FUNCS(backtrace_symbols dirfd getusershell pread pwrite pselect)
-AC_CHECK_FUNCS(setlinebuf strlcat strlcpy strnlen mempcpy)
+AC_CHECK_FUNCS(setlinebuf strlcat strlcpy strnlen mempcpy vasprintf)
 AC_CHECK_FUNCS(mmap utime getpagesize) dnl needed by tbd
 
 dnl search for necessary libraries
index a2d318f43a4f81942210a3a922941224f01e043b..52198dc91903c5df437e43daa24dc5cd35e51f6b 100644 (file)
@@ -54,4 +54,10 @@ extern size_t strlcpy (char *, const char *, size_t);
 extern size_t strlcat (char *, const char *, size_t);
 #endif
 
+#ifndef HAVE_VASPRINTF
+#include <stdio.h>
+#include <stdarg.h>
+extern int vasprintf(char **ret, const char *fmt, va_list ap);
+#endif
+
 #endif
index 5811531520ddcf860da59a6d76dae346d0e8a191..32e2943dded16b60795979e39b956725d5103349 100644 (file)
@@ -2,6 +2,9 @@
 #include "config.h"
 #endif /* HAVE_CONFIG_H */
 
+#include <stdio.h>
+#include <stdarg.h>
+
 #include <atalk/compat.h>
 
 #if !defined HAVE_DIRFD && defined SOLARIS
@@ -25,3 +28,36 @@ size_t strnlen(const char *s, size_t max)
     return len;  
 }
 #endif
+
+#ifndef HAVE_VASPRINTF
+int vasprintf(char **ret, const char *fmt, va_list ap)
+{
+    int n, size = 64;
+    char *p, *np;
+
+    if ((p = malloc(size)) == NULL)
+        return NULL;
+
+    while (1) {
+        /* Try to print in the allocated space. */
+        n = vsnprintf(p, size, fmt, ap);
+        /* If that worked, return the string. */
+        if (n > -1 && n < size) {
+            *ret = p;
+            return n;
+        }
+        /* Else try again with more space. */
+        if (n > -1)    /* glibc 2.1 */
+            size = n+1; /* precisely what is needed */
+        else           /* glibc 2.0 */
+            size *= 2;  /* twice the old size */
+        if ((np = realloc (p, size)) == NULL) {
+            free(p);
+            *ret = NULL;
+            return -1;
+        } else {
+            p = np;
+        }
+    }
+}
+#endif