diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-04-11 17:18:34 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-04-11 17:18:34 +0200 |
commit | 44257ad5d0790a846423c9ef69a50049366b4578 (patch) | |
tree | 8cb08256360770c85458b874e7489e5ba3183dd8 /shell/shell_common.c | |
parent | 9678636911b39a7adf9b51d5b625cf4dc7e4ac81 (diff) | |
download | busybox-w32-44257ad5d0790a846423c9ef69a50049366b4578.tar.gz busybox-w32-44257ad5d0790a846423c9ef69a50049366b4578.tar.bz2 busybox-w32-44257ad5d0790a846423c9ef69a50049366b4578.zip |
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.c | 37 |
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), | |||
274 | 274 | ||
275 | if (argv[0]) { | 275 | if (argv[0]) { |
276 | /* Remove trailing space $IFS chars */ | 276 | /* Remove trailing space $IFS chars */ |
277 | while (--bufpos >= 0 && isspace(buffer[bufpos]) && strchr(ifs, buffer[bufpos]) != NULL) | 277 | while (--bufpos >= 0 |
278 | && isspace(buffer[bufpos]) | ||
279 | && strchr(ifs, buffer[bufpos]) != NULL | ||
280 | ) { | ||
278 | continue; | 281 | continue; |
282 | } | ||
279 | buffer[bufpos + 1] = '\0'; | 283 | buffer[bufpos + 1] = '\0'; |
284 | |||
285 | /* Last variable takes the entire remainder with delimiters | ||
286 | * (sans trailing whitespace $IFS), | ||
287 | * but ***only "if there are fewer vars than fields"(c)***! | ||
288 | * The "X:Y:" case below: there are two fields, | ||
289 | * and therefore last delimiter (:) is eaten: | ||
290 | * IFS=": " | ||
291 | * echo "X:Y:Z:" | (read x y; echo "|$x|$y|") # |X|Y:Z:| | ||
292 | * echo "X:Y:Z" | (read x y; echo "|$x|$y|") # |X|Y:Z| | ||
293 | * echo "X:Y:" | (read x y; echo "|$x|$y|") # |X|Y|, not |X|Y:| | ||
294 | * echo "X:Y : " | (read x y; echo "|$x|$y|") # |X|Y| | ||
295 | */ | ||
296 | if (bufpos >= 0 | ||
297 | && strchr(ifs, buffer[bufpos]) != NULL | ||
298 | ) { | ||
299 | /* There _is_ a non-whitespace IFS char */ | ||
300 | /* Skip whitespace IFS char before it */ | ||
301 | while (--bufpos >= 0 | ||
302 | && isspace(buffer[bufpos]) | ||
303 | && strchr(ifs, buffer[bufpos]) != NULL | ||
304 | ) { | ||
305 | continue; | ||
306 | } | ||
307 | /* Are there $IFS chars? */ | ||
308 | if (strcspn(buffer, ifs) >= ++bufpos) { | ||
309 | /* No: last var takes one field, not more */ | ||
310 | /* So, drop trailing IFS delims */ | ||
311 | buffer[bufpos] = '\0'; | ||
312 | } | ||
313 | } | ||
314 | |||
280 | /* Use the remainder as a value for the next variable */ | 315 | /* Use the remainder as a value for the next variable */ |
281 | setvar(*argv, buffer); | 316 | setvar(*argv, buffer); |
282 | /* Set the rest to "" */ | 317 | /* Set the rest to "" */ |