1 /* Like vsprintf but provides a pointer to malloc'd storage, which must
2 be freed by the caller.
3 Copyright (C) 1994, 2002 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., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
30 #ifndef va_copy /* According to POSIX, va_copy is a macro. */
31 #if defined (__GNUC__) && defined (__PPC__) \
32 && (defined (_CALL_SYSV) || defined (_WIN32))
33 #define va_copy(d, s) (*(d) = *(s))
34 #elif defined (MUST_COPY_VA_BYVAL)
35 #define va_copy(d, s) ((d) = (s))
37 #define va_copy(d, s) memcpy ((d), (s), sizeof (va_list))
43 int global_total_width;
46 static int int_vasprintf (char **, const char *, va_list *);
49 int_vasprintf (result, format, args)
54 const char *p = format;
55 /* Add one to make sure that it is never zero, which might cause malloc
57 int total_width = strlen (format) + 1;
66 while (strchr ("-+ #0", *p))
71 total_width += abs (va_arg (ap, int));
74 total_width += strtoul (p, (char **) &p, 10);
81 total_width += abs (va_arg (ap, int));
84 total_width += strtoul (p, (char **) &p, 10);
86 while (strchr ("hlL", *p))
88 /* Should be big enough for any format specifier except %s and floats. */
99 (void) va_arg (ap, int);
106 (void) va_arg (ap, double);
107 /* Since an ieee double can have an exponent of 307, we'll
108 make the buffer wide enough to cover the gross case. */
113 char *tmp = va_arg (ap, char *);
115 total_width += strlen (tmp);
116 else /* in case the vsprintf does prints a text */
117 total_width += 25; /* e.g. "(null pointer reference)" */
122 (void) va_arg (ap, char *);
129 global_total_width = total_width;
131 *result = malloc (total_width);
133 return vsprintf (*result, format, *args);
139 vasprintf (result, format, args)
142 #if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
148 return int_vasprintf (result, format, &args);
153 asprintf (char **buf, const char *fmt, ...)
159 status = vasprintf (buf, fmt, ap);
167 checkit (const char* format, ...)
172 va_start (args, format);
173 vasprintf (&result, format, args);
174 if (strlen (result) < global_total_width)
178 printf ("%d %s\n", global_total_width, result);
184 checkit ("%d", 0x12345678);
185 checkit ("%200d", 5);
186 checkit ("%.300d", 6);
187 checkit ("%100.150d", 7);
188 checkit ("%s", "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
189 777777777777777777333333333333366666666666622222222222777777777777733333");
190 checkit ("%f%s%d%s", 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx");