1 /* Estimate the length of the string generated by a vprintf-like
2 function. Used by vasprintf and xvasprintf.
3 Copyright (C) 1994, 2003, 2011, 2013, 2014 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 ((PTR) &ap, (PTR) &args, sizeof (va_list));
59 while (strchr ("-+ #0", *p))
64 total_width += abs (va_arg (ap, int));
67 total_width += strtoul (p, (char **) &p, 10);
74 total_width += abs (va_arg (ap, int));
77 total_width += strtoul (p, (char **) &p, 10);
79 while (strchr ("hlL", *p))
81 /* Should be big enough for any format specifier except %s and floats. */
92 (void) va_arg (ap, int);
99 (void) va_arg (ap, double);
100 /* Since an ieee double can have an exponent of 307, we'll
101 make the buffer wide enough to cover the gross case. */
105 total_width += strlen (va_arg (ap, char *));
109 (void) va_arg (ap, char *);