From e836e480dcc72824667f4582b7d6c2bbe70e424e Mon Sep 17 00:00:00 2001 From: Bruce Evans Date: Mon, 3 Mar 1997 17:53:02 +0000 Subject: [PATCH] Fixed handling of input failure by the scanf family. - 0 was returned instead of EOF when an input failure occured while skipping white-space after 0 assignments. This fixes PR2606. The diagnosis in PR2606 is wrong. - EOF was returned instead of 0 when an input failure occurred after zero assignments and nonzero suppressed assignments. - EOF was spelled -1. This should be in 2.2. --- lib/libc/stdio/vfscanf.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/libc/stdio/vfscanf.c b/lib/libc/stdio/vfscanf.c index b39f78948737..54cf77674d46 100644 --- a/lib/libc/stdio/vfscanf.c +++ b/lib/libc/stdio/vfscanf.c @@ -39,7 +39,7 @@ static char sccsid[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93"; #endif static const char rcsid[] = - "$Id$"; + "$Id: vfscanf.c,v 1.7 1997/02/22 15:02:41 peter Exp $"; #endif /* LIBC_SCCS and not lint */ #include @@ -115,6 +115,7 @@ __svfscanf(fp, fmt0, ap) register int flags; /* flags as defined above */ register char *p0; /* saves original value of p when necessary */ int nassigned; /* number of fields assigned */ + int nconversions; /* number of conversions */ int nread; /* number of characters consumed from fp */ int base; /* base argument to strtol/strtoul */ u_long (*ccfn)(); /* conversion function (strtol/strtoul) */ @@ -126,6 +127,7 @@ __svfscanf(fp, fmt0, ap) { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; nassigned = 0; + nconversions = 0; nread = 0; base = 0; /* XXX just to keep gcc happy */ ccfn = NULL; /* XXX just to keep gcc happy */ @@ -136,7 +138,7 @@ __svfscanf(fp, fmt0, ap) if (isspace(c)) { for (;;) { if (fp->_r <= 0 && __srefill(fp)) - return (nassigned); + goto input_failure; if (!isspace(*fp->_p)) break; nread++, fp->_r--, fp->_p++; @@ -261,6 +263,7 @@ again: c = *fmt++; break; case 'n': + nconversions++; if (flags & SUPPRESS) /* ??? */ continue; if (flags & SHORT) @@ -349,6 +352,7 @@ again: c = *fmt++; nread += r; nassigned++; } + nconversions++; break; case CT_CCL: @@ -390,6 +394,7 @@ again: c = *fmt++; nassigned++; } nread += n; + nconversions++; break; case CT_STRING: @@ -420,6 +425,7 @@ again: c = *fmt++; nread += p - p0; nassigned++; } + nconversions++; continue; case CT_INT: @@ -556,6 +562,7 @@ again: c = *fmt++; nassigned++; } nread += p - buf; + nconversions++; break; #ifdef FLOATING_POINT @@ -647,12 +654,13 @@ again: c = *fmt++; nassigned++; } nread += p - buf; + nconversions++; break; #endif /* FLOATING_POINT */ } } input_failure: - return (nassigned ? nassigned : -1); + return (nconversions != 0 ? nassigned : EOF); match_failure: return (nassigned); }