diff --git a/codemp/game/q_shared.c b/codemp/game/q_shared.c index fd7da90..5d60f01 100644 --- a/codemp/game/q_shared.c +++ b/codemp/game/q_shared.c @@ -850,6 +850,39 @@ char* Q_strrchr( const char* string, int c ) return sp; } +#ifdef _MSC_VER +/* +============= +Q_vsnprintf + +Special wrapper function for Microsoft's broken _vsnprintf() function. +MinGW comes with its own snprintf() which is not broken. +============= +*/ + +int Q_vsnprintf(char *str, size_t size, const char *format, va_list ap) +{ + int retval; + + retval = _vsnprintf(str, size, format, ap); + + if(retval < 0 || retval == size) + { + // Microsoft doesn't adhere to the C99 standard of vsnprintf, + // which states that the return value must be the number of + // bytes written if the output string had sufficient length. + // + // Obviously we cannot determine that value from Microsoft's + // implementation, so we have no choice but to return size. + + str[size - 1] = '\0'; + return size; + } + + return retval; +} +#endif + /* ============= Q_strncpyz diff --git a/codemp/game/q_shared.h b/codemp/game/q_shared.h index 2730466..5264fb6 100644 --- a/codemp/game/q_shared.h +++ b/codemp/game/q_shared.h @@ -88,6 +88,14 @@ extern int g_G2AllocServer; #include #include +#ifdef _MSC_VER + // vsnprintf is ISO/IEC 9899:1999 + // abstracting this to make it portable + int Q_vsnprintf(char *str, size_t size, const char *format, va_list ap); +#else + #define Q_vsnprintf vsnprintf +#endif + // Special min treatment for Xbox C++ version #ifdef _XBOX