Back to index

nagios-plugins  1.4.16
vasnprintf.h
Go to the documentation of this file.
00001 /* vsprintf with automatic memory allocation.
00002    Copyright (C) 2002-2004, 2007-2010 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 General Public License as published by
00006    the Free Software Foundation; either version 3, 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 General Public License for more details.
00013 
00014    You should have received a copy of the GNU 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 /* The __attribute__ feature is available in gcc versions 2.5 and later.
00029    The __-protected variants of the attributes 'format' and 'printf' are
00030    accepted by gcc versions 2.6.4 (effectively 2.7) and later.
00031    We enable __attribute__ only if these are supported too, because
00032    gnulib and libintl do '#define printf __printf__' when they override
00033    the 'printf' function.  */
00034 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
00035 #  define __attribute__(Spec)   /* empty */
00036 # endif
00037 #endif
00038 
00039 #ifdef __cplusplus
00040 extern "C" {
00041 #endif
00042 
00043 /* Write formatted output to a string dynamically allocated with malloc().
00044    You can pass a preallocated buffer for the result in RESULTBUF and its
00045    size in *LENGTHP; otherwise you pass RESULTBUF = NULL.
00046    If successful, return the address of the string (this may be = RESULTBUF
00047    if no dynamic memory allocation was necessary) and set *LENGTHP to the
00048    number of resulting bytes, excluding the trailing NUL.  Upon error, set
00049    errno and return NULL.
00050 
00051    When dynamic memory allocation occurs, the preallocated buffer is left
00052    alone (with possibly modified contents).  This makes it possible to use
00053    a statically allocated or stack-allocated buffer, like this:
00054 
00055           char buf[100];
00056           size_t len = sizeof (buf);
00057           char *output = vasnprintf (buf, &len, format, args);
00058           if (output == NULL)
00059             ... error handling ...;
00060           else
00061             {
00062               ... use the output string ...;
00063               if (output != buf)
00064                 free (output);
00065             }
00066   */
00067 #if REPLACE_VASNPRINTF
00068 # define asnprintf rpl_asnprintf
00069 # define vasnprintf rpl_vasnprintf
00070 #endif
00071 extern char * asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
00072        __attribute__ ((__format__ (__printf__, 3, 4)));
00073 extern char * vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args)
00074        __attribute__ ((__format__ (__printf__, 3, 0)));
00075 
00076 #ifdef __cplusplus
00077 }
00078 #endif
00079 
00080 #endif /* _VASNPRINTF_H */