2 * Replacement for a missing asprintf and vasprintf.
4 * Provides the same functionality as the standard GNU library routines
5 * asprintf and vasprintf for those platforms that don't have them.
7 * Written by Russ Allbery <rra@stanford.edu>
8 * This work is hereby placed in the public domain by its author.
12 #include <portable/system.h>
15 * If we're running the test suite, rename the functions to avoid conflicts
16 * with the system versions.
19 # define asprintf test_asprintf
20 # define vasprintf test_vasprintf
21 int test_asprintf(char **, const char *, ...)
22 __attribute__((__format__(printf, 2, 3)));
23 int test_vasprintf(char **, const char *, va_list);
27 asprintf(char **strp, const char *fmt, ...)
33 status = vasprintf(strp, fmt, args);
39 vasprintf(char **strp, const char *fmt, va_list args)
44 va_copy(args_copy, args);
45 needed = vsnprintf(NULL, 0, fmt, args_copy);
51 *strp = malloc(needed + 1);
54 status = vsnprintf(*strp, needed + 1, fmt, args);