diff options
author | Christian Eggers <ceggers@arri.de> | 2020-06-29 17:57:24 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-01-01 14:30:58 +0100 |
commit | 94f6d1ece0b6d023f173c5b5141b92229a3830fa (patch) | |
tree | 9d5195918ea15756501bcfccaa098f6bdc2bd2ae | |
parent | fb957125d8aa83c9df6e54292327983a51ee7ffd (diff) | |
download | busybox-w32-94f6d1ece0b6d023f173c5b5141b92229a3830fa.tar.gz busybox-w32-94f6d1ece0b6d023f173c5b5141b92229a3830fa.tar.bz2 busybox-w32-94f6d1ece0b6d023f173c5b5141b92229a3830fa.zip |
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>
-rw-r--r-- | shell/ash_test/ash-read/read_d0.right | 1 | ||||
-rwxr-xr-x | shell/ash_test/ash-read/read_d0.tests | 1 | ||||
-rw-r--r-- | shell/hush_test/hush-read/read_d0.right | 1 | ||||
-rwxr-xr-x | shell/hush_test/hush-read/read_d0.tests | 1 | ||||
-rw-r--r-- | shell/shell_common.c | 4 |
5 files changed, 6 insertions, 2 deletions
diff --git a/shell/ash_test/ash-read/read_d0.right b/shell/ash_test/ash-read/read_d0.right new file mode 100644 index 000000000..9daeafb98 --- /dev/null +++ b/shell/ash_test/ash-read/read_d0.right | |||
@@ -0,0 +1 @@ | |||
test | |||
diff --git a/shell/ash_test/ash-read/read_d0.tests b/shell/ash_test/ash-read/read_d0.tests new file mode 100755 index 000000000..630d80787 --- /dev/null +++ b/shell/ash_test/ash-read/read_d0.tests | |||
@@ -0,0 +1 @@ | |||
printf 'test\0zest\n' | (read -d '' reply; echo "$reply") | |||
diff --git a/shell/hush_test/hush-read/read_d0.right b/shell/hush_test/hush-read/read_d0.right new file mode 100644 index 000000000..9daeafb98 --- /dev/null +++ b/shell/hush_test/hush-read/read_d0.right | |||
@@ -0,0 +1 @@ | |||
test | |||
diff --git a/shell/hush_test/hush-read/read_d0.tests b/shell/hush_test/hush-read/read_d0.tests new file mode 100755 index 000000000..630d80787 --- /dev/null +++ b/shell/hush_test/hush-read/read_d0.tests | |||
@@ -0,0 +1 @@ | |||
printf 'test\0zest\n' | (read -d '' reply; echo "$reply") | |||
diff --git a/shell/shell_common.c b/shell/shell_common.c index 12c4a073c..42c4c9c97 100644 --- a/shell/shell_common.c +++ b/shell/shell_common.c | |||
@@ -209,8 +209,6 @@ shell_builtin_read(struct builtin_read_params *params) | |||
209 | } | 209 | } |
210 | 210 | ||
211 | c = buffer[bufpos]; | 211 | c = buffer[bufpos]; |
212 | if (c == '\0') | ||
213 | continue; | ||
214 | if (!(read_flags & BUILTIN_READ_RAW)) { | 212 | if (!(read_flags & BUILTIN_READ_RAW)) { |
215 | if (backslash) { | 213 | if (backslash) { |
216 | backslash = 0; | 214 | backslash = 0; |
@@ -225,6 +223,8 @@ shell_builtin_read(struct builtin_read_params *params) | |||
225 | } | 223 | } |
226 | if (c == delim) /* '\n' or -d CHAR */ | 224 | if (c == delim) /* '\n' or -d CHAR */ |
227 | break; | 225 | break; |
226 | if (c == '\0') | ||
227 | continue; | ||
228 | 228 | ||
229 | /* $IFS splitting. NOT done if we run "read" | 229 | /* $IFS splitting. NOT done if we run "read" |
230 | * without variable names (bash compat). | 230 | * without variable names (bash compat). |