xref: /netbsd-src/external/gpl3/gcc/dist/libiberty/vprintf-support.c (revision e9e6e0f6fbc36b8de7586170291cf5fc97cab8b6)
14d5abbe8Smrg /* Estimate the length of the string generated by a vprintf-like
24d5abbe8Smrg    function.  Used by vasprintf and xvasprintf.
3*e9e6e0f6Smrg    Copyright (C) 1994-2022 Free Software Foundation, Inc.
44d5abbe8Smrg 
54d5abbe8Smrg This file is part of the libiberty library.
64d5abbe8Smrg Libiberty is free software; you can redistribute it and/or
74d5abbe8Smrg modify it under the terms of the GNU Library General Public
84d5abbe8Smrg License as published by the Free Software Foundation; either
94d5abbe8Smrg version 2 of the License, or (at your option) any later version.
104d5abbe8Smrg 
114d5abbe8Smrg Libiberty is distributed in the hope that it will be useful,
124d5abbe8Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
134d5abbe8Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
144d5abbe8Smrg Library General Public License for more details.
154d5abbe8Smrg 
164d5abbe8Smrg You should have received a copy of the GNU Library General Public
174d5abbe8Smrg License along with libiberty; see the file COPYING.LIB.  If not, write
184d5abbe8Smrg to the Free Software Foundation, Inc., 51 Franklin Street - Fifth
194d5abbe8Smrg Floor, Boston, MA 02110-1301, USA.  */
204d5abbe8Smrg 
214d5abbe8Smrg #ifdef HAVE_CONFIG_H
224d5abbe8Smrg #include "config.h"
234d5abbe8Smrg #endif
244d5abbe8Smrg #include <ansidecl.h>
254d5abbe8Smrg #include <stdarg.h>
264d5abbe8Smrg #if !defined (va_copy) && defined (__va_copy)
274d5abbe8Smrg # define va_copy(d,s)  __va_copy((d),(s))
284d5abbe8Smrg #endif
294d5abbe8Smrg #include <stdio.h>
304d5abbe8Smrg #ifdef HAVE_STRING_H
314d5abbe8Smrg #include <string.h>
324d5abbe8Smrg #endif
334d5abbe8Smrg #ifdef HAVE_STDLIB_H
344d5abbe8Smrg #include <stdlib.h>
354d5abbe8Smrg #else
364d5abbe8Smrg extern unsigned long strtoul ();
374d5abbe8Smrg #endif
384d5abbe8Smrg #include "libiberty.h"
39c91d24e1Smrg #include "vprintf-support.h"
404d5abbe8Smrg 
414d5abbe8Smrg int
libiberty_vprintf_buffer_size(const char * format,va_list args)424d5abbe8Smrg libiberty_vprintf_buffer_size (const char *format, va_list args)
434d5abbe8Smrg {
444d5abbe8Smrg   const char *p = format;
454d5abbe8Smrg   /* Add one to make sure that it is never zero, which might cause malloc
464d5abbe8Smrg      to return NULL.  */
474d5abbe8Smrg   int total_width = strlen (format) + 1;
484d5abbe8Smrg   va_list ap;
494d5abbe8Smrg 
504d5abbe8Smrg #ifdef va_copy
514d5abbe8Smrg   va_copy (ap, args);
524d5abbe8Smrg #else
534d5abbe8Smrg   memcpy ((PTR) &ap, (PTR) &args, sizeof (va_list));
544d5abbe8Smrg #endif
554d5abbe8Smrg 
564d5abbe8Smrg   while (*p != '\0')
574d5abbe8Smrg     {
584d5abbe8Smrg       if (*p++ == '%')
594d5abbe8Smrg 	{
604d5abbe8Smrg 	  while (strchr ("-+ #0", *p))
614d5abbe8Smrg 	    ++p;
624d5abbe8Smrg 	  if (*p == '*')
634d5abbe8Smrg 	    {
644d5abbe8Smrg 	      ++p;
654d5abbe8Smrg 	      total_width += abs (va_arg (ap, int));
664d5abbe8Smrg 	    }
674d5abbe8Smrg 	  else
684d5abbe8Smrg 	    total_width += strtoul (p, (char **) &p, 10);
694d5abbe8Smrg 	  if (*p == '.')
704d5abbe8Smrg 	    {
714d5abbe8Smrg 	      ++p;
724d5abbe8Smrg 	      if (*p == '*')
734d5abbe8Smrg 		{
744d5abbe8Smrg 		  ++p;
754d5abbe8Smrg 		  total_width += abs (va_arg (ap, int));
764d5abbe8Smrg 		}
774d5abbe8Smrg 	      else
784d5abbe8Smrg 	      total_width += strtoul (p, (char **) &p, 10);
794d5abbe8Smrg 	    }
804d5abbe8Smrg 	  while (strchr ("hlL", *p))
814d5abbe8Smrg 	    ++p;
824d5abbe8Smrg 	  /* Should be big enough for any format specifier except %s and floats.  */
834d5abbe8Smrg 	  total_width += 30;
844d5abbe8Smrg 	  switch (*p)
854d5abbe8Smrg 	    {
864d5abbe8Smrg 	    case 'd':
874d5abbe8Smrg 	    case 'i':
884d5abbe8Smrg 	    case 'o':
894d5abbe8Smrg 	    case 'u':
904d5abbe8Smrg 	    case 'x':
914d5abbe8Smrg 	    case 'X':
924d5abbe8Smrg 	    case 'c':
934d5abbe8Smrg 	      (void) va_arg (ap, int);
944d5abbe8Smrg 	      break;
954d5abbe8Smrg 	    case 'f':
964d5abbe8Smrg 	    case 'e':
974d5abbe8Smrg 	    case 'E':
984d5abbe8Smrg 	    case 'g':
994d5abbe8Smrg 	    case 'G':
1004d5abbe8Smrg 	      (void) va_arg (ap, double);
1014d5abbe8Smrg 	      /* Since an ieee double can have an exponent of 307, we'll
1024d5abbe8Smrg 		 make the buffer wide enough to cover the gross case. */
1034d5abbe8Smrg 	      total_width += 307;
1044d5abbe8Smrg 	      break;
1054d5abbe8Smrg 	    case 's':
1064d5abbe8Smrg 	      total_width += strlen (va_arg (ap, char *));
1074d5abbe8Smrg 	      break;
1084d5abbe8Smrg 	    case 'p':
1094d5abbe8Smrg 	    case 'n':
1104d5abbe8Smrg 	      (void) va_arg (ap, char *);
1114d5abbe8Smrg 	      break;
1124d5abbe8Smrg 	    }
1134d5abbe8Smrg 	  p++;
1144d5abbe8Smrg 	}
1154d5abbe8Smrg     }
1164d5abbe8Smrg #ifdef va_copy
1174d5abbe8Smrg   va_end (ap);
1184d5abbe8Smrg #endif
1194d5abbe8Smrg   return total_width;
1204d5abbe8Smrg }
121