您的位置:首页 > 运维架构 > Linux

LINUX:用C语言来模拟密码输入

2012-12-29 15:13 337 查看
    以前在LINUX环境下,想输入密码(关闭回显)时都是用getpass函数,今天无意中看到手册上说:This function is obsolete.  Do not use it. 

    那我就自己实现一个类似的功能吧(功能相同,原理不同)

程序的思路很简单:关闭回显,读取输入,恢复设置。

上代码:

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <string.h>

#define MAXLEN 256

//参数dest是目标字符串, maxlen是最大长度,
//如果输入超过了最大长度,则密码将会被截断
//成功返回0,否则返回-1
int new_getpass(char *dest, int maxlen)
{
struct termios oldflags, newflags;
int len;

//设置终端为不回显模式
tcgetattr(fileno(stdin), &oldflags);
newflags = oldflags;
newflags.c_lflag &= ~ECHO;
newflags.c_lflag |= ECHONL;
if (tcsetattr(fileno(stdin), TCSANOW, &newflags) != 0)
{
perror("tcsetattr");
return -1;
}

//获取来自键盘的输入
fgets(dest, maxlen, stdin);
len = strlen(dest);
if( len > maxlen-1 )
len = maxlen - 1;
dest[len-1] = 0;

//恢复原来的终端设置
if (tcsetattr(fileno(stdin), TCSANOW, &oldflags) != 0)
{
perror("tcsetattr");
return -1;
}
return 0;
}

int main()
{
char password[MAXLEN];
printf("Enter password: ");
new_getpass(password, MAXLEN);
printf("You password is: %s\n", password);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: