Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1461362
  • 博文数量: 408
  • 博客积分: 10036
  • 博客等级: 上将
  • 技术积分: 4440
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-06 13:57
文章分类

全部博文(408)

文章存档

2011年(1)

2010年(2)

2009年(1)

2008年(3)

2007年(7)

2006年(394)

我的朋友

分类: C/C++

2006-07-24 17:54:22

linux终端输入时关闭回显
方法一:
示例程序
#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:ioctl\n");
  exit(1);
 }
 
 saveflag=tbuf.c_lflag;
 tbuf.c_lflag&=~(ECHO|ECHOE|ECHOK|ECHONL);
 
 if(ioctl(0,TCSETA,&tbuf)==-1) {
  printf("error:ioctl\n");
  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:%s\n",passwd);
 
 tbuf.c_lflag=saveflag;

 if(ioctl(0,TCSETA,&tbuf)==-1) {
  printf("error:ioctl\n");
  exit(1);
 }
 
 exit(0);
}

方法二:
termios.h里声明的函数:
int tcgetattr (int __fd, struct termios *__termios_p);
int tcsetattr (int __fd, int __optional_actions,__const struct termios *__termios_p);

例如:

struct termios sg;
tcgetattr(f, &sg); /* get settings */
sg.c_lflag &= ~ECHO; /* turn echo off */
sg.c_lflag |= ECHO; /* turn echo on */
tcsetattr(f,TCSAFLUSH,&sg); /* set settings
阅读(2019) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~