-#include "config.h"
-
-#if HAVE_VASPRINTF
-
-int dummy;
-
-#else
-
-/* $Id: compat_vasprintf.c,v 1.1 2015/03/19 14:57:29 schwarze Exp $ */
+/* $Id: compat_vasprintf.c,v 1.4 2020/06/15 01:37:15 schwarze Exp $ */
/*
* Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
*
* printf(3) or completely reimplementing printf(3), i can't think
* of another portable solution.
*/
+#include "config.h"
#include <stdarg.h>
#include <stdio.h>
vasprintf(char **ret, const char *format, va_list ap)
{
char buf[2];
+ va_list ap2;
int sz;
- if ((sz = vsnprintf(buf, sizeof(buf), format, ap)) != -1 &&
- (*ret = malloc(sz + 1)) != NULL) {
+ va_copy(ap2, ap);
+ sz = vsnprintf(buf, sizeof(buf), format, ap2);
+ va_end(ap2);
+
+ if (sz != -1 && (*ret = malloc(sz + 1)) != NULL) {
if (vsnprintf(*ret, sz + 1, format, ap) == sz)
- return(sz);
+ return sz;
free(*ret);
}
*ret = NULL;
- return(-1);
+ return -1;
}
-
-#endif