*** empty log message ***
[shishi.git] / lib / vasprintf.c
bloba5ed2c0b80e5c862b6f89288f02d579cfef18c8e
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)
9 any later version.
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. */
20 #include "internal.h"
22 static int
23 int_vasprintf (char **result, char *format, va_list * args)
25 char *p = format;
26 /* Add one to make sure that it is never zero, which might cause malloc
27 to return NULL. */
28 int total_width = strlen (format) + 1;
29 va_list ap;
31 memcpy (&ap, args, sizeof (va_list));
33 while (*p != '\0')
35 if (*p++ == '%')
37 while (strchr ("-+ #0", *p))
38 ++p;
39 if (*p == '*')
41 ++p;
42 total_width += abs (va_arg (ap, int));
44 else
45 total_width += strtoul (p, &p, 10);
46 if (*p == '.')
48 ++p;
49 if (*p == '*')
51 ++p;
52 total_width += abs (va_arg (ap, int));
54 else
55 total_width += strtoul (p, (char **) &p, 10);
57 while (strchr ("hlL", *p))
58 ++p;
59 /* Should be big enough for any format specifier except %s. */
60 total_width += 30;
61 switch (*p)
63 case 'd':
64 case 'i':
65 case 'o':
66 case 'u':
67 case 'x':
68 case 'X':
69 case 'c':
70 (void) va_arg (ap, int);
71 break;
72 case 'f':
73 case 'e':
74 case 'E':
75 case 'g':
76 case 'G':
77 (void) va_arg (ap, double);
78 break;
79 case 's':
80 total_width += strlen (va_arg (ap, char *));
81 break;
82 case 'p':
83 case 'n':
84 (void) va_arg (ap, char *);
85 break;
89 *result = malloc (total_width);
90 if (*result != NULL)
91 return vsprintf (*result, format, *args);
92 else
93 return 0;
96 static int
97 _shishi_vasprintf (char **result, char *format, va_list args)
99 return int_vasprintf (result, format, &args);
103 shishi_asprintf (char **result, char *format, ...)
105 va_list args;
106 int done;
108 va_start (args, format);
109 done = _shishi_vasprintf (result, format, args);
110 va_end (args);
112 return done;