Here is an example to get started:
1 2 int main(int argc, char * argv[]) { 3 fd_set rfds; 4 struct timeval tv; 5 int seconds = 5; 6 int retval; 7 const int MAXBUF = 120; 8 char buf[MAXBUF]; 9 char magic[] = "select"; 10 11 12 /* Get timeout seconds */ 13 if ( argc > 1 ) { 14 seconds = atoi(argv[1]); 15 if ( seconds == 0 ) { 16 seconds = 5; 17 } 18 } 19 /* Watch stdin (fd 0) to see when it has input. */ 20 FD_ZERO(&rfds); 21 FD_SET(0, &rfds); 22 23 /* Wait up to ? seconds. */ 24 tv.tv_sec = seconds; 25 tv.tv_usec = 0; 26 27 setbuf(stdout, NULL); 28 printf("You have %d seconds to enter the magic word.\n", seconds); 29 printf("> "); 30 retval = select(1, &rfds, NULL, NULL, &tv); 31 /* Donb 32 t rely on the value of tv now! */ 33 34 35 if (retval == -1) 36 perror("select()"); 37 else if (retval) { 38 fgets(buf, MAXBUF, stdin); 39 buf[strlen(buf) - 1] = 0; 40 if (!strcmp(buf, magic)) { 41 printf("Correct!\n"); 42 } else { 43 printf("Sorry, '%s' is not the magic word.\n", buf); 44 } 45 } 46 47 /* FD_ISSET(0, &rfds) will be true. */ 48 else { 49 printf("\nSorry, %d seconds expired with no guess entered.\n", 50 seconds); 51 } 52 53 return 0; 54 }