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>
11 * Copyright 2006, 2015 Russ Allbery <eagle@eyrie.org>
12 * Copyright 2008-2009, 2011, 2013
13 * The Board of Trustees of the Leland Stanford Junior University
15 * Copying and distribution of this file, with or without modification, are
16 * permitted in any medium without royalty provided the copyright notice and
17 * this notice are preserved. This file is offered as-is, without any
20 * SPDX-License-Identifier: FSFAP
24 #include <portable/macros.h>
25 #include <portable/system.h>
30 * If we're running the test suite, rename the functions to avoid conflicts
31 * with the system versions.
36 # define asprintf test_asprintf
37 # define vasprintf test_vasprintf
38 int test_asprintf(char **, const char *, ...)
39 __attribute__((__format__(printf, 2, 3)));
40 int test_vasprintf(char **, const char *, va_list)
41 __attribute__((__format__(printf, 2, 0)));
46 asprintf(char **strp, const char *fmt, ...)
52 status = vasprintf(strp, fmt, args);
59 vasprintf(char **strp, const char *fmt, va_list args)
62 int status, needed, oerrno;
64 va_copy(args_copy, args);
65 needed = vsnprintf(NULL, 0, fmt, args_copy);
71 *strp = malloc(needed + 1);
74 status = vsnprintf(*strp, needed + 1, fmt, args);