unix terminal i/o

Paul S. R. Chisholm psc at lzwi.UUCP
Tue Jun 18 23:17:57 AEST 1985


< Smokey the Bar says, "Help stamp out software pirates" [squish!] >

In article <452 at mmintl.UUCP>, robg at mmintl.UUCP (Robert Goldman) writes:
> 
> I am working on a program that needs to access the terminal in a pretty raw
> state.  In particular, it must have some way to tell whether there are any
> keys in the input buffer waiting to be read.  Is there some portable unix
> call that responds with this information?
> 			Robert Goldman, MultiMate International

In Unix(tm) System III and V (all releases), you can ioctl() the line
to have VMIN and VTIME set to 0; a read() will only get any characters
in the input queue.  (Note that ioctl(fd,TCFLSH,0) will flush the input
queue.)

More portably, you can fcntl() the file descriptor with O_NDELAY, which
avoids blocking on tty's and pipes (and fifos).  If you're always going
to avoid blocking, you can open() the file with O_NDELAY.

Both ioctl() and fcntl() are pretty cheap to call, so it's not too
expensive to go back and forth.
--
Unix is a trademark of some entity within AT&T, but I don't know what
it's called this week.  AT&T's Bell Labs?
-- 
       -Paul S. R. Chisholm       The above opinions are my own,
       {pegasus,vax135}!lzwi!psc  not necessarily those of any
       {mtgzz,ihnp4}!lznv!psc     telecommunications company.



More information about the Comp.unix.wizards mailing list