From bcb41b85b62fcc24337c9a7748dd64c10044fe46 Mon Sep 17 00:00:00 2001 From: Ralph Boehme Date: Tue, 22 Oct 2013 19:34:01 +0200 Subject: [PATCH] Add vasprintf compatibility function --- configure.ac | 2 +- include/atalk/compat.h | 6 ++++++ libatalk/compat/misc.c | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 12a0ce2f..b3dba043 100644 --- a/configure.ac +++ b/configure.ac @@ -76,7 +76,7 @@ AC_CHECK_MEMBERS(struct tm.tm_gmtoff,,, [#include ]) 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 diff --git a/include/atalk/compat.h b/include/atalk/compat.h index a2d318f4..52198dc9 100644 --- a/include/atalk/compat.h +++ b/include/atalk/compat.h @@ -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 +#include +extern int vasprintf(char **ret, const char *fmt, va_list ap); +#endif + #endif diff --git a/libatalk/compat/misc.c b/libatalk/compat/misc.c index 58115315..32e2943d 100644 --- a/libatalk/compat/misc.c +++ b/libatalk/compat/misc.c @@ -2,6 +2,9 @@ #include "config.h" #endif /* HAVE_CONFIG_H */ +#include +#include + #include #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 -- 2.39.2