1169695Skan/* Like sprintf but provides a pointer to malloc'd storage, which must
2169695Skan   be freed by the caller.
3169695Skan   Copyright (C) 1997, 2003 Free Software Foundation, Inc.
4169695Skan   Contributed by Cygnus Solutions.
5169695Skan
6169695SkanThis file is part of the libiberty library.
7169695SkanLibiberty is free software; you can redistribute it and/or
8169695Skanmodify it under the terms of the GNU Library General Public
9169695SkanLicense as published by the Free Software Foundation; either
10169695Skanversion 2 of the License, or (at your option) any later version.
11169695Skan
12169695SkanLibiberty is distributed in the hope that it will be useful,
13169695Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of
14169695SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15169695SkanLibrary General Public License for more details.
16169695Skan
17169695SkanYou should have received a copy of the GNU Library General Public
18169695SkanLicense along with libiberty; see the file COPYING.LIB.  If
19169695Skannot, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20169695SkanBoston, MA 02110-1301, USA.  */
21169695Skan
22169695Skan#ifdef HAVE_CONFIG_H
23169695Skan#include "config.h"
24169695Skan#endif
25169695Skan#include "ansidecl.h"
26169695Skan#include "libiberty.h"
27169695Skan
28169695Skan#include <stdarg.h>
29169695Skan
30169695Skan/*
31169695Skan
32169695Skan@deftypefn Extension int asprintf (char **@var{resptr}, const char *@var{format}, ...)
33169695Skan
34169695SkanLike @code{sprintf}, but instead of passing a pointer to a buffer, you
35169695Skanpass a pointer to a pointer.  This function will compute the size of
36169695Skanthe buffer needed, allocate memory with @code{malloc}, and store a
37169695Skanpointer to the allocated memory in @code{*@var{resptr}}.  The value
38169695Skanreturned is the same as @code{sprintf} would return.  If memory could
39169695Skannot be allocated, minus one is returned and @code{NULL} is stored in
40169695Skan@code{*@var{resptr}}.
41169695Skan
42169695Skan@end deftypefn
43169695Skan
44169695Skan*/
45169695Skan
46169695Skanint
47169695Skanasprintf (char **buf, const char *fmt, ...)
48169695Skan{
49169695Skan  int status;
50169695Skan  VA_OPEN (ap, fmt);
51169695Skan  VA_FIXEDARG (ap, char **, buf);
52169695Skan  VA_FIXEDARG (ap, const char *, fmt);
53169695Skan  status = vasprintf (buf, fmt, ap);
54169695Skan  VA_CLOSE (ap);
55169695Skan  return status;
56169695Skan}
57