date and time created 88/10/18 22:07:11 by bostic
[unix-history] / usr / src / bin / mv / mv.c
index a4f18bf..69801c4 100644 (file)
@@ -8,11 +8,11 @@
 char copyright[] =
 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  All rights reserved.\n";
 char copyright[] =
 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  All rights reserved.\n";
-#endif not lint
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)mv.c       5.2 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)mv.c       5.7 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  * mv file1 file2
 
 /*
  * mv file1 file2
@@ -20,11 +20,9 @@ static char sccsid[] = "@(#)mv.c     5.2 (Berkeley) %G%";
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/time.h>
-
+#include <sys/file.h>
 #include <stdio.h>
 #include <stdio.h>
-#include <sys/dir.h>
 #include <errno.h>
 #include <errno.h>
-#include <signal.h>
 
 #define        DELIM   '/'
 #define MODEBITS 07777
 
 #define        DELIM   '/'
 #define MODEBITS 07777
@@ -35,64 +33,49 @@ static char sccsid[] = "@(#)mv.c    5.2 (Berkeley) %G%";
 #define        ISDEV(st) \
        (((st).st_mode&S_IFMT) == S_IFCHR || ((st).st_mode&S_IFMT) == S_IFBLK)
 
 #define        ISDEV(st) \
        (((st).st_mode&S_IFMT) == S_IFCHR || ((st).st_mode&S_IFMT) == S_IFBLK)
 
-char   *sprintf();
 char   *dname();
 char   *dname();
-struct stat s1, s2;
 int    iflag = 0;      /* interactive mode */
 int    fflag = 0;      /* force overwriting */
 extern unsigned errno;
 
 main(argc, argv)
 int    iflag = 0;      /* interactive mode */
 int    fflag = 0;      /* force overwriting */
 extern unsigned errno;
 
 main(argc, argv)
-       register char *argv[];
+       register int argc;
+       register char **argv;
 {
 {
-       register i, r;
-       register char *arg;
+       extern int optind;
+       struct stat st;
+       int ch, r;
        char *dest;
 
        char *dest;
 
-       if (argc < 2)
-               goto usage;
-       while (argc > 1 && *argv[1] == '-') {
-               argc--;
-               arg = *++argv;
-
-               /*
-                * all files following a null option
-                * are considered file names
-                */
-               if (*(arg+1) == '\0')
+       while ((ch = getopt(argc, argv, "-fi")) != EOF)
+               switch((char)ch) {
+               case '-':
+                       goto endarg;
+               case 'f':
+                       fflag++;
                        break;
                        break;
-               while (*++arg != '\0') switch (*arg) {
-
                case 'i':
                        iflag++;
                        break;
                case 'i':
                        iflag++;
                        break;
-
-               case 'f':
-                       fflag++;
-                       break;
-
+               case '?':
                default:
                default:
-                       goto usage;
+                       usage();
                }
                }
-       }
-       if (argc < 3)
-               goto usage;
-       dest = argv[argc-1];
-       if (stat(dest, &s2) >= 0 && ISDIR(s2)) {
-               r = 0;
-               for (i = 1; i < argc-1; i++)
-                       r |= movewithshortname(argv[i], dest);
+endarg:        argv += optind;
+       argc -= optind;
+
+       if (argc < 2)
+               usage();
+       dest = argv[argc - 1];
+       if (stat(dest, &st) >= 0 && ISDIR(st)) {
+               for (r = 0; --argc; ++argv)
+                       r |= movewithshortname(*argv, dest);
                exit(r);
        }
                exit(r);
        }
-       if (argc > 3)
-               goto usage;
-       r = move(argv[1], argv[2]);
+       if (argc != 2)
+               usage();
+       r = move(argv[0], argv[1]);
        exit(r);
        exit(r);
-       /*NOTREACHED*/
-usage:
-       fprintf(stderr,
-"usage: mv [-if] f1 f2 or mv [-if] f1 ... fn d1 (`fn' is a file or directory)\n");
-       return (1);
 }
 
 movewithshortname(src, dest)
 }
 
 movewithshortname(src, dest)
@@ -107,7 +90,7 @@ movewithshortname(src, dest)
                        shortname);
                return (1);
        }
                        shortname);
                return (1);
        }
-       sprintf(target, "%s/%s", dest, shortname);
+       (void)sprintf(target, "%s/%s", dest, shortname);
        return (move(src, target));
 }
 
        return (move(src, target));
 }
 
@@ -115,6 +98,7 @@ move(source, target)
        char *source, *target;
 {
        int targetexists;
        char *source, *target;
 {
        int targetexists;
+       struct stat s1, s2;
 
        if (lstat(source, &s1) < 0) {
                Perror2(source, "Cannot access");
 
        if (lstat(source, &s1) < 0) {
                Perror2(source, "Cannot access");
@@ -132,19 +116,21 @@ move(source, target)
                        error("%s and %s are identical", source, target);
                        return (1);
                }
                        error("%s and %s are identical", source, target);
                        return (1);
                }
-               if (iflag && !fflag && isatty(fileno(stdin)) &&
-                   query("remove %s? ", target) == 0)
-                       return (1);
-               if (access(target, 2) < 0 && !fflag && isatty(fileno(stdin))) {
-                       if (query("override protection %o for %s? ",
-                         s2.st_mode & MODEBITS, target) == 0)
+               if (!fflag && isatty(fileno(stdin)))
+                       if (iflag) {
+                               if (!query("remove %s? ", target))
+                                       return (1);
+                       }
+                       else if (access(target, W_OK) < 0 &&
+                           !query("override protection %o for %s? ",
+                           s2.st_mode & MODEBITS, target))
                                return (1);
                                return (1);
-               }
        }
        if (rename(source, target) >= 0)
                return (0);
        if (errno != EXDEV) {
        }
        if (rename(source, target) >= 0)
                return (0);
        if (errno != EXDEV) {
-               Perror2(source, "rename");
+               Perror2(errno == ENOENT && targetexists == 0 ? target : source,
+                   "rename");
                return (1);
        }
        if (ISDIR(s1)) {
                return (1);
        }
        if (ISDIR(s1)) {
@@ -171,14 +157,14 @@ move(source, target)
                }
                symln[m] = '\0';
 
                }
                symln[m] = '\0';
 
-               m = umask(~(s1.st_mode & MODEBITS));
+               (void) umask(~(s1.st_mode & MODEBITS));
                if (symlink(symln, target) < 0) {
                        Perror(target);
                        return (1);
                }
                if (symlink(symln, target) < 0) {
                        Perror(target);
                        return (1);
                }
-               (void) umask(m);
                goto cleanup;
        }
                goto cleanup;
        }
+       (void) umask(0);
        if (ISDEV(s1)) {
                struct timeval tv[2];
 
        if (ISDEV(s1)) {
                struct timeval tv[2];
 
@@ -290,8 +276,8 @@ Perror(s)
        char *s;
 {
        char buf[MAXPATHLEN + 10];
        char *s;
 {
        char buf[MAXPATHLEN + 10];
-       
-       sprintf(buf, "mv: %s", s);
+
+       (void)sprintf(buf, "mv: %s", s);
        perror(buf);
 }
 
        perror(buf);
 }
 
@@ -300,6 +286,12 @@ Perror2(s1, s2)
 {
        char buf[MAXPATHLEN + 20];
 
 {
        char buf[MAXPATHLEN + 20];
 
-       sprintf(buf, "mv: %s: %s", s1, s2);
+       (void)sprintf(buf, "mv: %s: %s", s1, s2);
        perror(buf);
 }
        perror(buf);
 }
+
+usage()
+{
+       fputs("usage: mv [-if] file1 file2 or mv [-if] file/directory ... directory\n", stderr);
+       exit(1);
+}