1
2
3
4
5
6
7
8
9#include "libbb.h"
10
11
12static void askpass_timeout(int UNUSED_PARAM ignore)
13{
14}
15
16char* FAST_FUNC bb_ask_noecho(int fd, int timeout, const char *prompt)
17{
18#define MAX_LINE 0xfff
19 char *ret;
20 int i;
21 struct sigaction sa, oldsa;
22 struct termios tio, oldtio;
23
24 tcflush(fd, TCIFLUSH);
25
26
27
28 fputs(prompt, stdout);
29 fflush_all();
30
31 tcgetattr(fd, &oldtio);
32 tio = oldtio;
33
34
35
36
37
38
39
40
41
42
43
44 tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
45 tcsetattr(fd, TCSANOW, &tio);
46
47 memset(&sa, 0, sizeof(sa));
48
49
50 sa.sa_handler = askpass_timeout;
51 sigaction(SIGINT, &sa, &oldsa);
52 if (timeout) {
53 sigaction_set(SIGALRM, &sa);
54 alarm(timeout);
55 }
56
57 ret = NULL;
58 i = 0;
59 while (1) {
60 int r;
61
62
63
64
65 ret = xrealloc(ret, i + 2);
66 r = read(fd, &ret[i], 1);
67
68 if ((i == 0 && r == 0)
69 || r < 0
70 ) {
71 ret[i] = '\0';
72 nuke_str(ret);
73 free(ret);
74 ret = NULL;
75 break;
76 }
77
78 if (r == 0
79 || ret[i] == '\r' || ret[i] == '\n'
80 || ++i == MAX_LINE
81 ) {
82 ret[i] = '\0';
83 break;
84 }
85 }
86
87 if (timeout) {
88 alarm(0);
89 }
90 sigaction_set(SIGINT, &oldsa);
91 tcsetattr(fd, TCSANOW, &oldtio);
92 bb_putchar('\n');
93 fflush_all();
94 return ret;
95}
96char* FAST_FUNC bb_ask_noecho_stdin(const char *prompt)
97{
98 return bb_ask_noecho(STDIN_FILENO, 0, prompt);
99}
100