home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / manual / examples / select.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-19  |  642 b   |  30 lines

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/time.h>
  5.  
  6. int 
  7. 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);
  14.   FD_SET (filedes, &set);
  15.  
  16.   /* Initialize the timeout data structure. */
  17.   timeout.tv_sec = seconds;
  18.   timeout.tv_usec = 0;
  19.  
  20.   /* @code{select} returns 0 if timeout, 1 if input available, -1 if error. */
  21.   return TEMP_FAILURE_RETRY (select (FD_SETSIZE, &set, NULL, NULL, &timeout));
  22. }
  23.  
  24. int
  25. main (void)
  26. {
  27.   fprintf (stderr, "select returned %d.\n", input_timeout (STDIN_FILENO, 5));
  28.   return 0;
  29. }
  30.