|
Packit Service |
a2489d |
/* Formatted output to strings.
|
|
Packit Service |
a2489d |
Copyright (C) 2004, 2006-2018 Free Software Foundation, Inc.
|
|
Packit Service |
a2489d |
Written by Simon Josefsson and Yoann Vandoorselaere <yoann@prelude-ids.org>.
|
|
Packit Service |
a2489d |
|
|
Packit Service |
a2489d |
This program is free software; you can redistribute it and/or modify
|
|
Packit Service |
a2489d |
it under the terms of the GNU General Public License as published by
|
|
Packit Service |
a2489d |
the Free Software Foundation; either version 3, or (at your option)
|
|
Packit Service |
a2489d |
any later version.
|
|
Packit Service |
a2489d |
|
|
Packit Service |
a2489d |
This program is distributed in the hope that it will be useful,
|
|
Packit Service |
a2489d |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
a2489d |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
a2489d |
GNU General Public License for more details.
|
|
Packit Service |
a2489d |
|
|
Packit Service |
a2489d |
You should have received a copy of the GNU General Public License along
|
|
Packit Service |
a2489d |
with this program; if not, see <https://www.gnu.org/licenses/>. */
|
|
Packit Service |
a2489d |
|
|
Packit Service |
a2489d |
#ifdef HAVE_CONFIG_H
|
|
Packit Service |
a2489d |
# include <config.h>
|
|
Packit Service |
a2489d |
#endif
|
|
Packit Service |
a2489d |
|
|
Packit Service |
a2489d |
/* Specification. */
|
|
Packit Service |
a2489d |
#include <stdio.h>
|
|
Packit Service |
a2489d |
|
|
Packit Service |
a2489d |
#include <errno.h>
|
|
Packit Service |
a2489d |
#include <limits.h>
|
|
Packit Service |
a2489d |
#include <stdarg.h>
|
|
Packit Service |
a2489d |
#include <stdlib.h>
|
|
Packit Service |
a2489d |
#include <string.h>
|
|
Packit Service |
a2489d |
|
|
Packit Service |
a2489d |
#include "vasnprintf.h"
|
|
Packit Service |
a2489d |
|
|
Packit Service |
a2489d |
/* Print formatted output to string STR. Similar to vsprintf, but
|
|
Packit Service |
a2489d |
additional length SIZE limit how much is written into STR. Returns
|
|
Packit Service |
a2489d |
string length of formatted string (which may be larger than SIZE).
|
|
Packit Service |
a2489d |
STR may be NULL, in which case nothing will be written. On error,
|
|
Packit Service |
a2489d |
return a negative value. */
|
|
Packit Service |
a2489d |
int
|
|
Packit Service |
a2489d |
vsnprintf (char *str, size_t size, const char *format, va_list args)
|
|
Packit Service |
a2489d |
{
|
|
Packit Service |
a2489d |
char *output;
|
|
Packit Service |
a2489d |
size_t len;
|
|
Packit Service |
a2489d |
size_t lenbuf = size;
|
|
Packit Service |
a2489d |
|
|
Packit Service |
a2489d |
output = vasnprintf (str, &lenbuf, format, args);
|
|
Packit Service |
a2489d |
len = lenbuf;
|
|
Packit Service |
a2489d |
|
|
Packit Service |
a2489d |
if (!output)
|
|
Packit Service |
a2489d |
return -1;
|
|
Packit Service |
a2489d |
|
|
Packit Service |
a2489d |
if (output != str)
|
|
Packit Service |
a2489d |
{
|
|
Packit Service |
a2489d |
if (size)
|
|
Packit Service |
a2489d |
{
|
|
Packit Service |
a2489d |
size_t pruned_len = (len < size ? len : size - 1);
|
|
Packit Service |
a2489d |
memcpy (str, output, pruned_len);
|
|
Packit Service |
a2489d |
str[pruned_len] = '\0';
|
|
Packit Service |
a2489d |
}
|
|
Packit Service |
a2489d |
|
|
Packit Service |
a2489d |
free (output);
|
|
Packit Service |
a2489d |
}
|
|
Packit Service |
a2489d |
|
|
Packit Service |
a2489d |
if (len > INT_MAX)
|
|
Packit Service |
a2489d |
{
|
|
Packit Service |
a2489d |
errno = EOVERFLOW;
|
|
Packit Service |
a2489d |
return -1;
|
|
Packit Service |
a2489d |
}
|
|
Packit Service |
a2489d |
|
|
Packit Service |
a2489d |
return len;
|
|
Packit Service |
a2489d |
}
|