1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19: #include <stdio.h>
20: #include <stdio_ext.h>
21: #include <string.h>
22: #include <termios.h>
23: #include <unistd.h>
24:
25: #include <wchar.h>
26: #define flockfile(s) _IO_flockfile (s)
27: #define funlockfile(s) _IO_funlockfile (s)
28: #include <bits/libc-lock.h>
29:
30:
31:
32:
33:
34:
35: #ifndef TCSASOFT
36: #define TCSASOFT 0
37: #endif
38:
39: static void
40: call_fclose (void *arg)
41: {
42: if (arg != NULL)
43: fclose (arg);
44: }
45:
46: char *
47: getpass (prompt)
48: const char *prompt;
49: {
50: FILE *in, *out;
51: struct termios s, t;
52: int tty_changed;
53: static char *buf;
54: static size_t bufsize;
55: ssize_t nread;
56:
57:
58:
59:
60: in = fopen ("/dev/tty", "w+c");
61: if (in == NULL)
62: {
63: in = stdin;
64: out = stderr;
65: }
66: else
67: {
68:
69: __fsetlocking (in, FSETLOCKING_BYCALLER);
70:
71: out = in;
72: }
73:
74:
75:
76: __libc_cleanup_push (call_fclose, in == out ? in : NULL);
77:
78: flockfile (out);
79:
80:
81:
82: if (__tcgetattr (fileno (in), &t) == 0)
83: {
84:
85: s = t;
86:
87: t.c_lflag &= ~(ECHO|ISIG);
88: tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
89: }
90: else
91: tty_changed = 0;
92:
93:
94: __fxprintf (out, "%s", prompt);
95: fflush_unlocked (out);
96:
97:
98: nread = __getline (&buf, &bufsize, in);
99: if (buf != NULL)
100: {
101: if (nread < 0)
102: buf[0] = '\0';
103: else if (buf[nread - 1] == '\n')
104: {
105:
106: buf[nread - 1] = '\0';
107: if (tty_changed)
108:
109: __fxprintf (out, "\n");
110: }
111: }
112:
113:
114: if (tty_changed)
115: (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
116:
117: funlockfile (out);
118:
119: __libc_cleanup_pop (0);
120:
121: if (in != stdin)
122:
123: fclose (in);
124:
125: return buf;
126: }