home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / pine4.10.tar.gz / pine4.10.tar / pine4.10 / pico / osdep / read.sel < prev    next >
Text File  |  1998-04-01  |  2KB  |  90 lines

  1. /*
  2.  *  Check whether or not a character is ready to be read, or time out.
  3.  *  This version uses a select call.
  4.  *
  5.  *   Args: time_out -- number of seconds before it will time out.
  6.  *
  7.  * Result: NO_OP_IDLE:      timed out before any chars ready, time_out > 25
  8.  *         NO_OP_COMMAND: timed out before any chars ready, time_out <= 25
  9.  *         READ_INTR:      read was interrupted
  10.  *         READY_TO_READ: input is available
  11.  *         BAIL_OUT:      reading input failed, time to bail gracefully
  12.  *         PANIC_NOW:      system call error, die now
  13.  */
  14. int
  15. input_ready(time_out)
  16.      int time_out;
  17. {
  18.      struct timeval tmo;
  19.      fd_set         readfds, errfds;
  20.      int        res;
  21.  
  22.      fflush(stdout);
  23.  
  24.      if(time_out > 0){
  25.          /* Check to see if there are bytes to read with a timeout */
  26.      FD_ZERO(&readfds);
  27.      FD_ZERO(&errfds);
  28.      FD_SET(STDIN_FD, &readfds);
  29.      FD_SET(STDIN_FD, &errfds);
  30.      tmo.tv_sec  = time_out;
  31.          tmo.tv_usec = 0; 
  32.          res = select(STDIN_FD+1, &readfds, 0, &errfds, &tmo);
  33.          if(res < 0){
  34.              if(errno == EINTR || errno == EAGAIN)
  35.                return(READ_INTR);
  36.  
  37.          return(BAIL_OUT);
  38.          }
  39.  
  40.          if(res == 0){ /* the select timed out */
  41.          if(getppid() == 1){
  42.          /* Parent is init! */
  43.              return(BAIL_OUT);
  44.          }
  45.            
  46.          /*
  47.           * "15" is the minimum allowed mail check interval.
  48.           * Anything less, and we're being told to cycle thru
  49.           * the command loop because some task is pending...
  50.           */
  51.              return(time_out < 15 ? NO_OP_COMMAND : NO_OP_IDLE);
  52.          }
  53.      }
  54.  
  55.      return(READY_TO_READ);
  56. }
  57.  
  58.  
  59. /*
  60.  * Read one character from STDIN.
  61.  *
  62.  * Result:           -- the single character read
  63.  *         READ_INTR -- read was interrupted
  64.  *         BAIL_OUT  -- read error of some sort
  65.  */
  66. int
  67. read_one_char()
  68. {
  69.      int        res;
  70.      unsigned char  c;
  71.  
  72.      res = read(STDIN_FD, &c, 1);
  73.  
  74.      if(res <= 0){
  75.      /*
  76.       * Error reading from terminal!
  77.       * The only acceptable failure is being interrupted.  If so,
  78.       * return a value indicating such...
  79.       */
  80.      if(res < 0 && errno == EINTR)
  81.        return(READ_INTR);
  82.      else
  83.        return(BAIL_OUT);
  84.      }
  85.  
  86.      return((int)c);
  87. }
  88.  
  89.  
  90.