77113a310e727eee8b8aa980b36c4bc6e68180c6
[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   const char *p = format;
55   /* Add one to make sure that it is never zero, which might cause malloc
56      to return NULL.  */
57   int total_width = strlen (format) + 1;
58   va_list ap;
59
60   va_copy (ap, *args);
61
62   while (*p != '\0')
63     {
64       if (*p++ == '%')
65         {
66           while (strchr ("-+ #0", *p))
67             ++p;
68           if (*p == '*')
69             {
70               ++p;
71               total_width += abs (va_arg (ap, int));
72             }
73           else
74             total_width += strtoul (p, (char **) &p, 10);
75           if (*p == '.')
76             {
77               ++p;
78               if (*p == '*')
79                 {
80                   ++p;
81                   total_width += abs (va_arg (ap, int));
82                 }
83               else
84               total_width += strtoul (p, (char **) &p, 10);
85             }
86           while (strchr ("hlL", *p))
87             ++p;
88           /* Should be big enough for any format specifier except %s and floats.  */
89           total_width += 30;
90           switch (*p)
91             {
92             case 'd':
93             case 'i':
94             case 'o':
95             case 'u':
96             case 'x':
97             case 'X':
98             case 'c':
99               (void) va_arg (ap, int);
100               break;
101             case 'f':
102             case 'e':
103             case 'E':
104             case 'g':
105             case 'G':
106               (void) va_arg (ap, double);
107               /* Since an ieee double can have an exponent of 307, we'll
108                  make the buffer wide enough to cover the gross case. */
109               total_width += 307;
110               break;
111             case 's':
112               {
113                 char *tmp = va_arg (ap, char *);
114                 if (tmp)
115                   total_width += strlen (tmp);
116                 else /* in case the vsprintf does prints a text */
117                   total_width += 25; /* e.g. "(null pointer reference)" */
118               }
119               break;
120             case 'p':
121             case 'n':
122               (void) va_arg (ap, char *);
123               break;
124             }
125           p++;
126         }
127     }
128 #ifdef TEST
129   global_total_width = total_width;
130 #endif
131   *result = malloc (total_width);
132   if (*result != NULL)
133     return vsprintf (*result, format, *args);
134   else
135     return 0;
136 }
137
138 int
139 vasprintf (result, format, args)
140      char **result;
141      const char *format;
142 #if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
143      _BSD_VA_LIST_ args;
144 #else
145      va_list args;
146 #endif
147 {
148   return int_vasprintf (result, format, &args);
149 }
150
151
152 int
153 asprintf (char **buf, const char *fmt, ...)
154 {
155   int status;
156   va_list ap;
157
158   va_start (ap, fmt);
159   status = vasprintf (buf, fmt, ap);
160   va_end (ap);
161   return status;
162 }
163
164
165 #ifdef TEST
166 void
167 checkit (const char* format, ...)
168 {
169   va_list args;
170   char *result;
171
172   va_start (args, format);
173   vasprintf (&result, format, args);
174   if (strlen (result) < global_total_width)
175     printf ("PASS: ");
176   else
177     printf ("FAIL: ");
178   printf ("%d %s\n", global_total_width, result);
179 }
180
181 int
182 main (void)
183 {
184   checkit ("%d", 0x12345678);
185   checkit ("%200d", 5);
186   checkit ("%.300d", 6);
187   checkit ("%100.150d", 7);
188   checkit ("%s", "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
189 777777777777777777333333333333366666666666622222222222777777777777733333");
190   checkit ("%f%s%d%s", 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx");
191 }
192 #endif /* TEST */