[comp.unix.programmer] Non-destructive read of keyboard buffer

jit@slic.cellbio.duke.edu (06/08/91)

Hi, I am trying to find out if a keyboard buffer is empty but not reading
it because I don't want to get stuck if there isn't any buffered.
(on ATT SVR3)

Is there a 'standard' way on unix to do this ?

(I am currently reading using non-canonical mode with vmin=0, then
ungetc if there is any.  Hopefully there is an easier way.)

Thanks in advance.
-- 

--------------------------------------------------------
Jit Keong Tan     | internet: jit@slic.cellbio.duke.edu
(919) 684-8098    | bitnet  : tan00001@dukemc.bitnet

mycroft@kropotki.gnu.ai.mit.edu (Charles Hannum) (06/08/91)

In article <22368@duke.cs.duke.edu> jit@slic.cellbio.duke.edu writes:

   Hi, I am trying to find out if a keyboard buffer is empty but not reading
   it because I don't want to get stuck if there isn't any buffered.
   (on ATT SVR3)

   Is there a 'standard' way on unix to do this ?

You can use select(2) under BSD, or set the terminal to non-blocking I/O
(using the FIONBIO ioctl), try to read, and see if you get EWOULDBLOCK.

Details available upon request.  B-)

guy@auspex.auspex.com (Guy Harris) (06/10/91)

>   Hi, I am trying to find out if a keyboard buffer is empty but not reading
>   it because I don't want to get stuck if there isn't any buffered.
>   (on ATT SVR3)
>
>   Is there a 'standard' way on unix to do this ?
>
>You can use select(2) under BSD,

Unfortunately, he's not *using* BSD; he's using System V Release 3, as
he said.  No, SVR3 doesn't necessarily have "select()", nor the FIONBIO
"ioctl", nor EWOULDBLOCK.

Answer: it does have non-blocking I/O on ttys; check out the F_GETFL and
F_SETFL "fcntl" calls.  WARNING: unlike various things in BSD,
non-streams ttys return 0, not -1, if no data is available to be read
and they're in non-blocking mode.  The program must make absolutely
positively sure that it doesn't leave the terminal in non-blocking mode;
otherwise, if it exits and leaves the terminal in that mode, the shell
may see a zero-byte return if the user doesn't type a command fast
enough, will think that's an end-of-file, and exit.