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