4.2bsd eof flag in stdio

Kenneth Almquist ka at cbosgd.UUCP
Fri Nov 30 17:23:41 AEST 1984


> The fact that some programs have in the past misinterpreted this bug as a
> feature and made use of it is unfortunate, but something we will have to
> live with.

Arggh!

The 4.2 BSD manual page for getchar states that, "These functions return
the integer constant EOF at end of file...."  Now for a standard UNIX
file, the end of file is the location immediately above the last byte
written.  Thus if getchar returns EOF, something is appended to the
input file, and getchar is called again, getchar should not return EOF
because the file pointer is no longer at end of file.

The fact that the 4.2 BSD implementation of getchar handles EOF differ-
ently not only from all other variants of UNIX, but also from the way
its own documentation says it should handle EOF, is indeed "unfortunate,
but something we will have to live with."

If we may believe Bill Shannon, the whole issue arose because nobody
could figure out how to make a straightforward change to fread.  The
change could have been implemented as follows:

1)  Add a new flag named _EOF_PUSHED_BACK to stdio.h.
2)  When _filbuf is called with this flag set, have it clear the flag
    and return EOF.
3)  While a new routine called pushback which is just like ungetc except
    that pushback(EOF, fp) should set _EOF_PUSHED_BACK and return.
4)  When fread encounter EOF and it has read at least one item, have it
    call pushback(EOF, fp) before returning.

				Kenneth Almquist



More information about the Comp.unix.wizards mailing list