/*#* noncanonical.c Usage: see below. */ #include #include #include #include #include #include #include #include #include #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \ } while (0) int main(int argc, char *argv[]) { char buf[100000]; struct termios tp; ssize_t numRead; int sleepTime; if (argc < 4) { fprintf(stderr, "Usage: %s TIME MIN num-bytes-to-read " "[sleep-secs [O_NONBLOCK flag]]\n", argv[0]); exit(EXIT_FAILURE); } if (tcgetattr(STDIN_FILENO, &tp) == -1) errExit("tcgetattr"); tp.c_lflag &= ~ICANON; tp.c_cc[VTIME] = atoi(argv[1]); tp.c_cc[VMIN] = atoi(argv[2]); if (tcsetattr(STDIN_FILENO, TCSANOW, &tp) == -1) errExit("tcsetattr"); if (argc > 5) { int flags; flags = fcntl(STDIN_FILENO, F_GETFL); if (flags == -1) errExit("fcntl"); flags |= O_NONBLOCK; if (fcntl(STDIN_FILENO, F_SETFL, flags) == -1) errExit("fcntl"); } sleepTime = (argc > 4) ? atoi(argv[4]) : 5; printf("About to sleep for %d seconds\n", sleepTime); sleep(sleepTime); printf("Done sleeping\n"); printf("About to read %d bytes\n", atoi(argv[3])); numRead = read(STDIN_FILENO, buf, atoi(argv[3])); if (numRead == -1) errExit("read"); printf("read returned %ld\n", (long) numRead); exit(EXIT_SUCCESS); } /* main */