From: Bruno Haible Date: Sun, 25 Sep 2011 12:54:47 +0000 (+0200) Subject: strerror_r-posix: Fix for MSVC 9. X-Git-Tag: v0.1~1743 X-Git-Url: http://erislabs.org.uk/gitweb/?a=commitdiff_plain;h=c6ca5b7979c7c38c92e9853f3d96c9494f8e0658;p=gnulib.git strerror_r-posix: Fix for MSVC 9. * lib/strerror_r.c (local_snprintf): New function. (snprintf): Define to local_snprintf, not to _snprintf. --- diff --git a/ChangeLog b/ChangeLog index 12ef00a68..af8ce6038 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2011-09-25 Bruno Haible + strerror_r-posix: Fix for MSVC 9. + * lib/strerror_r.c (local_snprintf): New function. + (snprintf): Define to local_snprintf, not to _snprintf. + +2011-09-25 Bruno Haible + ftruncate: Support for MSVC 9. * lib/ftruncate.c: Include errno.h, msvc-inval.h. (chsize_nothrow): New function. diff --git a/lib/strerror_r.c b/lib/strerror_r.c index 7fd90b708..e6cf99b25 100644 --- a/lib/strerror_r.c +++ b/lib/strerror_r.c @@ -87,9 +87,24 @@ gl_lock_define_initialized(static, strerror_lock) #endif /* On MSVC, there is no snprintf() function, just a _snprintf(). - It is of lower quality, but sufficient for the simple use here. */ + It is of lower quality, but sufficient for the simple use here. + We only have to make sure to NUL terminate the result (_snprintf + does not NUL terminate, like strncpy). */ #if !HAVE_SNPRINTF -# define snprintf _snprintf +static int +local_snprintf (char *buf, size_t buflen, const char *format, ...) +{ + va_list args; + int result; + + va_start (args, format); + result = _vsnprintf (buf, buflen, format, args); + va_end (args); + if (buflen > 0 && (result < 0 || result >= buflen)) + buf[buflen - 1] = '\0'; + return result; +} +# define snprintf local_snprintf #endif /* Copy as much of MSG into BUF as possible, without corrupting errno.