您的位置:首页 > 运维架构 > Linux

linux回删密码,密码以*显示

2016-12-06 19:49 246 查看
#include <stdio.h>

#include <termios.h>

#include <unistd.h>

int my_getch()

{

struct termios oldt,newt;

int ch;

tcgetattr(STDIN_FILENO,&oldt);

newt = oldt;

newt.c_lflag &= ~(ICANON | ECHO);

tcsetattr(STDIN_FILENO, TCSANOW, &newt);

ch = getchar();

tcsetattr(STDIN_FILENO, TCSANOW, &oldt);

return ch;

}

int getpasswd(char * passwd, int size)

{

int c;

int n = 0;

do

{

c = my_getch();

if(c != '\n' && c != '\r' && c != 127)

{

passwd
= c;

printf("*");

n++;

}

else if((c != '\n' | c != '\r') && c == 127)

{

if(n > 0)

{

n--;

printf("\b \b");//输出退格一个空格!!!

}

}

}while(c != '\n' && c != '\r' && n < (size - 1));

passwd
= '\0';

return 0;

}

int main()

{

char passWord[20];

printf("输入密码:\n");

getpasswd(passWord,20);

printf("\n");

printf("密码是:%s\n",passWord);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: