Path: utzoo!utgpu!attcan!uunet!cbmvax!ditto From: ditto@cbmvax.UUCP (Michael "Ford" Ditto) Newsgroups: comp.unix.wizards Subject: Re: How can I read keyboard without stopping Summary: read(ttyfd,...)==0 means no data ready Keywords: curses keyboard Message-ID: <4617@cbmvax.UUCP> Date: 1 Sep 88 04:13:05 GMT References: <813@ms3.UUCP> <1246@mcgill-vision.UUCP> <1988Aug15.130550.8571@ateng.uucp> <1267@ficc.UUCP> <669@super.ORG> Reply-To: ditto@cbmvax.UUCP (Michael "Ford" Ditto) Organization: Commodore Technology, West Chester, PA Lines: 31 In article <669@super.ORG> rminnich@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@kenobi.cts.com This space under construction, ...!ucsd!elgar!ford pardon our dust. ditto@cbmvax.commodore.com