Hi Antti,

Antti Harri wrote on Tue, Feb 02, 2010 at 07:31:57PM +0200:

> xargs' -L switch isn't working when using -0 flag.

After checking POSIX.1 (2008), i conclude that our implementation and
manual are correct in this respect.  The -L option is concerned
with "lines of arguments from standard input".  ASCII nul characters
do not delimit lines.

Also, be careful not to confuse -L with -n.

> Here's a demonstration:
> 
> $ cd /tmp/
> $ mkdir foo bar
> $ find /tmp/foo /tmp/bar | xargs -L 1 echo
> /tmp/foo
> /tmp/bar
> $ find /tmp/foo /tmp/bar -print0 | xargs -0 -L 1 echo
> /tmp/foo /tmp/bar

Fine, that's correct behaviour.  Telling find(1) to -print0,
you put the output on one single line, like this:

  $ printf "/tmp/foo\0/tmp/bar\0" | xargs -0 -L1      
  /tmp/foo /tmp/bar

Your patch breaks X/Open System Interfaces (XSI) compatibility.
Arguably, that's not terribly important because -0 is a non-standard
extension anyway.  But still, even if we didn't care about the
standard, i don't see the point of changing this patch.
As it is now, -L with and without -0 harmonize very well:

  schwa...@gini $ printf "a b\nc" | xargs -L1
  a b
  c
  schwa...@gini $ printf "a\0b\nc" | xargs -0 -L1
  a b
  c

So, xargs -0 treats '\0' just like plain xargs treats the blank:
delimiting an argument, but not a line.


In addition to that, your patch is wrong because it treats '\0'
as an end-of-line indicator even when -0 is not in effect.


> Tested also on OS X and Linux and they print two lines with -0.

So you might wish to file bug reports with these operating systems.

Yours,
  Ingo


> Index: xargs.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/xargs/xargs.c,v
> retrieving revision 1.26
> diff -u -r1.26 xargs.c
> --- xargs.c   27 Oct 2009 23:59:50 -0000      1.26
> +++ xargs.c   2 Feb 2010 16:45:26 -0000
> @@ -259,6 +259,7 @@
>                       goto addch;
>               goto arg2;
>       case '\0':
> +             count++;
>               if (zflag)
>                       goto arg2;
>               goto addch;

Reply via email to