projects
/
gnulib.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Typo.
[gnulib.git]
/
lib
/
snprintf.c
diff --git
a/lib/snprintf.c
b/lib/snprintf.c
index
417dd33
..
5870f8d
100644
(file)
--- a/
lib/snprintf.c
+++ b/
lib/snprintf.c
@@
-14,23
+14,20
@@
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation,
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., 5
9 Temple Place - Suite 330, Boston, MA 02111-1307
, USA. */
+ Inc., 5
1 Franklin Street, Fifth Floor, Boston, MA 02110-1301
, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
-/* Get specification. */
#include "snprintf.h"
#include "snprintf.h"
-/* Get memcpy. */
+#include <stdarg.h>
+#include <stdlib.h>
#include <string.h>
#include <string.h>
-/* Get vasnprintf. */
-#include "vasnprintf.h"
-
-/* Get MIN. */
#include "minmax.h"
#include "minmax.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
/* Print formatted output to string STR. Similar to sprintf, but
additional length SIZE limit how much is written into STR. Returns
@@
-40,19
+37,24
@@
int
snprintf (char *str, size_t size, const char *format, ...)
{
int
snprintf (char *str, size_t size, const char *format, ...)
{
+ char *output;
size_t len;
size_t len;
- char *out = vasnprintf (NULL, &len, format, args);
+ va_list args;
+
+ va_start (args, format);
+ len = size;
+ output = vasnprintf (str, &len, format, args);
+ va_end (args);
- if (!out)
+ if (!out
put
)
return -1;
return -1;
- if (str)
- {
- memcpy (str, out, MIN (len + 1, size));
+ if (str != NULL)
+ if (len > size - 1) /* equivalent to: (size > 0 && len >= size) */
str[size - 1] = '\0';
str[size - 1] = '\0';
- }
- free (out);
+ if (output != str)
+ free (output);
return len;
}
return len;
}