0
|
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 /*
|
|
22 Changes for importing vasprintf.c to FreeWnn:
|
|
23 - Comment out #include "libiberty.h" (aono)
|
|
24 - Header inclusions were modified due to our use of configure. (H.Ono)
|
|
25 */
|
|
26
|
|
27 #ifdef HAVE_CONFIG_H
|
|
28 # include <config.h>
|
|
29 #endif
|
|
30 #include <ansidecl.h>
|
|
31
|
|
32 #include <stdio.h>
|
|
33 #if STDC_HEADERS
|
|
34 # include <stdarg.h>
|
|
35 # include <string.h>
|
|
36 # include <stdlib.h>
|
|
37 #else
|
|
38 # include <varargs.h>
|
|
39 # if HAVE_STRINGS_H
|
|
40 # include <strings.h>
|
|
41 # endif
|
|
42 extern unsigned long strtoul ();
|
|
43 # if HAVE_MALLOC_H
|
|
44 # include <malloc.h>
|
|
45 # else
|
|
46 extern PTR malloc ();
|
|
47 # endif /* HAVE_MALLOC_H */
|
|
48 #endif /* STDC_HEADERS */
|
|
49
|
|
50 /* #include "libiberty.h" */
|
|
51
|
|
52 #ifdef TEST
|
|
53 int global_total_width;
|
|
54 #endif
|
|
55
|
|
56
|
|
57 static int int_vasprintf PARAMS ((char **, const char *, va_list *));
|
|
58
|
|
59 static int
|
|
60 int_vasprintf (result, format, args)
|
|
61 char **result;
|
|
62 const char *format;
|
|
63 va_list *args;
|
|
64 {
|
|
65 const char *p = format;
|
|
66 /* Add one to make sure that it is never zero, which might cause malloc
|
|
67 to return NULL. */
|
|
68 int total_width = strlen (format) + 1;
|
|
69 va_list ap;
|
|
70
|
|
71 memcpy ((PTR) &ap, (PTR) args, sizeof (va_list));
|
|
72
|
|
73 while (*p != '\0')
|
|
74 {
|
|
75 if (*p++ == '%')
|
|
76 {
|
|
77 while (strchr ("-+ #0", *p))
|
|
78 ++p;
|
|
79 if (*p == '*')
|
|
80 {
|
|
81 ++p;
|
|
82 total_width += abs (va_arg (ap, int));
|
|
83 }
|
|
84 else
|
|
85 total_width += strtoul (p, (char **) &p, 10);
|
|
86 if (*p == '.')
|
|
87 {
|
|
88 ++p;
|
|
89 if (*p == '*')
|
|
90 {
|
|
91 ++p;
|
|
92 total_width += abs (va_arg (ap, int));
|
|
93 }
|
|
94 else
|
|
95 total_width += strtoul (p, (char **) &p, 10);
|
|
96 }
|
|
97 while (strchr ("hlL", *p))
|
|
98 ++p;
|
|
99 /* Should be big enough for any format specifier except %s and floats. */
|
|
100 total_width += 30;
|
|
101 switch (*p)
|
|
102 {
|
|
103 case 'd':
|
|
104 case 'i':
|
|
105 case 'o':
|
|
106 case 'u':
|
|
107 case 'x':
|
|
108 case 'X':
|
|
109 case 'c':
|
|
110 (void) va_arg (ap, int);
|
|
111 break;
|
|
112 case 'f':
|
|
113 case 'e':
|
|
114 case 'E':
|
|
115 case 'g':
|
|
116 case 'G':
|
|
117 (void) va_arg (ap, double);
|
|
118 /* Since an ieee double can have an exponent of 307, we'll
|
|
119 make the buffer wide enough to cover the gross case. */
|
|
120 total_width += 307;
|
|
121 break;
|
|
122 case 's':
|
|
123 total_width += strlen (va_arg (ap, char *));
|
|
124 break;
|
|
125 case 'p':
|
|
126 case 'n':
|
|
127 (void) va_arg (ap, char *);
|
|
128 break;
|
|
129 }
|
|
130 p++;
|
|
131 }
|
|
132 }
|
|
133 #ifdef TEST
|
|
134 global_total_width = total_width;
|
|
135 #endif
|
|
136 *result = malloc (total_width);
|
|
137 if (*result != NULL)
|
|
138 return vsprintf (*result, format, *args);
|
|
139 else
|
|
140 return 0;
|
|
141 }
|
|
142
|
|
143 int
|
|
144 vasprintf (result, format, args)
|
|
145 char **result;
|
|
146 const char *format;
|
|
147 #if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
|
|
148 _BSD_VA_LIST_ args;
|
|
149 #else
|
|
150 va_list args;
|
|
151 #endif
|
|
152 {
|
|
153 return int_vasprintf (result, format, &args);
|
|
154 }
|
|
155
|
|
156 #ifdef TEST
|
|
157 static void checkit PARAMS ((const char *, ...));
|
|
158
|
|
159 static void
|
|
160 checkit VPARAMS ((const char* format, ...))
|
|
161 {
|
|
162 va_list args;
|
|
163 char *result;
|
|
164 #ifndef ANSI_PROTOTYPES
|
|
165 const char *format;
|
|
166 #endif
|
|
167
|
|
168 VA_START (args, format);
|
|
169
|
|
170 #ifndef ANSI_PROTOTYPES
|
|
171 format = va_arg (args, const char *);
|
|
172 #endif
|
|
173
|
|
174 vasprintf (&result, format, args);
|
|
175 if (strlen (result) < (size_t) global_total_width)
|
|
176 printf ("PASS: ");
|
|
177 else
|
|
178 printf ("FAIL: ");
|
|
179 printf ("%d %s\n", global_total_width, result);
|
|
180 }
|
|
181
|
|
182 extern int main PARAMS ((void));
|
|
183
|
|
184 int
|
|
185 main ()
|
|
186 {
|
|
187 checkit ("%d", 0x12345678);
|
|
188 checkit ("%200d", 5);
|
|
189 checkit ("%.300d", 6);
|
|
190 checkit ("%100.150d", 7);
|
|
191 checkit ("%s", "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
|
|
192 777777777777777777333333333333366666666666622222222222777777777777733333");
|
|
193 checkit ("%f%s%d%s", 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx");
|
|
194
|
|
195 return 0;
|
|
196 }
|
|
197 #endif /* TEST */
|