|
看看这个
{:soso_e100:}
1#define TRUE 1
2#define FALSE 0
3#define MIN_INPUT 0x20
4#define MAX_INPUT 0x7e
5/*
6* 所有功能有此函数实现:
7* pszPw : 保存密码的缓冲
8* iMaxSize :最大的密码长度,该长度必须小于缓冲区大小。
9* 返回值为TRUE为成功获取密码。总是返回TRUE
10*/
11int GetPassword(unsigned char* pszPw,int iMaxSize)
12{
13unsigned char ch;
14int i=0;
15
16int bIsEcho=TRUE;
17//while( ! kbhit() && i<iMaxSize )
18while( ( ch = (unsigned char)getch() ) && i < iMaxSize )
19{
20//ch = (unsigned char)getch();
21bIsEcho=TRUE;
22
23if ( ch == 13)
24{
25pszPw[i++]=0;
26break;
27}
28else if ( ch >= MIN_INPUT && ch <= MAX_INPUT) /*所有可打印字符*/
29{
30pszPw[i++]=ch;
31}
32else if ( ch == 8 && i> 0 ) /*退格键*/
33{
34pszPw[i--] = 0;
35bIsEcho = FALSE;
36putchar( ch );
37putchar( ' ' );
38putchar( ch );
39
40}
41else
42bIsEcho = FALSE;
43
44if(bIsEcho)
45putchar('*');
46
47}
48
49pszPw=0;
50return TRUE;
51}
52
53int main(void)
54{
55int iMaxSize=80;
56unsigned char pw[99];
57if ( GetPassword(pw,iMaxSize) == TRUE ){
58printf("\npassword=%s",pw);
59}
60else{
61printf("\nCan not get the password!");
62}
63}
|
|