1
/* Like vsprintf but provides a pointer to malloc'd storage, which must
2
be freed by the caller.
3
Copyright (C) 1994, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 2, or (at your option)
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
36
int global_total_width;
40
int_vasprintf (result, format, args)
45
const char *p = format;
46
/* Add one to make sure that it is never zero, which might cause malloc
48
int total_width = strlen (format) + 1;
51
memcpy (&ap, args, sizeof (va_list));
57
while (strchr ("-+ #0", *p))
62
total_width += abs (va_arg (ap, int));
65
total_width += strtoul (p, (char **) &p, 10);
72
total_width += abs (va_arg (ap, int));
75
total_width += strtoul (p, (char **) &p, 10);
77
while (strchr ("hlLjtz", *p))
79
/* Should be big enough for any format specifier except %s
91
(void) va_arg (ap, int);
95
double arg = va_arg (ap, double);
96
if (arg >= 1.0 || arg <= -1.0)
97
/* Since an ieee double can have an exponent of 307, we'll
98
make the buffer wide enough to cover the gross case. */
106
(void) va_arg (ap, double);
109
total_width += strlen (va_arg (ap, char *));
113
(void) va_arg (ap, char *);
120
global_total_width = total_width;
122
*result = malloc (total_width);
124
return vsprintf (*result, format, *args);
130
vasprintf (result, format, args)
135
return int_vasprintf (result, format, &args);
141
(char **result, const char *format, ...)
152
va_start (args, format);
156
format = va_arg (args, char *);
158
done = vasprintf (result, format, args);