diff options
Diffstat (limited to 'src/mongo/util/password.cpp')
-rw-r--r-- | src/mongo/util/password.cpp | 101 |
1 files changed, 50 insertions, 51 deletions
diff --git a/src/mongo/util/password.cpp b/src/mongo/util/password.cpp index 2c0eeb02a89..11c936bac45 100644 --- a/src/mongo/util/password.cpp +++ b/src/mongo/util/password.cpp @@ -44,66 +44,65 @@ using namespace std; namespace mongo { - string askPassword() { - - std::string password; - cout << "Enter password: "; +string askPassword() { + std::string password; + cout << "Enter password: "; #ifndef _WIN32 - const int stdinfd = 0; - termios termio; - tcflag_t old = 0; - if ( isatty( stdinfd ) ) { - int i = tcgetattr( stdinfd, &termio ); - if( i == -1 ) { - cerr << "Cannot get terminal attributes " << errnoWithDescription() << endl; - return string(); - } - old = termio.c_lflag; - termio.c_lflag &= ~ECHO; - i = tcsetattr( stdinfd, TCSANOW, &termio ); - if( i == -1 ) { - cerr << "Cannot set terminal attributes " << errnoWithDescription() << endl; - return string(); - } - } - - getline( cin, password ); - - if ( isatty( stdinfd ) ) { - termio.c_lflag = old; - int i = tcsetattr( stdinfd, TCSANOW, &termio ); - if( i == -1 ) { - cerr << "Cannot set terminal attributes " << errnoWithDescription() << endl; - return string(); - } - } -#else - HANDLE stdinh = GetStdHandle( STD_INPUT_HANDLE ); - if ( stdinh == INVALID_HANDLE_VALUE) { - cerr << "Cannot get stdin handle " << GetLastError() << "\n"; + const int stdinfd = 0; + termios termio; + tcflag_t old = 0; + if (isatty(stdinfd)) { + int i = tcgetattr(stdinfd, &termio); + if (i == -1) { + cerr << "Cannot get terminal attributes " << errnoWithDescription() << endl; return string(); } - - DWORD old; - if ( !GetConsoleMode( stdinh, &old ) ) { - cerr << "Cannot get console mode " << GetLastError() << "\n"; + old = termio.c_lflag; + termio.c_lflag &= ~ECHO; + i = tcsetattr(stdinfd, TCSANOW, &termio); + if (i == -1) { + cerr << "Cannot set terminal attributes " << errnoWithDescription() << endl; return string(); } + } + + getline(cin, password); - DWORD noecho = ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT; - if ( !SetConsoleMode( stdinh, noecho ) ) { - cerr << "Cannot set console mode " << GetLastError() << "\n"; + if (isatty(stdinfd)) { + termio.c_lflag = old; + int i = tcsetattr(stdinfd, TCSANOW, &termio); + if (i == -1) { + cerr << "Cannot set terminal attributes " << errnoWithDescription() << endl; return string(); } + } +#else + HANDLE stdinh = GetStdHandle(STD_INPUT_HANDLE); + if (stdinh == INVALID_HANDLE_VALUE) { + cerr << "Cannot get stdin handle " << GetLastError() << "\n"; + return string(); + } - getline( cin, password ); + DWORD old; + if (!GetConsoleMode(stdinh, &old)) { + cerr << "Cannot get console mode " << GetLastError() << "\n"; + return string(); + } - if ( !SetConsoleMode( stdinh, old ) ) { - cerr << "Cannot set console mode " << GetLastError() << "\n"; - return string(); - } -#endif - cout << "\n"; - return password; + DWORD noecho = ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT; + if (!SetConsoleMode(stdinh, noecho)) { + cerr << "Cannot set console mode " << GetLastError() << "\n"; + return string(); } + + getline(cin, password); + + if (!SetConsoleMode(stdinh, old)) { + cerr << "Cannot set console mode " << GetLastError() << "\n"; + return string(); + } +#endif + cout << "\n"; + return password; +} } |