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