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