00001 /* vsprintf with automatic memory allocation. 00002 Copyright (C) 2002-2004 Free Software Foundation, Inc. 00003 00004 This program is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU Lesser General Public License as published by 00006 the Free Software Foundation; either version 2.1, or (at your option) 00007 any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU Lesser General Public License for more details. 00013 00014 You should have received a copy of the GNU Lesser General Public License along 00015 with this program; if not, write to the Free Software Foundation, 00016 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 00017 00018 #ifndef _VASNPRINTF_H 00019 #define _VASNPRINTF_H 00020 00021 /* Get va_list. */ 00022 #include <stdarg.h> 00023 00024 /* Get size_t. */ 00025 #include <stddef.h> 00026 00027 #ifndef __attribute__ 00028 /* This feature is available in gcc versions 2.5 and later. */ 00029 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__ 00030 # define __attribute__(Spec) /* empty */ 00031 # endif 00032 /* The __-protected variants of `format' and `printf' attributes 00033 are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */ 00034 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7) 00035 # define __format__ format 00036 # define __printf__ printf 00037 # endif 00038 #endif 00039 00040 #ifdef __cplusplus 00041 extern "C" { 00042 #endif 00043 00044 /* Write formatted output to a string dynamically allocated with malloc(). 00045 You can pass a preallocated buffer for the result in RESULTBUF and its 00046 size in *LENGTHP; otherwise you pass RESULTBUF = NULL. 00047 If successful, return the address of the string (this may be = RESULTBUF 00048 if no dynamic memory allocation was necessary) and set *LENGTHP to the 00049 number of resulting bytes, excluding the trailing NUL. Upon error, set 00050 errno and return NULL. 00051 00052 When dynamic memory allocation occurs, the preallocated buffer is left 00053 alone (with possibly modified contents). This makes it possible to use 00054 a statically allocated or stack-allocated buffer, like this: 00055 00056 char buf[100]; 00057 size_t len = sizeof (buf); 00058 char *output = vasnprintf (buf, &len, format, args); 00059 if (output == NULL) 00060 ... error handling ...; 00061 else 00062 { 00063 ... use the output string ...; 00064 if (output != buf) 00065 free (output); 00066 } 00067 */ 00068 extern char * asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...) 00069 __attribute__ ((__format__ (__printf__, 3, 4))); 00070 extern char * vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args) 00071 __attribute__ ((__format__ (__printf__, 3, 0))); 00072 00073 #ifdef __cplusplus 00074 } 00075 #endif 00076 00077 #endif /* _VASNPRINTF_H */