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;
39
/* Make sure we have a va_copy that will work on all platforms */
42
# define va_copy(d, s) __va_copy((d), (s))
44
# define va_copy(d, s) memcpy(&(d), &(s), sizeof(va_list))
48
int lw_vasprintf (char **result, const char *format, va_list args);
51
(char **result, const char *format, ...);
60
int_vasprintf (result, format, args)
65
const char *p = format;
66
/* Add one to make sure that it is never zero, which might cause malloc
68
int total_width = strlen (format) + 1;
71
memcpy (&ap, args, sizeof (va_list));
77
while (strchr ("-+ #0", *p))
82
total_width += abs (va_arg (ap, int));
85
total_width += strtoul (p, (char **) &p, 10);
92
total_width += abs (va_arg (ap, int));
95
total_width += strtoul (p, (char **) &p, 10);
97
while (strchr ("hlLjtz", *p))
99
/* Should be big enough for any format specifier except %s
111
(void) va_arg (ap, int);
115
double arg = va_arg (ap, double);
116
if (arg >= 1.0 || arg <= -1.0)
117
/* Since an ieee double can have an exponent of 307, we'll
118
make the buffer wide enough to cover the gross case. */
126
(void) va_arg (ap, double);
129
total_width += strlen (va_arg (ap, char *));
133
(void) va_arg (ap, char *);
140
global_total_width = total_width;
142
*result = malloc (total_width);
144
return vsprintf (*result, format, *args);
150
lw_vasprintf (result, format, args)
159
return int_vasprintf (result, format, &temp);
165
(char **result, const char *format, ...)
176
va_start (args, format);
180
format = va_arg (args, char *);
182
done = lw_vasprintf (result, format, args);