xref: /netbsd-src/external/gpl3/gdb/dist/libiberty/xasprintf.c (revision 5173eb0a33e5d83890ba976253e703be4c92557c)
1837edd6bSchristos /* Implement the xasprintf function.
2*5173eb0aSchristos    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 "libiberty.h"
26837edd6bSchristos 
27837edd6bSchristos #include <stdarg.h>
28837edd6bSchristos 
29837edd6bSchristos /*
30837edd6bSchristos 
31837edd6bSchristos @deftypefn Replacement char* xasprintf (const char *@var{format}, ...)
32837edd6bSchristos 
33837edd6bSchristos Print to allocated string without fail.  If @code{xasprintf} fails,
34837edd6bSchristos this will print a message to @code{stderr} (using the name set by
35837edd6bSchristos @code{xmalloc_set_program_name}, if any) and then call @code{xexit}.
36837edd6bSchristos 
37837edd6bSchristos @end deftypefn
38837edd6bSchristos 
39837edd6bSchristos */
40837edd6bSchristos 
41837edd6bSchristos char *
42837edd6bSchristos xasprintf (const char *fmt, ...)
43837edd6bSchristos {
44837edd6bSchristos   char *buf;
45837edd6bSchristos   va_list ap;
46837edd6bSchristos   va_start (ap, fmt);
47837edd6bSchristos   buf = xvasprintf (fmt, ap);
48837edd6bSchristos   va_end (ap);
49837edd6bSchristos   return buf;
50837edd6bSchristos }
51