regarding:
while(getch() != ENTER){
ch = getch();
this gets 2 characters every pass through the loop. Not what you want. Suggest:
while( ( ch = getch() ) != ENTER ){
To avoid echoing the char when the user enters it:
You need to turn echo off
then for each char entered by the user, output a masking character, like *
since the array pwd[]
is only 100 characters long, need to limit the number of characters the user can enter to 99 (leaves room for the trailing NUL byte) Suggest:
WINDOW *mainwin;
mainwin = initscr(); /* start the curses mode with default window */
clear() /* clear the default window */
...
raw(); /* stop terminal driver from interpreting key strokes */
cbreak(); /* input chars without waiting for newline */
nodelay( mainwin, true); /* causes getch() to wait for a key rather than returning ERR */
keypad( mainwin, true ); /* program must handle all meta keys including newline */
noecho(); /* stop terminal echo while entering password */
size_t i = 0;
while( i < (sizeof( pwd ) -1) && ( ch = getch() ) != ENTER )
{
pwd[i] = ch;
i++;
printw( "*" );
refresh();
}
echo(); /* resume echoing characters on terminal */
keypad( mainwin, false ); /* stop passing keypad chars to program */
noraw(); /* terminal driver resumes interpreting key strokes */
pwd[i] = '\0'; /* NUL terminate the string */
...
endwin();
The above may be a bit of overkill.
due to several problems, suggest NOT using the C library function: getpass()
if you use the raw()
function, then the program will also need to handle backspace (and similar) characters from the user.