I want to mask my password while writing it with *
.
I use Linux GCC for this code.
I know one solution is to use getch()
function like this
In C you can use getpasswd()
function which pretty much doing similar thing as stty
in shell, example:
#include
#include
#include
#include
int main()
{
char acct[80], password[80];
printf(“Account: “);
fgets(acct, 80, stdin);
acct[strlen(acct)-1] = 0; /* remove carriage return */
strncpy(password, getpass(“Password: “), 80);
printf(“You entered acct %s and pass %s\n”, acct, password);
return 0;
}
Here is equivalent shell script which use stty
(which changes the settings of your tty
):
save_state=$(stty -g)
/bin/echo -n “Account: “
read acct
/bin/echo -n “Password: “
stty -echo
read password # this won’t echo
stty “$save_state”
echo “”
echo account = $acct and password = $password
Source: How can I read a password without echoing it in C?