added utility to hide console echo for entering passwords
This commit is contained in:
parent
ded475f45a
commit
2743b56b56
2 changed files with 54 additions and 0 deletions
8
include/fc/io/console.hpp
Normal file
8
include/fc/io/console.hpp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#pragma once
|
||||
namespace fc
|
||||
{
|
||||
/** enables / disables echoing of console input, useful for
|
||||
* entering passwords on the console.
|
||||
*/
|
||||
void set_console_echo( bool enable_echo );
|
||||
} // namespace fc
|
||||
46
src/io/console.cpp
Normal file
46
src/io/console.cpp
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace fc {
|
||||
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
|
||||
void set_console_echo( bool enable_echo )
|
||||
{
|
||||
auto stdin_handle = GetStdHandle( STD_INPUT_HANDLE );
|
||||
DWORD mode = 0;
|
||||
GetConsoleMode( stdin_handle, &mode );
|
||||
if( enable_echo )
|
||||
{
|
||||
SetConsoleMode( stdin_handle, mode | ENABLE_ECHO_INPUT );
|
||||
}
|
||||
else
|
||||
{
|
||||
SetConsoleMode( stdin_handle, mode & (~ENABLE_ECHO_INPUT) );
|
||||
}
|
||||
}
|
||||
|
||||
#else // NOT WIN32
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void set_console_echo( bool enable_echo )
|
||||
{
|
||||
termios oldt;
|
||||
tcgetattr(STDIN_FILENO, &oldt);
|
||||
termios newt = oldt;
|
||||
if( enable_echo )
|
||||
{
|
||||
newt.c_lflag |= ~ECHO;
|
||||
}
|
||||
else
|
||||
{
|
||||
newt.c_lflag &= ~ECHO;
|
||||
}
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
|
||||
}
|
||||
|
||||
#endif // WIN32
|
||||
|
||||
} // namespace fc
|
||||
Loading…
Reference in a new issue