Jordan Hubbard <[EMAIL PROTECTED]> writes:
> > And to come back on topic:  Portable scripts also should
> > _not_ assume that there are no limits on the length of
> > shell commands.  On the other hand, portable scripts can
> > legitimately assume that xargs supports -i and -I, which
> > ours doesn't.
> 
> Agreed on both counts.  I guess we should fix that.

I don't have a copy of SuSv2 or anything else that defines -I and -i,
but from what I can gather, -i is the same as "-I {}" and -I allows
things like this:

        dima@spike% ./xargs -I [] echo CMD LINE [] ARGS < test
        CMD LINE this is the contents of the test file ARGS

        dima@spike% ./xargs -I [] echo CMD [] LINE ARGS < test
        CMD this is the contents of the test file LINE ARGS

        dima@spike% ./xargs -I [] echo [] CMD LINE ARGS < test
        this is the contents of the test file CMD LINE ARGS

Does that mean everyone is blind and missed my arrogant cross-post of
the amazingly short patch to do this, or are we just interested in
discussing it and not testing the implementation? ;-)

FWIW, I'm not sure the patch is entirely correct; xargs' processing of
this stuff looks like black magic.  It works, but I'm not sure if I
failed to cater to some other weird assumptions it makes.  This is why
it'd help if someone would at least look at it.

Thanks,

                                        Dima Dorfman
                                        [EMAIL PROTECTED]


Index: xargs.c
===================================================================
RCS file: /st/src/FreeBSD/src/usr.bin/xargs/xargs.c,v
retrieving revision 1.9
diff -u -r1.9 xargs.c
--- xargs.c     1999/08/28 01:07:50     1.9
+++ xargs.c     2001/04/21 20:15:27
@@ -73,6 +73,8 @@
        int cnt, indouble, insingle, nargs, nflag, nline, xflag, wasquoted;
        char **av, *argp, **ep = env;
        long arg_max;
+       int apargs = 0;
+       char **avv, *replstr = NULL;
 
        /*
         * POSIX.2 limits the exec line length to ARG_MAX - 2K.  Running that
@@ -96,8 +98,14 @@
                nline -= strlen(*ep++) + 1 + sizeof(*ep);
        }
        nflag = xflag = wasquoted = 0;
-       while ((ch = getopt(argc, argv, "0n:s:tx")) != -1)
+       while ((ch = getopt(argc, argv, "0I:in:s:tx")) != -1)
                switch(ch) {
+               case 'I':
+                       replstr = optarg;
+                       break;
+               case 'i':
+                       replstr = "{}";
+                       break;
                case 'n':
                        nflag = 1;
                        if ((nargs = atoi(optarg)) <= 0)
@@ -144,6 +152,13 @@
        else {
                cnt = 0;
                do {
+                       if (replstr && strcmp(*argv, replstr) == 0) {
+                               apargs = 1;
+                               *argv++;
+                               for (avv = argv; *avv; *avv++)
+                                       cnt += strlen(*avv) + 1;
+                               break;
+                       }
                        cnt += strlen(*bxp++ = *argv) + 1;
                } while (*++argv);
        }
@@ -211,6 +226,8 @@
                        if (xp == exp || p > ebp || ch == EOF) {
                                if (xflag && xp != exp && p > ebp)
                                        errx(1, "insufficient space for arguments");
+                               for (avv = argv; apargs && *avv; *avv++)
+                                       strlen(*xp++ = *avv) + 1;
                                *xp = NULL;
                                run(av);
                                if (ch == EOF)
@@ -253,6 +270,8 @@
                        if (xflag)
                                errx(1, "insufficient space for arguments");
 
+                       for (avv = argv; apargs && *avv; *avv++)
+                               strlen(*xp++ = *avv) + 1;
                        *xp = NULL;
                        run(av);
                        xp = bxp;


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-current" in the body of the message

Reply via email to