From 8e699173370d68262273390e1b631300d9c5017c Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Wed, 12 Jul 2023 11:52:06 +0100 Subject: ash: fix 'read' shell built-in (1) Consider this test case: { echo -n te; sleep 3; echo st; } | (read -t 1 x; echo "$x") - bash echoes "te" after 1 second. - Upstream BusyBox echoes an empty "$x" after 1 second. - busybox-w32 echoes an empty "$x" after 3 seconds. The delayed echo in busybox-w32 arises because the 'read' shell built-in omits the code to poll for input. Rearrange the code so that polling takes place. This doesn't address the difference between BusyBox and bash. Costs 48-64 bytes. --- shell/shell_common.c | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/shell/shell_common.c b/shell/shell_common.c index 99e56a050..657ee9969 100644 --- a/shell/shell_common.c +++ b/shell/shell_common.c @@ -195,7 +195,6 @@ shell_builtin_read(struct builtin_read_params *params) } } -#if !ENABLE_PLATFORM_MINGW32 /* We must poll even if timeout is -1: * we want to be interrupted if signal arrives, * regardless of SA_RESTART-ness of that signal! @@ -209,13 +208,7 @@ shell_builtin_read(struct builtin_read_params *params) retval = (const char *)(uintptr_t)1; goto ret; } - if (read(fd, &buffer[bufpos], 1) != 1) { - err = errno; - retval = (const char *)(uintptr_t)1; - break; - } -#else - errno = 0; +#if ENABLE_PLATFORM_MINGW32 if (isatty(fd)) { int64_t key; @@ -245,15 +238,13 @@ shell_builtin_read(struct builtin_read_params *params) /* echo input if not in silent mode */ console_write(buffer + bufpos, 1); } - } - else { - if (read(fd, &buffer[bufpos], 1) != 1) { - err = errno; - retval = (const char *)(uintptr_t)1; - break; - } - } + } else #endif + if (read(fd, &buffer[bufpos], 1) != 1) { + err = errno; + retval = (const char *)(uintptr_t)1; + break; + } c = buffer[bufpos]; #if ENABLE_PLATFORM_MINGW32 -- cgit v1.2.3-55-g6feb