#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[n] = 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[n] = '\0';
return 0;
}
int main()
{
char passWord[20];
printf("输入密码:\n");
getpasswd(passWord,20);
printf("\n");
printf("密码是:%s\n",passWord);
}
#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[n] = 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[n] = '\0';
return 0;
}
int main()
{
char passWord[20];
printf("输入密码:\n");
getpasswd(passWord,20);
printf("\n");
printf("密码是:%s\n",passWord);
}