2010-11-17 Marcus Brinkmann <mb@g10code.com>
[gpgme.git] / src / vasprintf.c
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.
4
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.
10
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.
15
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.  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28
29
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))
36 #else 
37 #define va_copy(d, s) memcpy ((d), (s), sizeof (va_list))
38 #endif 
39 #endif 
40
41
42 #ifdef TEST
43 int global_total_width;
44 #endif
45
46 static int int_vasprintf (char **, const char *, va_list *);
47
48 static int
49 int_vasprintf (result, format, args)
50      char **result;
51      const char *format;
52      va_list *args;
53 {
54 #ifdef HAVE_W32CE_SYSTEM
55   /* No va_copy and the replacement above doesn't work.  */
56 #define MAX_STRLEN 256
57   *result = malloc (MAX_STRLEN);
58   if (*result != NULL)
59     {
60       int res = _vsnprintf (*result, MAX_STRLEN, format, *args);
61       (*result)[MAX_STRLEN - 1] = '\0';
62       return res;
63     }
64   else
65     return 0;
66 #else
67   const char *p = format;
68   /* Add one to make sure that it is never zero, which might cause malloc
69      to return NULL.  */
70   int total_width = strlen (format) + 1;
71   va_list ap;
72
73   va_copy (ap, *args);
74
75   while (*p != '\0')
76     {
77       if (*p++ == '%')
78         {
79           while (strchr ("-+ #0", *p))
80             ++p;
81           if (*p == '*')
82             {
83               ++p;
84               total_width += abs (va_arg (ap, int));
85             }
86           else
87             total_width += strtoul (p, (char **) &p, 10);
88           if (*p == '.')
89             {
90               ++p;
91               if (*p == '*')
92                 {
93                   ++p;
94                   total_width += abs (va_arg (ap, int));
95                 }
96               else
97               total_width += strtoul (p, (char **) &p, 10);
98             }
99           while (strchr ("hlL", *p))
100             ++p;
101           /* Should be big enough for any format specifier except %s and floats.  */
102           total_width += 30;
103           switch (*p)
104             {
105             case 'd':
106             case 'i':
107             case 'o':
108             case 'u':
109             case 'x':
110             case 'X':
111             case 'c':
112               (void) va_arg (ap, int);
113               break;
114             case 'f':
115             case 'e':
116             case 'E':
117             case 'g':
118             case 'G':
119               (void) va_arg (ap, double);
120               /* Since an ieee double can have an exponent of 307, we'll
121                  make the buffer wide enough to cover the gross case. */
122               total_width += 307;
123               break;
124             case 's':
125               {
126                 char *tmp = va_arg (ap, char *);
127                 if (tmp)
128                   total_width += strlen (tmp);
129                 else /* in case the vsprintf does prints a text */
130                   total_width += 25; /* e.g. "(null pointer reference)" */
131               }
132               break;
133             case 'p':
134             case 'n':
135               (void) va_arg (ap, char *);
136               break;
137             }
138           p++;
139         }
140     }
141 #ifdef TEST
142   global_total_width = total_width;
143 #endif
144   *result = malloc (total_width);
145   if (*result != NULL)
146     return vsprintf (*result, format, *args);
147   else
148     return 0;
149 #endif
150 }
151
152 int
153 vasprintf (result, format, args)
154      char **result;
155      const char *format;
156 #if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
157      _BSD_VA_LIST_ args;
158 #else
159      va_list args;
160 #endif
161 {
162   return int_vasprintf (result, format, &args);
163 }
164
165
166 int
167 asprintf (char **buf, const char *fmt, ...)
168 {
169   int status;
170   va_list ap;
171
172   va_start (ap, fmt);
173   status = vasprintf (buf, fmt, ap);
174   va_end (ap);
175   return status;
176 }
177
178
179 #ifdef TEST
180 void
181 checkit (const char* format, ...)
182 {
183   va_list args;
184   char *result;
185
186   va_start (args, format);
187   vasprintf (&result, format, args);
188   if (strlen (result) < global_total_width)
189     printf ("PASS: ");
190   else
191     printf ("FAIL: ");
192   printf ("%d %s\n", global_total_width, result);
193 }
194
195 int
196 main (void)
197 {
198   checkit ("%d", 0x12345678);
199   checkit ("%200d", 5);
200   checkit ("%.300d", 6);
201   checkit ("%100.150d", 7);
202   checkit ("%s", "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
203 777777777777777777333333333333366666666666622222222222777777777777733333");
204   checkit ("%f%s%d%s", 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx");
205 }
206 #endif /* TEST */