在Linux世界中,屏蔽通常不是用星号完成的,通常回显只是关闭,并且终端显示空白,例如,如果您使用
su或登录虚拟终端等。
有一个用于获取密码的库函数,它不会用星号掩盖密码,但会禁止将密码回显到终端。我从一本Linux书中删除了这本书。我相信它是posix标准的一部分
#include <unistd.h>char *getpass(const char *prompt);getpass()函数首先禁用回显和终端特殊字符(例如中断字符,通常为Control-C)的所有处理。
然后,它打印提示所指向的字符串,并读取输入行,并返回以空终止的输入字符串,并删除尾随的换行符作为其函数结果。
谷歌搜索getpass()引用了GNU实现(应该在大多数linux发行版中)以及一些示例代码,用于在需要时实现自己的代码
http://www.gnu.org/s/hello/manual/libc/getpass.html
他们自己滚动的示例:
#include <termios.h>#include <stdio.h>ssize_tmy_getpass (char **lineptr, size_t *n, FILE *stream){ struct termios old, new; int nread; if (tcgetattr (fileno (stream), &old) != 0) return -1; new = old; new.c_lflag &= ~ECHO; if (tcsetattr (fileno (stream), TCSAFLUSH, &new) != 0) return -1; nread = getline (lineptr, n, stream); (void) tcsetattr (fileno (stream), TCSAFLUSH, &old); return nread;}如果需要,您可以以此为基础进行修改以显示星号。



