Commit | Line | Data |
---|---|---|
252b5132 RH |
1 | /* Like vsprintf but provides a pointer to malloc'd storage, which must |
2 | be freed by the caller. | |
5a4e47bd | 3 | Copyright (C) 1994, 2003 Free Software Foundation, Inc. |
252b5132 RH |
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 | ||
74bcd529 DD |
21 | #ifdef HAVE_CONFIG_H |
22 | #include "config.h" | |
23 | #endif | |
24 | #include <ansidecl.h> | |
25 | #ifdef ANSI_PROTOTYPES | |
252b5132 RH |
26 | #include <stdarg.h> |
27 | #else | |
28 | #include <varargs.h> | |
29 | #endif | |
f9b4e353 DD |
30 | #if !defined (va_copy) && defined (__va_copy) |
31 | # define va_copy(d,s) __va_copy((d),(s)) | |
32 | #endif | |
252b5132 | 33 | #include <stdio.h> |
843f21be | 34 | #ifdef HAVE_STRING_H |
252b5132 | 35 | #include <string.h> |
843f21be | 36 | #endif |
74bcd529 DD |
37 | #ifdef HAVE_STDLIB_H |
38 | #include <stdlib.h> | |
39 | #else | |
40 | extern unsigned long strtoul (); | |
41 | extern PTR malloc (); | |
42 | #endif | |
43 | #include "libiberty.h" | |
252b5132 RH |
44 | |
45 | #ifdef TEST | |
46 | int global_total_width; | |
47 | #endif | |
48 | ||
ba19b94f DD |
49 | /* |
50 | ||
5d852400 | 51 | @deftypefn Extension int vasprintf (char **@var{resptr}, const char *@var{format}, va_list @var{args}) |
ba19b94f DD |
52 | |
53 | Like @code{vsprintf}, but instead of passing a pointer to a buffer, | |
54 | you pass a pointer to a pointer. This function will compute the size | |
55 | of the buffer needed, allocate memory with @code{malloc}, and store a | |
56 | pointer to the allocated memory in @code{*@var{resptr}}. The value | |
57 | returned is the same as @code{vsprintf} would return. If memory could | |
5a4e47bd | 58 | not be allocated, minus one is returned and @code{NULL} is stored in |
ba19b94f DD |
59 | @code{*@var{resptr}}. |
60 | ||
61 | @end deftypefn | |
62 | ||
63 | */ | |
74bcd529 | 64 | |
2382c03f | 65 | static int int_vasprintf PARAMS ((char **, const char *, va_list)); |
252b5132 RH |
66 | |
67 | static int | |
68 | int_vasprintf (result, format, args) | |
69 | char **result; | |
70 | const char *format; | |
2382c03f | 71 | va_list args; |
252b5132 RH |
72 | { |
73 | const char *p = format; | |
74 | /* Add one to make sure that it is never zero, which might cause malloc | |
75 | to return NULL. */ | |
76 | int total_width = strlen (format) + 1; | |
77 | va_list ap; | |
78 | ||
2382c03f DD |
79 | #ifdef va_copy |
80 | va_copy (ap, args); | |
81 | #else | |
82 | memcpy ((PTR) &ap, (PTR) &args, sizeof (va_list)); | |
83 | #endif | |
252b5132 RH |
84 | |
85 | while (*p != '\0') | |
86 | { | |
87 | if (*p++ == '%') | |
88 | { | |
89 | while (strchr ("-+ #0", *p)) | |
90 | ++p; | |
91 | if (*p == '*') | |
92 | { | |
93 | ++p; | |
94 | total_width += abs (va_arg (ap, int)); | |
95 | } | |
96 | else | |
74bcd529 | 97 | total_width += strtoul (p, (char **) &p, 10); |
252b5132 RH |
98 | if (*p == '.') |
99 | { | |
100 | ++p; | |
101 | if (*p == '*') | |
102 | { | |
103 | ++p; | |
104 | total_width += abs (va_arg (ap, int)); | |
105 | } | |
106 | else | |
74bcd529 | 107 | total_width += strtoul (p, (char **) &p, 10); |
252b5132 RH |
108 | } |
109 | while (strchr ("hlL", *p)) | |
110 | ++p; | |
111 | /* Should be big enough for any format specifier except %s and floats. */ | |
112 | total_width += 30; | |
113 | switch (*p) | |
114 | { | |
115 | case 'd': | |
116 | case 'i': | |
117 | case 'o': | |
118 | case 'u': | |
119 | case 'x': | |
120 | case 'X': | |
121 | case 'c': | |
122 | (void) va_arg (ap, int); | |
123 | break; | |
124 | case 'f': | |
125 | case 'e': | |
126 | case 'E': | |
127 | case 'g': | |
128 | case 'G': | |
129 | (void) va_arg (ap, double); | |
130 | /* Since an ieee double can have an exponent of 307, we'll | |
131 | make the buffer wide enough to cover the gross case. */ | |
132 | total_width += 307; | |
133 | break; | |
134 | case 's': | |
135 | total_width += strlen (va_arg (ap, char *)); | |
136 | break; | |
137 | case 'p': | |
138 | case 'n': | |
139 | (void) va_arg (ap, char *); | |
140 | break; | |
141 | } | |
df840b63 | 142 | p++; |
252b5132 RH |
143 | } |
144 | } | |
2382c03f DD |
145 | #ifdef va_copy |
146 | va_end (ap); | |
147 | #endif | |
252b5132 RH |
148 | #ifdef TEST |
149 | global_total_width = total_width; | |
150 | #endif | |
585cc78f | 151 | *result = (char *) malloc (total_width); |
252b5132 | 152 | if (*result != NULL) |
2382c03f | 153 | return vsprintf (*result, format, args); |
252b5132 | 154 | else |
5a4e47bd | 155 | return -1; |
252b5132 RH |
156 | } |
157 | ||
158 | int | |
159 | vasprintf (result, format, args) | |
160 | char **result; | |
161 | const char *format; | |
162 | #if defined (_BSD_VA_LIST_) && defined (__FreeBSD__) | |
163 | _BSD_VA_LIST_ args; | |
164 | #else | |
165 | va_list args; | |
166 | #endif | |
167 | { | |
2382c03f | 168 | return int_vasprintf (result, format, args); |
252b5132 RH |
169 | } |
170 | ||
171 | #ifdef TEST | |
843f21be DD |
172 | static void ATTRIBUTE_PRINTF_1 |
173 | checkit VPARAMS ((const char *format, ...)) | |
252b5132 | 174 | { |
252b5132 | 175 | char *result; |
843f21be DD |
176 | VA_OPEN (args, format); |
177 | VA_FIXEDARG (args, const char *, format); | |
252b5132 | 178 | vasprintf (&result, format, args); |
843f21be DD |
179 | VA_CLOSE (args); |
180 | ||
74bcd529 | 181 | if (strlen (result) < (size_t) global_total_width) |
252b5132 RH |
182 | printf ("PASS: "); |
183 | else | |
184 | printf ("FAIL: "); | |
185 | printf ("%d %s\n", global_total_width, result); | |
843f21be DD |
186 | |
187 | free (result); | |
252b5132 RH |
188 | } |
189 | ||
74bcd529 DD |
190 | extern int main PARAMS ((void)); |
191 | ||
252b5132 RH |
192 | int |
193 | main () | |
194 | { | |
195 | checkit ("%d", 0x12345678); | |
196 | checkit ("%200d", 5); | |
197 | checkit ("%.300d", 6); | |
198 | checkit ("%100.150d", 7); | |
199 | checkit ("%s", "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\ | |
200 | 777777777777777777333333333333366666666666622222222222777777777777733333"); | |
201 | checkit ("%f%s%d%s", 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"); | |
74bcd529 DD |
202 | |
203 | return 0; | |
252b5132 RH |
204 | } |
205 | #endif /* TEST */ |