Timed reading in C (?)

Chris Torek chris at trantor.umd.edu
Wed Feb 10 01:30:32 AEST 1988


In article <2890 at dasys1.UUCP> pepper at 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 at mimsy.umd.edu		Path: not easily reachable



More information about the Comp.unix.questions mailing list