//------------------------------------------------------------------- // noecho.cpp // // This example program is written in a high-level language in // order to more clearly illustrate an important idea in Linux // "systems programming" (which will be needed for Project 2). // // Adapted from: Michael K. Johnson and Erik W. Troan, "Linux // Application Development," Addison-Wesley (1998), 267-274. // // programmer: ALLAN CRUSE // written on: 11 SEP 2003 //------------------------------------------------------------------- #include // for printf(), perror() #include // for open() #include // for exit() #include // for read(), write(), close() #include // for tcgetarrt(), tcsetattr() #define MAX_INPUT 40 int main( int argc, char **argv ) { //---------- // prologue //---------- // preserve tty settings struct termios orig_termios; tcgetattr( STDIN_FILENO, &orig_termios ); // reconfigure the tty struct termios temp_termios = orig_termios; temp_termios.c_lflag &= ~ECHO; temp_termios.c_lflag |= ECHONL; tcsetattr( STDIN_FILENO, TCSANOW, &temp_termios ); //------------- // main action //------------- // obtain_input char prompt[] = "\n\tPlease type in your name: "; write( STDOUT_FILENO, prompt, sizeof( prompt ) ); char buffer[ MAX_INPUT ] = ""; read( STDIN_FILENO, buffer, MAX_INPUT ); // process_data if (( buffer[0] >= 'a' )&&( buffer[0] <= 'z' )) buffer[0] -= 32; // print_output printf( "\n\tThank you, %s\n", buffer ); //---------- // epilogue //---------- // restore original tty settings tcsetattr( STDIN_FILENO, TCSANOW, &orig_termios ); }