shell: Fix "read -d ''" behavior

With bash's read builtin it is possible to read from a file (e.g.
device-tree) until the first '\0' character:

IFS= read -r -d '' VARIABLE < file

In busybox ash the -d extension is also implemented, but checking the
read character for '\0' has to be performed after comparing with the
delimiter.

Signed-off-by: Christian Eggers <ceggers@arri.de>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Christian Eggers 2020-06-29 17:57:24 +02:00 committed by Denys Vlasenko
parent a088da4476
commit 39925026f6
5 changed files with 6 additions and 2 deletions

View File

@ -0,0 +1 @@
test

View File

@ -0,0 +1 @@
printf 'test\0zest\n' | (read -d '' reply; echo "$reply")

View File

@ -0,0 +1 @@
test

View File

@ -0,0 +1 @@
printf 'test\0zest\n' | (read -d '' reply; echo "$reply")

View File

@ -209,8 +209,6 @@ shell_builtin_read(struct builtin_read_params *params)
}
c = buffer[bufpos];
if (c == '\0')
continue;
if (!(read_flags & BUILTIN_READ_RAW)) {
if (backslash) {
backslash = 0;
@ -225,6 +223,8 @@ shell_builtin_read(struct builtin_read_params *params)
}
if (c == delim) /* '\n' or -d CHAR */
break;
if (c == '\0')
continue;
/* $IFS splitting. NOT done if we run "read"
* without variable names (bash compat).