11debfc3dSmrg /* Implement the xvasprintf function.
2*8feb0f0bSmrg Copyright (C) 2014-2020 Free Software Foundation, Inc.
31debfc3dSmrg Contributed by Manuel Lopez-Ibanez.
41debfc3dSmrg
51debfc3dSmrg This file is part of the libiberty library.
61debfc3dSmrg Libiberty is free software; you can redistribute it and/or
71debfc3dSmrg modify it under the terms of the GNU Library General Public
81debfc3dSmrg License as published by the Free Software Foundation; either
91debfc3dSmrg version 2 of the License, or (at your option) any later version.
101debfc3dSmrg
111debfc3dSmrg Libiberty is distributed in the hope that it will be useful,
121debfc3dSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of
131debfc3dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
141debfc3dSmrg Library General Public License for more details.
151debfc3dSmrg
161debfc3dSmrg You should have received a copy of the GNU Library General Public
171debfc3dSmrg License along with libiberty; see the file COPYING.LIB. If not, write
181debfc3dSmrg to the Free Software Foundation, Inc., 51 Franklin Street - Fifth
191debfc3dSmrg Floor, Boston, MA 02110-1301, USA. */
201debfc3dSmrg
211debfc3dSmrg #ifdef HAVE_CONFIG_H
221debfc3dSmrg #include "config.h"
231debfc3dSmrg #endif
241debfc3dSmrg #include <ansidecl.h>
251debfc3dSmrg #include <stdarg.h>
261debfc3dSmrg #if !defined (va_copy) && defined (__va_copy)
271debfc3dSmrg # define va_copy(d,s) __va_copy((d),(s))
281debfc3dSmrg #endif
291debfc3dSmrg #include <stdio.h>
301debfc3dSmrg #ifdef HAVE_STRING_H
311debfc3dSmrg #include <string.h>
321debfc3dSmrg #endif
331debfc3dSmrg #include "libiberty.h"
341debfc3dSmrg #include "vprintf-support.h"
351debfc3dSmrg
361debfc3dSmrg /*
371debfc3dSmrg
381debfc3dSmrg @deftypefn Replacement char* xvasprintf (const char *@var{format}, va_list @var{args})
391debfc3dSmrg
401debfc3dSmrg Print to allocated string without fail. If @code{xvasprintf} fails,
411debfc3dSmrg this will print a message to @code{stderr} (using the name set by
421debfc3dSmrg @code{xmalloc_set_program_name}, if any) and then call @code{xexit}.
431debfc3dSmrg
441debfc3dSmrg @end deftypefn
451debfc3dSmrg
461debfc3dSmrg */
471debfc3dSmrg
481debfc3dSmrg char *
xvasprintf(const char * format,_BSD_VA_LIST_ args)491debfc3dSmrg xvasprintf (const char *format,
501debfc3dSmrg #if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
511debfc3dSmrg _BSD_VA_LIST_ args)
521debfc3dSmrg #else
531debfc3dSmrg va_list args)
541debfc3dSmrg #endif
551debfc3dSmrg {
561debfc3dSmrg char *result;
571debfc3dSmrg int total_width = libiberty_vprintf_buffer_size (format, args);
581debfc3dSmrg result = (char *) xmalloc (total_width);
591debfc3dSmrg vsprintf (result, format, args);
601debfc3dSmrg return result;
611debfc3dSmrg }
62