How can I read keyboard without stopping

Michael "Ford" Ditto ditto at cbmvax.UUCP
Thu Sep 1 14:13:05 AEST 1988


In article <669 at super.ORG> rminnich at metropolis.UUCP (Ronald G Minnich) writes:
[ about reading from ttys, possibly in O_NDELAY mode ]
>Also, on most unix's you can't tell whether there are 0 bytes
>to read because of an eof or 0 bytes cause nothing is there yet. 

But these are exactly the same condition.  Typing EOF on a tty in
cooked mode, by definition, causes a blocked read to "unblock", even if
it otherwise would have waited for some other contidion (like a line
terminator).  In other words, typing ^D causes exactly one read() to
magically be in NDELAY mode, causing it to return with whatever is in
the buffer, without blocking.  That's why typing ^D at the beginning of
a line looks to a program as if it has hit the end of a file -- the read
returns 0 bytes.

One might argue, however that O_NDELAY is a special case of an "early
return", and that an EWOULDBLOCK error is justified.  This could be
made consistant across all devices that support NDELAY reads.

The "unblocking" behavior of EOF can be useful sometimes... most
people think that the write(1) program only sends messages one-
line-at-a-time.  Actually, it sends them one-read()-at-a-time; if
you type ^D in the middle of your line, write's read() will unblock,
and what you have typed so far will appear on the destination terminal
(without a newline).  Then you can continue with the rest of the line.
-- 
					-=] Ford [=-

	.		.		(In Real Life: Mike Ditto)
.	    :	       ,		ford at kenobi.cts.com
This space under construction,		...!ucsd!elgar!ford
pardon our dust.			ditto at cbmvax.commodore.com



More information about the Comp.unix.wizards mailing list