tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: bin/44246: enhance mv to work better with xargs



Eric Schnoebelen writes:
- 
- David Holland writes:
- - On Sat, Jan 12, 2013 at 08:21:59PM -0600, Eric Schnoebelen wrote:
- -  > -        for (rval = 0; --argc; ++argv) {
- -  > +        for (rval = 0; argc--; argv++) {
- - 
- - Can you change that to a normal for (i=1; i<argc; i++) type of loop?
- 
- Sure, I was just going for the minimal number of changes..

And now with the updated patch file.

--
Eric Schnoebelen                eric%cirr.com@localhost         
http://www.cirr.com
        "...I knew I was working way too hard when I started having 
                nightmares in FORTRAN" -- Brent Chapman
? bin/mv/.gdbinit
? bin/mv/mv
? bin/mv/mv.html1
? bin/mv/qq
Index: bin/mv/mv.1
===================================================================
RCS file: /cvsroot/src/bin/mv/mv.1,v
retrieving revision 1.26
diff -b -u -w -r1.26 mv.1
--- bin/mv/mv.1 22 Mar 2012 07:58:17 -0000      1.26
+++ bin/mv/mv.1 13 Jan 2013 03:57:04 -0000
@@ -45,6 +45,9 @@
 .Nm
 .Op Fl fiv
 .Ar source ... directory
+.Nm
+.Op Fl fiv
+.Ar -t directory source ...
 .Sh DESCRIPTION
 In its first form, the
 .Nm
@@ -56,7 +59,7 @@
 This form is assumed when the last operand does not name an already
 existing directory.
 .Pp
-In its second form,
+In its second and third forms,
 .Nm
 moves each file named by a
 .Ar source
@@ -83,6 +86,9 @@
 Cause
 .Nm
 to be verbose, showing files as they are processed.
+.It Fl t 
+Causes the immediately following argument as the destination, instead of
+the final argument.
 .El
 .Pp
 The last of any
@@ -142,5 +148,7 @@
 .Pp
 The
 .Fl v
-option is an extension to
+and
+.Fl t
+options are extensions to
 .St -p1003.2 .
Index: bin/mv/mv.c
===================================================================
RCS file: /cvsroot/src/bin/mv/mv.c,v
retrieving revision 1.43
diff -b -u -w -r1.43 mv.c
--- bin/mv/mv.c 29 Aug 2011 14:46:54 -0000      1.43
+++ bin/mv/mv.c 13 Jan 2013 03:57:04 -0000
@@ -76,16 +76,17 @@
 int
 main(int argc, char *argv[])
 {
-       int ch, len, rval;
+       int ch, len, rval, i;
        char *p, *endp;
        struct stat sb;
        char path[MAXPATHLEN + 1];
+       char *target = NULL;
        size_t baselen;
 
        setprogname(argv[0]);
        (void)setlocale(LC_ALL, "");
 
-       while ((ch = getopt(argc, argv, "ifv")) != -1)
+       while ((ch = getopt(argc, argv, "ifvt:")) != -1)
                switch (ch) {
                case 'i':
                        fflg = 0;
@@ -98,13 +99,21 @@
                case 'v':
                        vflg = 1;
                        break;
+               case 't':
+                       target = optarg;
+                       break;
                default:
                        usage();
                }
        argc -= optind;
        argv += optind;
 
-       if (argc < 2)
+       if (target == NULL) {
+           target = argv[argc - 1];
+           argc--;
+       }
+
+       if (argc < 1)
                usage();
 
        stdin_ok = isatty(STDIN_FILENO);
@@ -113,27 +122,28 @@
         * If the stat on the target fails or the target isn't a directory,
         * try the move.  More than 2 arguments is an error in this case.
         */
-       if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
-               if (argc > 2)
+       if (stat(target, &sb) || !S_ISDIR(sb.st_mode)) {
+               if (argc > 1)
                        usage();
-               exit(do_move(argv[0], argv[1]));
+               exit(do_move(argv[0], target));
        }
 
        /* It's a directory, move each file into it. */
-       baselen = strlcpy(path, argv[argc - 1], sizeof(path));
+       baselen = strlcpy(path, target, sizeof(path));
        if (baselen >= sizeof(path))
-               errx(1, "%s: destination pathname too long", argv[argc - 1]);
+               errx(1, "%s: destination pathname too long", target);
        endp = &path[baselen];
        if (!baselen || *(endp - 1) != '/') {
                *endp++ = '/';
                ++baselen;
        }
-       for (rval = 0; --argc; ++argv) {
-               p = *argv + strlen(*argv) - 1;
-               while (*p == '/' && p != *argv)
+       rval = 0;
+       for (i = 0; i < argc; i++) {
+               p = argv[i] + strlen(argv[i]) - 1;
+               while (*p == '/' && p != argv[i])
                        *p-- = '\0';
-               if ((p = strrchr(*argv, '/')) == NULL)
-                       p = *argv;
+               if ((p = strrchr(argv[i], '/')) == NULL)
+                       p = argv[i];
                else
                        ++p;
 
@@ -142,7 +152,7 @@
                        rval = 1;
                } else {
                        memmove(endp, p, len + 1);
-                       if (do_move(*argv, path))
+                       if (do_move(argv[i], path))
                                rval = 1;
                }
        }
@@ -385,8 +395,9 @@
 usage(void)
 {
        (void)fprintf(stderr, "usage: %s [-fiv] source target\n"
-           "       %s [-fiv] source ... directory\n", getprogname(),
-           getprogname());
+           "       %s [-fiv] source ... directory\n"
+           "       %s [-fiv] -t directory source ...\n", getprogname(),
+           getprogname(), getprogname());
        exit(1);
        /* NOTREACHED */
 }


Home | Main Index | Thread Index | Old Index