aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-02-01 04:55:30 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2010-02-01 04:55:30 +0100
commite936c6d1c501517a68404612235c79e0a3600fad (patch)
treeebb76a682fd7d3d7fab7e3ff453320d3ef408690
parent460f8276449f0933f242c9295b241ec213bcef82 (diff)
downloadbusybox-w32-e936c6d1c501517a68404612235c79e0a3600fad.tar.gz
busybox-w32-e936c6d1c501517a68404612235c79e0a3600fad.tar.bz2
busybox-w32-e936c6d1c501517a68404612235c79e0a3600fad.zip
make echo -e "foo\nfoo" | passwd USER work
Suggested by Michael Zhu (linuxsir320 AT gmail.com) function old new delta bb_ask 333 340 +7 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--libbb/bb_askpass.c38
1 files changed, 21 insertions, 17 deletions
diff --git a/libbb/bb_askpass.c b/libbb/bb_askpass.c
index f9b918cec..bdb756659 100644
--- a/libbb/bb_askpass.c
+++ b/libbb/bb_askpass.c
@@ -30,10 +30,6 @@ char* FAST_FUNC bb_ask(const int fd, int timeout, const char *prompt)
30 struct sigaction sa, oldsa; 30 struct sigaction sa, oldsa;
31 struct termios tio, oldtio; 31 struct termios tio, oldtio;
32 32
33 if (!passwd)
34 passwd = xmalloc(sizeof_passwd);
35 memset(passwd, 0, sizeof_passwd);
36
37 tcgetattr(fd, &oldtio); 33 tcgetattr(fd, &oldtio);
38 tcflush(fd, TCIFLUSH); 34 tcflush(fd, TCIFLUSH);
39 tio = oldtio; 35 tio = oldtio;
@@ -46,7 +42,7 @@ char* FAST_FUNC bb_ask(const int fd, int timeout, const char *prompt)
46 42
47 memset(&sa, 0, sizeof(sa)); 43 memset(&sa, 0, sizeof(sa));
48 /* sa.sa_flags = 0; - no SA_RESTART! */ 44 /* sa.sa_flags = 0; - no SA_RESTART! */
49 /* SIGINT and SIGALRM will interrupt read below */ 45 /* SIGINT and SIGALRM will interrupt reads below */
50 sa.sa_handler = askpass_timeout; 46 sa.sa_handler = askpass_timeout;
51 sigaction(SIGINT, &sa, &oldsa); 47 sigaction(SIGINT, &sa, &oldsa);
52 if (timeout) { 48 if (timeout) {
@@ -56,18 +52,26 @@ char* FAST_FUNC bb_ask(const int fd, int timeout, const char *prompt)
56 52
57 fputs(prompt, stdout); 53 fputs(prompt, stdout);
58 fflush_all(); 54 fflush_all();
59 ret = NULL; 55
60 /* On timeout or Ctrl-C, read will hopefully be interrupted, 56 if (!passwd)
61 * and we return NULL */ 57 passwd = xmalloc(sizeof_passwd);
62 if (read(fd, passwd, sizeof_passwd - 1) > 0) { 58 memset(passwd, 0, sizeof_passwd);
63 ret = passwd; 59 ret = passwd;
64 i = 0; 60 i = 0;
65 /* Last byte is guaranteed to be 0 61 while (1) {
66 (read did not overwrite it) */ 62 int r = read(fd, &ret[i], 1);
67 do { 63 if (r < 0) {
68 if (passwd[i] == '\r' || passwd[i] == '\n') 64 /* read is interrupted by timeout or ^C */
69 passwd[i] = '\0'; 65 ret = NULL;
70 } while (passwd[i++]); 66 break;
67 }
68 if (r == 0 /* EOF */
69 || ret[i] == '\r' || ret[i] == '\n' /* EOL */
70 || ++i == sizeof_passwd-1 /* line limit */
71 ) {
72 ret[i] = '\0';
73 break;
74 }
71 } 75 }
72 76
73 if (timeout) { 77 if (timeout) {