1 // kompilacia: gcc -o priklad5 priklad5.c 2 3 #include <errno.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <unistd.h> 7 #include <sys/types.h> 8 #include <sys/time.h> 9 10 int wait_for_input_2s () { 11 fd_set set; 12 struct timeval timeout; 13 14 FD_ZERO(&set); 15 FD_SET(0, &set); // 0 - stdin 16 17 timeout.tv_sec = 2; 18 timeout.tv_usec = 0; 19 20 return select(FD_SETSIZE, &set, NULL, NULL, &timeout); 21 } 22 23 int main (int argc, char ** argv) { 24 while (1) { 25 printf("napis nieco a stlac ENTER: "); 26 fflush(stdout); 27 if (wait_for_input_2s() > 0) { 28 char * str; 29 scanf("%as", &str); 30 printf("\nvyborne, vies aj pisat (%s)\n\n", str); 31 break; 32 } else { 33 printf("\nnic si nenapisal, este raz,\n"); 34 } 35 } 36 37 exit(EXIT_SUCCESS); 38 }