termios示例之不回显字符
erhuabushuo
posted @ 2012年8月26日 15:27
in C
, 1402 阅读
#include <termios.h> #include <stdio.h> #include <stdlib.h> #define PASSWORD_LEN 8 int main() { struct termios initialrsettings, newrsettings; char password[PASSWORD_LEN + 1]; tcgetattr(fileno(stdin), &initialrsettings); newrsettings = initialrsettings; newrsettings.c_lflag &= ~ECHO; printf("Enter password: "); if (tcsetattr(fileno(stdin), TCSAFLUSH, &newrsettings) != 0) { fprintf(stderr, "Could not set attributes\n"); } else { fgets(password, PASSWORD_LEN, stdin); tcsetattr(fileno(stdin), TCSANOW, &initialrsettings); fprintf(stdout, "\nYou entered %s\n", password); } exit(0); }
Enter password:
You entered 123456
2012年8月26日 20:46
printf 那里不需要再 fflush 下?
2012年8月26日 20:47
这篇文章的分类……
2012年8月27日 20:44
man 3 getpass
2012年8月31日 01:05
@Mike Manilone: 谢谢!
2012年8月31日 01:07
@依云: fflush 哪个缓冲区? 分类我神经了。
2012年8月31日 02:29
@erhuabushuo: stdout 啊。我觉得在你输入密码的时候文本应该还没显示出来吧?
2012年8月31日 14:47
@依云: 不会的,这里只对stdin设置了~ECHO特性
2012年9月01日 22:23
@erhuabushuo: 果然不用 fflush。这不科学!(好吧,老实看 APUE 去……)
2012年9月01日 22:24
「任何时候只要通过标准输入输出库要求从 ( a )一个不带缓存的流,或者 ( b )一个行缓存的流(它预先要求从内核得到数据)得到输入数据,那么就会造成刷新所有行缓存输出流。」——原来如此。
2024年1月15日 22:09
We are manufacturers and exporters of high quality Sportswear & Textile products. Our company produces custom designs and sizes as per our customer’s requirements. For biker jacket visit!