116dce513Schristos /* Implement the xvasprintf function.
2*e992f068Schristos Copyright (C) 2014-2022 Free Software Foundation, Inc.
316dce513Schristos Contributed by Manuel Lopez-Ibanez.
416dce513Schristos
516dce513Schristos This file is part of the libiberty library.
616dce513Schristos Libiberty is free software; you can redistribute it and/or
716dce513Schristos modify it under the terms of the GNU Library General Public
816dce513Schristos License as published by the Free Software Foundation; either
916dce513Schristos version 2 of the License, or (at your option) any later version.
1016dce513Schristos
1116dce513Schristos Libiberty is distributed in the hope that it will be useful,
1216dce513Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1316dce513Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1416dce513Schristos Library General Public License for more details.
1516dce513Schristos
1616dce513Schristos You should have received a copy of the GNU Library General Public
1716dce513Schristos License along with libiberty; see the file COPYING.LIB. If not, write
1816dce513Schristos to the Free Software Foundation, Inc., 51 Franklin Street - Fifth
1916dce513Schristos Floor, Boston, MA 02110-1301, USA. */
2016dce513Schristos
2116dce513Schristos #ifdef HAVE_CONFIG_H
2216dce513Schristos #include "config.h"
2316dce513Schristos #endif
2416dce513Schristos #include <ansidecl.h>
2516dce513Schristos #include <stdarg.h>
2616dce513Schristos #if !defined (va_copy) && defined (__va_copy)
2716dce513Schristos # define va_copy(d,s) __va_copy((d),(s))
2816dce513Schristos #endif
2916dce513Schristos #include <stdio.h>
3016dce513Schristos #ifdef HAVE_STRING_H
3116dce513Schristos #include <string.h>
3216dce513Schristos #endif
3316dce513Schristos #include "libiberty.h"
3416dce513Schristos #include "vprintf-support.h"
3516dce513Schristos
3616dce513Schristos /*
3716dce513Schristos
3816dce513Schristos @deftypefn Replacement char* xvasprintf (const char *@var{format}, va_list @var{args})
3916dce513Schristos
4016dce513Schristos Print to allocated string without fail. If @code{xvasprintf} fails,
4116dce513Schristos this will print a message to @code{stderr} (using the name set by
4216dce513Schristos @code{xmalloc_set_program_name}, if any) and then call @code{xexit}.
4316dce513Schristos
4416dce513Schristos @end deftypefn
4516dce513Schristos
4616dce513Schristos */
4716dce513Schristos
4816dce513Schristos char *
xvasprintf(const char * format,_BSD_VA_LIST_ args)4916dce513Schristos xvasprintf (const char *format,
5016dce513Schristos #if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
5116dce513Schristos _BSD_VA_LIST_ args)
5216dce513Schristos #else
5316dce513Schristos va_list args)
5416dce513Schristos #endif
5516dce513Schristos {
5616dce513Schristos char *result;
5716dce513Schristos int total_width = libiberty_vprintf_buffer_size (format, args);
5816dce513Schristos result = (char *) xmalloc (total_width);
5916dce513Schristos vsprintf (result, format, args);
6016dce513Schristos return result;
6116dce513Schristos }
62