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 * The canonical version of this file is maintained in the rra-c-util package,
8 * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
10 * Written by Russ Allbery <rra@stanford.edu>
12 * The authors hereby relinquish any claim to any copyright that they may have
13 * in this work, whether granted under contract or by operation of law or
14 * international treaty, and hereby commit to the public, at large, that they
15 * shall not, at any time in the future, seek to enforce any copyright in this
16 * work against any person or entity, or prevent any person or entity from
17 * copying, publishing, distributing or creating derivative works of this
22 #include <portable/system.h>
25 * If we're running the test suite, rename the functions to avoid conflicts
26 * with the system versions.
29 # define asprintf test_asprintf
30 # define vasprintf test_vasprintf
31 int test_asprintf(char **, const char *, ...)
32 __attribute__((__format__(printf, 2, 3)));
33 int test_vasprintf(char **, const char *, va_list);
37 asprintf(char **strp, const char *fmt, ...)
43 status = vasprintf(strp, fmt, args);
49 vasprintf(char **strp, const char *fmt, va_list args)
54 va_copy(args_copy, args);
55 needed = vsnprintf(NULL, 0, fmt, args_copy);
61 *strp = malloc(needed + 1);
64 status = vsnprintf(*strp, needed + 1, fmt, args);