How can I read keyboard without stopping
Ronald G Minnich
rminnich at super.ORG
Sat Sep 3 00:43:30 AEST 1988
In article <4617 at cbmvax.UUCP> ditto at cbmvax.UUCP (Michael "Ford" Ditto) writes:
>In article <669 at super.ORG> rminnich at metropolis.UUCP (Ronald G Minnich) writes:
>>Also, on most unix's you can't tell whether there are 0 bytes
>>to read because of an eof or 0 bytes cause nothing is there yet.
>But these are exactly the same condition. Typing EOF on a tty in
> ... good explanation of how the read() tells you what's going on.
Sure, you can tell lots of things from the return of a read().
As i understood the original question it was what you
could determine WITHOUT doing the read(). There are cases where
you don't want to read without knowing whether you will block.
That's why people use FIONREAD (which not everyone supports), or
select (ditto), or in this case stat(). Or all the other
not-quite-universal mechanisms.
There is a way to tell you are at eof, btw. ON 4.2-style systems,
do a select on the file descriptor, and see if it returns indicating
'something there'. Then do an ioctl(fildes, FIONREAD, &length) and
see if length is zero. If it is, the system is telling you:
there is something you can read, but you won't get anything, i.e. eof.
It is also telling you that you will return immediately if you do read.
SO, do a read(fildes, buf, 1), get the EOF indication back, fildes is
now closed.
This works on pipes (i.e. sockets) and ttys. I never got
around to trying it on files.
ron
More information about the Comp.unix.wizards
mailing list