[net.unix-wizards] unix terminal i/o

robg@mmintl.UUCP (Robert Goldman) (06/14/85)

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?  A getchar()-type function that
doesn't wait for input?  I'd appreciate any help you can give me.  

						Robert Goldman
						MultiMate International

P.S.  If you are answering from net.unix-wizards, please send answer via
mail -- I'm not wizardly enough for this newsgroup. :-(

psc@lzwi.UUCP (Paul S. R. Chisholm) (06/18/85)

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

In article <452@mmintl.UUCP>, robg@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.