Disable terminal echo in Linux

Very short code snippet to disable/enable the terminal echo in Linux. If, as example, in your tool you need user to insert a password and don't want show it in the typing terminal you simply can disable echo. Once echo disabled your tool will receive keyboard input but the letters will not be showed on the screen.



Here a ready made function allow to make enable/disable operation of echo feature:

void EchoEnable(int EchoOn)
{
    struct termios TermConf;

    tcgetattr(STDIN_FILENO, &TermConf);

    if(EchoOn)
       TermConf.c_lflag |= (ICANON | ECHO);
    else
       TermConf.c_lflag &= ~(ICANON | ECHO);

    tcsetattr(STDIN_FILENO, TCSANOW, &TermConf);
}

Simply copy and paste in your code for immediate use. EchoOn equal to zero disable echo, positive value enable echo.

Comments

Popular posts from this blog

Access GPIO from Linux user space

Android: adb push and read-only file system error

Tree in SQL database: The Nested Set Model