Altering Terminal Characteristics
/***************************  Terminal Control Demo  **********************/
#include <termios.h>
#include <unistd.h>
#include <stdio.h>   /******  Controlling Control Characters!  ******/
main()
{
  struct termios term;
  int status;
  long vdisable;
  char s[80];

  tcgetattr(STDIN_FILENO, &term);

  /*  Print out current octal value of ctrl characters. */
  printf("EOF = %o, EOL = %o, ERASE = %o, INTR = %o, KILL = %o, QUIT = %o\n",
         term.c_cc[VEOF], term.c_cc[VEOL], term.c_cc[VERASE], term.c_cc[VINTR],
         term.c_cc[VKILL], term.c_cc[VQUIT]);

  printf("START = %o, STOP = %o, SUSP = %o\n", term.c_cc[VSTART],
          term.c_cc[VSTOP], term.c_cc[VSUSP]);

  vdisable = fpathconf(STDIN_FILENO, _PC_VDISABLE);

  term.c_cc[VERASE] = vdisable;
  term.c_cc[VINTR]  = vdisable;
  term.c_cc[VEOF]   = '\002';
  tcsetattr(STDIN_FILENO, TCSADRAIN, &term); 

  while ( gets(s)) puts(s);  

  term.c_cc[VERASE] = '\177';   /*  Reset disabled control chars */
  term.c_cc[VEOF]   = '\004';
  term.c_cc[VINTR]  = '\003';
  tcsetattr(STDIN_FILENO, TCSADRAIN, &term); 

  printf("I am here now.\n");
  pause();
}
/*******************************  Output Below  ****************************/

csh> a.out
EOF = 4, EOL = 377, ERASE = 177, INTR = 3, KILL = 25, QUIT = 34
START = 21, STOP = 23, SUSP = 32
stuff
stuff
junk^?^?^C        /*  Erases (^?) and interrupts (^C) are disabled. */
junk
^BI am here now.  /*  Control B acts as end-of-file. */
^Ccsh>            /*  Control-C restored to former capability. */
/*********************  Controlling Echo and Caps Lock  ********************/
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
main()
{
  struct termios term;
  char s[80];

  tcgetattr(STDOUT_FILENO, &term);
  term.c_oflag |= OLCUC;
  tcsetattr(STDOUT_FILENO, TCSANOW, &term);

  while (gets(s)) puts(s);     /* Chars typed all appear uppercase! */

  term.c_oflag &= ~OLCUC;
  tcsetattr(STDOUT_FILENO, TCSANOW, &term);

  while (gets(s)) puts(s);     /*  Chars are whatever case typed. */

  term.c_lflag &= ~ECHO;
  tcsetattr(STDOUT_FILENO, TCSANOW, &term);
  gets(s);

  term.c_lflag |= ECHO;
  tcsetattr(STDOUT_FILENO, TCSANOW, &term);
  printf("You entered %s\n", s);
}
/*********************************  Output Below  *************************/
csh> a.out
ABCDEFG             /*  Caps lock key NOT on when these were typed. */
ABCDEFG             /*  Puts echoes chars. */
^D                  /*  Ends first while loop. */

abcdefg             /*  Mapping to uppercase turned off. */
abcdefg
^DYou entered abcdefg  /*  After control-D abcdefg typed but does not appear!*/
/*********************  Flow Control and Buffer Flushing  *******************/
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
main()
{
  struct termios term, termsave;
  char s[80];
  FILE *fp;

  tcgetattr(STDIN_FILENO, &term);
  termsave = term;

  termsave.c_cc[VSTOP] = '\023';     /*  Save ^S */
  termsave.c_cc[VSTART]= '\021';     /*  Save ^Q */
  termsave.c_iflag |= IXON | IXOFF;

  term.c_cc[VSTOP] = '\030';         /*  Make ^X the stop char  */
  term.c_cc[VSTART]= '\002';         /*  Make ^B the start char */
  term.c_iflag |= IXON | IXOFF;      /*  Turn ^X, ^B back on    */
  tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);

  fp = fopen("signal1.demo","r");
  while (fgets(s, 80, fp))puts(s);
  rewind(fp);

  term.c_iflag &= ~(IXON | IXOFF);   /*  Turn off stop/start    */ 
  tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);

  tcsetattr(STDIN_FILENO, TCSAFLUSH, &termsave);


/***
 ***  Creating a password system.
 ***/
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
main()
{
  struct termios term;
  char s[80], passwd[] = "foobar", userpass[9], *mover;
  int c;

  printf("Enter password: ");
  tcgetattr(STDOUT_FILENO, &term);

  term.c_lflag &= ~(ECHO | ICANON);
  tcsetattr(STDOUT_FILENO, TCSANOW, &term);
  
  mover = userpass;
  while ((c = getchar()) != '\n') 
  {
     *mover++ = c;
     putchar('*');
  }
  *mover = '\0';
  putchar('\n');

  term.c_lflag |= (ECHO | ICANON);;
  tcsetattr(STDOUT_FILENO, TCSANOW, &term);
  printf("You entered %s\n", userpass);
  printf("I will %slet you in!!\n\n",
               strcmp(userpass, passwd) ? "NOT " : "");
}
/******************************  Sample Executions  **************************/

$ term
Enter password: ***
You entered foo
I will NOT let you in!!

$ term
Enter password: ******
You entered foobar
I will let you in!!

$ term
Enter password:   /***  I hit a terminal interrupt, which I can disable. ***/