1 /* Like vsprintf but provides a pointer to malloc'd storage, which must
2 be freed by the caller.
3 Copyright (C) 1994, 2003 Free Software Foundation, Inc.
4 Copyright (C) 2002 Simon Josefsson
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 int_vasprintf (char **result
, char *format
, va_list * args
)
26 /* Add one to make sure that it is never zero, which might cause malloc
28 int total_width
= strlen (format
) + 1;
31 memcpy (&ap
, args
, sizeof (va_list));
37 while (strchr ("-+ #0", *p
))
42 total_width
+= abs (va_arg (ap
, int));
45 total_width
+= strtoul (p
, &p
, 10);
52 total_width
+= abs (va_arg (ap
, int));
55 total_width
+= strtoul (p
, (char **) &p
, 10);
57 while (strchr ("hlL", *p
))
59 /* Should be big enough for any format specifier except %s. */
70 (void) va_arg (ap
, int);
77 (void) va_arg (ap
, double);
80 total_width
+= strlen (va_arg (ap
, char *));
84 (void) va_arg (ap
, char *);
89 *result
= malloc (total_width
);
91 return vsprintf (*result
, format
, *args
);
97 _shishi_vasprintf (char **result
, char *format
, va_list args
)
99 return int_vasprintf (result
, format
, &args
);
103 shishi_asprintf (char **result
, char *format
, ...)
108 va_start (args
, format
);
109 done
= _shishi_vasprintf (result
, format
, args
);