使用libncurses.so實作隐藏輸入,即用 * 掩蓋終端輸入密碼明文
源代碼 test.c
#include <stdio.h>
#include <curses.h>
void main()
{
char pasword[10], ch;
int i;
WINDOW * win= initscr();
cbreak();
noecho();
printf("Enter the password <any 8 characters>: ");
for(i=0; i<8; i++)
{
ch = getchar();
pasword[i] = ch;
printf("*");
}
pasword[i] = '\0';
endwin();
/*If you want to know what you have entered as password, you can print it*/
printf("\nYour password is :");
for(i=0;i<8;i++)
{
printf("%c",pasword[i]);
}
printf("\n");
}
Makefie:
test:
gcc test.c -o test -lncurses
clean:
rm -rf test
編譯并運作:
# make
gcc test.c -o test -lncurses
# ./test
*nter the password <any 8 characters>: *******
Your password is :12345678