[net.unix-wizards] getchar

mm1751@Almsa-1.ARPA (04/23/84)

From:      Mary Mallott <mm1751@Almsa-1.ARPA>

We recently converted to 4.2 from 4.1 and I had some fairly simple programs
that worked fine before and the compiled versions continued to work fine
under 4.2.  But when I recompiled the same source under 4.2, getchar() seems
to hang on to EOF characters.  If I do getchars and end with ^D, the next 
time (or multiple times) I do either getchar or scanf, it doesn't wait
for input.  It just continues on and the variables read into are null.

What am I missing?  These programs were all originally written for
system III but I can't think why that would matter.

If this sounds too simple for wizards, reply directly to me.

Thanks.
Mary Mallott
mm1751@almsa-1.ARPA

tony@asgb.UUCP (05/01/84)

The stdio library was modified in 4.2 so that an EOF condition is not cleared
as it used to be on subsequent reads from a tty. That breaks a lot of things
that used to act intelligently on EOF (learn, for example).

The easiest fix is to call "clrerr(stdin)" when an EOF is detected to reset
the condition. Further reads should then work.

I don't know what the rationale for this change was, but it's probably safer
to fix the things it affects than to revert to the old behavior of stdio.

Tony Andrews
Burroughs Corp.
ihnp4!sdcrdcf!bmcg!asgb!tony

larry@uthub.UUCP (Larry Philps) (05/02/84)

[]
I have also been bothered by the change in the stdio "get"
interface.  In particular the code that was changed is
in filbuf.c where |_IOEOF was added to the if condition.

	if (iop->_flag&(_IOSTRG|_IOEOF))
		return(EOF);

This is a reasonable thing to do when reading from a real
file, but when the reads are from a tty, it is quite possible
to have multiple EOF's on the same stream (ex. learn, addbib, ...).

Can anyone think of any problems that would result from changing
the above to:

	if (iop->_flag&_IOSTRG)
		return(EOF);
	if ((iop->_flag&_IOEOF) && !isatty(fileno(iop)))
		return(EOF);

This would force the 4.1BSD protocol when reading from tty's
and the 4.2BSD protocol at all other times.  I have not tried
this myself, but think it should work without mucking up anything else.
-- 
UUCP:	{ allegra cornell decvax decwrl floyd ihnp4 linus
	  sask ubc-vision utzoo uw-beaver watmath } !utcsrgv!uthub!larry