Back to index

glibc  2.9
termios.c
Go to the documentation of this file.
00001 #include <unistd.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <termios.h>
00005 
00006 /* Use this variable to remember original terminal attributes. */
00007 
00008 struct termios saved_attributes;
00009 
00010 void 
00011 reset_input_mode (void)
00012 {
00013   tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes);
00014 }
00015 
00016 void 
00017 set_input_mode (void)
00018 {
00019   struct termios tattr;
00020   char *name;
00021 
00022   /* Make sure stdin is a terminal. */
00023   if (!isatty (STDIN_FILENO))
00024     {
00025       fprintf (stderr, "Not a terminal.\n");
00026       exit (EXIT_FAILURE);
00027     }
00028 
00029   /* Save the terminal attributes so we can restore them later. */
00030   tcgetattr (STDIN_FILENO, &saved_attributes);
00031   atexit (reset_input_mode);
00032 
00033 /*@group*/
00034   /* Set the funny terminal modes. */
00035   tcgetattr (STDIN_FILENO, &tattr);
00036   tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO.  */
00037   tattr.c_cc[VMIN] = 1;
00038   tattr.c_cc[VTIME] = 0;
00039   tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
00040 }
00041 /*@end group*/
00042 
00043 int
00044 main (void)
00045 {
00046   char c;
00047 
00048   set_input_mode ();
00049 
00050   while (1)
00051     {
00052       read (STDIN_FILENO, &c, 1);
00053       if (c == '\004')             /* @kbd{C-d} */
00054        break;
00055       else
00056        putchar (c);
00057     }
00058 
00059   return EXIT_SUCCESS;
00060 }