xref: /netbsd-src/external/gpl3/gcc/dist/libiberty/xvasprintf.c (revision b1e838363e3c6fc78a55519254d99869742dd33c)
14d5abbe8Smrg /* Implement the xvasprintf function.
2*b1e83836Smrg    Copyright (C) 2014-2022 Free Software Foundation, Inc.
34d5abbe8Smrg    Contributed by Manuel Lopez-Ibanez.
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 #include "libiberty.h"
344d5abbe8Smrg #include "vprintf-support.h"
354d5abbe8Smrg 
364d5abbe8Smrg /*
374d5abbe8Smrg 
384d5abbe8Smrg @deftypefn Replacement char* xvasprintf (const char *@var{format}, va_list @var{args})
394d5abbe8Smrg 
404d5abbe8Smrg Print to allocated string without fail.  If @code{xvasprintf} fails,
414d5abbe8Smrg this will print a message to @code{stderr} (using the name set by
424d5abbe8Smrg @code{xmalloc_set_program_name}, if any) and then call @code{xexit}.
434d5abbe8Smrg 
444d5abbe8Smrg @end deftypefn
454d5abbe8Smrg 
464d5abbe8Smrg */
474d5abbe8Smrg 
484d5abbe8Smrg char *
xvasprintf(const char * format,_BSD_VA_LIST_ args)494d5abbe8Smrg xvasprintf (const char *format,
504d5abbe8Smrg #if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
514d5abbe8Smrg            _BSD_VA_LIST_ args)
524d5abbe8Smrg #else
534d5abbe8Smrg            va_list args)
544d5abbe8Smrg #endif
554d5abbe8Smrg {
564d5abbe8Smrg   char *result;
574d5abbe8Smrg   int total_width = libiberty_vprintf_buffer_size (format, args);
584d5abbe8Smrg   result = (char *) xmalloc (total_width);
594d5abbe8Smrg   vsprintf (result, format, args);
604d5abbe8Smrg   return result;
614d5abbe8Smrg }
62