1 /* Like vsprintf but provides a pointer to malloc'd storage, which must
2 be freed by the caller.
3 Copyright (C) 1994, 2003, 2011 Free Software Foundation, Inc.
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 Libiberty is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with libiberty; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19 Boston, MA 02110-1301, USA. */
26 #if !defined (va_copy) && defined (__va_copy)
27 # define va_copy(d,s) __va_copy((d),(s))
36 extern unsigned long strtoul ();
39 #include "libiberty.h"
42 int global_total_width;
47 @deftypefn Extension int vasprintf (char **@var{resptr}, @
48 const char *@var{format}, va_list @var{args})
50 Like @code{vsprintf}, but instead of passing a pointer to a buffer,
51 you pass a pointer to a pointer. This function will compute the size
52 of the buffer needed, allocate memory with @code{malloc}, and store a
53 pointer to the allocated memory in @code{*@var{resptr}}. The value
54 returned is the same as @code{vsprintf} would return. If memory could
55 not be allocated, minus one is returned and @code{NULL} is stored in
62 static int int_vasprintf (char **, const char *, va_list);
65 int_vasprintf (char **result, const char *format, va_list args)
67 const char *p = format;
68 /* Add one to make sure that it is never zero, which might cause malloc
70 int total_width = strlen (format) + 1;
76 memcpy ((PTR) &ap, (PTR) &args, sizeof (va_list));
83 while (strchr ("-+ #0", *p))
88 total_width += abs (va_arg (ap, int));
91 total_width += strtoul (p, (char **) &p, 10);
98 total_width += abs (va_arg (ap, int));
101 total_width += strtoul (p, (char **) &p, 10);
103 while (strchr ("hlL", *p))
105 /* Should be big enough for any format specifier except %s and floats. */
116 (void) va_arg (ap, int);
123 (void) va_arg (ap, double);
124 /* Since an ieee double can have an exponent of 307, we'll
125 make the buffer wide enough to cover the gross case. */
129 total_width += strlen (va_arg (ap, char *));
133 (void) va_arg (ap, char *);
143 global_total_width = total_width;
145 *result = (char *) malloc (total_width);
147 return vsprintf (*result, format, args);
153 vasprintf (char **result, const char *format,
154 #if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
160 return int_vasprintf (result, format, args);
164 static void ATTRIBUTE_PRINTF_1
165 checkit (const char *format, ...)
168 VA_OPEN (args, format);
169 VA_FIXEDARG (args, const char *, format);
170 vasprintf (&result, format, args);
173 if (strlen (result) < (size_t) global_total_width)
177 printf ("%d %s\n", global_total_width, result);
182 extern int main (void);
187 checkit ("%d", 0x12345678);
188 checkit ("%200d", 5);
189 checkit ("%.300d", 6);
190 checkit ("%100.150d", 7);
191 checkit ("%s", "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
192 777777777777777777333333333333366666666666622222222222777777777777733333");
193 checkit ("%f%s%d%s", 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx");