bolte@umn-cs.UUCP (01/26/86)
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"); } }