Blame lib/vasnprintf.h

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