#include #include #include #include #include #include #include int main(int ac, char **av) { if (ac != 2) return 1; int time; time = atoi(av[1]); if (setvbuf(stdin, NULL, _IONBF, 0) != 0) { err(1, "setvbuf"); } struct termios o, n; tcgetattr(0, &o); tcgetattr(0, &n); cfmakeraw(&n); tcsetattr(0, TCSADRAIN, &n); while (1) { while (1) { fd_set reads, errors; FD_ZERO(&reads); FD_ZERO(&errors); FD_SET(0, &reads); FD_SET(0, &errors); struct timeval tv; tv.tv_sec = time / 1000000; tv.tv_usec = time % 1000000; printf("select: "); int rv = select(1, &reads, NULL, &errors, &tv); printf("rv = %d, errno = %s\r\n", rv, strerror(errno)); if (rv == 0) { if (FD_ISSET(0, &reads)) printf ("unexpected read\r\n"); if (FD_ISSET(0, &errors)) printf ("unexpected error\r\n"); if (FD_ISSET(0, &reads) || FD_ISSET(0, &errors)) break; // otherwise continue } else if (rv == 1) { if (FD_ISSET(0, &reads)) { // select found something normally, go read it break; } else if (FD_ISSET(0, &errors)) { printf("error on stdin\r\n"); break; } else { printf("no ready fd found\r\n"); break; } } else if (rv > 1) { printf("unexpected count = %d\r\n", rv); break; } else { printf("rv %d, error %s\r\n", rv, strerror(errno)); break; } } printf("reading char: "); char c = getc(stdin); if (c == 0x7f) break; printf("got 0x%02x\r\n", c); } tcsetattr(0, TCSADRAIN, &o); return 0; }