diff options
author | Ron Yorston <rmy@pobox.com> | 2023-07-12 11:52:06 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2023-07-12 11:52:06 +0100 |
commit | 8e699173370d68262273390e1b631300d9c5017c (patch) | |
tree | fd99ba45187323847b29b7bdb3ee5924cc851d25 | |
parent | 2a4b086d4848616306f97f6378e0f10a48d41929 (diff) | |
download | busybox-w32-8e699173370d68262273390e1b631300d9c5017c.tar.gz busybox-w32-8e699173370d68262273390e1b631300d9c5017c.tar.bz2 busybox-w32-8e699173370d68262273390e1b631300d9c5017c.zip |
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.
-rw-r--r-- | shell/shell_common.c | 23 |
1 files 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) | |||
195 | } | 195 | } |
196 | } | 196 | } |
197 | 197 | ||
198 | #if !ENABLE_PLATFORM_MINGW32 | ||
199 | /* We must poll even if timeout is -1: | 198 | /* We must poll even if timeout is -1: |
200 | * we want to be interrupted if signal arrives, | 199 | * we want to be interrupted if signal arrives, |
201 | * regardless of SA_RESTART-ness of that signal! | 200 | * regardless of SA_RESTART-ness of that signal! |
@@ -209,13 +208,7 @@ shell_builtin_read(struct builtin_read_params *params) | |||
209 | retval = (const char *)(uintptr_t)1; | 208 | retval = (const char *)(uintptr_t)1; |
210 | goto ret; | 209 | goto ret; |
211 | } | 210 | } |
212 | if (read(fd, &buffer[bufpos], 1) != 1) { | 211 | #if ENABLE_PLATFORM_MINGW32 |
213 | err = errno; | ||
214 | retval = (const char *)(uintptr_t)1; | ||
215 | break; | ||
216 | } | ||
217 | #else | ||
218 | errno = 0; | ||
219 | if (isatty(fd)) { | 212 | if (isatty(fd)) { |
220 | int64_t key; | 213 | int64_t key; |
221 | 214 | ||
@@ -245,15 +238,13 @@ shell_builtin_read(struct builtin_read_params *params) | |||
245 | /* echo input if not in silent mode */ | 238 | /* echo input if not in silent mode */ |
246 | console_write(buffer + bufpos, 1); | 239 | console_write(buffer + bufpos, 1); |
247 | } | 240 | } |
248 | } | 241 | } else |
249 | else { | ||
250 | if (read(fd, &buffer[bufpos], 1) != 1) { | ||
251 | err = errno; | ||
252 | retval = (const char *)(uintptr_t)1; | ||
253 | break; | ||
254 | } | ||
255 | } | ||
256 | #endif | 242 | #endif |
243 | if (read(fd, &buffer[bufpos], 1) != 1) { | ||
244 | err = errno; | ||
245 | retval = (const char *)(uintptr_t)1; | ||
246 | break; | ||
247 | } | ||
257 | 248 | ||
258 | c = buffer[bufpos]; | 249 | c = buffer[bufpos]; |
259 | #if ENABLE_PLATFORM_MINGW32 | 250 | #if ENABLE_PLATFORM_MINGW32 |