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
Your method is correct, however you'll need to turn off terminal echo while the password is being entered:
#include
void echo_off()
{
struct sgttyb state;
(void)ioctl(0, (int)TIOCGETP, (char *)&state);
state.sg_flags &= ~ECHO;
(void)ioctl(0, (int)TIOCSETP, (char *)&state);
}
void echo_on()
{
struct sgttyb state;
(void)ioctl(0, (int)TIOCGETP, (char *)&state);
state.sg_flags |= ECHO;
(void)ioctl(0, (int)TIOCSETP, (char *)&state);
}
Instead of getch()
, why not just use getc()
instead?