[comp.unix.wizards] EOF problem under 4.3 and 2.10 BSD

jack@cadre.dsl.PITTSBURGH.EDU (Jack Nelson) (06/04/89)

   I have encountered behavior in BSD 4.3 and 2.10 which differs from
sys V, rel 3.2 behavior, and seems to me to be a bug.  If this program
is run, and a letter or a few are input, the behavior is as expected:
the line gets printed out.  If one now hits ^D (EOF) at the prompt, the
last line (or else just the prompt if there was no input after the
last prompt) spews out repeatedly until interrupted with ^C.
   Under sysV, a ^D just reechos the last line, which seems more
reasonable.
   Any ideas?  Any workarounds?

#include <stdio.h>

main()
{
	char line[128];
	for (;;) {
		printf("> ");
		gets(line);
		printf("line is %s\n",line);
	}
}

John P. Nelson, M.D., 3811 O'Hara St, Pittsburgh, PA 15213, t:412-624-1769 Dept. of Psychiatry, U. of Pittsburgh
UUCP: { akgua | allegra | cmcl2 | idis | ihnp4 | mi-cec | pitt | psuvax1 | sun | sunrise | vax135 } ! cadre ! jack
ARPA: jack@cadre.dsl.pittsburgh.edu
-- 
John P. Nelson, M.D., 3811 O'Hara St, Pittsburgh, PA 15213, t:412-624-1769 Dept. of Psychiatry, U. of Pittsburgh
UUCP: { akgua | allegra | cmcl2 | idis | ihnp4 | mi-cec | pitt | psuvax1 | sun | sunrise | vax135 } ! cadre ! jack
ARPA: jack@cadre.dsl.pittsburgh.edu

maart@cs.vu.nl (Maarten Litmaath) (06/06/89)

jack@cadre.dsl.PITTSBURGH.EDU (Jack Nelson) writes:
\...  If one now hits ^D (EOF) at the prompt, the
\last line (or else just the prompt if there was no input after the
\last prompt) spews out repeatedly until interrupted with ^C.
\   Under sysV, a ^D just reechos the last line, which seems more
\reasonable.

It is indeed! The problem: some BSD developer introduced the `sticky EOF'
concept for stdio - once EOF has been encountered, the flag signaling this
condition won't be reset if more input is available, UNTIL clearerr() has
been called for the corresponding stream.
Note: you didn't test the return value from gets() - it will return (char *) 0
for EOF (or error; use ferror()/feof() to distinguish).

\...
\	for (;;) {
\		printf("> ");
\		gets(line);
\		printf("line is %s\n",line);
\	}
-- 
 "Your password [should be] like your |Maarten Litmaath @ VU Amsterdam:
      toothbrush." (Don Alvarez)      |maart@cs.vu.nl, mcvax!botter!maart