aboutsummaryrefslogtreecommitdiff
path: root/shell/shell_common.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-04-11 17:18:34 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2018-04-11 17:18:34 +0200
commit44257ad5d0790a846423c9ef69a50049366b4578 (patch)
tree8cb08256360770c85458b874e7489e5ba3183dd8 /shell/shell_common.c
parent9678636911b39a7adf9b51d5b625cf4dc7e4ac81 (diff)
downloadbusybox-44257ad5d0790a846423c9ef69a50049366b4578.tar.gz
hush: fix IFS handling in read
$ echo "X:Y:" | (IFS=": " read x y; echo "|$x|$y|") |X|Y| $ echo "X:Y : " | (IFS=": " read x y; echo "|$x|$y|") |X|Y| function old new delta shell_builtin_read 1320 1426 +106 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell/shell_common.c')
-rw-r--r--shell/shell_common.c37
1 files changed, 36 insertions, 1 deletions
diff --git a/shell/shell_common.c b/shell/shell_common.c
index 9e58ee4fe..0a07296f3 100644
--- a/shell/shell_common.c
+++ b/shell/shell_common.c
@@ -274,9 +274,44 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
if (argv[0]) {
/* Remove trailing space $IFS chars */
- while (--bufpos >= 0 && isspace(buffer[bufpos]) && strchr(ifs, buffer[bufpos]) != NULL)
+ while (--bufpos >= 0
+ && isspace(buffer[bufpos])
+ && strchr(ifs, buffer[bufpos]) != NULL
+ ) {
continue;
+ }
buffer[bufpos + 1] = '\0';
+
+ /* Last variable takes the entire remainder with delimiters
+ * (sans trailing whitespace $IFS),
+ * but ***only "if there are fewer vars than fields"(c)***!
+ * The "X:Y:" case below: there are two fields,
+ * and therefore last delimiter (:) is eaten:
+ * IFS=": "
+ * echo "X:Y:Z:" | (read x y; echo "|$x|$y|") # |X|Y:Z:|
+ * echo "X:Y:Z" | (read x y; echo "|$x|$y|") # |X|Y:Z|
+ * echo "X:Y:" | (read x y; echo "|$x|$y|") # |X|Y|, not |X|Y:|
+ * echo "X:Y : " | (read x y; echo "|$x|$y|") # |X|Y|
+ */
+ if (bufpos >= 0
+ && strchr(ifs, buffer[bufpos]) != NULL
+ ) {
+ /* There _is_ a non-whitespace IFS char */
+ /* Skip whitespace IFS char before it */
+ while (--bufpos >= 0
+ && isspace(buffer[bufpos])
+ && strchr(ifs, buffer[bufpos]) != NULL
+ ) {
+ continue;
+ }
+ /* Are there $IFS chars? */
+ if (strcspn(buffer, ifs) >= ++bufpos) {
+ /* No: last var takes one field, not more */
+ /* So, drop trailing IFS delims */
+ buffer[bufpos] = '\0';
+ }
+ }
+
/* Use the remainder as a value for the next variable */
setvar(*argv, buffer);
/* Set the rest to "" */