cvs: php4 /main snprintf.c snprintf.h

From: Date: Tue, 07 May 2002 05:26:30 +0000
Subject: cvs: php4 /main snprintf.c snprintf.h
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-11734@lists.php.net to get a copy of this message
sas Tue May 7 01:26:30 2002 EDT Modified files: /php4/main snprintf.h snprintf.c Log: Add thread-safety to some conversion functions and sync a bit with APR. Index: php4/main/snprintf.h diff -u php4/main/snprintf.h:1.15 php4/main/snprintf.h:1.16 --- php4/main/snprintf.h:1.15 Tue Apr 9 21:07:49 2002 +++ php4/main/snprintf.h Tue May 7 01:26:30 2002 @@ -34,6 +34,10 @@ #define sprintf php_sprintf #endif +typedef enum { + NO = 0, YES = 1 +} boolean_e; + #ifdef HAVE_GCVT #define ap_php_ecvt ecvt @@ -42,16 +46,12 @@ #else -extern char * ap_php_cvt(double arg, int ndigits, int *decpt, int *sign, int eflag); -extern char * ap_php_ecvt(double arg, int ndigits, int *decpt, int *sign); -extern char * ap_php_fcvt(double arg, int ndigits, int *decpt, int *sign); -extern char * ap_php_gcvt(double number, int ndigit, char *buf); +extern char * ap_php_cvt(double arg, int ndigits, int *decpt, int *sign, int eflag, char *buf); +extern char * ap_php_ecvt(double arg, int ndigits, int *decpt, int *sign, char *buf); +extern char * ap_php_fcvt(double arg, int ndigits, int *decpt, int *sign, char *buf); +extern char * ap_php_gcvt(double number, int ndigit, char *buf, boolean_e altform); #endif - -typedef enum { - NO = 0, YES = 1 -} boolean_e; #define WIDE_INT long typedef WIDE_INT wide_int; Index: php4/main/snprintf.c diff -u php4/main/snprintf.c:1.13 php4/main/snprintf.c:1.14 --- php4/main/snprintf.c:1.13 Thu Apr 11 03:28:24 2002 +++ php4/main/snprintf.c Tue May 7 01:26:30 2002 @@ -88,7 +88,7 @@ * is declared as buf[ 100 ], buf_end should be &buf[ 100 ]) */ char * - ap_php_conv_10(register wide_int num, register bool_int is_unsigned, +ap_php_conv_10(register wide_int num, register bool_int is_unsigned, register bool_int * is_negative, char *buf_end, register int *len) { register char *p = buf_end; @@ -147,17 +147,19 @@ register char *s = buf; register char *p; int decimal_point; + char buf1[NDIG]; if (format == 'f') - p = ap_php_fcvt(num, precision, &decimal_point, is_negative); + p = ap_php_fcvt(num, precision, &decimal_point, is_negative, buf1); else /* either e or E format */ - p = ap_php_ecvt(num, precision + 1, &decimal_point, is_negative); + p = ap_php_ecvt(num, precision + 1, &decimal_point, is_negative, buf1); /* * Check for Infinity and NaN */ if (isalpha((int)*p)) { - *len = strlen(strcpy(buf, p)); + *len = strlen(p); + memcpy(buf, p, *len + 1); *is_negative = FALSE; return (buf); } @@ -274,12 +276,11 @@ #define NDIG 80 char * - ap_php_cvt(double arg, int ndigits, int *decpt, int *sign, int eflag) +ap_php_cvt(double arg, int ndigits, int *decpt, int *sign, int eflag, char *buf) { register int r2; double fi, fj; register char *p, *p1; - static char buf[NDIG]; if (ndigits >= NDIG - 1) ndigits = NDIG - 2; @@ -319,8 +320,7 @@ return (buf); } while (p <= p1 && p < &buf[NDIG]) { - arg *= 10; - arg = modf(arg, &fj); + arg = modf(arg * 10, &fj); *p++ = (int) fj + '0'; } if (p1 >= &buf[NDIG]) { @@ -348,15 +348,15 @@ } char * - ap_php_ecvt(double arg, int ndigits, int *decpt, int *sign) +ap_php_ecvt(double arg, int ndigits, int *decpt, int *sign, char *buf) { - return (ap_php_cvt(arg, ndigits, decpt, sign, 1)); + return (ap_php_cvt(arg, ndigits, decpt, sign, 1, buf)); } char * - ap_php_fcvt(double arg, int ndigits, int *decpt, int *sign) +ap_php_fcvt(double arg, int ndigits, int *decpt, int *sign, char *buf) { - return (ap_php_cvt(arg, ndigits, decpt, sign, 0)); + return (ap_php_cvt(arg, ndigits, decpt, sign, 0, buf)); } /* @@ -365,13 +365,14 @@ */ char * - ap_php_gcvt(double number, int ndigit, char *buf) +ap_php_gcvt(double number, int ndigit, char *buf, boolean_e altform) { int sign, decpt; register char *p1, *p2; register i; + char buf1[NDIG]; - p1 = ap_php_ecvt(number, ndigit, &decpt, &sign); + p1 = ap_php_ecvt(number, ndigit, &decpt, &sign, buf1); p2 = buf; if (sign) *p2++ = '-'; @@ -415,7 +416,7 @@ *p2++ = '.'; } } - if (p2[-1] == '.') + if (p2[-1] == '.' && !altform) p2--; *p2 = '\0'; return (buf); @@ -762,7 +763,8 @@ /* * * We use &num_buf[ 1 ], so that we have room for the sign */ - s = ap_php_gcvt(va_arg(ap, double), precision, &num_buf[1]); + s = ap_php_gcvt(va_arg(ap, double), precision, &num_buf[1], + alternate_form); if (*s == '-') prefix_char = *s++; else if (print_sign)

« previous php.cvs (#11734) next »