From 97169cef9776dd0b9adfd3f6d5743ccf0ab38736 Mon Sep 17 00:00:00 2001 From: Sude Date: Fri, 26 Aug 2016 23:24:04 +0300 Subject: [PATCH] Get rid of getpass Use termios to control terminal ECHO and read password with std::getline instead of using obsolete getpass() --- src/downloader.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/downloader.cpp b/src/downloader.cpp index bbb05c2..7d2ee1c 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -25,6 +25,7 @@ #include #include #include +#include namespace bptime = boost::posix_time; @@ -130,7 +131,6 @@ int Downloader::init() */ int Downloader::login() { - char *pwd; std::string email; if (!isatty(STDIN_FILENO)) { std::cerr << "Unable to read email and password" << std::endl; @@ -138,8 +138,18 @@ int Downloader::login() } std::cerr << "Email: "; std::getline(std::cin,email); - pwd = getpass("Password: "); - std::string password = (std::string)pwd; + + std::string password; + std::cerr << "Password: "; + struct termios termios_old, termios_new; + tcgetattr(STDIN_FILENO, &termios_old); // Get current terminal attributes + termios_new = termios_old; + termios_new.c_lflag &= ~ECHO; // Set ECHO off + tcsetattr(STDIN_FILENO, TCSANOW, &termios_new); // Set terminal attributes + std::getline(std::cin, password); + tcsetattr(STDIN_FILENO, TCSANOW, &termios_old); // Restore old terminal attributes + std::cerr << std::endl; + if (email.empty() || password.empty()) { std::cerr << "Email and/or password empty" << std::endl;