Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!husc6!mit-eddie!gatech!udel!rochester!pt!ius2.cs.cmu.edu!edw From: edw@ius2.cs.cmu.edu (Eddie Wyatt) Newsgroups: comp.unix.wizards Subject: Re: reading from terminal Message-ID: <1189@ius2.cs.cmu.edu> Date: Mon, 8-Jun-87 23:21:11 EDT Article-I.D.: ius2.1189 Posted: Mon Jun 8 23:21:11 1987 Date-Received: Sat, 20-Jun-87 09:00:14 EDT References: <7749@brl-adm.ARPA> Organization: Carnegie-Mellon University, CS/RI Lines: 71 In article <7749@brl-adm.ARPA>, jfjr@mitre-bedford.arpa (Freedman) writes: > > This may be a trivial question to the wizards but I am a unix > novice (~3 months). I need to read a character from the terminal > without stopping a program. I have a program thats going to > do a lot of interesting things you wouldn't be interested in. > I want to give the user the ability to halt the processing > at any time - poke around the program and then resume processing. > While the program is doing its thing all sorts of terminal and > file i/o are happening. I have done this successfully and simply > on VMS using qio with the proper flag but I am having problems > doing it on Unix. I would rather not have to do this trick by > using a second process nor would I want to usurp Control-C. > > I really must say that Unix documentation compared to > VMS doesn't measure up( or maybe I am looking in the wrong > places if so enlighten me) > > Thirster after knowledge > > Jerry Freedman, Jr "If at first you don't succeed, lower your standards" > jfjr@mitre-bedford.arpa > (617)271-6248 or 8658 The functions you want to look into are ioctl (i/o control) or stty (set tty). Do a "man 4 tty" to find what all the options to ioctl are. To do what you want to do : enable_quick_input() { struct sgttyb mode; int fd = fileno(stdin); gtty(fd, &mode); /* get old mode */ mode.sg_flags &= ~RAW; /* process special characters like ^H */ mode.sg_flags &= ECHO; /* make sure echo is on */ mode.sg_flags &= CBREAK; /* make characters available to process as soon as they are typed (this may disable cooked mode). */ stty(fd, &mode); /* set mode */ } I believe that will do it. To poll for input you can use select if you want. But that's a little slow so use ioctl(). int ready_to_read(fd) int fd; /* a file desciptor, not a FILE pointer here */ { int num; ioctl(fd,FIONREAD,(char *)&num); return(num > 0); } Which brings up a question. Does anyone know of a quicker way of determining if any characters are on a port other than using ioctl (select is slower, at least by what I have measured)? -- Eddie Wyatt e-mail: edw@ius2.cs.cmu.edu