做linux下的程序,如何在从标准输入读密码时关闭回显?
+ f. A4 j9 k0 j4 H, \: r使用getpass函数
# s7 l; ^" Q! {# i# F' ~或者:
复制内容到剪贴板
代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <termio.h>
int getPasswd(char *passwd)
{
struct termio tbuf_old,tbuf;
ioctl(0, TCGETA, &tbuf_old);
ioctl(0, TCGETA, &tbuf);
tbuf.c_lflag &=~ECHO;
if (ioctl(0, TCSETAF, &tbuf) != 0)
return;
printf("please input passwd:");
scanf("%s", passwd);
ioctl(0, TCSETAF, &tbuf_old);
return 0;
};
int main()
{
char passwd[8];
getPasswd( passwd );
printf("pass: %s", passwd);
return 1;
}