#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)
//函數set_disp_mode用於控制是否開啟輸入回顯功能
//如果option為0,則關閉回顯,為1則開啟回顯
int set_disp_mode(int fd,int option)
{
int err;
struct termios term;
if(tcgetattr(fd,&term)==-1){
perror("Cannot get the attribution of the terminal");
return 1;
}
if(option)
term.c_lflag|=ECHOFLAGS;
else
term.c_lflag &=~ECHOFLAGS;
err=tcsetattr(fd,TCSAFLUSH,&term);
if(err==-1 && err==EINTR){
perror("Cannot set the attribution of the terminal");
return 1;
}
return 0;
}
//函數getpasswd用於獲得使用者輸入的密碼,並將其儲存在指定的字元數組中
int getpasswd(char* passwd, int size)
{
int c;
int n = 0;
printf("Please Input password:");
do{
c=getchar();
if (c != '/n'|c!='/r'){
passwd[n++] = c;
}
}while(c != '/n' && c !='/r' && n < (size - 1));
passwd[n] = '/0';
return n;
}
int main()
{
char passwd[20];
//首先關閉輸出回顯,這樣輸入密碼時就不會顯示輸入的字元資訊
set_disp_mode(STDIN_FILENO,0);
//調用getpasswd函數獲得使用者輸入的密碼
getpasswd(passwd, sizeof(passwd));
printf("/nYour passwd is:%s/n", passwd);
printf("Press any key continue .../n");
set_disp_mode(STDIN_FILENO,1);
getchar();
return 0;
}