4.2bsd eof flag in stdio

Doug Gwyn <gwyn> gwyn at brl-tgr.ARPA
Thu Nov 22 01:09:06 AEST 1984


> #include <stdio.h>
> 
> char	buf[256];
> 
> main()
> {
> 	register int n;
> 
> 	while (n = fread(buf, 1, sizeof buf, stdin))
> 		fwrite(buf, 1, n, stdout);
> 	printf("got EOF\n");
> }
> 
> Run it and type (e.g.):
> 
> testing 1 2 3
> ^D
> another test
> 
> Where ^D is your EOT character.  If the program terminates
> when you type ^D then your stdio works properly.  The 4.1
> version of stdio would "eat" the ^D and echo the first and
> third lines.  It would only terminate if you typed ^D twice
> in a row.

Thanks for the example, Bill.  I guess we disagree about what is
expected here.  The "EOFish" nature of the input is reflected in
fread()'s short return count; as expected the 0-length read forces
fread() to return prematurely.  I see no reason for it to "stick"
at EOF, though.  The programmer certainly can tell that he is at
EOF from the short count.  Continuing to read the stream is a
programming error (that happens to work on "ordinary" files,
unless they are being dynamically appended to), and more than a bit
sloppy besides (just like the internals of most UNIX utilities).

I see the argument for the other interpretation; I just don't
agree with it.



More information about the Comp.unix.wizards mailing list