1198160Srrs/* vsprintf with automatic memory allocation.
2198625Srrs   Copyright (C) 2002-2004 Free Software Foundation, Inc.
3198160Srrs
4198160Srrs   This program is free software; you can redistribute it and/or modify
5198160Srrs   it under the terms of the GNU General Public License as published by
6198160Srrs   the Free Software Foundation; either version 2, or (at your option)
7198160Srrs   any later version.
8198160Srrs
9198160Srrs   This program is distributed in the hope that it will be useful,
10198160Srrs   but WITHOUT ANY WARRANTY; without even the implied warranty of
11198160Srrs   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12198160Srrs   GNU General Public License for more details.
13198160Srrs
14198160Srrs   You should have received a copy of the GNU General Public License along
15198160Srrs   with this program; if not, write to the Free Software Foundation,
16198160Srrs   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17198160Srrs
18198160Srrs#ifndef _VASNPRINTF_H
19198160Srrs#define _VASNPRINTF_H
20198160Srrs
21198160Srrs/* Get va_list.  */
22198160Srrs#include <stdarg.h>
23198160Srrs
24198160Srrs/* Get size_t.  */
25198160Srrs#include <stddef.h>
26198160Srrs
27198160Srrs#ifndef __attribute__
28202173Simp/* This feature is available in gcc versions 2.5 and later.  */
29198160Srrs# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
30198160Srrs#  define __attribute__(Spec) /* empty */
31198160Srrs# endif
32198160Srrs/* The __-protected variants of `format' and `printf' attributes
33198160Srrs   are accepted by gcc versions 2.6.4 (effectively 2.7) and later.  */
34198160Srrs# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
35198160Srrs#  define __format__ format
36198160Srrs#  define __printf__ printf
37198160Srrs# endif
38198160Srrs#endif
39198160Srrs
40198160Srrs#ifdef	__cplusplus
41198956Srrsextern "C" {
42198160Srrs#endif
43198160Srrs
44198956Srrs/* Write formatted output to a string dynamically allocated with malloc().
45198160Srrs   You can pass a preallocated buffer for the result in RESULTBUF and its
46198160Srrs   size in *LENGTHP; otherwise you pass RESULTBUF = NULL.
47198160Srrs   If successful, return the address of the string (this may be = RESULTBUF
48198160Srrs   if no dynamic memory allocation was necessary) and set *LENGTHP to the
49198160Srrs   number of resulting bytes, excluding the trailing NUL.  Upon error, set
50198625Srrs   errno and return NULL.
51198625Srrs
52198625Srrs   When dynamic memory allocation occurs, the preallocated buffer is left
53198625Srrs   alone (with possibly modified contents).  This makes it possible to use
54198160Srrs   a statically allocated or stack-allocated buffer, like this:
55198160Srrs
56198160Srrs          char buf[100];
57198160Srrs          size_t len = sizeof (buf);
58198160Srrs          char *output = vasnprintf (buf, &len, format, args);
59198160Srrs          if (output == NULL)
60198160Srrs            ... error handling ...;
61198160Srrs          else
62198956Srrs            {
63198956Srrs              ... use the output string ...;
64198160Srrs              if (output != buf)
65198160Srrs                free (output);
66198160Srrs            }
67198160Srrs  */
68198160Srrsextern char * asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
69198956Srrs       __attribute__ ((__format__ (__printf__, 3, 4)));
70198625Srrsextern char * vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args)
71198956Srrs       __attribute__ ((__format__ (__printf__, 3, 0)));
72198956Srrs
73198956Srrs#ifdef	__cplusplus
74198956Srrs}
75198956Srrs#endif
76198625Srrs
77198160Srrs#endif /* _VASNPRINTF_H */
78198160Srrs