-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpasswd.cpp
More file actions
46 lines (45 loc) · 1.2 KB
/
passwd.cpp
File metadata and controls
46 lines (45 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*************************************************************************
> File Name: passwd.cpp
> Author: MidCHeck
> Mail: midcheck@foxmail.com
> Created Time: 2019年07月01日 星期一 22时28分13秒
************************************************************************/
#include "passwd.h"
namespace MidCHeck{
int mygetch()
{
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, n = 0;
do
{
c = mygetch();
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 n;
}
} // end namespace MidCHeck