From dcd8df258a402658ad1fdc018a245b06a610ca8d Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 1 Jul 2025 09:47:14 +0100 Subject: shell: improve bash compatibility of read built-in Make the read built-in more compatible with bash: - Return an exit code of 142 on timeout. - When the timeout expires before a newline is detected in the input bash captures the partial input. This behaviour is new since bash version 4.4. BusyBox shells had the pre-4.4 behaviour where the input was lost. Update the tests to suit and fix a couple of compiler errors in the testsuite. function old new delta builtin_read 154 174 +20 readcmd 213 228 +15 shell_builtin_read 1364 1370 +6 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 3/0 up/down: 41/0) Total: 41 bytes Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- shell/shell_common.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'shell/shell_common.c') diff --git a/shell/shell_common.c b/shell/shell_common.c index e5c2cefb3..9a03f7265 100644 --- a/shell/shell_common.c +++ b/shell/shell_common.c @@ -204,8 +204,8 @@ shell_builtin_read(struct builtin_read_params *params) * 32-bit unix time wrapped (year 2038+). */ if (timeout <= 0) { /* already late? */ - retval = (const char *)(uintptr_t)1; - goto ret; + retval = (const char *)(uintptr_t)2; + break; } } @@ -217,8 +217,12 @@ shell_builtin_read(struct builtin_read_params *params) pfd[0].events = POLLIN; //TODO race with a signal arriving just before the poll! if (poll(pfd, 1, timeout) <= 0) { - /* timed out, or EINTR */ + /* timed out, or some error */ err = errno; + if (!err) { + retval = (const char *)(uintptr_t)2; + break; + } retval = (const char *)(uintptr_t)1; goto ret; } -- cgit v1.2.3-55-g6feb