]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/vasprintf.c
* toplev.c (print_switch_values): Make static to match prototype.
[thirdparty/gcc.git] / libiberty / vasprintf.c
CommitLineData
6599da04
JM
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
5This file is part of the libiberty library.
6Libiberty is free software; you can redistribute it and/or
7modify it under the terms of the GNU Library General Public
8License as published by the Free Software Foundation; either
9version 2 of the License, or (at your option) any later version.
10
11Libiberty is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14Library General Public License for more details.
15
16You should have received a copy of the GNU Library General Public
17License along with libiberty; see the file COPYING.LIB. If
18not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
20
21#include <stdio.h>
22#include <string.h>
23#include <ansidecl.h>
24#ifdef __STDC__
25#include <stdarg.h>
26#else
27#include <varargs.h>
28#endif
29
30#ifdef TEST
31int global_total_width;
32#endif
33
34unsigned long strtoul ();
35char *malloc ();
36
37static int
38int_vasprintf (result, format, args)
39 char **result;
40 const char *format;
41 va_list *args;
42{
43 const char *p = format;
44 /* Add one to make sure that it is never zero, which might cause malloc
45 to return NULL. */
46 int total_width = strlen (format) + 1;
47 va_list ap;
48
49 memcpy ((PTR) &ap, (PTR) args, sizeof (va_list));
50
51 while (*p != '\0')
52 {
53 if (*p++ == '%')
54 {
55 while (strchr ("-+ #0", *p))
56 ++p;
57 if (*p == '*')
58 {
59 ++p;
60 total_width += abs (va_arg (ap, int));
61 }
62 else
63 total_width += strtoul (p, &p, 10);
64 if (*p == '.')
65 {
66 ++p;
67 if (*p == '*')
68 {
69 ++p;
70 total_width += abs (va_arg (ap, int));
71 }
72 else
73 total_width += strtoul (p, &p, 10);
74 }
75 while (strchr ("hlL", *p))
76 ++p;
5890bc92 77 /* Should be big enough for any format specifier except %s and floats. */
6599da04
JM
78 total_width += 30;
79 switch (*p)
80 {
81 case 'd':
82 case 'i':
83 case 'o':
84 case 'u':
85 case 'x':
86 case 'X':
87 case 'c':
88 (void) va_arg (ap, int);
89 break;
90 case 'f':
91 case 'e':
92 case 'E':
93 case 'g':
94 case 'G':
95 (void) va_arg (ap, double);
5890bc92
JL
96 /* Since an ieee double can have an exponent of 307, we'll
97 make the buffer wide enough to cover the gross case. */
98 total_width += 307;
6599da04
JM
99 break;
100 case 's':
101 total_width += strlen (va_arg (ap, char *));
102 break;
103 case 'p':
104 case 'n':
105 (void) va_arg (ap, char *);
106 break;
107 }
108 }
109 }
110#ifdef TEST
111 global_total_width = total_width;
112#endif
113 *result = malloc (total_width);
114 if (*result != NULL)
115 return vsprintf (*result, format, *args);
116 else
117 return 0;
118}
119
120int
121vasprintf (result, format, args)
122 char **result;
123 const char *format;
19ddc834
JM
124#if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
125 _BSD_VA_LIST_ args;
126#else
6599da04 127 va_list args;
19ddc834 128#endif
6599da04
JM
129{
130 return int_vasprintf (result, format, &args);
131}
132
133#ifdef TEST
134void
135checkit
136#ifdef __STDC__
137 (const char* format, ...)
138#else
139 (va_alist)
140 va_dcl
141#endif
142{
143 va_list args;
144 char *result;
145
146#ifdef __STDC__
147 va_start (args, format);
148#else
149 char *format;
150 va_start (args);
151 format = va_arg (args, char *);
152#endif
153 vasprintf (&result, format, args);
154 if (strlen (result) < global_total_width)
155 printf ("PASS: ");
156 else
157 printf ("FAIL: ");
158 printf ("%d %s\n", global_total_width, result);
159}
160
161int
162main ()
163{
164 checkit ("%d", 0x12345678);
165 checkit ("%200d", 5);
166 checkit ("%.300d", 6);
167 checkit ("%100.150d", 7);
168 checkit ("%s", "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
169777777777777777777333333333333366666666666622222222222777777777777733333");
170 checkit ("%f%s%d%s", 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx");
171}
172#endif /* TEST */