From 47dbad5047e6bb9fb1f287245791711ceb822c96 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sat, 8 Feb 2020 08:39:35 +0000 Subject: ash: catch EOF in read builtin The read builtin didn't detect EOF so it wasn't possible to cleanly terminate a loop like: while read -r; do echo $REPLY; done Consider how Linux handles EOF (represented as ^D, though this isn't echoed): $ awk '{print}' | xxd abc^D123 ^D 00000000: 6162 6331 3233 0a abc123. Contrast with busybox-w32 on Windows (where the ^Z is echoed): $ awk '{print}' | xxd abc^Z123 ^Z 00000000: 6162 631a 3132 330a abc.123. In both cases EOF is only detected at the start of a line. On Linux EOF within a line is dropped; on Windows it's output as a literal ctrl-Z. Implement similar behaviour for the read builtin. --- shell/shell_common.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/shell_common.c b/shell/shell_common.c index af05625a4..320cd88fd 100644 --- a/shell/shell_common.c +++ b/shell/shell_common.c @@ -235,8 +235,8 @@ shell_builtin_read(struct builtin_read_params *params) int64_t key; key = read_key(fd, NULL, timeout); - if (key == 0x03 || key == -1) { - /* ^C or timeout */ + if (key == 0x03 || key == -1 || (key == 0x1a && bufpos == 0)) { + /* ^C, timeout or ^Z at start of buffer */ retval = (const char *)(uintptr_t)1; goto ret; } -- cgit v1.2.3-55-g6feb