Querying tty input with ioctl

Dave Hammond daveh at marob.MASA.COM
Mon Jul 18 22:44:52 AEST 1988


In article <Jul.14.21.26.56.1988.8739 at topaz.rutgers.edu> ron at topaz.rutgers.edu (Ron Natalie) writes:
>I believe what you are asking is how to detect when there
>is TTY input to read.  This can be done with the select
>call.  A simplistic example:
>[...stuff deleted...]
>    select(1, &rflags, (int *) 0,  (int *) 0, &tv);
>    if(rflags)  {
>	/* There is something to read  */
>	read(ttyfd, &c, 1);
>    }

Or in SysV land-

#include <fcntl.h>

int fcntl_flags = fcntl(0, F_GETFL, 0);		/* get fcntl flags */
fcntl(0, F_SETFL, fcntl_flags | O_NDELAY);	/* set "no delay" */
if (read(0, &ch, 1) > 0)			/* there's a char queued */
    do_something();
fcntl(0, F_SETFL, fcntl_flags);			/* unset "no delay" */

Read in O_NDELAY mode will return -1 of the read would block (i.e. there
are no chars waiting in the tty queue). This allows you to check for
pending input without causing the program to "block" waiting for input.

Dave Hammond
UUCP:   {uunet|rutgers|spl1|...}!hombre!marob!daveh
-----------------------------------------------------



More information about the Comp.unix.questions mailing list