Update.
[glibc/history.git] / manual / examples / termios.c
blob6db5990a0cc34c455e5196162864e87097095798
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <termios.h>
6 /* Use this variable to remember original terminal attributes. */
8 struct termios saved_attributes;
10 void
11 reset_input_mode (void)
13 tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes);
16 void
17 set_input_mode (void)
19 struct termios tattr;
20 char *name;
22 /* Make sure stdin is a terminal. */
23 if (!isatty (STDIN_FILENO))
25 fprintf (stderr, "Not a terminal.\n");
26 exit (EXIT_FAILURE);
29 /* Save the terminal attributes so we can restore them later. */
30 tcgetattr (STDIN_FILENO, &saved_attributes);
31 atexit (reset_input_mode);
33 /*@group*/
34 /* Set the funny terminal modes. */
35 tcgetattr (STDIN_FILENO, &tattr);
36 tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
37 tattr.c_cc[VMIN] = 1;
38 tattr.c_cc[VTIME] = 0;
39 tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
41 /*@end group*/
43 int
44 main (void)
46 char c;
48 set_input_mode ();
50 while (1)
52 read (STDIN_FILENO, &c, 1);
53 if (c == '\004') /* @kbd{C-d} */
54 break;
55 else
56 putchar (c);
59 return EXIT_SUCCESS;