12a6b7db3Sskrll /* Like sprintf but provides a pointer to malloc'd storage, which must
22a6b7db3Sskrll    be freed by the caller.
3*f22f0ef4Schristos    Copyright (C) 1997-2022 Free Software Foundation, Inc.
42a6b7db3Sskrll    Contributed by Cygnus Solutions.
52a6b7db3Sskrll 
62a6b7db3Sskrll This file is part of the libiberty library.
72a6b7db3Sskrll Libiberty is free software; you can redistribute it and/or
82a6b7db3Sskrll modify it under the terms of the GNU Library General Public
92a6b7db3Sskrll License as published by the Free Software Foundation; either
102a6b7db3Sskrll version 2 of the License, or (at your option) any later version.
112a6b7db3Sskrll 
122a6b7db3Sskrll Libiberty is distributed in the hope that it will be useful,
132a6b7db3Sskrll but WITHOUT ANY WARRANTY; without even the implied warranty of
142a6b7db3Sskrll MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
152a6b7db3Sskrll Library General Public License for more details.
162a6b7db3Sskrll 
172a6b7db3Sskrll You should have received a copy of the GNU Library General Public
182a6b7db3Sskrll License along with libiberty; see the file COPYING.LIB.  If
192a6b7db3Sskrll not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
202a6b7db3Sskrll Boston, MA 02110-1301, USA.  */
212a6b7db3Sskrll 
222a6b7db3Sskrll #ifdef HAVE_CONFIG_H
232a6b7db3Sskrll #include "config.h"
242a6b7db3Sskrll #endif
252a6b7db3Sskrll #include "ansidecl.h"
262a6b7db3Sskrll #include "libiberty.h"
272a6b7db3Sskrll 
282a6b7db3Sskrll #include <stdarg.h>
292a6b7db3Sskrll 
302a6b7db3Sskrll /*
312a6b7db3Sskrll 
322a6b7db3Sskrll @deftypefn Extension int asprintf (char **@var{resptr}, const char *@var{format}, ...)
332a6b7db3Sskrll 
342a6b7db3Sskrll Like @code{sprintf}, but instead of passing a pointer to a buffer, you
352a6b7db3Sskrll pass a pointer to a pointer.  This function will compute the size of
362a6b7db3Sskrll the buffer needed, allocate memory with @code{malloc}, and store a
372a6b7db3Sskrll pointer to the allocated memory in @code{*@var{resptr}}.  The value
382a6b7db3Sskrll returned is the same as @code{sprintf} would return.  If memory could
392a6b7db3Sskrll not be allocated, minus one is returned and @code{NULL} is stored in
402a6b7db3Sskrll @code{*@var{resptr}}.
412a6b7db3Sskrll 
422a6b7db3Sskrll @end deftypefn
432a6b7db3Sskrll 
442a6b7db3Sskrll */
452a6b7db3Sskrll 
462a6b7db3Sskrll int
asprintf(char ** buf,const char * fmt,...)472a6b7db3Sskrll asprintf (char **buf, const char *fmt, ...)
482a6b7db3Sskrll {
492a6b7db3Sskrll   int status;
505ba6b03cSchristos   va_list ap;
515ba6b03cSchristos   va_start (ap, fmt);
522a6b7db3Sskrll   status = vasprintf (buf, fmt, ap);
535ba6b03cSchristos   va_end (ap);
542a6b7db3Sskrll   return status;
552a6b7db3Sskrll }
56