2N/A/* vsprintf with automatic memory allocation.
2N/A Copyright (C) 2002-2004, 2007-2010 Free Software Foundation, Inc.
2N/A
2N/A This program is free software; you can redistribute it and/or modify
2N/A it under the terms of the GNU General Public License as published by
2N/A the Free Software Foundation; either version 3, or (at your option)
2N/A any later version.
2N/A
2N/A This program is distributed in the hope that it will be useful,
2N/A but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A GNU General Public License for more details.
2N/A
2N/A You should have received a copy of the GNU General Public License along
2N/A with this program; if not, write to the Free Software Foundation,
2N/A Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
2N/A
2N/A#ifndef _VASNPRINTF_H
2N/A#define _VASNPRINTF_H
2N/A
2N/A/* Get va_list. */
2N/A#include <stdarg.h>
2N/A
2N/A/* Get size_t. */
2N/A#include <stddef.h>
2N/A
2N/A#ifndef __attribute__
2N/A/* The __attribute__ feature is available in gcc versions 2.5 and later.
2N/A The __-protected variants of the attributes 'format' and 'printf' are
2N/A accepted by gcc versions 2.6.4 (effectively 2.7) and later.
2N/A We enable __attribute__ only if these are supported too, because
2N/A gnulib and libintl do '#define printf __printf__' when they override
2N/A the 'printf' function. */
2N/A# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
2N/A# define __attribute__(Spec) /* empty */
2N/A# endif
2N/A#endif
2N/A
2N/A#ifdef __cplusplus
2N/Aextern "C" {
2N/A#endif
2N/A
2N/A/* Write formatted output to a string dynamically allocated with malloc().
2N/A You can pass a preallocated buffer for the result in RESULTBUF and its
2N/A size in *LENGTHP; otherwise you pass RESULTBUF = NULL.
2N/A If successful, return the address of the string (this may be = RESULTBUF
2N/A if no dynamic memory allocation was necessary) and set *LENGTHP to the
2N/A number of resulting bytes, excluding the trailing NUL. Upon error, set
2N/A errno and return NULL.
2N/A
2N/A When dynamic memory allocation occurs, the preallocated buffer is left
2N/A alone (with possibly modified contents). This makes it possible to use
2N/A a statically allocated or stack-allocated buffer, like this:
2N/A
2N/A char buf[100];
2N/A size_t len = sizeof (buf);
2N/A char *output = vasnprintf (buf, &len, format, args);
2N/A if (output == NULL)
2N/A ... error handling ...;
2N/A else
2N/A {
2N/A ... use the output string ...;
2N/A if (output != buf)
2N/A free (output);
2N/A }
2N/A */
2N/A#if REPLACE_VASNPRINTF
2N/A# define asnprintf rpl_asnprintf
2N/A# define vasnprintf rpl_vasnprintf
2N/A#endif
2N/Aextern char * asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
2N/A __attribute__ ((__format__ (__printf__, 3, 4)));
2N/Aextern char * vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args)
2N/A __attribute__ ((__format__ (__printf__, 3, 0)));
2N/A
2N/A#ifdef __cplusplus
2N/A}
2N/A#endif
2N/A
2N/A#endif /* _VASNPRINTF_H */