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 |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4 | blocking. |
| 5 | |
| 6 | [Nb: no longer true, since the ioctl-VTIME weird hack no longer exists] |
| 7 | */ |
sewardj | 8d365b5 | 2002-05-12 10:52:16 +0000 | [diff] [blame] | 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <sys/ioctl.h> |
| 11 | #include <termio.h> |
| 12 | |
| 13 | int main ( void ) |
| 14 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 15 | int c, i; |
sewardj | 8d365b5 | 2002-05-12 10:52:16 +0000 | [diff] [blame] | 16 | int res; |
| 17 | struct termio tty, oldtty; |
| 18 | |
| 19 | /** |
| 20 | ** Save the old tty settings, and get rid of echo |
| 21 | ** for the new tty settings |
| 22 | **/ |
| 23 | ioctl(0, TCGETA, &oldtty); |
| 24 | tty = oldtty; |
| 25 | tty.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
| 26 | tty.c_cc[VMIN] = 0; |
| 27 | tty.c_cc[VTIME] = 5; |
| 28 | res = ioctl(0, TCSETA, &tty); |
| 29 | printf("first ioctl returned %d\n", res); |
| 30 | |
| 31 | /** |
| 32 | ** Now do whatever stuff you want non-echoed |
| 33 | **/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 34 | i = 0; |
| 35 | while (i++ < 50) { |
sewardj | 8d365b5 | 2002-05-12 10:52:16 +0000 | [diff] [blame] | 36 | c = getchar(); |
| 37 | printf("got %d\n", c); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | ** Now reset the old settings |
| 42 | **/ |
| 43 | res = ioctl(0, TCSETA, &oldtty); |
| 44 | printf("second ioctl returned %d\n", res); |
| 45 | |
| 46 | return 0; |
| 47 | } |