mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-27 08:05:24 +00:00
130426c659
* gnulib/lib/inet_ntop.h: Remove file. * gnulib/m4/onceonly_2_57.m4: Remove file. * build-aux/useless-if-before-free: Update. * build-aux/vc-list-files: Update. * gnulib/lib/.cvsignore: Update. * gnulib/lib/Makefile.am: Update. * gnulib/lib/fseeko.c: Update. * gnulib/lib/getaddrinfo.c: Update. * gnulib/lib/getdelim.c: Update. * gnulib/lib/inet_ntop.c: Update. * gnulib/lib/inet_ntop.h: Update. * gnulib/lib/poll.c: Update. * gnulib/lib/snprintf.c: Update. * gnulib/lib/stdlib.in.h: Update. * gnulib/lib/string.in.h: Update. * gnulib/lib/sys_socket.in.h: Update. * gnulib/lib/sys_stat.in.h: Update. * gnulib/lib/vasnprintf.c: Update. * gnulib/lib/vasprintf.c: Update. * gnulib/lib/wchar.in.h: Update. * gnulib/m4/arpa_inet_h.m4: Update. * gnulib/m4/extensions.m4: Update. * gnulib/m4/gnulib-common.m4: Update. * gnulib/m4/gnulib-comp.m4: Update. * gnulib/m4/include_next.m4: Update. * gnulib/m4/inet_ntop.m4: Update. * gnulib/m4/netinet_in_h.m4: Update. * gnulib/m4/onceonly_2_57.m4: Update. * gnulib/m4/physmem.m4: Update. * gnulib/m4/snprintf.m4: Update. * gnulib/m4/stdint.m4: Update. * gnulib/m4/stdio_h.m4: Update. * gnulib/m4/stdlib_h.m4: Update. * gnulib/m4/string_h.m4: Update. * gnulib/m4/strndup.m4: Update. * gnulib/m4/sys_select_h.m4: Update. * gnulib/m4/sys_socket_h.m4: Update. * gnulib/m4/sys_stat_h.m4: Update. * gnulib/m4/vasnprintf.m4: Update. * gnulib/tests/Makefile.am: Update. * gnulib/tests/test-fseeko.c: Update. * gnulib/tests/test-getaddrinfo.c: Update. * gnulib/tests/test-getdelim.c: Update. * gnulib/tests/test-getline.c: Update. * gnulib/tests/test-lseek.c: Update. * gnulib/tests/test-snprintf.c: Update. * gnulib/tests/test-sys_stat.c: Update. * gnulib/tests/test-vasnprintf.c: Update. * gnulib/tests/test-vasprintf.c: Update.
73 lines
1.9 KiB
C
73 lines
1.9 KiB
C
/* Formatted output to strings.
|
|
Copyright (C) 2004, 2006-2008 Free Software Foundation, Inc.
|
|
Written by Simon Josefsson and Paul Eggert.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation; either version 2.1, or (at your option)
|
|
any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
with this program; if not, write to the Free Software Foundation,
|
|
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
|
|
|
#include <config.h>
|
|
|
|
/* Specification. */
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
#include <limits.h>
|
|
#include <stdarg.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "vasnprintf.h"
|
|
|
|
/* Print formatted output to string STR. Similar to sprintf, but
|
|
additional length SIZE limit how much is written into STR. Returns
|
|
string length of formatted string (which may be larger than SIZE).
|
|
STR may be NULL, in which case nothing will be written. On error,
|
|
return a negative value. */
|
|
int
|
|
snprintf (char *str, size_t size, const char *format, ...)
|
|
{
|
|
char *output;
|
|
size_t len;
|
|
size_t lenbuf = size;
|
|
va_list args;
|
|
|
|
va_start (args, format);
|
|
output = vasnprintf (str, &lenbuf, format, args);
|
|
len = lenbuf;
|
|
va_end (args);
|
|
|
|
if (!output)
|
|
return -1;
|
|
|
|
if (output != str)
|
|
{
|
|
if (size)
|
|
{
|
|
size_t pruned_len = (len < size ? len : size - 1);
|
|
memcpy (str, output, pruned_len);
|
|
str[pruned_len] = '\0';
|
|
}
|
|
|
|
free (output);
|
|
}
|
|
|
|
if (INT_MAX < len)
|
|
{
|
|
errno = EOVERFLOW;
|
|
return -1;
|
|
}
|
|
|
|
return len;
|
|
}
|