stat() in general (was read keyboard without stopping)

Chris Torek chris at mimsy.UUCP
Wed Sep 7 01:32:44 AEST 1988


In article <690 at super.ORG> rminnich at super.ORG (Ronald G Minnich) writes:
>   I was not talking about tty devices, rather peter da silva's original
>comment questioning stat's not supporting the size field for ttys. 
>That comment got me to thinking about stat()s lack of generality, 
>since i had been burned by it once. I think that he was making the same point.

Ah.  Well, perhaps there should be several more stat fields, like
`distance to EOF if known', `number of bytes available without blocking',
and so forth.  (Note that the first two are not the same!  The subject
is `read keyboard without stopping' and I had been interpreting this
entire discussion from that viewpoint.  Please edit the subject line
when appropriate.)

(Incidentally, all the `on SysV' and `in SunOS 3.x' provisos I named
were intended as jabs at SunOS and SysV for botching generality.  Naturally
I consider 4BSD the One True Unix :-) and tend to ignore the others
when making statements about how `Unix' works.)

>>On regular files, you are at EOF if your current seek offset (available
>>via lseek()) matches the size returned via fstat().

>Only if someone else is not writing?

This is irrelevant.

>If you are reading a regular file, and someone else is writing, then
>there are times when your current seek offset will match the returned
>size. You are still not at eof, as someone else has the file open.

You are still at EOF.  That someone else has the file open is irrelevant.

>More data can come along. This is 'nothing available to read, but you
>are not at eof'.  I.e. tail -f, tail never really does know when it hits
>eof- you, peculiar creature that you are, decide for it via ^C :-).

But even if *no one* has the file open, someone can open it, append
data, and possibly close it again; then you are not at EOF anymore.
Any multiprocessing system without mandatory locks has this `feature'.
EOF is a fleeting concept.  You cannot pin it down save momentarily.

>P.S. Does it strike anyone as strange that stdio and level 2 have such
>different ideas about eof? Level 2 you can just read right through ^D; 
>As was pointed out, it is more like an enforced one-read FNDELAY than 
>anything else. for standard IO, one is enough; you lose it all right there. 
>The FILE * gets closed for you. 

False.  You must, however, call `clearerr' on the FILE pointer:

	...
	if ((c = getc(fp)) == EOF) {
		if (ferror(fp)) /* read error */ ...
		clearerr(fp); /* clobber EOF condition */
	} else {
		... process ...
	}
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.unix.wizards mailing list