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:
Meep-Eep
2009-11-28 18:00:46 +00:00
parent 441b7d9cdf
commit 1c6b9baa73
3 changed files with 67 additions and 8 deletions
+29 -4
View File
@@ -171,11 +171,36 @@ typedef unsigned short mode_t;
// String formatting // String formatting
#ifdef _MSC_VER #ifdef _MSC_VER
#define snprintf _snprintf # include <stdarg.h>
#define vsnprintf _vsnprintf # include <stdio.h>
#endif
// 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.
static inline 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;
}
static inline 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 */
#endif /* _UIOPORT_H */ #endif /* _UIOPORT_H */
+33
View File
@@ -24,6 +24,10 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#ifdef _MSC_VER
# include <stdarg.h>
# include <stdio.h>
#endif /* _MSC_VER */
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#if !defined (_MSC_VER) && !defined (HAVE_READDIR_R) #if !defined (_MSC_VER) && !defined (HAVE_READDIR_R)
@@ -110,3 +114,32 @@ readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) {
} }
#endif #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 */
+5 -4
View File
@@ -140,11 +140,12 @@ typedef unsigned short mode_t;
# include <alloca.h> # include <alloca.h>
#endif #endif
// Printf // String formatting
#ifdef _MSC_VER #ifdef _MSC_VER
#define snprintf _snprintf // Defined in port.c
#define vsnprintf _vsnprintf int snprintf(char *str, size_t size, const char *format, ...);
#endif int vsnprintf(char *str, size_t size, const char *format, va_list args);
#endif /* _MSC_VER */
// setenv() // setenv()
#ifndef HAVE_SETENV #ifndef HAVE_SETENV