1837edd6bSchristos /* Estimate the length of the string generated by a vprintf-like 2837edd6bSchristos function. Used by vasprintf and xvasprintf. 3*7e120ff0Schristos Copyright (C) 1994-2024 Free Software Foundation, Inc. 4837edd6bSchristos 5837edd6bSchristos This file is part of the libiberty library. 6837edd6bSchristos Libiberty is free software; you can redistribute it and/or 7837edd6bSchristos modify it under the terms of the GNU Library General Public 8837edd6bSchristos License as published by the Free Software Foundation; either 9837edd6bSchristos version 2 of the License, or (at your option) any later version. 10837edd6bSchristos 11837edd6bSchristos Libiberty is distributed in the hope that it will be useful, 12837edd6bSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 13837edd6bSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14837edd6bSchristos Library General Public License for more details. 15837edd6bSchristos 16837edd6bSchristos You should have received a copy of the GNU Library General Public 17837edd6bSchristos License along with libiberty; see the file COPYING.LIB. If not, write 18837edd6bSchristos to the Free Software Foundation, Inc., 51 Franklin Street - Fifth 19837edd6bSchristos Floor, Boston, MA 02110-1301, USA. */ 20837edd6bSchristos 21837edd6bSchristos #ifdef HAVE_CONFIG_H 22837edd6bSchristos #include "config.h" 23837edd6bSchristos #endif 24837edd6bSchristos #include <ansidecl.h> 25837edd6bSchristos #include <stdarg.h> 26837edd6bSchristos #if !defined (va_copy) && defined (__va_copy) 27837edd6bSchristos # define va_copy(d,s) __va_copy((d),(s)) 28837edd6bSchristos #endif 29837edd6bSchristos #include <stdio.h> 30837edd6bSchristos #ifdef HAVE_STRING_H 31837edd6bSchristos #include <string.h> 32837edd6bSchristos #endif 33837edd6bSchristos #ifdef HAVE_STDLIB_H 34837edd6bSchristos #include <stdlib.h> 35837edd6bSchristos #else 36837edd6bSchristos extern unsigned long strtoul (); 37837edd6bSchristos #endif 38837edd6bSchristos #include "libiberty.h" 39837edd6bSchristos 40837edd6bSchristos int 41837edd6bSchristos libiberty_vprintf_buffer_size (const char *format, va_list args) 42837edd6bSchristos { 43837edd6bSchristos const char *p = format; 44837edd6bSchristos /* Add one to make sure that it is never zero, which might cause malloc 45837edd6bSchristos to return NULL. */ 46837edd6bSchristos int total_width = strlen (format) + 1; 47837edd6bSchristos va_list ap; 48837edd6bSchristos 49837edd6bSchristos #ifdef va_copy 50837edd6bSchristos va_copy (ap, args); 51837edd6bSchristos #else 524b169a6bSchristos memcpy ((void *) &ap, (void *) &args, sizeof (va_list)); 53837edd6bSchristos #endif 54837edd6bSchristos 55837edd6bSchristos while (*p != '\0') 56837edd6bSchristos { 57837edd6bSchristos if (*p++ == '%') 58837edd6bSchristos { 59837edd6bSchristos while (strchr ("-+ #0", *p)) 60837edd6bSchristos ++p; 61837edd6bSchristos if (*p == '*') 62837edd6bSchristos { 63837edd6bSchristos ++p; 64837edd6bSchristos total_width += abs (va_arg (ap, int)); 65837edd6bSchristos } 66837edd6bSchristos else 67837edd6bSchristos total_width += strtoul (p, (char **) &p, 10); 68837edd6bSchristos if (*p == '.') 69837edd6bSchristos { 70837edd6bSchristos ++p; 71837edd6bSchristos if (*p == '*') 72837edd6bSchristos { 73837edd6bSchristos ++p; 74837edd6bSchristos total_width += abs (va_arg (ap, int)); 75837edd6bSchristos } 76837edd6bSchristos else 77837edd6bSchristos total_width += strtoul (p, (char **) &p, 10); 78837edd6bSchristos } 79837edd6bSchristos while (strchr ("hlL", *p)) 80837edd6bSchristos ++p; 81837edd6bSchristos /* Should be big enough for any format specifier except %s and floats. */ 82837edd6bSchristos total_width += 30; 83837edd6bSchristos switch (*p) 84837edd6bSchristos { 85837edd6bSchristos case 'd': 86837edd6bSchristos case 'i': 87837edd6bSchristos case 'o': 88837edd6bSchristos case 'u': 89837edd6bSchristos case 'x': 90837edd6bSchristos case 'X': 91837edd6bSchristos case 'c': 92837edd6bSchristos (void) va_arg (ap, int); 93837edd6bSchristos break; 94837edd6bSchristos case 'f': 95837edd6bSchristos case 'e': 96837edd6bSchristos case 'E': 97837edd6bSchristos case 'g': 98837edd6bSchristos case 'G': 99837edd6bSchristos (void) va_arg (ap, double); 100837edd6bSchristos /* Since an ieee double can have an exponent of 307, we'll 101837edd6bSchristos make the buffer wide enough to cover the gross case. */ 102837edd6bSchristos total_width += 307; 103837edd6bSchristos break; 104837edd6bSchristos case 's': 105837edd6bSchristos total_width += strlen (va_arg (ap, char *)); 106837edd6bSchristos break; 107837edd6bSchristos case 'p': 108837edd6bSchristos case 'n': 109837edd6bSchristos (void) va_arg (ap, char *); 110837edd6bSchristos break; 111837edd6bSchristos } 112837edd6bSchristos p++; 113837edd6bSchristos } 114837edd6bSchristos } 115837edd6bSchristos #ifdef va_copy 116837edd6bSchristos va_end (ap); 117837edd6bSchristos #endif 118837edd6bSchristos return total_width; 119837edd6bSchristos } 120