This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Problem with select() on console


When select() is used to test for input availability on the standard
cygwin console in normal (cooked) mode, it indicates input is available
as soon as any key is pressed.  However, a call to read(0,...)
will (correctly) block until a terminating RETURN is entered.

select() should only indicate input is available when a call
to read would *not* block - ie when a read call will immediately
return at least one character or an error such as EOF.

The behaviour of the following test case illustrates this.  When run
in a console window typing a single key causes the program to wait
for the whole line.  When run under mintty or on Linux the
select() calls will continue to return no input until RETURN is
entered.

#include <stdio.h>
#include <stdlib.h>
#include <sys/io.h>
#include <sys/time.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>

int main(void)
{
  fd_set rs;
  struct timeval tv;
  char buff[100];
  while (1)
  {
    sleep(1);
    printf("Calling select\n");
    FD_ZERO(&rs);
    FD_SET(0, &rs);
    tv.tv_sec = tv.tv_usec = 0;
    int k = select(1, &rs, NULL, NULL, &tv);
    if (k < 0)
      perror("Error calling select");
    else if (FD_ISSET(0, &rs))
    {
      printf("Input available\n");
      int n = read(0, buff, sizeof(buff));
      printf("read returned %d\n", n);
    }
  }
  return 0;
}

-- Cliff

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]