Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable local echo display with input passwords in linux #33

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions CPP/7zip/UI/Console/UserInputUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,18 @@ NUserAnswerMode::EEnum ScanUserYesNoAllQuit(CStdOutStream *outStream)
#ifdef _WIN32
#ifndef UNDER_CE
#define MY_DISABLE_ECHO
#define MY_DISABLE_ECHO_WIN32
#endif
#endif

#ifdef unix
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#define MY_DISABLE_ECHO
#define MY_DISABLE_ECHO_UNIX
#endif

static bool GetPassword(CStdOutStream *outStream, UString &psw)
{
if (outStream)
Expand All @@ -72,7 +81,7 @@ static bool GetPassword(CStdOutStream *outStream, UString &psw)
outStream->Flush();
}

#ifdef MY_DISABLE_ECHO
#ifdef MY_DISABLE_ECHO_WIN32

const HANDLE console = GetStdHandle(STD_INPUT_HANDLE);

Expand All @@ -90,7 +99,29 @@ static bool GetPassword(CStdOutStream *outStream, UString &psw)
const bool res = g_StdIn.ScanUStringUntilNewLine(psw);
if (wasChanged)
SetConsoleMode(console, mode);


#elif defined(MY_DISABLE_ECHO_UNIX)

int ifd = fileno(stdin);
bool wasChanged = false;
struct termios old_mode = {};
struct termios new_mode = {};

if (tcgetattr(ifd, &old_mode) == 0) {
new_mode = old_mode;
new_mode.c_lflag &= ~ECHO;

wasChanged = true;

tcsetattr(ifd, TCSAFLUSH, &new_mode);
}

bool res = g_StdIn.ScanUStringUntilNewLine(psw);

if (wasChanged) {
tcsetattr(ifd, TCSAFLUSH, &old_mode);
}

#else

const bool res = g_StdIn.ScanUStringUntilNewLine(psw);
Expand Down