Need Non-Blocking Terminal Input Fun
bolte at umn-cs.UUCP
bolte at umn-cs.UUCP
Sun Jan 26 13:38:00 AEST 1986
There is a very simple way to do what you want. The select (2) call
can be used to wait for input from a file descriptor or return after
a certain amount of time.
Here is a complete program to do just that. Note: it is for BSD 4.2
Scott Bolte
U. of Minnesota A.I. Lab
ihnp4!umn-cs!bolte
================================================================================
#include <sys/time.h>
#include <stdio.h>
#include <fcntl.h>
main(argc,argv)
char *argv[];
{
int ifd,flag;
struct timeval tim;
tim.tv_sec = (argc>1)?atoi(argv[1]):3; /* wait for argv[1] seconds */
/* default is 9 */
tim.tv_usec = 0;
ifd = 1<<0; /* Since stdin is file descriptor 0 only shift
by zero. To wait for file descriptor x as
well add the line 'ifd |= 1<<x' */
printf("You have %d seconds to type something\n",tim.tv_sec);
fflush(stdin); /* clear all previous, unprocessed input */
fflush(stdout); /* insure you get message before timer starts */
if( select(2,&ifd,0,0,&tim) == 0){
printf("Nothing was typed before time ran out.\n");
} else {
while( getchar() != '\n'); /* clear what was typed */
printf("Something was typed.\n");
}
}
More information about the Comp.unix.wizards
mailing list