Mercurial > hg > toybox
comparison toys/login.c @ 633:98bde84a888c
toys/login.c: make use of the read_passwd function offered by the passwd library.
author | Elie De Brauwer <eliedebrauwer@gmail.com> |
---|---|
date | Fri, 20 Jul 2012 08:42:44 +0200 |
parents | 8a88a9e3c30b |
children | 9aeea680acc7 |
comparison
equal
deleted
inserted
replaced
632:6cafecf34728 | 633:98bde84a888c |
---|---|
65 unsetenv(*p); | 65 unsetenv(*p); |
66 p++; | 66 p++; |
67 } while (*p); | 67 } while (*p); |
68 } | 68 } |
69 | 69 |
70 int read_password(char * buff, int buflen) | |
71 { | |
72 int i = 0; | |
73 struct termios termio, oldtermio; | |
74 tcgetattr(0, &oldtermio); | |
75 tcflush(0, TCIFLUSH); | |
76 termio = oldtermio; | |
77 | |
78 termio.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY); | |
79 termio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP); | |
80 tcsetattr(0, TCSANOW, &termio); | |
81 | |
82 fputs("Password: ", stdout); | |
83 fflush(stdout); | |
84 | |
85 while (1) { | |
86 int ret = read(0, &buff[i], 1); | |
87 if ( ret < 0 ) | |
88 { | |
89 buff[0] = 0; | |
90 tcsetattr(0, TCSANOW, &oldtermio); | |
91 return 1; | |
92 } | |
93 else if ( ret == 0 || buff[i] == '\n' || | |
94 buff[i] == '\r' || buflen == i+1) | |
95 { | |
96 buff[i] = '\0'; | |
97 break; | |
98 } | |
99 i++; | |
100 } | |
101 | |
102 tcsetattr(0, TCSANOW, &oldtermio); | |
103 puts("\n"); | |
104 fflush(stdout); | |
105 return 0; | |
106 } | |
107 | |
108 int verify_password(char * pwd) | 70 int verify_password(char * pwd) |
109 { | 71 { |
110 char * pass; | 72 char * pass; |
111 | 73 |
112 if (read_password(toybuf, sizeof(toybuf))) | 74 if (read_passwd(toybuf, sizeof(toybuf), "Password: ")) |
113 return 1; | 75 return 1; |
114 if (!pwd) | 76 if (!pwd) |
115 return 1; | 77 return 1; |
116 if (pwd[0] == '!' || pwd[0] == '*') | 78 if (pwd[0] == '!' || pwd[0] == '*') |
117 return 1; | 79 return 1; |