#include #include #include #include #include int peek (int fd, int msec) { fd_set readfds; fd_set exceptfds; struct timeval timeoutstru; FD_ZERO (& readfds); FD_SET (fd, & readfds); FD_ZERO (& exceptfds); FD_SET (fd, & exceptfds); timeoutstru.tv_sec = msec / 1000; timeoutstru.tv_usec = (msec % 1000) * 1000; errno = 0; printf ("calling select\n"); int nfds = select (fd + 1, & readfds, 0, & exceptfds, & timeoutstru); printf ("select -> %d (%s), read %02X except %02X\n", nfds, strerror (errno), readfds, exceptfds); return nfds; } void catch_HUP (int hup) { printf ("HUP\n"); signal (SIGHUP, catch_HUP); } int main () { int fdstdin = 0; system ("stty cbreak"); signal (SIGHUP, catch_HUP); while (1) { char buf; int buflen = 1; int nfds = peek (fdstdin, 1500); if (nfds > 0) { printf ("calling read\n"); errno = 0; int n = read (fdstdin, & buf, buflen); if (n <= 0) { printf ("read -> %d (%s); exit\n", n, strerror (errno)); exit (0); } printf ("read -> %d: %c\n", n, buf); } sleep (2); } }