]>
Commit | Line | Data |
---|---|---|
438998a9 JM |
1 | /* Like sprintf but provides a pointer to malloc'd storage, which must |
2 | be freed by the caller. | |
8d398258 | 3 | Copyright (C) 1997, 2003 Free Software Foundation, Inc. |
438998a9 JM |
4 | Contributed by Cygnus Solutions. |
5 | ||
6 | This file is part of the libiberty library. | |
7 | Libiberty is free software; you can redistribute it and/or | |
8 | modify it under the terms of the GNU Library General Public | |
9 | License as published by the Free Software Foundation; either | |
10 | version 2 of the License, or (at your option) any later version. | |
11 | ||
12 | Libiberty is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Library General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Library General Public | |
18 | License along with libiberty; see the file COPYING.LIB. If | |
ee58dffd NC |
19 | not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, |
20 | Boston, MA 02110-1301, USA. */ | |
438998a9 | 21 | |
3c60ae5a GDR |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "config.h" | |
24 | #endif | |
438998a9 JM |
25 | #include "ansidecl.h" |
26 | #include "libiberty.h" | |
27 | ||
438998a9 | 28 | #include <stdarg.h> |
438998a9 | 29 | |
aac04c15 DD |
30 | /* |
31 | ||
5bed56d9 | 32 | @deftypefn Extension int asprintf (char **@var{resptr}, const char *@var{format}, ...) |
aac04c15 DD |
33 | |
34 | Like @code{sprintf}, but instead of passing a pointer to a buffer, you | |
35 | pass a pointer to a pointer. This function will compute the size of | |
36 | the buffer needed, allocate memory with @code{malloc}, and store a | |
37 | pointer to the allocated memory in @code{*@var{resptr}}. The value | |
38 | returned is the same as @code{sprintf} would return. If memory could | |
8d398258 | 39 | not be allocated, minus one is returned and @code{NULL} is stored in |
aac04c15 DD |
40 | @code{*@var{resptr}}. |
41 | ||
42 | @end deftypefn | |
43 | ||
44 | */ | |
45 | ||
438998a9 | 46 | int |
9486db4f | 47 | asprintf (char **buf, const char *fmt, ...) |
438998a9 JM |
48 | { |
49 | int status; | |
7a98d9b2 KG |
50 | VA_OPEN (ap, fmt); |
51 | VA_FIXEDARG (ap, char **, buf); | |
52 | VA_FIXEDARG (ap, const char *, fmt); | |
438998a9 | 53 | status = vasprintf (buf, fmt, ap); |
7a98d9b2 | 54 | VA_CLOSE (ap); |
438998a9 JM |
55 | return status; |
56 | } |