Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     fd_set rfds;
  8.     struct timeval tv;
  9.     int retval, len;
  10.     char buff[255] = {0};
  11.  
  12.     /* Watch stdin (fd 0) to see when it has input. */
  13.     FD_ZERO(&rfds);
  14.     FD_SET(0, &rfds);
  15.  
  16.     /* Wait up to five seconds. */
  17.     tv.tv_sec = 5;
  18.     tv.tv_usec = 0;
  19.  
  20.     retval = select(1, &rfds, NULL, NULL, &tv);
  21.  
  22.     if (retval == -1){
  23.         perror("select()");
  24.         exit(EXIT_FAILURE);
  25.     }
  26.     else if (retval){
  27.         /* FD_ISSET(0, &rfds) is true so input is available now. */
  28.  
  29.         /* Read data from stdin using fgets. */
  30.         fgets(buff, sizeof(buff), stdin);
  31.  
  32.         /* Remove trailing newline character from the input buffer if needed. */
  33.         len = strlen(buff) - 1;
  34.         if (buff[len] == '\n')
  35.             buff[len] = '\0';
  36.  
  37.         printf("'%s' was read from stdin.\n", buff);
  38.     }
  39.     else
  40.         printf("No data within five seconds.\n");            
  41.  
  42.     exit(EXIT_SUCCESS);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement