xargs: restore correct behaviour of -n option
authorRon Yorston <rmy@pobox.com>
Wed, 19 Feb 2020 11:01:39 +0000 (11:01 +0000)
committerDenys Vlasenko <vda.linux@googlemail.com>
Wed, 29 Apr 2020 13:53:51 +0000 (15:53 +0200)
Since commit 1ff7002b1 (xargs: fix handling of quoted arguments, closes
11441) the -n option hasn't worked properly:

   $ echo 1 2 3 | xargs -n 1 echo
   1
   2

   3

   $

Because state is now remembered between calls to process_stdin() it's
necessary to update the state before any premature return.

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
findutils/xargs.c
testsuite/xargs.tests

index 4fb306bb879b8fb0a2f30e6fa5c8c2aba33eac25..ff04bfe7c967d88f93cc8023e5765ee665088a17 100644 (file)
@@ -315,6 +315,7 @@ static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
                        }
                }
                if (state == SPACE) {   /* word's delimiter or EOF detected */
+                       state = NORM;
                        if (q) {
                                bb_error_msg_and_die("unmatched %s quote",
                                        q == '\'' ? "single" : "double");
@@ -335,7 +336,6 @@ static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
                        if (n_max_arg == 0) {
                                goto ret;
                        }
-                       state = NORM;
                }
                if (p == buf) {
                        goto ret;
index 855b33bc2664f8da383b8939c67739eb5c216bff..159f1ff69190f151d42b2748cb66919feb128461 100755 (executable)
@@ -48,6 +48,17 @@ testing "xargs argument line too long" \
        "seq 10000 99999 | sed -e 's/^/\"/' -e 's/$/\"/' | xargs echo | grep -o 99999; echo \$?" \
        "99999\n0\n" \
        "" ""
+
+testing "xargs -n1" \
+       "xargs -n1 echo" \
+       "1\n2\n3\n4\n5\n" \
+       "" "1 2 3 4 5\n"
+
+testing "xargs -n2" \
+       "xargs -n2 echo" \
+       "1 2\n3 4\n5\n" \
+       "" "1 2 3 4 5\n"
+
 SKIP=
 
 exit $FAILCOUNT