使用libncurses.so实现隐藏输入,即用 * 掩盖终端输入密码明文
centos上安装libncurses如下
# yum install ncurses-devel ncurses
示例源代码 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