[PATCH] shell: more read built-in tweaks

Ron Yorston rmy at pobox.com
Fri Aug 15 14:20:57 UTC 2025


Further to commit dcd8df258 (shell: improve bash compatibility of
read built-in):

- Check explicitly for a timeout rather than implicitly via non-
  zero errno.

- If poll() fails attempt to process any data collected so far,
  as is done in all other cases of failure.

function                                             old     new   delta
shell_builtin_read                                  1360    1340     -20
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-20)             Total: -20 bytes

Signed-off-by: Ron Yorston <rmy at pobox.com>
---
 shell/shell_common.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/shell/shell_common.c b/shell/shell_common.c
index 754fef34b..552c1ece0 100644
--- a/shell/shell_common.c
+++ b/shell/shell_common.c
@@ -191,7 +191,7 @@ shell_builtin_read(struct builtin_read_params *params)
 	delim = params->opt_d ? params->opt_d[0] : '\n';
 	do {
 		char c;
-		int timeout;
+		int timeout, ret;
 
 		if ((bufpos & 0xff) == 0)
 			buffer = xrealloc(buffer, bufpos + 0x101);
@@ -217,15 +217,15 @@ shell_builtin_read(struct builtin_read_params *params)
 		pfd->events = POLLIN;
 
 		/* test bb_got_signal, then poll(), atomically wrt signals */
-		if (check_got_signal_and_poll(pfd, timeout) <= 0) {
+		if ((ret = check_got_signal_and_poll(pfd, timeout)) <= 0) {
 			/* timed out, or some error */
-			err = errno;
-			if (!err) { /* timed out */
+			if (ret == 0) { /* timed out */
 				retval = (const char *)(uintptr_t)2;
 				break;
 			}
+			err = errno;
 			retval = (const char *)(uintptr_t)1;
-			goto ret;
+			break;
 		}
 		if (read(fd, &buffer[bufpos], 1) != 1) {
 			err = errno;
@@ -332,7 +332,6 @@ shell_builtin_read(struct builtin_read_params *params)
 		params->setvar("REPLY", buffer);
 	}
 
- ret:
 	free(buffer);
 	if (read_flags & BUILTIN_READ_SILENT)
 		tcsetattr(fd, TCSANOW, &old_tty);
-- 
2.50.1



More information about the busybox mailing list