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