diff options
Diffstat (limited to 'src/game')
-rw-r--r-- | src/game/bg_lib.c | 939 | ||||
-rw-r--r-- | src/game/bg_lib.h | 2 | ||||
-rw-r--r-- | src/game/g_main.c | 10 | ||||
-rw-r--r-- | src/game/g_team.c | 2 |
4 files changed, 689 insertions, 264 deletions
diff --git a/src/game/bg_lib.c b/src/game/bg_lib.c index b007d256..7524a53e 100644 --- a/src/game/bg_lib.c +++ b/src/game/bg_lib.c @@ -1591,9 +1591,9 @@ unsigned int _hextoi( const char **stringPtr ) int c; int i; const char *string; - + string = *stringPtr; - + // skip whitespace while( *string <= ' ' ) { @@ -1631,334 +1631,761 @@ unsigned int _hextoi( const char **stringPtr ) //========================================================= +/* + * New implementation by Patrick Powell and others for vsnprintf. + * Supports length checking in strings. + */ -#define ALT 0x00000001 /* alternate form */ -#define HEX 0x00000002 /* hexadecimal */ -#define LADJUST 0x00000004 /* left adjustment */ -#define LONGDBL 0x00000008 /* long double */ -#define LONGINT 0x00000010 /* long integer */ -#define QUADINT 0x00000020 /* quad integer */ -#define SHORTINT 0x00000040 /* short integer */ -#define ZEROPAD 0x00000080 /* zero (as opposed to blank) pad */ -#define FPT 0x00000100 /* floating point number */ +/* + * Copyright Patrick Powell 1995 + * This code is based on code written by Patrick Powell (papowell@astart.com) + * It may be used for any purpose as long as this notice remains intact + * on all source code distributions + */ -#define to_digit(c) ((c) - '0') -#define is_digit(c) ((unsigned)to_digit(c) <= 9) -#define to_char(n) ((n) + '0') +/************************************************************** + * Original: + * Patrick Powell Tue Apr 11 09:48:21 PDT 1995 + * A bombproof version of doprnt (dopr) included. + * Sigh. This sort of thing is always nasty do deal with. Note that + * the version here does not include floating point... + * + * snprintf() is used instead of sprintf() as it does limit checks + * for string length. This covers a nasty loophole. + * + * The other functions are there to prevent NULL pointers from + * causing nast effects. + * + * More Recently: + * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43 + * This was ugly. It is still ugly. I opted out of floating point + * numbers, but the formatter understands just about everything + * from the normal C string format, at least as far as I can tell from + * the Solaris 2.5 printf(3S) man page. + * + * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1 + * Ok, added some minimal floating point support, which means this + * probably requires libm on most operating systems. Don't yet + * support the exponent (e,E) and sigfig (g,G). Also, fmtint() + * was pretty badly broken, it just wasn't being exercised in ways + * which showed it, so that's been fixed. Also, formated the code + * to mutt conventions, and removed dead code left over from the + * original. Also, there is now a builtin-test, just compile with: + * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm + * and run snprintf for results. + * + * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i + * The PGP code was using unsigned hexadecimal formats. + * Unfortunately, unsigned formats simply didn't work. + * + * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8 + * The original code assumed that both snprintf() and vsnprintf() were + * missing. Some systems only have snprintf() but not vsnprintf(), so + * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF. + * + * Andrew Tridgell (tridge@samba.org) Oct 1998 + * fixed handling of %.0f + * added test for HAVE_LONG_DOUBLE + * + * Russ Allbery <rra@stanford.edu> 2000-08-26 + * fixed return value to comply with C99 + * fixed handling of snprintf(NULL, ...) + * + * Hrvoje Niksic <hniksic@arsdigita.com> 2000-11-04 + * include <config.h> instead of "config.h". + * moved TEST_SNPRINTF stuff out of HAVE_SNPRINTF ifdef. + * include <stdio.h> for NULL. + * added support and test cases for long long. + * don't declare argument types to (v)snprintf if stdarg is not used. + * use int instead of short int as 2nd arg to va_arg. + * + **************************************************************/ -void AddInt( char **buf_p, int val, int width, int flags ) -{ - char text[ 32 ]; - int digits; - char *buf; +/* BDR 2002-01-13 %e and %g were being ignored. Now do something, + if not necessarily correctly */ - digits = 0; +#if (SIZEOF_LONG_DOUBLE > 0) +/* #ifdef HAVE_LONG_DOUBLE */ +#define LDOUBLE long double +#else +#define LDOUBLE double +#endif - if( flags & HEX ) +#if (SIZEOF_LONG_LONG > 0) +/* #ifdef HAVE_LONG_LONG */ +# define LLONG long long +#else +# define LLONG long +#endif + +static int dopr (char *buffer, size_t maxlen, const char *format, + va_list args); +static int fmtstr (char *buffer, size_t *currlen, size_t maxlen, + char *value, int flags, int min, int max); +static int fmtint (char *buffer, size_t *currlen, size_t maxlen, + LLONG value, int base, int min, int max, int flags); +static int fmtfp (char *buffer, size_t *currlen, size_t maxlen, + LDOUBLE fvalue, int min, int max, int flags); +static int dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c ); + +/* + * dopr(): poor man's version of doprintf + */ + +/* format read states */ +#define DP_S_DEFAULT 0 +#define DP_S_FLAGS 1 +#define DP_S_MIN 2 +#define DP_S_DOT 3 +#define DP_S_MAX 4 +#define DP_S_MOD 5 +#define DP_S_MOD_L 6 +#define DP_S_CONV 7 +#define DP_S_DONE 8 + +/* format flags - Bits */ +#define DP_F_MINUS (1 << 0) +#define DP_F_PLUS (1 << 1) +#define DP_F_SPACE (1 << 2) +#define DP_F_NUM (1 << 3) +#define DP_F_ZERO (1 << 4) +#define DP_F_UP (1 << 5) +#define DP_F_UNSIGNED (1 << 6) + +/* Conversion Flags */ +#define DP_C_SHORT 1 +#define DP_C_LONG 2 +#define DP_C_LLONG 3 +#define DP_C_LDOUBLE 4 + +#define char_to_int(p) (p - '0') + +static int dopr (char *buffer, size_t maxlen, const char *format, va_list args) +{ + char ch; + LLONG value; + LDOUBLE fvalue; + char *strvalue; + int min; + int max; + int state; + int flags; + int cflags; + int total; + size_t currlen; + + state = DP_S_DEFAULT; + currlen = flags = cflags = min = 0; + max = -1; + ch = *format++; + total = 0; + + while (state != DP_S_DONE) { - char c; - int n = 0; + if (ch == '\0') + state = DP_S_DONE; - while( n < 32 ) + switch(state) { - c = "0123456789abcdef"[ ( val >> n ) & 0xF ]; - n += 4; - if( c == '0' && !digits ) - continue; - text[ digits++ ] = c; + case DP_S_DEFAULT: + if (ch == '%') + state = DP_S_FLAGS; + else + total += dopr_outch (buffer, &currlen, maxlen, ch); + ch = *format++; + break; + case DP_S_FLAGS: + switch (ch) + { + case '-': + flags |= DP_F_MINUS; + ch = *format++; + break; + case '+': + flags |= DP_F_PLUS; + ch = *format++; + break; + case ' ': + flags |= DP_F_SPACE; + ch = *format++; + break; + case '#': + flags |= DP_F_NUM; + ch = *format++; + break; + case '0': + flags |= DP_F_ZERO; + ch = *format++; + break; + default: + state = DP_S_MIN; + break; + } + break; + case DP_S_MIN: + if ('0' <= ch && ch <= '9') + { + min = 10*min + char_to_int (ch); + ch = *format++; + } + else if (ch == '*') + { + min = va_arg (args, int); + ch = *format++; + state = DP_S_DOT; + } + else + state = DP_S_DOT; + break; + case DP_S_DOT: + if (ch == '.') + { + state = DP_S_MAX; + ch = *format++; + } + else + state = DP_S_MOD; + break; + case DP_S_MAX: + if ('0' <= ch && ch <= '9') + { + if (max < 0) + max = 0; + max = 10*max + char_to_int (ch); + ch = *format++; + } + else if (ch == '*') + { + max = va_arg (args, int); + ch = *format++; + state = DP_S_MOD; + } + else + state = DP_S_MOD; + break; + case DP_S_MOD: + switch (ch) + { + case 'h': + cflags = DP_C_SHORT; + ch = *format++; + break; + case 'l': + cflags = DP_C_LONG; + ch = *format++; + break; + case 'L': + cflags = DP_C_LDOUBLE; + ch = *format++; + break; + default: + break; + } + if (cflags != DP_C_LONG) + state = DP_S_CONV; + else + state = DP_S_MOD_L; + break; + case DP_S_MOD_L: + switch (ch) + { + case 'l': + cflags = DP_C_LLONG; + ch = *format++; + break; + default: + break; + } + state = DP_S_CONV; + break; + case DP_S_CONV: + switch (ch) + { + case 'd': + case 'i': + if (cflags == DP_C_SHORT) + value = (short int)va_arg (args, int); + else if (cflags == DP_C_LONG) + value = va_arg (args, long int); + else if (cflags == DP_C_LLONG) + value = va_arg (args, LLONG); + else + value = va_arg (args, int); + total += fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); + break; + case 'o': + flags |= DP_F_UNSIGNED; + if (cflags == DP_C_SHORT) + // value = (unsigned short int) va_arg (args, unsigned short int); // Thilo: This does not work because the rcc compiler cannot do that cast correctly. + value = va_arg (args, unsigned int) & ( (1 << sizeof(unsigned short int) * 8) - 1); // Using this workaround instead. + else if (cflags == DP_C_LONG) + value = va_arg (args, unsigned long int); + else if (cflags == DP_C_LLONG) + value = va_arg (args, unsigned LLONG); + else + value = va_arg (args, unsigned int); + total += fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags); + break; + case 'u': + flags |= DP_F_UNSIGNED; + if (cflags == DP_C_SHORT) + value = va_arg (args, unsigned int) & ( (1 << sizeof(unsigned short int) * 8) - 1); + else if (cflags == DP_C_LONG) + value = va_arg (args, unsigned long int); + else if (cflags == DP_C_LLONG) + value = va_arg (args, unsigned LLONG); + else + value = va_arg (args, unsigned int); + total += fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); + break; + case 'X': + flags |= DP_F_UP; + case 'x': + flags |= DP_F_UNSIGNED; + if (cflags == DP_C_SHORT) + value = va_arg (args, unsigned int) & ( (1 << sizeof(unsigned short int) * 8) - 1); + else if (cflags == DP_C_LONG) + value = va_arg (args, unsigned long int); + else if (cflags == DP_C_LLONG) + value = va_arg (args, unsigned LLONG); + else + value = va_arg (args, unsigned int); + total += fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags); + break; + case 'f': + if (cflags == DP_C_LDOUBLE) + fvalue = va_arg (args, LDOUBLE); + else + fvalue = va_arg (args, double); + /* um, floating point? */ + total += fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); + break; + case 'E': + flags |= DP_F_UP; + case 'e': + if (cflags == DP_C_LDOUBLE) + fvalue = va_arg (args, LDOUBLE); + else + fvalue = va_arg (args, double); + /* um, floating point? */ + total += fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); + break; + case 'G': + flags |= DP_F_UP; + case 'g': + if (cflags == DP_C_LDOUBLE) + fvalue = va_arg (args, LDOUBLE); + else + fvalue = va_arg (args, double); + /* um, floating point? */ + total += fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); + break; + case 'c': + total += dopr_outch (buffer, &currlen, maxlen, va_arg (args, int)); + break; + case 's': + strvalue = va_arg (args, char *); + total += fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max); + break; + case 'p': + strvalue = va_arg (args, void *); + total += fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, + max, flags); + break; + case 'n': + if (cflags == DP_C_SHORT) + { + short int *num; + num = va_arg (args, short int *); + *num = currlen; + } + else if (cflags == DP_C_LONG) + { + long int *num; + num = va_arg (args, long int *); + *num = currlen; + } + else if (cflags == DP_C_LLONG) + { + LLONG *num; + num = va_arg (args, LLONG *); + *num = currlen; + } + else + { + int *num; + num = va_arg (args, int *); + *num = currlen; + } + break; + case '%': + total += dopr_outch (buffer, &currlen, maxlen, ch); + break; + case 'w': + /* not supported yet, treat as next char */ + ch = *format++; + break; + default: + /* Unknown, skip */ + break; + } + ch = *format++; + state = DP_S_DEFAULT; + flags = cflags = min = 0; + max = -1; + break; + case DP_S_DONE: + break; + default: + /* hmm? */ + break; /* some picky compilers need this */ } - text[ digits ] = '\0'; } - else + if (buffer != NULL) { - int signedVal = val; + if (currlen < maxlen - 1) + buffer[currlen] = '\0'; + else + buffer[maxlen - 1] = '\0'; + } + return total; +} - if( val < 0 ) - val = -val; - do - { - text[ digits++ ] = '0' + val % 10; - val /= 10; - } while( val ); +static int fmtstr (char *buffer, size_t *currlen, size_t maxlen, + char *value, int flags, int min, int max) +{ + int padlen, strln; /* amount to pad */ + int cnt = 0; + int total = 0; - if( signedVal < 0 ) - text[ digits++ ] = '-'; + if (value == 0) + { + value = "<NULL>"; } - buf = *buf_p; + for (strln = 0; value[strln]; ++strln); /* strlen */ + if (max >= 0 && max < strln) + strln = max; + padlen = min - strln; + if (padlen < 0) + padlen = 0; + if (flags & DP_F_MINUS) + padlen = -padlen; /* Left Justify */ - if( !( flags & LADJUST ) ) + while (padlen > 0) { - while( digits < width ) - { - *buf++ = ( flags & ZEROPAD ) ? '0' : ' '; - width--; - } + total += dopr_outch (buffer, currlen, maxlen, ' '); + --padlen; } - - while( digits-- ) + while (*value && ((max < 0) || (cnt < max))) { - *buf++ = text[ digits ]; - width--; + total += dopr_outch (buffer, currlen, maxlen, *value++); + ++cnt; } - - if( flags & LADJUST ) + while (padlen < 0) { - while( width-- > 0 ) - *buf++ = ( flags & ZEROPAD ) ? '0' : ' '; + total += dopr_outch (buffer, currlen, maxlen, ' '); + ++padlen; } - - *buf_p = buf; + return total; } -void AddFloat( char **buf_p, float fval, int width, int prec ) +/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */ + +static int fmtint (char *buffer, size_t *currlen, size_t maxlen, + LLONG value, int base, int min, int max, int flags) { - char text[ 32 ]; - int digits; - float signedVal; - char *buf; - int val; + int signvalue = 0; + unsigned LLONG uvalue; + char convert[24]; + int place = 0; + int spadlen = 0; /* amount to space pad */ + int zpadlen = 0; /* amount to zero pad */ + const char *digits; + int total = 0; - // get the sign - signedVal = fval; - if( fval < 0 ) - fval = -fval; + if (max < 0) + max = 0; - // write the float number - digits = 0; - val = (int)fval; + uvalue = value; - do + if(!(flags & DP_F_UNSIGNED)) { - text[ digits++ ] = '0' + val % 10; - val /= 10; - } while( val ); - - if( signedVal < 0 ) - text[digits++] = '-'; - - buf = *buf_p; + if( value < 0 ) { + signvalue = '-'; + uvalue = -value; + } + else + if (flags & DP_F_PLUS) /* Do a sign (+/i) */ + signvalue = '+'; + else + if (flags & DP_F_SPACE) + signvalue = ' '; + } - while( digits < width ) - *buf++ = ' '; - width--; + if (flags & DP_F_UP) + /* Should characters be upper case? */ + digits = "0123456789ABCDEF"; + else + digits = "0123456789abcdef"; - while( digits-- ) - *buf++ = text[ digits ]; + do { + convert[place++] = digits[uvalue % (unsigned)base]; + uvalue = (uvalue / (unsigned)base ); + } while(uvalue && (place < sizeof (convert))); + if (place == sizeof (convert)) place--; + convert[place] = 0; + + zpadlen = max - place; + spadlen = min - MAX (max, place) - (signvalue ? 1 : 0); + if (zpadlen < 0) zpadlen = 0; + if (spadlen < 0) spadlen = 0; + if (flags & DP_F_ZERO) + { + zpadlen = MAX(zpadlen, spadlen); + spadlen = 0; + } + if (flags & DP_F_MINUS) + spadlen = -spadlen; /* Left Justifty */ - *buf_p = buf; +#ifdef DEBUG_SNPRINTF + dprint (1, (debugfile, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n", + zpadlen, spadlen, min, max, place)); +#endif - if( prec < 0 ) - prec = 6; + /* Spaces */ + while (spadlen > 0) + { + total += dopr_outch (buffer, currlen, maxlen, ' '); + --spadlen; + } - // write the fraction - digits = 0; + /* Sign */ + if (signvalue) + total += dopr_outch (buffer, currlen, maxlen, signvalue); - while( digits < prec ) + /* Zeros */ + if (zpadlen > 0) { - fval -= (int)fval; - fval *= 10.0; - val = (int)fval; - text[ digits++ ] = '0' + val % 10; + while (zpadlen > 0) + { + total += dopr_outch (buffer, currlen, maxlen, '0'); + --zpadlen; + } } - if( digits > 0 ) - { - buf = *buf_p; - *buf++ = '.'; - for( prec = 0; prec < digits; prec++ ) - *buf++ = text[ prec ]; + /* Digits */ + while (place > 0) + total += dopr_outch (buffer, currlen, maxlen, convert[--place]); - *buf_p = buf; + /* Left Justified spaces */ + while (spadlen < 0) { + total += dopr_outch (buffer, currlen, maxlen, ' '); + ++spadlen; } + + return total; } -void AddVec3_t( char **buf_p, vec3_t v, int width, int prec ) +static LDOUBLE abs_val (LDOUBLE value) { - char *buf; + LDOUBLE result = value; - buf = *buf_p; + if (value < 0) + result = -value; - *buf++ = '['; + return result; +} + +static LDOUBLE pow10 (int exp) +{ + LDOUBLE result = 1; - AddFloat( &buf, v[ 0 ], width, prec ); - buf += width; - *buf++ = ' '; + while (exp) + { + result *= 10; + exp--; + } + + return result; +} - AddFloat( &buf, v[ 1 ], width, prec ); - buf += width; - *buf++ = ' '; +static long round (LDOUBLE value) +{ + long intpart; - AddFloat( &buf, v[ 2 ], width, prec ); - buf += width; - *buf++ = ']'; + intpart = value; + value = value - intpart; + if (value >= 0.5) + intpart++; - *buf_p = buf; + return intpart; } -void AddString( char **buf_p, char *string, int width, int prec ) +static int fmtfp (char *buffer, size_t *currlen, size_t maxlen, + LDOUBLE fvalue, int min, int max, int flags) { - int size; - char *buf; + int signvalue = 0; + LDOUBLE ufvalue; + char iconvert[20]; + char fconvert[20]; + int iplace = 0; + int fplace = 0; + int padlen = 0; /* amount to pad */ + int zpadlen = 0; + int caps = 0; + int total = 0; + long intpart; + long fracpart; - buf = *buf_p; + /* + * AIX manpage says the default is 0, but Solaris says the default + * is 6, and sprintf on AIX defaults to 6 + */ + if (max < 0) + max = 6; - if( string == NULL ) - { - string = "(null)"; - prec = -1; - } + ufvalue = abs_val (fvalue); - if( prec >= 0 ) - { - for( size = 0; size < prec; size++ ) - { - if( string[ size ] == '\0' ) - break; - } - } + if (fvalue < 0) + signvalue = '-'; else - size = strlen( string ); + if (flags & DP_F_PLUS) /* Do a sign (+/i) */ + signvalue = '+'; + else + if (flags & DP_F_SPACE) + signvalue = ' '; - width -= size; +#if 0 + if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ +#endif - while( size-- ) - *buf++ = *string++; + intpart = ufvalue; - while( width-- > 0 ) - *buf++ = ' '; + /* + * Sorry, we only support 9 digits past the decimal because of our + * conversion method + */ + if (max > 9) + max = 9; - *buf_p = buf; -} + /* We "cheat" by converting the fractional part to integer by + * multiplying by a factor of 10 + */ + fracpart = round ((pow10 (max)) * (ufvalue - intpart)); -/* -vsprintf + if (fracpart >= pow10 (max)) + { + intpart++; + fracpart -= pow10 (max); + } -I'm not going to support a bunch of the more arcane stuff in here -just to keep it simpler. For example, the '*' and '$' are not -currently supported. I've tried to make it so that it will just -parse and ignore formats we don't support. -*/ -int vsprintf( char *buffer, const char *fmt, va_list argptr ) -{ - int *arg; - char *buf_p; - char ch; - int flags; - int width; - int prec; - int n; - char sign; - - buf_p = buffer; - arg = (int *)argptr; - - while( qtrue ) +#ifdef DEBUG_SNPRINTF + dprint (1, (debugfile, "fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart)); +#endif + + /* Convert integer part */ + do { + iconvert[iplace++] = + (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10]; + intpart = (intpart / 10); + } while(intpart && (iplace < 20)); + if (iplace == 20) iplace--; + iconvert[iplace] = 0; + + /* Convert fractional part */ + do { + fconvert[fplace++] = + (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10]; + fracpart = (fracpart / 10); + } while(fracpart && (fplace < 20)); + if (fplace == 20) fplace--; + fconvert[fplace] = 0; + + /* -1 for decimal point, another -1 if we are printing a sign */ + padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); + zpadlen = max - fplace; + if (zpadlen < 0) + zpadlen = 0; + if (padlen < 0) + padlen = 0; + if (flags & DP_F_MINUS) + padlen = -padlen; /* Left Justifty */ + + if ((flags & DP_F_ZERO) && (padlen > 0)) { - // run through the format string until we hit a '%' or '\0' - for( ch = *fmt; ( ch = *fmt ) != '\0' && ch != '%'; fmt++ ) - *buf_p++ = ch; - - if( ch == '\0' ) - goto done; - - // skip over the '%' - fmt++; - - // reset formatting state - flags = 0; - width = 0; - prec = -1; - sign = '\0'; - -rflag: - ch = *fmt++; -reswitch: - switch( ch ) + if (signvalue) { - case '-': - flags |= LADJUST; - goto rflag; - - case '.': - n = 0; - while( is_digit( ( ch = *fmt++ ) ) ) - n = 10 * n + ( ch - '0' ); - - prec = n < 0 ? -1 : n; - goto reswitch; - - case '0': - flags |= ZEROPAD; - goto rflag; - - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - n = 0; - do - { - n = 10 * n + ( ch - '0' ); - ch = *fmt++; - } while( is_digit( ch ) ); - - width = n; - goto reswitch; + total += dopr_outch (buffer, currlen, maxlen, signvalue); + --padlen; + signvalue = 0; + } + while (padlen > 0) + { + total += dopr_outch (buffer, currlen, maxlen, '0'); + --padlen; + } + } + while (padlen > 0) + { + total += dopr_outch (buffer, currlen, maxlen, ' '); + --padlen; + } + if (signvalue) + total += dopr_outch (buffer, currlen, maxlen, signvalue); - case 'c': - *buf_p++ = (char)*arg; - arg++; - break; + while (iplace > 0) + total += dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]); - case 'd': - case 'i': - AddInt( &buf_p, *arg, width, flags ); - arg++; - break; + /* + * Decimal point. This should probably use locale to find the correct + * char to print out. + */ + if (max > 0) + { + total += dopr_outch (buffer, currlen, maxlen, '.'); - case 'f': - AddFloat( &buf_p, *(double *)arg, width, prec ); - arg += 1; // everything is 32 bit - break; + while (zpadlen-- > 0) + total += dopr_outch (buffer, currlen, maxlen, '0'); - case 's': - AddString( &buf_p, (char *)*arg, width, prec ); - arg++; - break; + while (fplace > 0) + total += dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]); + } - case 'v': - AddVec3_t( &buf_p, (vec_t *)*arg, width, prec ); - arg++; - break; - - case 'x': - flags |= HEX; - AddInt( &buf_p, *arg, width, prec ); - arg++; - break; + while (padlen < 0) + { + total += dopr_outch (buffer, currlen, maxlen, ' '); + ++padlen; + } - case '%': - *buf_p++ = ch; - break; + return total; +} - default: - *buf_p++ = (char)*arg; - arg++; - break; - } - } +static int dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c) +{ + if (*currlen + 1 < maxlen) + buffer[(*currlen)++] = c; + return 1; +} -done: - *buf_p = 0; - return buf_p - buffer; +int Q_vsnprintf(char *str, size_t length, const char *fmt, va_list args) +{ + if (str != NULL) + str[0] = 0; + return dopr(str, length, fmt, args); } +int Q_snprintf(char *str, size_t length, const char *fmt, ...) +{ + va_list ap; + int retval; + + va_start(ap, fmt); + retval = Q_vsnprintf(str, length, fmt, ap); + va_end(ap); + + return retval; +} /* this is really crappy */ int sscanf( const char *buffer, const char *fmt, ... ) diff --git a/src/game/bg_lib.h b/src/game/bg_lib.h index c9ffd65b..6225ce99 100644 --- a/src/game/bg_lib.h +++ b/src/game/bg_lib.h @@ -103,8 +103,6 @@ double _atof( const char **stringPtr ); int atoi( const char *string ); int _atoi( const char **stringPtr ); - -int vsprintf( char *buffer, const char *fmt, va_list argptr ); int sscanf( const char *buffer, const char *fmt, ... ); // Memory functions diff --git a/src/game/g_main.c b/src/game/g_main.c index 8af780fd..b39b7141 100644 --- a/src/game/g_main.c +++ b/src/game/g_main.c @@ -338,7 +338,7 @@ void QDECL G_Printf( const char *fmt, ... ) char text[ 1024 ]; va_start( argptr, fmt ); - vsprintf( text, fmt, argptr ); + Q_vsnprintf( text, sizeof( text ), fmt, argptr ); va_end( argptr ); trap_Print( text ); @@ -350,7 +350,7 @@ void QDECL G_Error( const char *fmt, ... ) char text[ 1024 ]; va_start( argptr, fmt ); - vsprintf( text, fmt, argptr ); + Q_vsnprintf( text, sizeof( text ), fmt, argptr ); va_end( argptr ); trap_Error( text ); @@ -712,7 +712,7 @@ void QDECL Com_Error( int level, const char *error, ... ) char text[ 1024 ]; va_start( argptr, error ); - vsprintf( text, error, argptr ); + Q_vsnprintf( text, sizeof( text ), error, argptr ); va_end( argptr ); G_Error( "%s", text ); @@ -724,7 +724,7 @@ void QDECL Com_Printf( const char *msg, ... ) char text[ 1024 ]; va_start( argptr, msg ); - vsprintf( text, msg, argptr ); + Q_vsnprintf( text, sizeof( text ), msg, argptr ); va_end( argptr ); G_Printf( "%s", text ); @@ -1615,7 +1615,7 @@ void QDECL G_LogPrintf( const char *fmt, ... ) Com_sprintf( string, sizeof( string ), "%3i:%i%i ", min, tens, sec ); va_start( argptr, fmt ); - vsprintf( string +7 , fmt,argptr ); + Q_vsnprintf( string + 7, sizeof( string ) - 7, fmt, argptr ); va_end( argptr ); if( g_dedicated.integer ) diff --git a/src/game/g_team.c b/src/game/g_team.c index 7d54c5fb..b0088ff5 100644 --- a/src/game/g_team.c +++ b/src/game/g_team.c @@ -32,7 +32,7 @@ void QDECL PrintMsg( gentity_t *ent, const char *fmt, ... ) va_start( argptr,fmt ); - if( vsprintf( msg, fmt, argptr ) > sizeof( msg ) ) + if( Q_vsnprintf( msg, sizeof( msg ), fmt, argptr ) > sizeof( msg ) ) G_Error ( "PrintMsg overrun" ); va_end( argptr ); |