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