Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | | - |
18 | #ifndef _LIBC | - |
19 | # include <config.h> | - |
20 | #endif | - |
21 | | - |
22 | #include "getpass.h" | - |
23 | | - |
24 | #include <stdio.h> | - |
25 | | - |
26 | #if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) | - |
27 | | - |
28 | # include <stdbool.h> | - |
29 | | - |
30 | # if HAVE_DECL___FSETLOCKING && HAVE___FSETLOCKING | - |
31 | # if HAVE_STDIO_EXT_H | - |
32 | # include <stdio_ext.h> | - |
33 | # endif | - |
34 | # else | - |
35 | # define __fsetlocking(stream, type) /* empty */ | - |
36 | # endif | - |
37 | | - |
38 | # if HAVE_TERMIOS_H | - |
39 | # include <termios.h> | - |
40 | # endif | - |
41 | | - |
42 | # if USE_UNLOCKED_IO | - |
43 | # include "unlocked-io.h" | - |
44 | # else | - |
45 | # if !HAVE_DECL_FFLUSH_UNLOCKED | - |
46 | # undef fflush_unlocked | - |
47 | # define fflush_unlocked(x) fflush (x) | - |
48 | # endif | - |
49 | # if !HAVE_DECL_FLOCKFILE | - |
50 | # undef flockfile | - |
51 | # define flockfile(x) ((void) 0) | - |
52 | # endif | - |
53 | # if !HAVE_DECL_FUNLOCKFILE | - |
54 | # undef funlockfile | - |
55 | # define funlockfile(x) ((void) 0) | - |
56 | # endif | - |
57 | # if !HAVE_DECL_FPUTS_UNLOCKED | - |
58 | # undef fputs_unlocked | - |
59 | # define fputs_unlocked(str,stream) fputs (str, stream) | - |
60 | # endif | - |
61 | # if !HAVE_DECL_PUTC_UNLOCKED | - |
62 | # undef putc_unlocked | - |
63 | # define putc_unlocked(c,stream) putc (c, stream) | - |
64 | # endif | - |
65 | # endif | - |
66 | | - |
67 | | - |
68 | | - |
69 | | - |
70 | | - |
71 | | - |
72 | # ifndef TCSASOFT | - |
73 | # define TCSASOFT 0 | - |
74 | # endif | - |
75 | | - |
76 | static void | - |
77 | call_fclose (void *arg) | - |
78 | { | - |
79 | if (arg != NULL)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
80 | fclose (arg); never executed: rpl_fclose (arg); | 0 |
81 | } never executed: end of block | 0 |
82 | | - |
83 | char * | - |
84 | getpass (const char *prompt) | - |
85 | { | - |
86 | FILE *tty; | - |
87 | FILE *in, *out; | - |
88 | # if HAVE_TCGETATTR | - |
89 | struct termios s, t; | - |
90 | # endif | - |
91 | bool tty_changed = false; | - |
92 | static char *buf; | - |
93 | static size_t bufsize; | - |
94 | ssize_t nread; | - |
95 | | - |
96 | | - |
97 | | - |
98 | | - |
99 | tty = fopen ("/dev/tty", "w+"); | - |
100 | if (tty == NULL)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
101 | { | - |
102 | in = stdin; | - |
103 | out = stderr; | - |
104 | } never executed: end of block | 0 |
105 | else | - |
106 | { | - |
107 | | - |
108 | __fsetlocking (tty, FSETLOCKING_BYCALLER); | - |
109 | | - |
110 | out = in = tty; | - |
111 | } never executed: end of block | 0 |
112 | | - |
113 | flockfile (out); | - |
114 | | - |
115 | | - |
116 | # if HAVE_TCGETATTR | - |
117 | if (tcgetattr (fileno (in), &t) == 0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
118 | { | - |
119 | | - |
120 | s = t; | - |
121 | | - |
122 | t.c_lflag &= ~(ECHO | ISIG); | - |
123 | tty_changed = (tcsetattr (fileno (in), TCSAFLUSH | TCSASOFT, &t) == 0); | - |
124 | } never executed: end of block | 0 |
125 | # endif | - |
126 | | - |
127 | | - |
128 | fputs_unlocked (prompt, out); | - |
129 | fflush_unlocked (out); | - |
130 | | - |
131 | | - |
132 | nread = getline (&buf, &bufsize, in); | - |
133 | | - |
134 | | - |
135 | | - |
136 | | - |
137 | | - |
138 | | - |
139 | | - |
140 | | - |
141 | | - |
142 | | - |
143 | fseeko (out, 0, SEEK_CUR); | - |
144 | | - |
145 | if (buf != NULL)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
146 | { | - |
147 | if (nread < 0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
148 | buf[0] = '\0'; never executed: buf[0] = '\0'; | 0 |
149 | else if (buf[nread - 1] == '\n')TRUE | never evaluated | FALSE | never evaluated |
| 0 |
150 | { | - |
151 | | - |
152 | buf[nread - 1] = '\0'; | - |
153 | if (tty_changed)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
154 | { | - |
155 | | - |
156 | putc_unlocked ('\n', out); | - |
157 | } never executed: end of block | 0 |
158 | } never executed: end of block | 0 |
159 | } never executed: end of block | 0 |
160 | | - |
161 | | - |
162 | # if HAVE_TCSETATTR | - |
163 | if (tty_changed)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
164 | tcsetattr (fileno (in), TCSAFLUSH | TCSASOFT, &s); never executed: tcsetattr (fileno (in), 2 | 0, &s); | 0 |
165 | # endif | - |
166 | | - |
167 | funlockfile (out); | - |
168 | | - |
169 | call_fclose (tty); | - |
170 | | - |
171 | return buf; never executed: return buf; | 0 |
172 | } | - |
173 | | - |
174 | #else /* W32 native */ | - |
175 | | - |
176 | | - |
177 | | - |
178 | | - |
179 | | - |
180 | # include <limits.h> | - |
181 | | - |
182 | # include <conio.h> | - |
183 | | - |
184 | # include <string.h> | - |
185 | | - |
186 | # ifndef PASS_MAX | - |
187 | # define PASS_MAX 512 | - |
188 | # endif | - |
189 | | - |
190 | char * | - |
191 | getpass (const char *prompt) | - |
192 | { | - |
193 | char getpassbuf[PASS_MAX + 1]; | - |
194 | size_t i = 0; | - |
195 | int c; | - |
196 | | - |
197 | if (prompt) | - |
198 | { | - |
199 | fputs (prompt, stderr); | - |
200 | fflush (stderr); | - |
201 | } | - |
202 | | - |
203 | for (;;) | - |
204 | { | - |
205 | c = _getch (); | - |
206 | if (c == '\r') | - |
207 | { | - |
208 | getpassbuf[i] = '\0'; | - |
209 | break; | - |
210 | } | - |
211 | else if (i < PASS_MAX) | - |
212 | { | - |
213 | getpassbuf[i++] = c; | - |
214 | } | - |
215 | | - |
216 | if (i >= PASS_MAX) | - |
217 | { | - |
218 | getpassbuf[i] = '\0'; | - |
219 | break; | - |
220 | } | - |
221 | } | - |
222 | | - |
223 | if (prompt) | - |
224 | { | - |
225 | fputs ("\r\n", stderr); | - |
226 | fflush (stderr); | - |
227 | } | - |
228 | | - |
229 | return strdup (getpassbuf); | - |
230 | } | - |
231 | #endif | - |
| | |