Blame lib/vasnprintf.h

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