I need to be able to block waiting for input from several fd's in select,
then if I get input on stdin, read from it.  On all platforms I've tried
except Cygwthe following works:

#include <stdio.h>
#include <sys/select.h>
#include <io.h>
#include <fcntl.h>
#include <errno.h>

main()
{
    int i, n;
    char buf[256];
    fd_set rs;

    printf("calling select\n");

    FD_ZERO(&rs);
    FD_SET(fileno(stdin), &rs);
    while (0 == (i = select(fileno(stdin)+1, &rs, NULL, NULL, NULL)))
        printf("select returns zero\n");

    printf("select returns non-zero... caling read\n");

    n = read(fileno(stdin), buf, sizeof(buf));
    printf("read returns %d\n", n);

    for (i = 0; i < n; i++)
        printf("0x%02lx %c\n", buf[i], buf[i]);

}

You run the program, type some chars and hit carriage return.  The select
then returns 1 (indicating 1 fd is satisfied); the read runs without
blocking, and it spits out
the characters you  typed.  Just what I need.

On Cygwin, the carrriage return causes the select to return 0.  A *second*
carriage return causes the select to return 1, and then the read works.

If you don't go back into select waiting for a 1, then the read blocks.

Can anyone tell me how to get the desired (eg. non-Cygwin) behavior?

Greg


Reply via email to