Home | History | Annotate | Line # | Download | only in libiberty
vasprintf.c revision 1.1.1.9
      1      1.1     skrll /* Like vsprintf but provides a pointer to malloc'd storage, which must
      2      1.1     skrll    be freed by the caller.
      3  1.1.1.9  christos    Copyright (C) 1994-2026 Free Software Foundation, Inc.
      4      1.1     skrll 
      5      1.1     skrll This file is part of the libiberty library.
      6      1.1     skrll Libiberty is free software; you can redistribute it and/or
      7      1.1     skrll modify it under the terms of the GNU Library General Public
      8      1.1     skrll License as published by the Free Software Foundation; either
      9      1.1     skrll version 2 of the License, or (at your option) any later version.
     10      1.1     skrll 
     11      1.1     skrll Libiberty is distributed in the hope that it will be useful,
     12      1.1     skrll but WITHOUT ANY WARRANTY; without even the implied warranty of
     13      1.1     skrll MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14      1.1     skrll Library General Public License for more details.
     15      1.1     skrll 
     16      1.1     skrll You should have received a copy of the GNU Library General Public
     17  1.1.1.3  christos License along with libiberty; see the file COPYING.LIB.  If not, write
     18  1.1.1.3  christos to the Free Software Foundation, Inc., 51 Franklin Street - Fifth
     19  1.1.1.3  christos Floor, Boston, MA 02110-1301, USA.  */
     20      1.1     skrll 
     21      1.1     skrll #ifdef HAVE_CONFIG_H
     22      1.1     skrll #include "config.h"
     23      1.1     skrll #endif
     24      1.1     skrll #include <ansidecl.h>
     25      1.1     skrll #include <stdarg.h>
     26      1.1     skrll #if !defined (va_copy) && defined (__va_copy)
     27      1.1     skrll # define va_copy(d,s)  __va_copy((d),(s))
     28      1.1     skrll #endif
     29      1.1     skrll #include <stdio.h>
     30      1.1     skrll #ifdef HAVE_STRING_H
     31      1.1     skrll #include <string.h>
     32      1.1     skrll #endif
     33      1.1     skrll #ifdef HAVE_STDLIB_H
     34      1.1     skrll #include <stdlib.h>
     35      1.1     skrll #else
     36  1.1.1.6  christos extern void *malloc ();
     37      1.1     skrll #endif
     38      1.1     skrll #include "libiberty.h"
     39  1.1.1.3  christos #include "vprintf-support.h"
     40      1.1     skrll 
     41      1.1     skrll #ifdef TEST
     42      1.1     skrll int global_total_width;
     43      1.1     skrll #endif
     44      1.1     skrll 
     45      1.1     skrll /*
     46      1.1     skrll 
     47  1.1.1.2  christos @deftypefn Extension int vasprintf (char **@var{resptr}, @
     48  1.1.1.2  christos   const char *@var{format}, va_list @var{args})
     49      1.1     skrll 
     50      1.1     skrll Like @code{vsprintf}, but instead of passing a pointer to a buffer,
     51      1.1     skrll you pass a pointer to a pointer.  This function will compute the size
     52      1.1     skrll of the buffer needed, allocate memory with @code{malloc}, and store a
     53      1.1     skrll pointer to the allocated memory in @code{*@var{resptr}}.  The value
     54      1.1     skrll returned is the same as @code{vsprintf} would return.  If memory could
     55      1.1     skrll not be allocated, minus one is returned and @code{NULL} is stored in
     56      1.1     skrll @code{*@var{resptr}}.
     57      1.1     skrll 
     58      1.1     skrll @end deftypefn
     59      1.1     skrll 
     60      1.1     skrll */
     61      1.1     skrll 
     62      1.1     skrll static int int_vasprintf (char **, const char *, va_list);
     63      1.1     skrll 
     64      1.1     skrll static int
     65      1.1     skrll int_vasprintf (char **result, const char *format, va_list args)
     66      1.1     skrll {
     67  1.1.1.3  christos   int total_width = libiberty_vprintf_buffer_size (format, args);
     68      1.1     skrll #ifdef TEST
     69      1.1     skrll   global_total_width = total_width;
     70      1.1     skrll #endif
     71      1.1     skrll   *result = (char *) malloc (total_width);
     72      1.1     skrll   if (*result != NULL)
     73      1.1     skrll     return vsprintf (*result, format, args);
     74      1.1     skrll   else
     75      1.1     skrll     return -1;
     76      1.1     skrll }
     77      1.1     skrll 
     78      1.1     skrll int
     79      1.1     skrll vasprintf (char **result, const char *format,
     80      1.1     skrll #if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
     81      1.1     skrll            _BSD_VA_LIST_ args)
     82      1.1     skrll #else
     83      1.1     skrll            va_list args)
     84      1.1     skrll #endif
     85      1.1     skrll {
     86      1.1     skrll   return int_vasprintf (result, format, args);
     87      1.1     skrll }
     88      1.1     skrll 
     89      1.1     skrll #ifdef TEST
     90      1.1     skrll static void ATTRIBUTE_PRINTF_1
     91      1.1     skrll checkit (const char *format, ...)
     92      1.1     skrll {
     93      1.1     skrll   char *result;
     94  1.1.1.3  christos   va_list args;
     95  1.1.1.3  christos   va_start (args, format);
     96      1.1     skrll   vasprintf (&result, format, args);
     97  1.1.1.3  christos   va_end (args);
     98      1.1     skrll 
     99      1.1     skrll   if (strlen (result) < (size_t) global_total_width)
    100      1.1     skrll     printf ("PASS: ");
    101      1.1     skrll   else
    102      1.1     skrll     printf ("FAIL: ");
    103      1.1     skrll   printf ("%d %s\n", global_total_width, result);
    104      1.1     skrll 
    105      1.1     skrll   free (result);
    106      1.1     skrll }
    107      1.1     skrll 
    108      1.1     skrll extern int main (void);
    109      1.1     skrll 
    110      1.1     skrll int
    111      1.1     skrll main (void)
    112      1.1     skrll {
    113      1.1     skrll   checkit ("%d", 0x12345678);
    114      1.1     skrll   checkit ("%200d", 5);
    115      1.1     skrll   checkit ("%.300d", 6);
    116      1.1     skrll   checkit ("%100.150d", 7);
    117      1.1     skrll   checkit ("%s", "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
    118      1.1     skrll 777777777777777777333333333333366666666666622222222222777777777777733333");
    119      1.1     skrll   checkit ("%f%s%d%s", 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx");
    120      1.1     skrll 
    121      1.1     skrll   return 0;
    122      1.1     skrll }
    123      1.1     skrll #endif /* TEST */
    124