Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site rlgvax.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!bellcore!decvax!genrad!teddy!panda!talcott!harvard!seismo!rlgvax!guy From: guy@rlgvax.UUCP (Guy Harris) Newsgroups: net.unix-wizards Subject: Re: Scanning stdin with no pause Message-ID: <352@rlgvax.UUCP> Date: Mon, 14-Jan-85 23:31:47 EST Article-I.D.: rlgvax.352 Posted: Mon Jan 14 23:31:47 1985 Date-Received: Thu, 17-Jan-85 03:45:48 EST References: <6674@brl-tgr.ARPA> <6678@brl-tgr.ARPA> <22@qfdts.OZ> Organization: CCI Office Systems Group, Reston, VA Lines: 44 > VTIME and VMIN index the control character array in the termio structure > of USG systems. They appear to be potentially very usefull, but I have > been unable to locate any printed documentation on how to manipulate > c_cc[VTIME] and c_cc[VMIN] to achieve a non-blocking read. Can anyone > enlighten me please? The way to do non-blocking reads on terminals in USG systems is not to fiddle with VMIN and VTIME, but to set the O_NDELAY flag for the terminal's file descriptor using "fcntl". A way to do it is: if ((oldflags = fcntl(tty_fd, F_GETFL, 0)) < 0) fprintf(stderr, "Oops! [THIS SHOULD NOT HAPPEN]\n"); fcntl(tty_fd, F_SETFL, oldflags|O_NDELAY); ... fcntl(tty_fd, F_SETFL, oldflags); exit(0); Now, any reads on "tty_fd" will only return the number of characters immediately available, and not wait for any to become available. Note that if there are no characters available, the read will return 0, which is indistinguishable from end-of-file. Because of this, make sure you set the flags back to their old value before you exit; otherwise, the program that ran your program, probably a shell, will try to read something and, if you haven't typed anything, will get a return of 0, think it's an EOF, and dutifully exit. (4.2BSD handles this differently; you set no-delay mode on the terminal (i.e., *all* file descriptors referring to that terminal) with the FIONBIO "ioctl" (you can do it with "fcntl" as well, but unlike the USG "fcntl" it affects all file descriptors) and, if there is no data available, a read returns -1 with the error code EWOULDBLOCK. (In 4.2, this also affects writes; the write will only write as much data as will go without blocking.) The purpose of VMIN and VTIME is to act like the input silo on a DMF32. The read doesn't complete unless c_cc[VMIN] characters have come in, or c_cc[VTIME] 10ths of a second have elapsed *AND* at least one character has come in. (Completing the read if nothing came in is useless; the silos on the DH11 and DZ11 have no builtin timeouts, so they have to be polled under the control of the CPU clock even if they're empty, which sort of loses. Admittedly, in System V (but NOT System III) if c_cc[VMIN] is zero, the read completes after c_cc[VTIME] 10ths of a second even if no data is available. This sounds to me like rewriting to code to fit user's misconceptions of what the code was supposed to do...) Guy Harris {seismo,ihnp4,allegra}!rlgvax!guy