2001-05-28 Philip Blundell <philb@gnu.org>
[binutils.git] / libiberty / vasprintf.c
blobc34585d04987c5efb2fc54f040c15bf71f4d5da7
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.
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.
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.
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. */
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"
40 #ifdef TEST
41 int global_total_width;
42 #endif
45 static int int_vasprintf PARAMS ((char **, const char *, va_list *));
47 static int
48 int_vasprintf (result, format, args)
49 char **result;
50 const char *format;
51 va_list *args;
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;
59 memcpy ((PTR) &ap, (PTR) args, sizeof (va_list));
61 while (*p != '\0')
63 if (*p++ == '%')
65 while (strchr ("-+ #0", *p))
66 ++p;
67 if (*p == '*')
69 ++p;
70 total_width += abs (va_arg (ap, int));
72 else
73 total_width += strtoul (p, (char **) &p, 10);
74 if (*p == '.')
76 ++p;
77 if (*p == '*')
79 ++p;
80 total_width += abs (va_arg (ap, int));
82 else
83 total_width += strtoul (p, (char **) &p, 10);
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)
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;
118 p++;
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;
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
141 return int_vasprintf (result, format, &args);
144 #ifdef TEST
145 static void checkit PARAMS ((const char *, ...));
147 static void
148 checkit VPARAMS ((const char* format, ...))
150 va_list args;
151 char *result;
152 #ifndef ANSI_PROTOTYPES
153 const char *format;
154 #endif
156 VA_START (args, format);
158 #ifndef ANSI_PROTOTYPES
159 format = va_arg (args, const char *);
160 #endif
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);
170 extern int main PARAMS ((void));
173 main ()
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");
183 return 0;
185 #endif /* TEST */