X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fsnprintf.c;h=667ed9462556311d63b256513d9440cf1aa8af54;hb=ec03ca352064740b271b27e0be2b562b687b4554;hp=6cc44aa91a9b6424ccb1d057f912b3eee2da843a;hpb=d6fa94ab159dbceb9291e431271f6733bbd586ce;p=gnulib.git diff --git a/lib/snprintf.c b/lib/snprintf.c index 6cc44aa91..667ed9462 100644 --- a/lib/snprintf.c +++ b/lib/snprintf.c @@ -20,14 +20,14 @@ # include #endif -/* Get specification. */ #include "snprintf.h" -/* Get vasnprintf. */ -#include "vasnprintf.h" +#include +#include +#include -/* Get MIN. */ -#include +#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 @@ -37,19 +37,24 @@ int snprintf (char *str, size_t size, const char *format, ...) { + char *output; size_t len; - char *out = vasnprintf (NULL, &len, format, args); + va_list args; + + va_start (args, format); + output = vasnprintf (NULL, &len, format, args); + va_end (args); - if (!out) + if (!output) return -1; - if (str) + if (str && size > 0) { - memcpy (str, out, MIN (len + 1, size)); + memcpy (str, output, MIN (len + 1, size)); str[size - 1] = '\0'; } - free (out); + free (output); return len; }