隐藏终端上输入密码 [英] Hide password input on terminal
问题描述
我要屏蔽我的密码,同时用 *
写它。
我用Linux GCC这个code。
我知道有一个解决方案是使用残培()
这样的功能。
的#include<&CONIO.H GT;
诠释的main()
{
焦C,密码[10];
INT I;
而((C =残培())='\\ n'!); {
密码由[i] = C;
的printf(*);
我++;
}
返回1;
}
但问题是, GCC
不包括 CONIO.H
文件,以便残培()
是没用的我。
有没有人有办法解决吗?
恩在Linux世界掩蔽通常不与asterixs完成的,通常呼应刚刚关闭,终端显示空白,例如,如果你使用su或登录虚拟终端等。
有一个库函数来处理得到的密码,也不会掩盖用星号的密码,但将禁用密码终端我拉了这一点,Linux的书我有回显。我相信它的POSIX标准的一部分
的#include<&unistd.h中GT;
字符*将getpass(为const char *提示);/ *返回指向静态分配输入密码字符串
成功,或错误NULL * /
提供了getpass()函数首先禁止呼应和所有的处理
终端特殊字符(如中断字符,通常
控制-C)。
据然后打印字符串,来提示指出,并且读取一行
输入,与尾随返回空值终止字符串输入
换行剥离,作为其函数的结果。
块引用>谷歌搜索将getpass()具有对GNU实现的引用(应该是在大多数Linux发行版)和一些示例code为implemnting自己如果需要的话
<一个href=\"http://www.gnu.org/s/hello/manual/libc/getpass.html\">http://www.gnu.org/s/hello/manual/libc/getpass.html
Thier滚动例如你自己的:
的#include&LT; termios.h&GT;
#包括LT&;&stdio.h中GT; ssize_t供
my_getpass(字符** lineptr,为size_t * N,FILE *流)
{
结构termios的旧的,新;
INT NREAD; / *打开关闭呼应,如果我们不能失败。 * /
如果(tcgetattr(的fileno(流),放大器;!旧)= 0)
返回-1;
新老=;
new.c_lflag&安培; =〜ECHO;
如果(tcsetattr(的fileno(流),TCSAFLUSH,&安培;!新)= 0)
返回-1; / *读取密码。 * /
NREAD =函数getline(lineptr,N,流); / *恢复终端。 * /
(无效)tcsetattr(的fileno(流),TCSAFLUSH,&安培;年老的); 返回NREAD;
}如果需要的话,你可以使用这个为基础可以修改它来显示asterixs。
I want to mask my password while writing it with
*
. I use Linux GCC for this code. I know one solution is to usegetch()
function like this#include <conio.h> int main() { char c,password[10]; int i; while( (c=getch())!= '\n');{ password[i] = c; printf("*"); i++; } return 1; }
but the problem is that
GCC
does not includeconio.h
file so,getch()
is useless for me. Does anyone have a solution?解决方案Hmm in the Linux world masking isn't usually done with asterixs, normally echoing is just turned off and the terminal displays blanks eg if you use su or log into a virtual terminal etc.
There is a library function to handle getting passwords, it won't mask the password with asterix's but will disable echoing of the password to terminal I pulled this out of a linux book I have. I believe its part of the posix standard
#include <unistd.h> char *getpass(const char *prompt); /*Returns pointer to statically allocated input password string on success, or NULL on error*/
The getpass() function first disables echoing and all processing of terminal special characters (such as the interrupt character, normally Control-C).
It then prints the string pointed to by prompt, and reads a line of input, returning the null-terminated input string with the trailing newline stripped, as its function result.
A google search for getpass() has a reference to the GNU implementation (should be in most linux distros) and some sample code for implemnting your own if need be
http://www.gnu.org/s/hello/manual/libc/getpass.html
Thier example for rolling your own:
#include <termios.h> #include <stdio.h> ssize_t my_getpass (char **lineptr, size_t *n, FILE *stream) { struct termios old, new; int nread; /* Turn echoing off and fail if we can't. */ if (tcgetattr (fileno (stream), &old) != 0) return -1; new = old; new.c_lflag &= ~ECHO; if (tcsetattr (fileno (stream), TCSAFLUSH, &new) != 0) return -1; /* Read the password. */ nread = getline (lineptr, n, stream); /* Restore terminal. */ (void) tcsetattr (fileno (stream), TCSAFLUSH, &old); return nread; }
If need be you could use this as the basis as modify it to display asterixs.
这篇关于隐藏终端上输入密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!