Home | History | Annotate | Line # | Download | only in libiberty
      1      1.1  christos /* Implement the xvasprintf function.
      2  1.1.1.6  christos    Copyright (C) 2014-2024 Free Software Foundation, Inc.
      3      1.1  christos    Contributed by Manuel Lopez-Ibanez.
      4      1.1  christos 
      5      1.1  christos This file is part of the libiberty library.
      6      1.1  christos Libiberty is free software; you can redistribute it and/or
      7      1.1  christos modify it under the terms of the GNU Library General Public
      8      1.1  christos License as published by the Free Software Foundation; either
      9      1.1  christos version 2 of the License, or (at your option) any later version.
     10      1.1  christos 
     11      1.1  christos Libiberty is distributed in the hope that it will be useful,
     12      1.1  christos but WITHOUT ANY WARRANTY; without even the implied warranty of
     13      1.1  christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14      1.1  christos Library General Public License for more details.
     15      1.1  christos 
     16      1.1  christos You should have received a copy of the GNU Library General Public
     17      1.1  christos License along with libiberty; see the file COPYING.LIB.  If not, write
     18      1.1  christos to the Free Software Foundation, Inc., 51 Franklin Street - Fifth
     19      1.1  christos Floor, Boston, MA 02110-1301, USA.  */
     20      1.1  christos 
     21      1.1  christos #ifdef HAVE_CONFIG_H
     22      1.1  christos #include "config.h"
     23      1.1  christos #endif
     24      1.1  christos #include <ansidecl.h>
     25      1.1  christos #include <stdarg.h>
     26      1.1  christos #if !defined (va_copy) && defined (__va_copy)
     27      1.1  christos # define va_copy(d,s)  __va_copy((d),(s))
     28      1.1  christos #endif
     29      1.1  christos #include <stdio.h>
     30      1.1  christos #ifdef HAVE_STRING_H
     31      1.1  christos #include <string.h>
     32      1.1  christos #endif
     33      1.1  christos #include "libiberty.h"
     34      1.1  christos #include "vprintf-support.h"
     35      1.1  christos 
     36      1.1  christos /*
     37      1.1  christos 
     38      1.1  christos @deftypefn Replacement char* xvasprintf (const char *@var{format}, va_list @var{args})
     39      1.1  christos 
     40      1.1  christos Print to allocated string without fail.  If @code{xvasprintf} fails,
     41      1.1  christos this will print a message to @code{stderr} (using the name set by
     42      1.1  christos @code{xmalloc_set_program_name}, if any) and then call @code{xexit}.
     43      1.1  christos 
     44      1.1  christos @end deftypefn
     45      1.1  christos 
     46      1.1  christos */
     47      1.1  christos 
     48      1.1  christos char *
     49      1.1  christos xvasprintf (const char *format,
     50      1.1  christos #if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
     51      1.1  christos            _BSD_VA_LIST_ args)
     52      1.1  christos #else
     53      1.1  christos            va_list args)
     54      1.1  christos #endif
     55      1.1  christos {
     56      1.1  christos   char *result;
     57      1.1  christos   int total_width = libiberty_vprintf_buffer_size (format, args);
     58      1.1  christos   result = (char *) xmalloc (total_width);
     59      1.1  christos   vsprintf (result, format, args);
     60      1.1  christos   return result;
     61      1.1  christos }
     62