uocis  CIS 432/532 Introduction to Computer Networks - Fall 2003


Turning off line-based input

These functions switch the terminal between the "raw" and "cooked" modes. By default, a terminal is in "cooked". In this mode, several things are performed for you automatically, such as line-buffering of user input and echoing each character the user types. In program #1, you will need to turn these off. The raw_mode() function does this for you.

These functions assume that you will call raw_mode() first. You should call cooked_mode() just before your program exits. You can set this up using the atexit(3) function, or just call it manually.

raw_mode() returns -1 if there is an error, but this will not typically happen.



#include <termios.h>
#include <unistd.h>
                                                                                
static struct termios oldterm;
 
/* Returns -1 on error, 0 on success */
int raw_mode (void)
{
        struct termios term;
 
        if (tcgetattr(STDIN_FILENO, &term) != 0) return -1;
     
        oldterm = term;
        term.c_lflag &= ~(ECHO);    /* Turn off echoing of typed charaters */
        term.c_lflag &= ~(ICANON);  /* Turn off line-based input */
        term.c_cc[VMIN] = 1;
        term.c_cc[VTIME] = 0;
        tcsetattr(STDIN_FILENO, TCSADRAIN, &term);
 
        return 0;
}
                                                                                
void cooked_mode (void)
{
        tcsetattr(STDIN_FILENO, TCSANOW, &oldterm);
}


Created by: Daniel Stutzbach December 15, 2003