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 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 12 | int c, i; |
sewardj | 8d365b5 | 2002-05-12 10:52:16 +0000 | [diff] [blame] | 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 | **/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 31 | i = 0; |
| 32 | while (i++ < 50) { |
sewardj | 8d365b5 | 2002-05-12 10:52:16 +0000 | [diff] [blame] | 33 | c = getchar(); |
| 34 | printf("got %d\n", c); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | ** Now reset the old settings |
| 39 | **/ |
| 40 | res = ioctl(0, TCSETA, &oldtty); |
| 41 | printf("second ioctl returned %d\n", res); |
| 42 | |
| 43 | return 0; |
| 44 | } |