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