当前位置: 技术问答>linux和unix
请问哪位大哥有命令行输入密码的C源程序?
来源: 互联网 发布时间:2015-04-29
本文导语: 我希望我输入密码的时候系统不显示或者显示“×”。 我找了一些代码,但是可惜是M$的,所以不能正常工作!!! | 很急么? #include #include #include #include #include #include int getPasswd...
我希望我输入密码的时候系统不显示或者显示“×”。
我找了一些代码,但是可惜是M$的,所以不能正常工作!!!
我找了一些代码,但是可惜是M$的,所以不能正常工作!!!
|
很急么?
#include
#include
#include
#include
#include
#include
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("请输入口令:");
scanf("%s", passwd);
ioctl(0, TCSETAF, &tbuf_old);
return 0;
}
#include
#include
#include
#include
#include
#include
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("请输入口令:");
scanf("%s", passwd);
ioctl(0, TCSETAF, &tbuf_old);
return 0;
}
|
用ioctl 设置no ECHO
|
对!修改termcap的属性。
参考:
http://www.fanqiang.com/a4/b9/20010820/0900001151.html
参考:
http://www.fanqiang.com/a4/b9/20010820/0900001151.html
|
打印* 的那种情况呢,谁有?
|
我将楼上的程序改了改,在红帽7.2上调试通过。
#include
#include
#include
#include
#include
#include
#include
#include
static struct termios originalTermParam;
static void set_keypress ( void )
{
struct termios currentTermParam;
tcgetattr( 0, &originalTermParam );
memcpy( ¤tTermParam, &originalTermParam, sizeof( struct termios ) );
currentTermParam.c_lflag &= ~ICANON;
currentTermParam.c_lflag &= ~ECHO;
currentTermParam.c_cc[ VTIME ] = 255;
currentTermParam.c_cc[VMIN] = 1;
tcsetattr( 0, TCSANOW, ¤tTermParam );
return;
}
void reset_keypress ( void )
{
tcsetattr( 0, TCSANOW, &originalTermParam );
return;
}
int getpasswd_x(char *passwd)
{
char ch;
int i = 0;
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 -1;
printf("pleas input your password:n");
read(0, &ch, 1);
while ( ch != 'n' ) {
passwd[i++] = ch;
write(0, "*", 1);
read(0, &ch, 1);
}
passwd[i] = '';
ioctl(0, TCSETAF, &tbuf_old);
return 0;
}
main()
{
char passd[100];
set_keypress();
getpasswd_x(passd);
reset_keypress();
printf("npassword:%sn",passd);
}
#include
#include
#include
#include
#include
#include
#include
#include
static struct termios originalTermParam;
static void set_keypress ( void )
{
struct termios currentTermParam;
tcgetattr( 0, &originalTermParam );
memcpy( ¤tTermParam, &originalTermParam, sizeof( struct termios ) );
currentTermParam.c_lflag &= ~ICANON;
currentTermParam.c_lflag &= ~ECHO;
currentTermParam.c_cc[ VTIME ] = 255;
currentTermParam.c_cc[VMIN] = 1;
tcsetattr( 0, TCSANOW, ¤tTermParam );
return;
}
void reset_keypress ( void )
{
tcsetattr( 0, TCSANOW, &originalTermParam );
return;
}
int getpasswd_x(char *passwd)
{
char ch;
int i = 0;
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 -1;
printf("pleas input your password:n");
read(0, &ch, 1);
while ( ch != 'n' ) {
passwd[i++] = ch;
write(0, "*", 1);
read(0, &ch, 1);
}
passwd[i] = '';
ioctl(0, TCSETAF, &tbuf_old);
return 0;
}
main()
{
char passd[100];
set_keypress();
getpasswd_x(passd);
reset_keypress();
printf("npassword:%sn",passd);
}
|
NAME
getpass - get a password
SYNOPSIS
#include
char *getpass( const char * prompt );
getpass - get a password
SYNOPSIS
#include
char *getpass( const char * prompt );
|
flush
|
不行的;
它好像要等终端输入一行完后,(就是回车后),它才开始getchar()一个一个地读呀
也就是说在输入那一行时是阻赛的;
它好像要等终端输入一行完后,(就是回车后),它才开始getchar()一个一个地读呀
也就是说在输入那一行时是阻赛的;
|
dchg2000(偏爱小龙女) == daehappy (追求120%结贴) ????
晕了,晕了……
晕了,晕了……