1 /* Formatted output to strings in C locale.
2 Copyright (C) 2004, 2006-2018 Free Software Foundation, Inc.
3 Written by Simon Josefsson and Paul Eggert.
4 Modified for C locale by Ben Pfaff.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, see <https://www.gnu.org/licenses/>. */
30 #include "c-vasnprintf.h"
32 /* Print formatted output to string STR. Similar to sprintf, but
33 additional length SIZE limit how much is written into STR. Returns
34 string length of formatted string (which may be larger than SIZE).
35 STR may be NULL, in which case nothing will be written. On error,
36 return a negative value.
38 Formatting takes place in the C locale, that is, the decimal point
39 used in floating-point formatting directives is always '.'. */
41 c_snprintf (char *str
, size_t size
, const char *format
, ...)
48 va_start (args
, format
);
49 output
= c_vasnprintf (str
, &lenbuf
, format
, args
);
60 size_t pruned_len
= (len
< size
? len
: size
- 1);
61 memcpy (str
, output
, pruned_len
);
62 str
[pruned_len
] = '\0';