1 /* Like vsprintf but provides a pointer to malloc'd storage, which must
2    be freed by the caller.
3    Copyright (C) 1994 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 #include "config.h"
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <ansidecl.h>
26 #ifdef __STDC__
27 #include <stdarg.h>
28 #else
29 #include <varargs.h>
30 #endif
31 
32 #ifdef TEST
33 int global_total_width;
34 #endif
35 
36 #ifndef HAVE_VASPRINTF
37 
38 static int
int_vasprintf(char ** result,const char * format,va_list * args)39 int_vasprintf (char **result, const char *format, va_list *args)
40 {
41   const char *p = format;
42   /* Add one to make sure that it is never zero, which might cause malloc
43      to return NULL.  */
44   int total_width = strlen (format) + 1;
45   va_list ap;
46 
47   memcpy ((PTR) &ap, (PTR) args, sizeof (va_list));
48 
49   while (*p != '\0')
50     {
51       if (*p++ == '%')
52         {
53           while (strchr ("-+ #0", *p))
54             ++p;
55           if (*p == '*')
56             {
57               ++p;
58               total_width += abs (va_arg (ap, int));
59             }
60           else
61             total_width += strtoul (p, (char **)&p, 10);
62           if (*p == '.')
63             {
64               ++p;
65               if (*p == '*')
66                 {
67                   ++p;
68                   total_width += abs (va_arg (ap, int));
69                 }
70               else
71               total_width += strtoul (p, (char **)&p, 10);
72             }
73           while (strchr ("hlL", *p))
74             ++p;
75           /* Should be big enough for any format specifier except %s and floats.  */
76           total_width += 30;
77           switch (*p)
78             {
79             case 'd':
80             case 'i':
81             case 'o':
82             case 'u':
83             case 'x':
84             case 'X':
85             case 'c':
86               (void) va_arg (ap, int);
87               break;
88             case 'f':
89             case 'e':
90             case 'E':
91             case 'g':
92             case 'G':
93               (void) va_arg (ap, double);
94               /* Since an ieee double can have an exponent of 307, we'll
95                  make the buffer wide enough to cover the gross case. */
96               total_width += 307;
97               break;
98             case 's':
99               total_width += strlen (va_arg (ap, char *));
100               break;
101             case 'p':
102             case 'n':
103               (void) va_arg (ap, char *);
104               break;
105             }
106         }
107     }
108 #ifdef TEST
109   global_total_width = total_width;
110 #endif
111   *result = malloc (total_width);
112   if (*result != NULL)
113     return vsprintf (*result, format, *args);
114   else
115     return 0;
116 }
117 
118 int
vasprintf(char ** result,const char * format,va_list args)119 vasprintf (char **result, const char *format, va_list args)
120 {
121   return int_vasprintf (result, format, &args);
122 }
123 
124 #else
125 extern int _I_dont_care_that_ISO_C_forbids_an_empty_source_file_;
126 #endif
127 
128 #ifdef TEST
129 void
checkit(const char * format,...)130 checkit
131 #ifdef __STDC__
132      (const char* format, ...)
133 #else
134      (va_alist)
135      va_dcl
136 #endif
137 {
138   va_list args;
139   char *result;
140   int ret;
141 
142 #ifdef __STDC__
143   va_start (args, format);
144 #else
145   char *format;
146   va_start (args);
147   format = va_arg (args, char *);
148 #endif
149   ret = vasprintf (&result, format, args);
150   if (ret >= 0 && strlen (result) < global_total_width)
151     printf ("PASS: ");
152   else
153     printf ("FAIL: ");
154   printf ("%d %s\n", global_total_width, result);
155 }
156 
157 int
main()158 main ()
159 {
160   checkit ("%d", 0x12345678);
161   checkit ("%200d", 5);
162   checkit ("%.300d", 6);
163   checkit ("%100.150d", 7);
164   checkit ("%s", "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
165 777777777777777777333333333333366666666666622222222222777777777777733333");
166   checkit ("%f%s%d%s", 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx");
167 }
168 #endif /* TEST */
169