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