diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-04-07 15:50:30 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-04-07 15:50:30 +0200 |
commit | 17058a06c4333fc0c492c168c8a971ebd0fd5a5a (patch) | |
tree | a32133b2e5c18be65796283177551f9bcd7e49bb /libbb | |
parent | bae8fc4436f9aeb43ef0aaccd1c9b1b35b5a4617 (diff) | |
download | busybox-w32-17058a06c4333fc0c492c168c8a971ebd0fd5a5a.tar.gz busybox-w32-17058a06c4333fc0c492c168c8a971ebd0fd5a5a.tar.bz2 busybox-w32-17058a06c4333fc0c492c168c8a971ebd0fd5a5a.zip |
libbb: switch bb_ask_noecho() to "mallocing" string return API
function old new delta
bb_ask_noecho 313 330 +17
get_cred_or_die 125 115 -10
passwd_main 995 958 -37
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 17/-47) Total: -30 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/bb_askpass.c | 45 |
1 files changed, 31 insertions, 14 deletions
diff --git a/libbb/bb_askpass.c b/libbb/bb_askpass.c index aadc69108..2dcead35a 100644 --- a/libbb/bb_askpass.c +++ b/libbb/bb_askpass.c | |||
@@ -13,16 +13,9 @@ static void askpass_timeout(int UNUSED_PARAM ignore) | |||
13 | { | 13 | { |
14 | } | 14 | } |
15 | 15 | ||
16 | char* FAST_FUNC bb_ask_noecho_stdin(const char *prompt) | 16 | char* FAST_FUNC bb_ask_noecho(int fd, int timeout, const char *prompt) |
17 | { | ||
18 | return bb_ask_noecho(STDIN_FILENO, 0, prompt); | ||
19 | } | ||
20 | char* FAST_FUNC bb_ask_noecho(const int fd, int timeout, const char *prompt) | ||
21 | { | 17 | { |
22 | /* Was static char[BIGNUM] */ | 18 | #define MAX_LINE 0xfff |
23 | enum { sizeof_passwd = 128 }; | ||
24 | |||
25 | char *passwd; | ||
26 | char *ret; | 19 | char *ret; |
27 | int i; | 20 | int i; |
28 | struct sigaction sa, oldsa; | 21 | struct sigaction sa, oldsa; |
@@ -37,7 +30,17 @@ char* FAST_FUNC bb_ask_noecho(const int fd, int timeout, const char *prompt) | |||
37 | 30 | ||
38 | tcgetattr(fd, &oldtio); | 31 | tcgetattr(fd, &oldtio); |
39 | tio = oldtio; | 32 | tio = oldtio; |
40 | /* Switch off echo */ | 33 | /* Switch off echo. ECHOxyz meaning: |
34 | * ECHO echo input chars | ||
35 | * ECHOE echo BS-SP-BS on erase character | ||
36 | * ECHOK echo kill char specially, not as ^c (ECHOKE controls how exactly) | ||
37 | * ECHOKE erase all input via BS-SP-BS on kill char (else go to next line) | ||
38 | * ECHOCTL Echo ctrl chars as ^c (else echo verbatim: | ||
39 | * e.g. up arrow emits "ESC-something" and thus moves cursor up!) | ||
40 | * ECHONL Echo NL even if ECHO is not set | ||
41 | * ECHOPRT On erase, echo erased chars | ||
42 | * [qwe<BS><BS><BS> input looks like "qwe\ewq/" on screen] | ||
43 | */ | ||
41 | tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL); | 44 | tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL); |
42 | tcsetattr(fd, TCSANOW, &tio); | 45 | tcsetattr(fd, TCSANOW, &tio); |
43 | 46 | ||
@@ -51,20 +54,30 @@ char* FAST_FUNC bb_ask_noecho(const int fd, int timeout, const char *prompt) | |||
51 | alarm(timeout); | 54 | alarm(timeout); |
52 | } | 55 | } |
53 | 56 | ||
54 | passwd = auto_string(xmalloc(sizeof_passwd)); | 57 | ret = NULL; |
55 | ret = passwd; | ||
56 | i = 0; | 58 | i = 0; |
57 | while (1) { | 59 | while (1) { |
58 | int r = read(fd, &ret[i], 1); | 60 | int r; |
61 | |||
62 | /* User input is uber-slow, no need to optimize reallocs. | ||
63 | * Grow it on every char. | ||
64 | */ | ||
65 | ret = xrealloc(ret, i + 2); | ||
66 | r = read(fd, &ret[i], 1); | ||
67 | |||
59 | if ((i == 0 && r == 0) /* EOF (^D) with no password */ | 68 | if ((i == 0 && r == 0) /* EOF (^D) with no password */ |
60 | || r < 0 /* read is interrupted by timeout or ^C */ | 69 | || r < 0 /* read is interrupted by timeout or ^C */ |
61 | ) { | 70 | ) { |
71 | ret[i] = '\0'; /* paranoia */ | ||
72 | nuke_str(ret); /* paranoia */ | ||
73 | free(ret); | ||
62 | ret = NULL; | 74 | ret = NULL; |
63 | break; | 75 | break; |
64 | } | 76 | } |
77 | |||
65 | if (r == 0 /* EOF */ | 78 | if (r == 0 /* EOF */ |
66 | || ret[i] == '\r' || ret[i] == '\n' /* EOL */ | 79 | || ret[i] == '\r' || ret[i] == '\n' /* EOL */ |
67 | || ++i == sizeof_passwd-1 /* line limit */ | 80 | || ++i == MAX_LINE /* line limit */ |
68 | ) { | 81 | ) { |
69 | ret[i] = '\0'; | 82 | ret[i] = '\0'; |
70 | break; | 83 | break; |
@@ -80,3 +93,7 @@ char* FAST_FUNC bb_ask_noecho(const int fd, int timeout, const char *prompt) | |||
80 | fflush_all(); | 93 | fflush_all(); |
81 | return ret; | 94 | return ret; |
82 | } | 95 | } |
96 | char* FAST_FUNC bb_ask_noecho_stdin(const char *prompt) | ||
97 | { | ||
98 | return bb_ask_noecho(STDIN_FILENO, 0, prompt); | ||
99 | } | ||