xref: /netbsd-src/external/gpl3/gdb/dist/libiberty/asprintf.c (revision 7e120ff03ede3fe64e2c8620c01465d528502ddb)
198b9484cSchristos /* Like sprintf but provides a pointer to malloc'd storage, which must
298b9484cSchristos    be freed by the caller.
3*7e120ff0Schristos    Copyright (C) 1997-2024 Free Software Foundation, Inc.
498b9484cSchristos    Contributed by Cygnus Solutions.
598b9484cSchristos 
698b9484cSchristos This file is part of the libiberty library.
798b9484cSchristos Libiberty is free software; you can redistribute it and/or
898b9484cSchristos modify it under the terms of the GNU Library General Public
998b9484cSchristos License as published by the Free Software Foundation; either
1098b9484cSchristos version 2 of the License, or (at your option) any later version.
1198b9484cSchristos 
1298b9484cSchristos Libiberty is distributed in the hope that it will be useful,
1398b9484cSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1498b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1598b9484cSchristos Library General Public License for more details.
1698b9484cSchristos 
1798b9484cSchristos You should have received a copy of the GNU Library General Public
1898b9484cSchristos License along with libiberty; see the file COPYING.LIB.  If
1998b9484cSchristos not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
2098b9484cSchristos Boston, MA 02110-1301, USA.  */
2198b9484cSchristos 
2298b9484cSchristos #ifdef HAVE_CONFIG_H
2398b9484cSchristos #include "config.h"
2498b9484cSchristos #endif
2598b9484cSchristos #include "ansidecl.h"
2698b9484cSchristos #include "libiberty.h"
2798b9484cSchristos 
2898b9484cSchristos #include <stdarg.h>
2998b9484cSchristos 
3098b9484cSchristos /*
3198b9484cSchristos 
3298b9484cSchristos @deftypefn Extension int asprintf (char **@var{resptr}, const char *@var{format}, ...)
3398b9484cSchristos 
3498b9484cSchristos Like @code{sprintf}, but instead of passing a pointer to a buffer, you
3598b9484cSchristos pass a pointer to a pointer.  This function will compute the size of
3698b9484cSchristos the buffer needed, allocate memory with @code{malloc}, and store a
3798b9484cSchristos pointer to the allocated memory in @code{*@var{resptr}}.  The value
3898b9484cSchristos returned is the same as @code{sprintf} would return.  If memory could
3998b9484cSchristos not be allocated, minus one is returned and @code{NULL} is stored in
4098b9484cSchristos @code{*@var{resptr}}.
4198b9484cSchristos 
4298b9484cSchristos @end deftypefn
4398b9484cSchristos 
4498b9484cSchristos */
4598b9484cSchristos 
4698b9484cSchristos int
4798b9484cSchristos asprintf (char **buf, const char *fmt, ...)
4898b9484cSchristos {
4998b9484cSchristos   int status;
50837edd6bSchristos   va_list ap;
51837edd6bSchristos   va_start (ap, fmt);
5298b9484cSchristos   status = vasprintf (buf, fmt, ap);
53837edd6bSchristos   va_end (ap);
5498b9484cSchristos   return status;
5598b9484cSchristos }
56