1 /* Estimate the length of the string generated by a vprintf-like
2 function. Used by vasprintf and xvasprintf.
3 Copyright (C) 1994-2025 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 not, write
18 to the Free Software Foundation, Inc., 51 Franklin Street - Fifth
19 Floor, Boston, MA 02110-1301, USA. */
26 #if !defined (va_copy) && defined (__va_copy)
27 # define va_copy(d,s) __va_copy((d),(s))
36 extern unsigned long strtoul ();
38 #include "libiberty.h"
41 libiberty_vprintf_buffer_size (const char *format
, va_list args
)
43 const char *p
= format
;
44 /* Add one to make sure that it is never zero, which might cause malloc
46 int total_width
= strlen (format
) + 1;
52 memcpy ((void *) &ap
, (void *) &args
, sizeof (va_list));
60 while (strchr ("-+ #0", *p
))
65 total_width
+= abs (va_arg (ap
, int));
68 total_width
+= strtoul (p
, (char **) &p
, 10);
75 total_width
+= abs (va_arg (ap
, int));
78 total_width
+= strtoul (p
, (char **) &p
, 10);
102 if (p
[1] == '6' && p
[2] == '4')
117 /* Should be big enough for any format specifier except %s and floats. */
129 case 0: (void) va_arg (ap
, int); break;
130 case 1: (void) va_arg (ap
, long int); break;
131 case 2: (void) va_arg (ap
, long long int); break;
132 case 3: (void) va_arg (ap
, size_t); break;
133 case 4: (void) va_arg (ap
, ptrdiff_t); break;
137 (void) va_arg (ap
, int);
146 (void) va_arg (ap
, double);
147 /* Since an ieee double can have an exponent of 308, we'll
148 make the buffer wide enough to cover the gross case. */
153 (void) va_arg (ap
, long double);
158 total_width
+= strlen (va_arg (ap
, char *));
162 (void) va_arg (ap
, char *);