X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fsnprintf.c;h=5870f8dde30f08250f6d78a1683cc14c034aaa27;hb=718b0598a7e9f5e9bdf3efc7e91e69a78e465327;hp=6cc44aa91a9b6424ccb1d057f912b3eee2da843a;hpb=d6fa94ab159dbceb9291e431271f6733bbd586ce;p=gnulib.git diff --git a/lib/snprintf.c b/lib/snprintf.c index 6cc44aa91..5870f8dde 100644 --- a/lib/snprintf.c +++ b/lib/snprintf.c @@ -14,20 +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, - Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifdef HAVE_CONFIG_H # 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); + len = size; + output = vasnprintf (str, &len, format, args); + va_end (args); - if (!out) + if (!output) 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'; - } - free (out); + if (output != str) + free (output); return len; }