sewardj | 8d365b5 | 2002-05-12 10:52:16 +0000 | [diff] [blame^] | 1 | |
| 2 | /* A program which sets a readable fd to have a timeout, and therefore |
| 3 | needs --weird-hacks=ioctl-VTIME in order to run without |
| 4 | blocking. */ |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include <sys/ioctl.h> |
| 8 | #include <termio.h> |
| 9 | |
| 10 | int main ( void ) |
| 11 | { |
| 12 | int c; |
| 13 | int res; |
| 14 | struct termio tty, oldtty; |
| 15 | |
| 16 | /** |
| 17 | ** Save the old tty settings, and get rid of echo |
| 18 | ** for the new tty settings |
| 19 | **/ |
| 20 | ioctl(0, TCGETA, &oldtty); |
| 21 | tty = oldtty; |
| 22 | tty.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
| 23 | tty.c_cc[VMIN] = 0; |
| 24 | tty.c_cc[VTIME] = 5; |
| 25 | res = ioctl(0, TCSETA, &tty); |
| 26 | printf("first ioctl returned %d\n", res); |
| 27 | |
| 28 | /** |
| 29 | ** Now do whatever stuff you want non-echoed |
| 30 | **/ |
| 31 | while (1) { |
| 32 | c = getchar(); |
| 33 | printf("got %d\n", c); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | ** Now reset the old settings |
| 38 | **/ |
| 39 | res = ioctl(0, TCSETA, &oldtty); |
| 40 | printf("second ioctl returned %d\n", res); |
| 41 | |
| 42 | return 0; |
| 43 | } |