From 49286c1eb2daf4494352921a0ca3dce4911f2023 Mon Sep 17 00:00:00 2001 From: sezero Date: Tue, 31 Aug 2010 14:09:00 +0000 Subject: [PATCH] added q_snprintf and q_vsnprintf from uhexen2. always use our wrapper versions to ensure consistent behavior accross platforms. git-svn-id: http://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@319 af15c1b1-3010-417e-b628-4374ebc0bcbd --- Quake/common.c | 27 +++++++++++++++++++++++++++ Quake/common.h | 22 ++++++++++++++-------- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/Quake/common.c b/Quake/common.c index c4f25516..10d78565 100644 --- a/Quake/common.c +++ b/Quake/common.c @@ -149,6 +149,33 @@ void InsertLinkAfter (link_t *l, link_t *after) ============================================================================ */ +int q_vsnprintf(char *str, size_t size, const char *format, va_list args) +{ + int ret; + + ret = vsnprintf_func (str, size, format, args); + + if (ret < 0) + ret = (int)size; + + if ((size_t)ret >= size) + str[size - 1] = '\0'; + + return ret; +} + +int q_snprintf (char *str, size_t size, const char *format, ...) +{ + int ret; + va_list argptr; + + va_start (argptr, format); + ret = q_vsnprintf (str, size, format, argptr); + va_end (argptr); + + return ret; +} + void Q_memset (void *dest, int fill, size_t count) { size_t i; diff --git a/Quake/common.h b/Quake/common.h index 192e904d..403847d4 100644 --- a/Quake/common.h +++ b/Quake/common.h @@ -25,8 +25,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // comndef.h -- general definitions #if defined(_WIN32) -#define q_snprintf _snprintf -#define q_vsnprintf _vsnprintf #ifdef _MSC_VER # pragma warning(disable:4244) /* 'argument' : conversion from 'type1' to 'type2', @@ -41,12 +39,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define fmax max #define fmin min #endif /* _MSC_VER */ - -#else /* _WIN32 */ - -#define q_snprintf snprintf -#define q_vsnprintf vsnprintf - #endif /* _WIN32 */ typedef struct sizebuf_s @@ -138,6 +130,20 @@ int Q_strncasecmp (const char *s1, const char *s2, int n); int Q_atoi (const char *str); float Q_atof (const char *str); +/* snprintf, vsnprintf : always use our versions. */ +/* platform dependant (v)snprintf function names: */ +#if defined(_WIN32) +#define snprintf_func _snprintf +#define vsnprintf_func _vsnprintf +#else +#define snprintf_func snprintf +#define vsnprintf_func vsnprintf +#endif + +extern int q_snprintf (char *str, size_t size, const char *format, ...) __attribute__((__format__(__printf__,3,4))); +extern int q_vsnprintf(char *str, size_t size, const char *format, va_list args) + __attribute__((__format__(__printf__,3,0))); + //============================================================================ extern char com_token[1024];