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.pol < prev    next >
Text File  |  1997-12-16  |  2KB  |  95 lines

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