]> pd.if.org Git - pdclib/blobdiff - functions/stdio/vfscanf.c
scanf() returns -1 on early input error.
[pdclib] / functions / stdio / vfscanf.c
index 99a752ff2db0258fc8ccc67d47faae2162f3e5ac..e1c8b4b40efd8fd28040cbf47600647ab0e9e4bb 100644 (file)
@@ -42,7 +42,7 @@ int vfscanf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict forma
                 {
                     ++status.i;
                 }
-                if ( c != EOF )
+                if ( ! feof( stream ) ) /* TODO: Check EOF status directly */
                 {
                     ungetc( c, stream );
                 }
@@ -50,10 +50,17 @@ int vfscanf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict forma
             else
             {
                 /* Non-whitespace char in format string: Match verbatim */
-                if ( ( c = getc( stream ) ) != *format )
+                if ( ( ( c = getc( stream ) ) != *format ) || feof( stream ) ) /* TODO: Check EOF status directly */
                 {
                     /* Matching error */
-                    ungetc( c, stream );
+                    if ( ! feof( stream ) && ! ferror( stream ) ) /* TODO: Check EOF status directly */
+                    {
+                        ungetc( c, stream );
+                    }
+                    else if ( status.n == 0 )
+                    {
+                        return EOF;
+                    }
                     return status.n;
                 }
                 else