Blame gnulib/lib/vasnprintf.h

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