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 <https://www.eyrie.org/~eagle/software/rra-c-util/>.
10 * Written by Russ Allbery <eagle@eyrie.org>
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/macros.h>
23 #include <portable/system.h>
28 * If we're running the test suite, rename the functions to avoid conflicts
29 * with the system versions.
34 # define asprintf test_asprintf
35 # define vasprintf test_vasprintf
36 int test_asprintf(char **, const char *, ...)
37 __attribute__((__format__(printf, 2, 3)));
38 int test_vasprintf(char **, const char *, va_list)
39 __attribute__((__format__(printf, 2, 0)));
44 asprintf(char **strp, const char *fmt, ...)
50 status = vasprintf(strp, fmt, args);
57 vasprintf(char **strp, const char *fmt, va_list args)
60 int status, needed, oerrno;
62 va_copy(args_copy, args);
63 needed = vsnprintf(NULL, 0, fmt, args_copy);
69 *strp = malloc(needed + 1);
72 status = vsnprintf(*strp, needed + 1, fmt, args);