[comp.sys.mac] Scanf bugfix for Think C 4.0

jp@Apple.COM (John Peterson) (09/07/89)

In playing with Think C 4.0, I've found a couple of bugs in their
scanf routines for floating point.  First, it drops leading zeros
occuring after "0.", e.g., "0.001234" gets read as "0.1234".  Second,
if you input a string of more than 20 digits, it starts dividing the
result by ten for every extra digit.

The following patch appears to correct these problems.  Note I haven't
tested these fixes extensivly (although I've apparently tested them more
than Think did...)

In routine _vfscanf (in C Libraries:sources:scanf.c) change the code 
fragment:
-----------				
	/*  (floating) process each digit  */
			
	case -2:
	    if (c >= '0' && c <= '9') {
                F.valid = TRUE;
		if (c != '0' || D.sig[0]) {
		    if (D.sig[0] < sizeof D.sig - 1)
			D.sig[++D.sig[0]] = c;
		    if (F.dot)
			--D.exp;
		}
	    }

to look like this:
-----------
	/*  (floating) process each digit  */
				
	case -2:
	    if (c >= '0' && c <= '9') {
		F.valid = TRUE;
		if (c != '0' || D.sig[0]) {
		    if (D.sig[0] < sizeof D.sig - 1) {
			D.sig[++D.sig[0]] = c;
			/* Bugfix JWP - Don't bump exp for ignored digits */
			if (F.dot)
			    --D.exp;
		    }
		}

		/* Bugfix JWP - Don't ignore zeros after "0." */
		if (c == '0' && (! D.sig[0]))
		    if (F.dot)
			--D.exp;
	    }

Then rebuild the various "ANSI" projects (note the configuration files
contained in the "config" directory.)  Thank heaven for sources.

Cheers,
John "so That's why my code broke" Peterson

[Disclaimer: This posting is not endorsed or supported by Apple Computer Inc.]