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