源码如下:
#include <termios.h>
#include <stdio.h>
#define PASSWORD_LEN 8
int main()
{
struct termios initialrsettings, newrsettings;
char password[PASSWORD_LEN + 1];
tcgetattr(fileno(stdin), &initialrsettings); //读出标准输入设备的当前设置值
//保存在刚刚创建的termios结构中去
newrsettings = initialrsettings;
newrsettings.c_lflag &= ~ECHO; //在newrsettings中关闭ECHO标志
printf("Enter password: ");
if(tcsetattr(fileno(stdin), TCSAFLUSH, &newrsettings) != 0) {
fprintf(stderr,"Could not set attributes\n");
}
else {
fgets(password, PASSWORD_LEN, stdin);
tcsetattr(fileno(stdin), TCSANOW, &initialrsettings);
fprintf(stdout, "\nYou entered %s\n", password);
} //读取口令字,并把终端设置“恢复”
exit(0);
}运行结果:



浙公网安备 33010602011771号