When using the -0 flag, a "line" is delimited by a NUL char, not a
newline.  This fixes things like:

$ printf 'hello\00world\00' | xargs -n 1 -0 -I arg printf '>%s<\n' "arg"

Before:

    >hello world<

After:

    >hello<
    >world<

This change is consistent with the behavior of GNU xargs, FreeBSD
and NetBSD.  We appear to be the only outlier.  The diff below is
adapted from the FreeBSD change.

 - todd

Index: usr.bin/xargs/xargs.c
===================================================================
RCS file: /cvs/src/usr.bin/xargs/xargs.c,v
retrieving revision 1.31
diff -u -p -u -r1.31 xargs.c
--- usr.bin/xargs/xargs.c       9 Dec 2015 19:29:49 -0000       1.31
+++ usr.bin/xargs/xargs.c       16 Jan 2017 16:20:25 -0000
@@ -278,15 +278,22 @@ parse_input(int argc, char *argv[])
                }
                goto arg1;
        case '\0':
-               if (zflag)
+               if (zflag) {
+                       /*
+                        * Increment 'count', so that nulls will be treated
+                        * as end-of-line, as well as end-of-argument.  This
+                        * is needed so -0 works properly with -I and -L.
+                        */
+                       count++;
                        goto arg2;
+               }
                goto addch;
        case '\n':
+               if (zflag)
+                       goto addch;
                hasblank = 1;
                if (hadblank == 0)
                        count++;
-               if (zflag)
-                       goto addch;
 
                /* Quotes do not escape newlines. */
 arg1:          if (insingle || indouble)
Index: regress/usr.bin/xargs/xargs-L.sh
===================================================================
RCS file: /cvs/src/regress/usr.bin/xargs/xargs-L.sh,v
retrieving revision 1.1
diff -u -p -u -r1.1 xargs-L.sh
--- regress/usr.bin/xargs/xargs-L.sh    25 Mar 2010 01:43:47 -0000      1.1
+++ regress/usr.bin/xargs/xargs-L.sh    16 Jan 2017 16:13:12 -0000
@@ -76,18 +76,18 @@ test_xargs 'a \\\nb'     '-0'      'a \\
 test_xargs 'a\\\n b'     '-0'      'a\\\n b|'
 test_xargs 'a \\\n b'    '-0'      'a \\\n b|'
 
-test_xargs 'a b\0c'      '-0 -L 1' 'a b|c|'
-test_xargs 'a  b\0c'     '-0 -L 1' 'a  b|c|'
+test_xargs 'a b\0c'      '-0 -L 1' 'a b|\nc|'
+test_xargs 'a  b\0c'     '-0 -L 1' 'a  b|\nc|'
 test_xargs 'a\nb\0c'     '-0 -L 1' 'a\nb|\nc|'
 test_xargs 'a\n\nb\0c'   '-0 -L 1' 'a\n\nb|\nc|'
-test_xargs 'a \nb\0c'    '-0 -L 1' 'a \nb|c|'
+test_xargs 'a \nb\0c'    '-0 -L 1' 'a \nb|\nc|'
 test_xargs 'a\n b\0c'    '-0 -L 1' 'a\n b|\nc|'
-test_xargs 'a \n b\0c'   '-0 -L 1' 'a \n b|c|'
+test_xargs 'a \n b\0c'   '-0 -L 1' 'a \n b|\nc|'
 test_xargs 'a\n \nb\0c'  '-0 -L 1' 'a\n \nb|\nc|'
-test_xargs 'a \n\nb\0c'  '-0 -L 1' 'a \n\nb|c|'
+test_xargs 'a \n\nb\0c'  '-0 -L 1' 'a \n\nb|\nc|'
 
-test_xargs 'a\\ b\0c'    '-0 -L 1' 'a\\ b|c|'
-test_xargs 'a\\ \nb\0c'  '-0 -L 1' 'a\\ \nb|c|'
+test_xargs 'a\\ b\0c'    '-0 -L 1' 'a\\ b|\nc|'
+test_xargs 'a\\ \nb\0c'  '-0 -L 1' 'a\\ \nb|\nc|'
 test_xargs 'a\n\\ b\0c'  '-0 -L 1' 'a\n\\ b|\nc|'
 
 test_xargs 'a\\\nb\0c'   '-0 -L 1' 'a\\\nb|\nc|'

Reply via email to