Linux终端不回显

关键字: Linux, 终端属性, 回显

一直想写段程序试试输入不回显的东西,但是一直被一些事情所羁绊(其实是自己懒,玩游戏去了),直到最近要在公司的代码上加东西。

于是乎,一轮 google 以后,终于找到了答案,其实是因为自己看APUE看的头晕了。代码很简单,先用 tcgetattr 取到现有的属性,然后在现有属性上设置 XXX.c_lflag &= ~ECHO ,再用 tcsetattr 把新属性给设置上去,就行了,对于两个终端相关的函数,需要提供 fd ,对于 stdin 而言是 0 ,话说不知道为啥昨天直接写 stdin 不行,下面呢,木有了~~~~

PS:突然想到,代码贴上来是不是好一点?于是下面又有了:

 1  #include <stdio.h>
 2  #include <stdlib.h>
 3  #include <termios.h>
 4  #include <time.h>
 5
 6
 7  int main (int argc, char* argv[])
 8  {
 9      struct termios init_setting;
10      struct termios pend_setting;
11
12      if (tcgetattr(0, &init_setting) < 0)
13      {
14          fprintf (stderr, "Error~~~\n");
15          exit (1);
16      }
17      pend_setting = init_setting;
18
19      printf ("Pending for 5 secone!\n");
20      pend_setting.c_lflag &= ~ECHO;
21      tcsetattr (0, TCSANOW, &pend_setting);
22      sleep (5);
23
24      return 0;
25  }

This entry was posted in 小企鹅Linux. Bookmark the permalink.

One Response to Linux终端不回显

  1. horizon says:

    文章不错,很好,这是我们的原创博客,访回访呀。如果有兴趣,请互换链接,以促进互访,谢谢!
    http://tangshudong.wordpress.com.cn

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>