19573673dSchristos /* Implement the xvasprintf function.
2*cb63e24eSchristos Copyright (C) 2014-2024 Free Software Foundation, Inc.
39573673dSchristos Contributed by Manuel Lopez-Ibanez.
49573673dSchristos
59573673dSchristos This file is part of the libiberty library.
69573673dSchristos Libiberty is free software; you can redistribute it and/or
79573673dSchristos modify it under the terms of the GNU Library General Public
89573673dSchristos License as published by the Free Software Foundation; either
99573673dSchristos version 2 of the License, or (at your option) any later version.
109573673dSchristos
119573673dSchristos Libiberty is distributed in the hope that it will be useful,
129573673dSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of
139573673dSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
149573673dSchristos Library General Public License for more details.
159573673dSchristos
169573673dSchristos You should have received a copy of the GNU Library General Public
179573673dSchristos License along with libiberty; see the file COPYING.LIB. If not, write
189573673dSchristos to the Free Software Foundation, Inc., 51 Franklin Street - Fifth
199573673dSchristos Floor, Boston, MA 02110-1301, USA. */
209573673dSchristos
219573673dSchristos #ifdef HAVE_CONFIG_H
229573673dSchristos #include "config.h"
239573673dSchristos #endif
249573673dSchristos #include <ansidecl.h>
259573673dSchristos #include <stdarg.h>
269573673dSchristos #if !defined (va_copy) && defined (__va_copy)
279573673dSchristos # define va_copy(d,s) __va_copy((d),(s))
289573673dSchristos #endif
299573673dSchristos #include <stdio.h>
309573673dSchristos #ifdef HAVE_STRING_H
319573673dSchristos #include <string.h>
329573673dSchristos #endif
339573673dSchristos #include "libiberty.h"
349573673dSchristos #include "vprintf-support.h"
359573673dSchristos
369573673dSchristos /*
379573673dSchristos
389573673dSchristos @deftypefn Replacement char* xvasprintf (const char *@var{format}, va_list @var{args})
399573673dSchristos
409573673dSchristos Print to allocated string without fail. If @code{xvasprintf} fails,
419573673dSchristos this will print a message to @code{stderr} (using the name set by
429573673dSchristos @code{xmalloc_set_program_name}, if any) and then call @code{xexit}.
439573673dSchristos
449573673dSchristos @end deftypefn
459573673dSchristos
469573673dSchristos */
479573673dSchristos
489573673dSchristos char *
xvasprintf(const char * format,_BSD_VA_LIST_ args)499573673dSchristos xvasprintf (const char *format,
509573673dSchristos #if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
519573673dSchristos _BSD_VA_LIST_ args)
529573673dSchristos #else
539573673dSchristos va_list args)
549573673dSchristos #endif
559573673dSchristos {
569573673dSchristos char *result;
579573673dSchristos int total_width = libiberty_vprintf_buffer_size (format, args);
589573673dSchristos result = (char *) xmalloc (total_width);
599573673dSchristos vsprintf (result, format, args);
609573673dSchristos return result;
619573673dSchristos }
62