Hide password input on terminal
Hide password input on terminal
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
#include int main() { char c,password[10]; int i; while( (c=getch())!= '\n');{ password[i] = c; printf("*"); i++; } return 1; }
but the problem is that GCC
does not include conio.h
file so, getch()
is useless for me. Does anyone have a solution?
Answer by Susam Pal for Hide password input on terminal
You can create your own getch()
function on Linux in this manner.
int getch() { struct termios oldtc, newtc; int ch; tcgetattr(STDIN_FILENO, &oldtc); newtc = oldtc; newtc.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newtc); ch=getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldtc); return ch; }
Demo code:
int main(int argc, char **argv) { int ch; printf("Press x to exit.\n\n"); for (;;) { ch = getch(); printf("ch = %c (%d)\n", ch, ch); if(ch == 'x') break; } return 0; }
Answer by Delan Azabani for Hide password input on terminal
The functionality of getch
(which is a non-standard, Windows function) can be emulated with this code:
#include #include int getch() { struct termios oldt, newt; int ch; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); return ch; }
Note that your approach is not perfect - it's better to use something like ncurses or another terminal library to handle these things.
Answer by Constantinius for Hide password input on terminal
Unfortunately in the C standard library there is no such function out of the box. Maybe in third party library.
One option is use ANSI escape sequences to set the background color to foreground color in the console to conceal the password. Try this link.
Answer by trojanfoe for Hide password input on terminal
Your method is correct, however you'll need to turn off terminal echo while the password is being entered:
#include echo_off() { struct sgttyb state; (void)ioctl(0, (int)TIOCGETP, (char *)&state); state.sg_flags &= ~ECHO; (void)ioctl(0, (int)TIOCSETP, (char *)&state); } 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?
Answer by Daniel Leschkowski for Hide password input on terminal
You might use ncurses.h if it is not necessary to be portable onto Windows for that, but here is some kind of a more "portable" version:
If it is not necessery to be portable ill point you to a ncurses solution
portablegetch.h
/*portablegetch.h*/ #ifndef PGETCH #define PGETCH #ifdef __unix__ #include #include static struct termios n_term; static struct termios o_term; static int cbreak(int fd) { if((tcgetattr(fd, &o_term)) == -1) return -1; n_term = o_term; n_term.c_lflag = n_term.c_lflag & ~(ECHO|ICANON); n_term.c_cc[VMIN] = 1; n_term.c_cc[VTIME]= 0; if((tcsetattr(fd, TCSAFLUSH, &n_term)) == -1) return -1; return 1; } int getch() { int cinput; if(cbreak(STDIN_FILENO) == -1) { fprintf(stderr, "cbreak failure, exiting \n"); exit(EXIT_FAILURE); } cinput = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &o_term); return cinput; } #elif _MSC_VER || __WIN32__ || __MS_DOS__ #include
0 comments:
Post a Comment