I managed doing a little program for Linux.
#include <stdio.h>
#include <termios.h>
int main()
{
int i,tam;
char usuario[80], senha[80];
struct termios new, old;
printf("Digite o usuario. -> ");
scanf("%s", usuario);
printf("Digite a senha. -> ");
// salvando atual terminal
tcgetattr (fileno (stdin), &old);
new = old;
new.c_lflag &= ~ECHO; // desligando o ECHO
// setando novo terminal
tcsetattr (fileno (stdin), TCSAFLUSH, &new);
scanf("%s", senha);
// setando para o antigo terminal
(void) tcsetattr (fileno (stdin), TCSAFLUSH, &old);
}
Here follows the definition of struct termios
:
struct termios
{
tcflag_t c_iflag; /* input modes */
tcflag_t c_oflag; /* output modes */
tcflag_t c_cflag; /* control modes */
tcflag_t c_lflag; /* local modes */
cc_t c_cc[NCCS]; /* special characters */
}
The function tcgetattr
retrieves the terminal information. Already, the function tcsetattr
arrow changes, you can pass some options. The option TCSAFLUSH
says if there are any bytes to read, it will be discarded.
References:
- reply in the OS;
- man of the tcsetattr command.
PS: I don’t know if it runs on Windows.
I’m trying to adapt the code but it’s asking twice for the password and the second time without mask. I need him to ask only once the password and store in the variable "password" following to a "for" that I use to validate a txt with login and password of what I have already registered.
– Marthaus Lucas
Remove the scanf("%s", &senha2) and compare condition below if(strcmp... What was done was only a validator, but you can remove without problems that the password will be stored in the variable "password'. I suggest you create a separate main function to be used when necessary the user type the password, so you can reuse the code.
– T. Bandeira
It worked out ! Thank you #
– Marthaus Lucas