Blame src/gl/vasnprintf.h

Packit aea12f
/* vsprintf with automatic memory allocation.
Packit Service 991b93
   Copyright (C) 2002-2004, 2007-2020 Free Software Foundation, Inc.
Packit aea12f
Packit aea12f
   This program is free software; you can redistribute it and/or modify
Packit aea12f
   it under the terms of the GNU General Public License as published by
Packit aea12f
   the Free Software Foundation; either version 3, or (at your option)
Packit aea12f
   any later version.
Packit aea12f
Packit aea12f
   This program is distributed in the hope that it will be useful,
Packit aea12f
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit aea12f
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit aea12f
   GNU General Public License for more details.
Packit aea12f
Packit aea12f
   You should have received a copy of the GNU General Public License along
Packit aea12f
   with this program; if not, see <https://www.gnu.org/licenses/>.  */
Packit aea12f
Packit aea12f
#ifndef _VASNPRINTF_H
Packit aea12f
#define _VASNPRINTF_H
Packit aea12f
Packit aea12f
/* Get va_list.  */
Packit aea12f
#include <stdarg.h>
Packit aea12f
Packit aea12f
/* Get size_t.  */
Packit aea12f
#include <stddef.h>
Packit aea12f
Packit aea12f
#ifdef __cplusplus
Packit aea12f
extern "C" {
Packit aea12f
#endif
Packit aea12f
Packit aea12f
/* Write formatted output to a string dynamically allocated with malloc().
Packit aea12f
   You can pass a preallocated buffer for the result in RESULTBUF and its
Packit aea12f
   size in *LENGTHP; otherwise you pass RESULTBUF = NULL.
Packit aea12f
   If successful, return the address of the string (this may be = RESULTBUF
Packit aea12f
   if no dynamic memory allocation was necessary) and set *LENGTHP to the
Packit aea12f
   number of resulting bytes, excluding the trailing NUL.  Upon error, set
Packit aea12f
   errno and return NULL.
Packit aea12f
Packit aea12f
   When dynamic memory allocation occurs, the preallocated buffer is left
Packit aea12f
   alone (with possibly modified contents).  This makes it possible to use
Packit aea12f
   a statically allocated or stack-allocated buffer, like this:
Packit aea12f
Packit aea12f
          char buf[100];
Packit aea12f
          size_t len = sizeof (buf);
Packit aea12f
          char *output = vasnprintf (buf, &len, format, args);
Packit aea12f
          if (output == NULL)
Packit aea12f
            ... error handling ...;
Packit aea12f
          else
Packit aea12f
            {
Packit aea12f
              ... use the output string ...;
Packit aea12f
              if (output != buf)
Packit aea12f
                free (output);
Packit aea12f
            }
Packit aea12f
  */
Packit aea12f
#if REPLACE_VASNPRINTF
Packit aea12f
# define asnprintf rpl_asnprintf
Packit aea12f
# define vasnprintf rpl_vasnprintf
Packit aea12f
#endif
Packit Service 991b93
extern char * asnprintf (char *restrict resultbuf, size_t *lengthp,
Packit Service 991b93
                         const char *format, ...)
Packit aea12f
       _GL_ATTRIBUTE_FORMAT ((__printf__, 3, 4));
Packit Service 991b93
extern char * vasnprintf (char *restrict resultbuf, size_t *lengthp,
Packit Service 991b93
                          const char *format, va_list args)
Packit aea12f
       _GL_ATTRIBUTE_FORMAT ((__printf__, 3, 0));
Packit aea12f
Packit aea12f
#ifdef __cplusplus
Packit aea12f
}
Packit aea12f
#endif
Packit aea12f
Packit aea12f
#endif /* _VASNPRINTF_H */