diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2004-09-30 23:29:25 +0000 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2004-09-30 23:29:25 +0000 |
commit | d6fa94ab159dbceb9291e431271f6733bbd586ce (patch) | |
tree | 4a3c87a26f920ebd4904dc8547b8953674a45a01 /lib/snprintf.c | |
parent | 7ae66b3089cf77d8226f1e6edc9433e215fe05ac (diff) | |
download | gnulib-d6fa94ab159dbceb9291e431271f6733bbd586ce.tar.gz |
New snprintf module from Simon Josefsson.
Diffstat (limited to 'lib/snprintf.c')
-rw-r--r-- | lib/snprintf.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/snprintf.c b/lib/snprintf.c new file mode 100644 index 0000000000..6cc44aa91a --- /dev/null +++ b/lib/snprintf.c @@ -0,0 +1,55 @@ +/* Formatted output to strings. + Copyright (C) 2004 Free Software Foundation, Inc. + Written by Simon Josefsson. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, 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 General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +/* Get specification. */ +#include "snprintf.h" + +/* Get vasnprintf. */ +#include "vasnprintf.h" + +/* Get MIN. */ +#include <minmax.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, ...) +{ + size_t len; + char *out = vasnprintf (NULL, &len, format, args); + + if (!out) + return -1; + + if (str) + { + memcpy (str, out, MIN (len + 1, size)); + str[size - 1] = '\0'; + } + + free (out); + + return len; +} |