Back to index

cell-binutils  2.17cvs20070401
asprintf.c
Go to the documentation of this file.
00001 /* Like sprintf but provides a pointer to malloc'd storage, which must
00002    be freed by the caller.
00003    Copyright (C) 1997, 2003 Free Software Foundation, Inc.
00004    Contributed by Cygnus Solutions.
00005 
00006 This file is part of the libiberty library.
00007 Libiberty is free software; you can redistribute it and/or
00008 modify it under the terms of the GNU Library General Public
00009 License as published by the Free Software Foundation; either
00010 version 2 of the License, or (at your option) any later version.
00011 
00012 Libiberty is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 Library General Public License for more details.
00016 
00017 You should have received a copy of the GNU Library General Public
00018 License along with libiberty; see the file COPYING.LIB.  If
00019 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00020 Boston, MA 02110-1301, USA.  */
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #include "config.h"
00024 #endif
00025 #include "ansidecl.h"
00026 #include "libiberty.h"
00027 
00028 #include <stdarg.h>
00029 
00030 /*
00031 
00032 @deftypefn Extension int asprintf (char **@var{resptr}, const char *@var{format}, ...)
00033 
00034 Like @code{sprintf}, but instead of passing a pointer to a buffer, you
00035 pass a pointer to a pointer.  This function will compute the size of
00036 the buffer needed, allocate memory with @code{malloc}, and store a
00037 pointer to the allocated memory in @code{*@var{resptr}}.  The value
00038 returned is the same as @code{sprintf} would return.  If memory could
00039 not be allocated, minus one is returned and @code{NULL} is stored in
00040 @code{*@var{resptr}}.
00041 
00042 @end deftypefn
00043 
00044 */
00045 
00046 int
00047 asprintf (char **buf, const char *fmt, ...)
00048 {
00049   int status;
00050   VA_OPEN (ap, fmt);
00051   VA_FIXEDARG (ap, char **, buf);
00052   VA_FIXEDARG (ap, const char *, fmt);
00053   status = vasprintf (buf, fmt, ap);
00054   VA_CLOSE (ap);
00055   return status;
00056 }