Skip to content

Instantly share code, notes, and snippets.

@ryangraham
Created May 10, 2020 03:46
Show Gist options
  • Save ryangraham/9593bd0d71e772f09caa96f9061c2ca9 to your computer and use it in GitHub Desktop.
Save ryangraham/9593bd0d71e772f09caa96f9061c2ca9 to your computer and use it in GitHub Desktop.
masked password prompt
#include <iostream>
#include <string>
#include <termios.h>
#include <unistd.h>
int password_prompt(std::string &password)
{
struct termios tty;
// Get current terminal settings
if (tcgetattr(STDIN_FILENO, &tty) != 0)
return -1;
// Unset ECHO flag
tty.c_lflag &= ~ECHO;
if (tcsetattr(STDIN_FILENO, TCSANOW, &tty) != 0)
return -1;
// Prompt for password
std::cout << "Password: ";
std::cin >> password;
// Set ECHO flag
tty.c_lflag |= ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &tty);
return 0;
}
void username_prompt(std::string &username)
{
std::cout << "Username: ";
std::cin >> username;
}
int main(void)
{
std::string username = "";
std::string password = "";
username_prompt(username);
password_prompt(password);
std::cout << std::endl;
// std::cout << password << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment