Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/time.h>
  6.  
  7. int input_timeout (int filedes, unsigned int seconds)
  8.     {
  9.     fd_set set;
  10.     struct timeval timeout;
  11.  
  12.     // Initialize the file descriptor set.
  13.     FD_ZERO (&set);                  // clears a file descriptor set
  14.     FD_SET (filedes, &set);          // adds fd to the set
  15.     // FD_CLR(int fd, fd_set *set)   // removes fd from the set
  16.     // FD_ISSET(int fd, fd_set *set) // tests to see if fd is in the set
  17.  
  18.     // Initialize the timeout data structure.
  19.     timeout.tv_sec = seconds;  // seconds
  20.     timeout.tv_usec = 0;       // microseconds
  21.  
  22.     // select returns 0 if timeout, 1 if input available, -1 if error.
  23.     return TEMP_FAILURE_RETRY (select (FD_SETSIZE,
  24.                                        &set, NULL, NULL,
  25.                                        &timeout));
  26.     }
  27.  
  28. int main (void)
  29.     {
  30.     fprintf (stderr, "select returned %d.\n",
  31.              input_timeout (STDIN_FILENO, 5));
  32.     return 0;
  33.     }