1299425Smm/* Implement the xvasprintf function.
2299425Smm   Copyright (C) 2014 Free Software Foundation, Inc.
3299425Smm   Contributed by Manuel Lopez-Ibanez.
4299425Smm
5299425SmmThis file is part of the libiberty library.
6299425SmmLibiberty is free software; you can redistribute it and/or
7299425Smmmodify it under the terms of the GNU Library General Public
8299425SmmLicense as published by the Free Software Foundation; either
9299425Smmversion 2 of the License, or (at your option) any later version.
10299425Smm
11299425SmmLibiberty is distributed in the hope that it will be useful,
12299425Smmbut WITHOUT ANY WARRANTY; without even the implied warranty of
13299425SmmMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14299425SmmLibrary General Public License for more details.
15299425Smm
16299425SmmYou should have received a copy of the GNU Library General Public
17299425SmmLicense along with libiberty; see the file COPYING.LIB.  If not, write
18299425Smmto the Free Software Foundation, Inc., 51 Franklin Street - Fifth
19299425SmmFloor, Boston, MA 02110-1301, USA.  */
20299425Smm
21299425Smm#ifdef HAVE_CONFIG_H
22299425Smm#include "config.h"
23299425Smm#endif
24299425Smm#include <ansidecl.h>
25299425Smm#include <stdarg.h>
26299425Smm#if !defined (va_copy) && defined (__va_copy)
27299425Smm# define va_copy(d,s)  __va_copy((d),(s))
28299425Smm#endif
29299425Smm#include <stdio.h>
30299425Smm#ifdef HAVE_STRING_H
31299425Smm#include <string.h>
32299425Smm#endif
33299425Smm#include "libiberty.h"
34299425Smm#include "vprintf-support.h"
35299425Smm
36299425Smm/*
37299425Smm
38299425Smm@deftypefn Replacement char* xvasprintf (const char *@var{format}, va_list @var{args})
39299425Smm
40299425SmmPrint to allocated string without fail.  If @code{xvasprintf} fails,
41299425Smmthis will print a message to @code{stderr} (using the name set by
42299425Smm@code{xmalloc_set_program_name}, if any) and then call @code{xexit}.
43299425Smm
44299425Smm@end deftypefn
45299425Smm
46299425Smm*/
47299425Smm
48299425Smmchar *
49299425Smmxvasprintf (const char *format,
50299425Smm#if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
51299425Smm           _BSD_VA_LIST_ args)
52299425Smm#else
53299425Smm           va_list args)
54299425Smm#endif
55299425Smm{
56299425Smm  char *result;
57299425Smm  int total_width = libiberty_vprintf_buffer_size (format, args);
58299425Smm  result = (char *) xmalloc (total_width);
59299425Smm  vsprintf (result, format, args);
60299425Smm  return result;
61299425Smm}
62299425Smm