Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site hocda.UUCP Path: utzoo!watmath!clyde!burl!hou3c!hocda!54394gt From: 54394gt@hocda.UUCP (G.TOMASEVICH) Newsgroups: net.unix Subject: Non-blocking tty read Message-ID: <355@hocda.UUCP> Date: Mon, 6-Feb-84 15:42:39 EST Article-I.D.: hocda.355 Posted: Mon Feb 6 15:42:39 1984 Date-Received: Thu, 9-Feb-84 07:36:11 EST Organization: Bell Labs, Holmdel Lines: 40 Recently I posted an article to 'net.unix-wizards' on nonblocking terminal reads. We have USG 4.2 UNIX, which has 'struct termio'; if you have an oldter system with 'struct sgtty' you cannot do it. One changes to raw mode by saving the contents of the struct for line 0, then changes some constants. Assume you have the following code: #includestruct termio svt; /* for saving initial parameters */ struct termio stt; /* for changing line parameters */ ... ioctl(0,TCGETA,&svt); /* save old */ stt.c_iflag = stt.c_oflag = stt.c_lflag = 0; stt.c_cflag = (svt.c_cflag&CBAUD)|CS8|CREAD|CLOCAL; /* direct line */ stt.c_cc[VMIN] = 0; /* no chars needed for read() to return */ stt.c_cc[VTIME] = 0; /* timeout limit = 0 */ ioctl(0,TCSETA,&stt); To try to get a char, do the following: char key; if(read(0,&key,1) == 1) there is a char; else there is no char; If there is a character available, then the read() returns 1, so you can test the return value. I use it in conjuction with a DR11-W read/write program, which must poll the DR11-W status while waiting for characters to be typed. Of course, you hog the computer while running such a loop. If you want a blocking read, then set stt.c_cc[VMIN] = 1; I change back and forth, depending on whether anything besides keyboard input is happening at any particular instant. To avoid lousing up your line, catch all signals so you can do ioctl(0,TCSETA,&svt); and then an abort() if you want a core dump; before the process exits;