Querying tty input with ioctl

Dave Hammond daveh at marob.MASA.COM
Mon Jul 25 22:43:18 AEST 1988


In article <5740024 at hpfcdc.HP.COM> rml at hpfcdc.HP.COM (Bob Lenk) writes:
>> int fcntl_flags = fcntl(0, F_GETFL, 0);		/* get fcntl flags */
>                             ^^^^^^^
>                             O_GETFL
>> fcntl(0, F_SETFL, fcntl_flags | O_NDELAY);	/* set "no delay" */
>           ^^^^^^^
>           O_SETFL
>> if (read(0, &ch, 1) > 0)			/* there's a char queued */
>>     do_something();
>> fcntl(0, F_SETFL, fcntl_flags);			/* unset "no delay" */
>           ^^^^^^^
>           O_GETFL

>From the XENIX man page for fcntl:

	  F_GETFL Gets file status flags:  O_RDONLY, O_WRONLY, O_RDWR,
		  O_NDELAY, or O_APPEND.

	  F_SETFL Sets file status flags	to arg.	 Only certain flags
		  can be	set.

While I'll agree that XENIX is not virgin SysV, it is a certified SysV port.
I can't recall programs written non-XENIX systems having to be modified
(relative to the above F_GETFL/O_GETFL question) to compile under XENIX, or
vice-versa.

>> Read in O_NDELAY mode will return -1 of the read would block (i.e. there
>                                    ^^
>                                     0

I stand corrected. Indeed, read() returns 0 if O_NDELAY is set and a dry
read() occurs. The above code fragment likely works because it tests
for a success rather than failure return value.

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



More information about the Comp.unix.questions mailing list