当前位置: 技术问答>linux和unix
linux 终端问题
来源: 互联网 发布时间:2016-05-31
本文导语: 代码如下: #include #include int main() { struct termio term, term_old; printf("Please input your password:"); ioctl(0, TCGETA, &term_old); term = term_old; term.c_lflag &= ~(ECHO|ICANON); #设置终端不回...
代码如下:
#include
#include
int main()
{
struct termio term, term_old;
printf("Please input your password:");
ioctl(0, TCGETA, &term_old);
term = term_old;
term.c_lflag &= ~(ECHO|ICANON); #设置终端不回显和原始模式
ioctl(0, TCSETA, &term);
read(0, pwd, 30);
printf("nThe input password is: %sn", pwd);
ioctl(0, TCSETA, &term_old);
}
运行如下:
b Please input your password:
The input password is: b?
为什么不是先打印出来Please input your password:,而是先输出了,有什么方法变成先打印Please input your password:,再输入b呢?
#include
#include
int main()
{
struct termio term, term_old;
printf("Please input your password:");
ioctl(0, TCGETA, &term_old);
term = term_old;
term.c_lflag &= ~(ECHO|ICANON); #设置终端不回显和原始模式
ioctl(0, TCSETA, &term);
read(0, pwd, 30);
printf("nThe input password is: %sn", pwd);
ioctl(0, TCSETA, &term_old);
}
运行如下:
b Please input your password:
The input password is: b?
为什么不是先打印出来Please input your password:,而是先输出了,有什么方法变成先打印Please input your password:,再输入b呢?
|
#include
#include
#include
#include
#include
#include
#include
int main()
{
struct termio tbuf;
int saveflag;
char passwd[256];
char * nl,*eol;
if(ioctl(0,TCGETA,&tbuf)==-1) {
printf("error:ioctln");
exit(1);
}
saveflag=tbuf.c_lflag;
tbuf.c_lflag&=~(ECHO|ECHOE|ECHOK|ECHONL);
if(ioctl(0,TCSETA,&tbuf)==-1) {
printf("error:ioctln");
exit(1);
}
printf("Input passwd:");
fflush(stdout);
fgets(passwd,255,stdin);
nl=strchr(passwd,'n');
eol=strchr(passwd,'r');
if(nl)
*nl=0;
if(eol)
*eol=0;
printf("nThe passwd is:%sn",passwd);
tbuf.c_lflag=saveflag;
if(ioctl(0,TCSETA,&tbuf)==-1) {
printf("error:ioctln");
exit(1);
}
exit(0);
}
别人的代码...
|
int main()
{
struct termio term, term_old;
printf("Please input your password:n"); //刷新一下缓冲区就可以了
ioctl(0, TCGETA, &term_old);
term = term_old;
term.c_lflag &= ~(ECHO|ICANON); #设置终端不回显和原始模式
ioctl(0, TCSETA, &term);
read(0, pwd, 30);
printf("nThe input password is: %sn", pwd);
ioctl(0, TCSETA, &term_old);
}
{
struct termio term, term_old;
printf("Please input your password:n"); //刷新一下缓冲区就可以了
ioctl(0, TCGETA, &term_old);
term = term_old;
term.c_lflag &= ~(ECHO|ICANON); #设置终端不回显和原始模式
ioctl(0, TCSETA, &term);
read(0, pwd, 30);
printf("nThe input password is: %sn", pwd);
ioctl(0, TCSETA, &term_old);
}