[git commit] libbb: shrink lineedit_read_key()

Denys Vlasenko vda.linux at googlemail.com
Mon Jan 17 23:36:42 UTC 2022


commit: https://git.busybox.net/busybox/commit/?id=1e825acf8d715fe49af040cb02f9e96c26955832
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
lineedit_read_key                                    237     231      -6

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 archival/libarchive/decompress_bunzip2.c |  2 +-
 coreutils/head.c                         |  6 +++---
 editors/patch.c                          |  2 +-
 editors/patch_toybox.c                   |  2 +-
 include/libbb.h                          |  2 ++
 libbb/lineedit.c                         | 26 ++++++++++++++++----------
 libbb/read_key.c                         |  1 +
 7 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/archival/libarchive/decompress_bunzip2.c b/archival/libarchive/decompress_bunzip2.c
index 42e2b4f88..4a2b668aa 100644
--- a/archival/libarchive/decompress_bunzip2.c
+++ b/archival/libarchive/decompress_bunzip2.c
@@ -654,7 +654,7 @@ static int read_bunzip(bunzip_data *bd, char *outbuf, int len)
 				/* Subtract the 1 copy we'd output anyway to get extras */
 				--bd->writeCopies;
 			}
-		} /* for(;;) */
+		} /* for (;;) */
 
 		/* Decompression of this input block completed successfully */
 		bd->writeCRC = CRC = ~CRC;
diff --git a/coreutils/head.c b/coreutils/head.c
index 9586f869f..c7537a20e 100644
--- a/coreutils/head.c
+++ b/coreutils/head.c
@@ -76,7 +76,7 @@ print_except_N_last_bytes(FILE *fp, unsigned count)
 {
 	unsigned char *circle = xmalloc(++count);
 	unsigned head = 0;
-	for(;;) {
+	for (;;) {
 		int c;
 		c = getc(fp);
 		if (c == EOF)
@@ -105,7 +105,7 @@ print_except_N_last_lines(FILE *fp, unsigned count)
 {
 	char **circle = xzalloc((++count) * sizeof(circle[0]));
 	unsigned head = 0;
-	for(;;) {
+	for (;;) {
 		char *c;
 		c = xmalloc_fgets(fp);
 		if (!c)
@@ -127,7 +127,7 @@ print_except_N_last_lines(FILE *fp, unsigned count)
 	}
  ret:
 	head = 0;
-	for(;;) {
+	for (;;) {
 		free(circle[head++]);
 		if (head == count)
 			break;
diff --git a/editors/patch.c b/editors/patch.c
index 110176630..aebb5073e 100644
--- a/editors/patch.c
+++ b/editors/patch.c
@@ -418,7 +418,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
 	}
 
 	// Loop through the lines in the patch
-	for(;;) {
+	for (;;) {
 		char *patchline;
 
 		patchline = xmalloc_fgetline(stdin);
diff --git a/editors/patch_toybox.c b/editors/patch_toybox.c
index aebab8132..69a508b2e 100644
--- a/editors/patch_toybox.c
+++ b/editors/patch_toybox.c
@@ -441,7 +441,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
 	TT.filein = TT.fileout = -1;
 
 	// Loop through the lines in the patch
-	for(;;) {
+	for (;;) {
 		char *patchline;
 
 		patchline = get_line(TT.filepatch);
diff --git a/include/libbb.h b/include/libbb.h
index b45ce91c5..8e3b7ae8e 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1900,6 +1900,8 @@ enum {
  * (unless fd is in non-blocking mode),
  * subsequent reads will time out after a few milliseconds.
  * Return of -1 means EOF or error (errno == 0 on EOF).
+ * Nonzero errno is not preserved across the call:
+ * if there was no error, errno will be cleared to 0.
  * buffer[0] is used as a counter of buffered chars and must be 0
  * on first call.
  * timeout:
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index f76afd37d..82624757e 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -2155,7 +2155,7 @@ static int lineedit_read_key(char *read_key_buffer, int timeout)
 #endif
 
 	fflush_all();
-	while (1) {
+	for (;;) {
 		/* Wait for input. TIMEOUT = -1 makes read_key wait even
 		 * on nonblocking stdin, TIMEOUT = 50 makes sure we won't
 		 * insist on full MB_CUR_MAX buffer to declare input like
@@ -2167,24 +2167,30 @@ static int lineedit_read_key(char *read_key_buffer, int timeout)
 		 *
 		 * Note: read_key sets errno to 0 on success.
 		 */
-		do {
+		for (;;) {
 			if ((state->flags & LI_INTERRUPTIBLE) && bb_got_signal) {
 				errno = EINTR;
 				return -1;
 			}
 //FIXME: still races here with signals, but small window to poll() inside read_key
 			IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 1;)
+			/* errno = 0; - read_key does this itself */
 			ic = read_key(STDIN_FILENO, read_key_buffer, timeout);
 			IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 0;)
-		} while (!(state->flags & LI_INTERRUPTIBLE) && errno == EINTR);
+			if (errno != EINTR)
+				break;
+			if (state->flags & LI_INTERRUPTIBLE) {
+				/* LI_INTERRUPTIBLE bails out on EINTR,
+				 * but nothing really guarantees that bb_got_signal
+				 * is nonzero. Follow the least surprise principle:
+				 */
+				if (bb_got_signal == 0)
+					bb_got_signal = 255;
+				goto ret;
+			}
+		}
 
 		if (errno) {
-			/* LI_INTERRUPTIBLE can bail out with EINTR here,
-			 * but nothing really guarantees that bb_got_signal
-			 * is nonzero. Follow the least surprise principle:
-			 */
-			if (errno == EINTR && bb_got_signal == 0)
-				bb_got_signal = 255; /* something nonzero */
 #if ENABLE_UNICODE_SUPPORT
 			if (errno == EAGAIN && unicode_idx != 0)
 				goto pushback;
@@ -2251,7 +2257,7 @@ static int lineedit_read_key(char *read_key_buffer, int timeout)
 #endif
 		break;
 	}
-
+ ret:
 	return ic;
 }
 
diff --git a/libbb/read_key.c b/libbb/read_key.c
index 829ae215c..cf8ed411e 100644
--- a/libbb/read_key.c
+++ b/libbb/read_key.c
@@ -291,6 +291,7 @@ int64_t FAST_FUNC safe_read_key(int fd, char *buffer, int timeout)
 {
 	int64_t r;
 	do {
+		/* errno = 0; - read_key does this itself */
 		r = read_key(fd, buffer, timeout);
 	} while (errno == EINTR);
 	return r;


More information about the busybox-cvs mailing list