[prev in list] [next in list] [prev in thread] [next in thread]
List: busybox
Subject: Re: [PATCH] function bb_askpass only read one line once
From: Michael Zhu <linuxsir320 () gmail ! com>
Date: 2010-01-29 5:42:20
Message-ID: 4B62753C.5040508 () gmail ! com
[Download RAW message or body]
["0001-function-bb_askpass-only-read-one-line-from-fd.patch" (text/x-patch)]
>From a4a39e3040426408371c07eef2e0b41931451e53 Mon Sep 17 00:00:00 2001
From: Michael Zhu <linuxsir320@gmail.com>
Date: Fri, 29 Jan 2010 00:27:00 -0500
Subject: [PATCH] function bb_askpass only read one line from fd
---
libbb/bb_askpass.c | 31 ++++++++++++++++++-------------
1 files changed, 18 insertions(+), 13 deletions(-)
diff --git a/libbb/bb_askpass.c b/libbb/bb_askpass.c
index f9b918c..ff02913 100644
--- a/libbb/bb_askpass.c
+++ b/libbb/bb_askpass.c
@@ -25,8 +25,9 @@ char* FAST_FUNC bb_ask(const int fd, int timeout, const char *prompt)
enum { sizeof_passwd = 128 };
static char *passwd;
- char *ret;
int i;
+ int len;
+ char ch;
struct sigaction sa, oldsa;
struct termios tio, oldtio;
@@ -56,18 +57,19 @@ char* FAST_FUNC bb_ask(const int fd, int timeout, const char *prompt)
fputs(prompt, stdout);
fflush_all();
- ret = NULL;
- /* On timeout or Ctrl-C, read will hopefully be interrupted,
- * and we return NULL */
- if (read(fd, passwd, sizeof_passwd - 1) > 0) {
- ret = passwd;
+
+ /* discards \n and \r */
+ while((len = read(fd, &ch, 1) > 0) && (ch == '\n' || ch == '\r'));
+
+ if( len > 0 )
+ {
i = 0;
- /* Last byte is guaranteed to be 0
- (read did not overwrite it) */
- do {
- if (passwd[i] == '\r' || passwd[i] == '\n')
- passwd[i] = '\0';
- } while (passwd[i++]);
+ passwd[i++] = ch;
+ while(i < sizeof_passwd - 1 && ( len = read(fd, &ch, 1) ) > 0
+ && ch != '\n' && ch != '\r'){
+ /* Last byte is guaranteed to be 0 */
+ passwd[i++] = ch;
+ }
}
if (timeout) {
@@ -78,5 +80,8 @@ char* FAST_FUNC bb_ask(const int fd, int timeout, const char *prompt)
tcsetattr_stdin_TCSANOW(&oldtio);
bb_putchar('\n');
fflush_all();
- return ret;
+
+ /* On timeout or Ctrl-C, read will hopefully be interrupted,
+ * and we return NULL */
+ return (len < 0 ? NULL : passwd);
}
--
1.6.3.3
_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic