Make snprintf/vsnprintf more compatible with the C standard on MSVC.
git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3361 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
@@ -24,6 +24,10 @@
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#ifdef _MSC_VER
|
||||
# include <stdarg.h>
|
||||
# include <stdio.h>
|
||||
#endif /* _MSC_VER */
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#if !defined (_MSC_VER) && !defined (HAVE_READDIR_R)
|
||||
@@ -110,3 +114,32 @@ readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) {
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// MSVC does not have snprintf() and vsnprintf(). It does have a _snprintf()
|
||||
// and _vsnprintf(), but these do not terminate a truncated string as
|
||||
// the C standard prescribes.
|
||||
int
|
||||
snprintf(char *str, size_t size, const char *format, ...)
|
||||
{
|
||||
int result;
|
||||
va_list args;
|
||||
|
||||
va_start (args, format);
|
||||
result = _vsnprintf (str, size, format, args);
|
||||
if (str != NULL && size != 0)
|
||||
str[size - 1] = '\0';
|
||||
va_end (args);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int
|
||||
vsnprintf(char *str, size_t size, const char *format, va_list args)
|
||||
{
|
||||
int result = _vsnprintf (str, size, format, args);
|
||||
if (str != NULL && size != 0)
|
||||
str[size - 1] = '\0';
|
||||
return result;
|
||||
}
|
||||
#endif /* _MSC_VER */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user