1 /* Like vsprintf but provides a pointer to malloc'd storage, which must
2 be freed by the caller.
3 Copyright (C) 1994, 2003 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. */
25 #ifdef ANSI_PROTOTYPES
30 #if !defined (va_copy) && defined (__va_copy)
31 # define va_copy(d,s) __va_copy((d),(s))
40 extern unsigned long strtoul ();
43 #include "libiberty.h"
46 int global_total_width
;
51 @deftypefn Extension int vasprintf (char **@var{resptr}, const char *@var{format}, va_list @var{args})
53 Like @code{vsprintf}, but instead of passing a pointer to a buffer,
54 you pass a pointer to a pointer. This function will compute the size
55 of the buffer needed, allocate memory with @code{malloc}, and store a
56 pointer to the allocated memory in @code{*@var{resptr}}. The value
57 returned is the same as @code{vsprintf} would return. If memory could
58 not be allocated, minus one is returned and @code{NULL} is stored in
65 static int int_vasprintf
PARAMS ((char **, const char *, va_list));
68 int_vasprintf (result
, format
, args
)
73 const char *p
= format
;
74 /* Add one to make sure that it is never zero, which might cause malloc
76 int total_width
= strlen (format
) + 1;
82 memcpy ((PTR
) &ap
, (PTR
) &args
, sizeof (va_list));
89 while (strchr ("-+ #0", *p
))
94 total_width
+= abs (va_arg (ap
, int));
97 total_width
+= strtoul (p
, (char **) &p
, 10);
104 total_width
+= abs (va_arg (ap
, int));
107 total_width
+= strtoul (p
, (char **) &p
, 10);
109 while (strchr ("hlL", *p
))
111 /* Should be big enough for any format specifier except %s and floats. */
122 (void) va_arg (ap
, int);
129 (void) va_arg (ap
, double);
130 /* Since an ieee double can have an exponent of 307, we'll
131 make the buffer wide enough to cover the gross case. */
135 total_width
+= strlen (va_arg (ap
, char *));
139 (void) va_arg (ap
, char *);
149 global_total_width
= total_width
;
151 *result
= (char *) malloc (total_width
);
153 return vsprintf (*result
, format
, args
);
159 vasprintf (result
, format
, args
)
162 #if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
168 return int_vasprintf (result
, format
, args
);
172 static void ATTRIBUTE_PRINTF_1
173 checkit
VPARAMS ((const char *format
, ...))
176 VA_OPEN (args
, format
);
177 VA_FIXEDARG (args
, const char *, format
);
178 vasprintf (&result
, format
, args
);
181 if (strlen (result
) < (size_t) global_total_width
)
185 printf ("%d %s\n", global_total_width
, result
);
190 extern int main
PARAMS ((void));
195 checkit ("%d", 0x12345678);
196 checkit ("%200d", 5);
197 checkit ("%.300d", 6);
198 checkit ("%100.150d", 7);
199 checkit ("%s", "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
200 777777777777777777333333333333366666666666622222222222777777777777733333");
201 checkit ("%f%s%d%s", 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx");
This page took 0.033461 seconds and 4 git commands to generate.