getpass()函数用于从控制台输入一行字符串,关闭了回显(输入时不显示输入的字符串),适用于用密码的输入。
语法 char * getpass (const char * prompt);
参数prompt为提示字符串地址。
getpass()函数返回值:输入字符串地址。
#include #include int main(void) { char *password; password = getpass("Input a password:"); /*输入密码*/ printf("The password is: %s/r/n", password); /*显示密码*/ return 0; }getpass函数通常会与crypt加密函数一同使用,
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/wait.h> #include <crypt.h> int main() { char passwd[13]; char *key; char slat[2]; key = getpass("Input first passward:"); slat[0] = key[0]; slat[1] = key[1]; strcpy(passwd,crypt(key,slat)); key = getpass("Input second passward:"); slat[0] = passwd[0]; slat[1] = passwd[1]; printf("After crypt(),1st passwd:%s\n",passwd); printf("After crypt(),2st passwd:%s\n",passwd); return 0; }gcc -lcrypt xxx.c
[root@iZ28o6dlzs9Z process]# ./a.out
Input first passward: //test
Input second passward: //test
After crypt(),1st passwd:teH0wLIpW0gyQ
After crypt(),2st passwd:teH0wLIpW0gyQ
getpass函数man翻译
函数说明:getpass()会显示参数prompt所指的字符串,然后从/dev/tty中读取所输入的密码,若无法从/dev/tty中读取则会转从标准输入设备中读取密码。所输入的密码长度限制在128个字符,包含结束字符NULL, 超过长度的字符及换行字符/n将会被忽略。在输入密码时getpass()会关闭字符回应,并忽略一些信号如CTRL-C 或 CTRL-Z所产生的信号
返回值 :返回一个指向以NULL结尾的密码字符串
附加说明:为了系统安全考虑,在般在使用getpass()输入密码后,该密码最好尽快处理完毕,然后将该密码字符串清除