xref: /netbsd-src/external/gpl3/gdb/dist/libiberty/xvasprintf.c (revision 7e120ff03ede3fe64e2c8620c01465d528502ddb)
1837edd6bSchristos /* Implement the xvasprintf function.
2*7e120ff0Schristos    Copyright (C) 2014-2024 Free Software Foundation, Inc.
3837edd6bSchristos    Contributed by Manuel Lopez-Ibanez.
4837edd6bSchristos 
5837edd6bSchristos This file is part of the libiberty library.
6837edd6bSchristos Libiberty is free software; you can redistribute it and/or
7837edd6bSchristos modify it under the terms of the GNU Library General Public
8837edd6bSchristos License as published by the Free Software Foundation; either
9837edd6bSchristos version 2 of the License, or (at your option) any later version.
10837edd6bSchristos 
11837edd6bSchristos Libiberty is distributed in the hope that it will be useful,
12837edd6bSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of
13837edd6bSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14837edd6bSchristos Library General Public License for more details.
15837edd6bSchristos 
16837edd6bSchristos You should have received a copy of the GNU Library General Public
17837edd6bSchristos License along with libiberty; see the file COPYING.LIB.  If not, write
18837edd6bSchristos to the Free Software Foundation, Inc., 51 Franklin Street - Fifth
19837edd6bSchristos Floor, Boston, MA 02110-1301, USA.  */
20837edd6bSchristos 
21837edd6bSchristos #ifdef HAVE_CONFIG_H
22837edd6bSchristos #include "config.h"
23837edd6bSchristos #endif
24837edd6bSchristos #include <ansidecl.h>
25837edd6bSchristos #include <stdarg.h>
26837edd6bSchristos #if !defined (va_copy) && defined (__va_copy)
27837edd6bSchristos # define va_copy(d,s)  __va_copy((d),(s))
28837edd6bSchristos #endif
29837edd6bSchristos #include <stdio.h>
30837edd6bSchristos #ifdef HAVE_STRING_H
31837edd6bSchristos #include <string.h>
32837edd6bSchristos #endif
33837edd6bSchristos #include "libiberty.h"
34837edd6bSchristos #include "vprintf-support.h"
35837edd6bSchristos 
36837edd6bSchristos /*
37837edd6bSchristos 
38837edd6bSchristos @deftypefn Replacement char* xvasprintf (const char *@var{format}, va_list @var{args})
39837edd6bSchristos 
40837edd6bSchristos Print to allocated string without fail.  If @code{xvasprintf} fails,
41837edd6bSchristos this will print a message to @code{stderr} (using the name set by
42837edd6bSchristos @code{xmalloc_set_program_name}, if any) and then call @code{xexit}.
43837edd6bSchristos 
44837edd6bSchristos @end deftypefn
45837edd6bSchristos 
46837edd6bSchristos */
47837edd6bSchristos 
48837edd6bSchristos char *
49837edd6bSchristos xvasprintf (const char *format,
50837edd6bSchristos #if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
51837edd6bSchristos            _BSD_VA_LIST_ args)
52837edd6bSchristos #else
53837edd6bSchristos            va_list args)
54837edd6bSchristos #endif
55837edd6bSchristos {
56837edd6bSchristos   char *result;
57837edd6bSchristos   int total_width = libiberty_vprintf_buffer_size (format, args);
58837edd6bSchristos   result = (char *) xmalloc (total_width);
59837edd6bSchristos   vsprintf (result, format, args);
60837edd6bSchristos   return result;
61837edd6bSchristos }
62