Blame lib/vasnprintf.h

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