1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #include <ccan/asprintf/asprintf.h>
6 char *PRINTF_FMT(1, 2) afmt(const char *fmt, ...)
12 /* The BSD version apparently sets ptr to NULL on fail. GNU loses. */
13 if (vasprintf(&ptr, fmt, ap) < 0)
23 int vasprintf(char **strp, const char *fmt, va_list ap)
28 /* We need to make a copy of ap, since it's a use-once. */
30 len = vsnprintf(NULL, 0, fmt, ap_copy);
33 /* Until version 2.0.6 glibc would return -1 on truncated output.
34 * OTOH, they had asprintf. */
38 *strp = malloc(len+1);
42 return vsprintf(*strp, fmt, ap);
45 int asprintf(char **strp, const char *fmt, ...)
51 len = vasprintf(strp, fmt, ap);
56 #endif /* !HAVE_ASPRINTF */