此功能最常见的应用莫过于控制输入密码的回显,如通过 $sudo /etc/** 获取管理权限或终端登录MySQL时输入的密码是不会显示出来的。
而termios结构的定义包含在termios.h头文件中,直接看代码:[code]#include
#include
#include
#define PASSWORD_LEN 12
int main(){
struct termios initialrsettings, newrsettings;
char password[PASSWORD_LEN + 1];
tcgetattr(fileno(stdin),&initialrsettings); #获取termios结构并保存
newrsettings = initialrsettings;
newrsettings.c_lflag &= ~ECHO; #清除变量c_lflag中由ECHO定义的比特
printf("Please Enter Password:");
if( tcsetattr(fileno(stdin),TCSAFLUSH,&newrsettings) != 0 ){
fprintf(stderr,"Could not set arrributes\n");
}else{
fgets(password,PASSWORD_LEN,stdin);
tcsetattr(fileno(stdin),TCSANOW,&initialrsettings); #还原之前的termios结构
fprintf(stdout,"\nYou entered %s as the password\n",password);
}
exit(0);
}[/code]最后编译、运行之,达到预期目的
不同Linux发行版需要包含的头文件可能存在细微的差异,以下是我主机的信息:
锟斤拷锟斤拷:Hardware:i686
OS:Linux (ubuntu desktop 7.10)
Release:2.6.22-14-generic
Version:#1 SMP Tue Feb 12 07:42:25 UTC 2008


Linux85fan 于 2008-03-26 22:23:49发表:
这些东西。。。。。。。就不用说了嘛