Path: utzoo!mnetor!uunet!husc6!mit-eddie!ll-xn!ames!umd5!trantor.umd.edu!chris From: chris@trantor.umd.edu (Chris Torek) Newsgroups: comp.unix.questions Subject: Re: Timed reading in C (?) Message-ID: <2266@umd5.umd.edu> Date: 9 Feb 88 15:30:32 GMT References: <2890@dasys1.UUCP> Sender: ris@umd5.umd.edu Reply-To: chris@trantor.umd.edu (Chris Torek) Organization: University of Maryland, College Park Lines: 24 In article <2890@dasys1.UUCP> pepper@dasys1.UUCP (Angelique Wahlstedt) writes: >What I want my program to do is wait a short while (like, say, 5 seconds >or so) for an user to type in something. If the user doesn't type in >anything after that time period, the program goes ahead and does something >else. But if the user has already started to type, the program waits and >lets him finish typing. > >What I'd like to know is how to do this in C. (By the way, I'm on BSD >Unix 4.3.) I want the user's terminal to stay in the canonical mode, >if possible. You will have to use CBREAK mode. Once you have given in that far, the rest is easy; use select() or alarm()+longjmp() (the former preferred). If the typist is slow enough and/or the system fast enough, you could get just the first character, then take the terminal out of CBREAK and use TIOCSTI to put that character back in the input queue; but if the user manages to type something else, the character put with TIOCSTI will insert after that something else. (UCBMail does this when editing header lines; I often find myself with a partial Cc: header because of this.) In-Real-Life: Chris Torek, Univ of MD Computer Science, +1 301 454 7163 (hiding out on trantor.umd.edu until mimsy is reassembled in its new home) Domain: chris@mimsy.umd.edu Path: not easily reachable