From ff4c188e615cf01482971a24c2003a5c118838de Mon Sep 17 00:00:00 2001 From: Dirk Zimoch Date: Tue, 3 Nov 2015 15:20:38 +0100 Subject: [PATCH] some fixes for systems without va_copy --- asprintf.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/asprintf.c b/asprintf.c index d623bf2..2c7bcd1 100644 --- a/asprintf.c +++ b/asprintf.c @@ -1,34 +1,49 @@ #include #include +#include #include "asprintf.h" -#ifndef va_copy +/* some implementations have __va_copy instead of va_copy */ +#if !defined(va_copy) && defined (__va_copy) #define va_copy __va_copy #endif int vasprintf(char** pbuffer, const char* format, va_list ap) { + int len = -1; + +#ifdef va_copy va_list ap2; - int len; + va_copy(ap2, ap); +#else + /* if we have no va_copy, we probably don't need one */ + #define ap2 ap +#endif #if defined(vxWorks) - FILE* f; - /* print to null device to get required buffer length */ - if ((f = fopen("/null","w")) != NULL) { - len = vfprintf(f, format, ap2); - fclose(f); + FILE* f; + /* print to null device to get required buffer length */ + if ((f = fopen("/null","w")) != NULL) + { + len = vfprintf(f, format, ap2); + fclose(f); + } } #elif defined(_WIN32) - len = _vscprintf(format, ap); + len = _vscprintf(format, ap2); #else - len = vsnprintf(NULL, 0, format, ap); + len = vsnprintf(NULL, 0, format, ap2); #endif + +#ifdef va_copy va_end(ap2); +#endif + if (len <= 0) { - fprintf(stderr, "vasprintf: too old version on vsnprintf\n"); - return len; + fprintf(stderr, "vasprintf: error calculating needed size\n"); + return -1; } *pbuffer = malloc(len+1); if (*pbuffer == NULL) return -1;