add getopt for "--" capability
[unix-history] / usr / src / bin / cp / cp.c
index 26073af..98872f0 100644 (file)
@@ -1,6 +1,18 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1983 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)cp.c        4.10 %G%";
-#endif
+static char sccsid[] = "@(#)cp.c       4.14 (Berkeley) %G%";
+#endif not lint
 
 /*
  * cp
 
 /*
  * cp
@@ -35,8 +47,10 @@ main(argc, argv)
                case 'r':
                        rflag++; break;
 
                case 'r':
                        rflag++; break;
 
-               case 'p':       /* preserve mtimes and atimes */
-                       pflag++; break;
+               case 'p':       /* preserve mtimes, atimes, and modes */
+                       pflag++;
+                       (void) umask(0);
+                       break;
 
                default:
                        goto usage;
 
                default:
                        goto usage;
@@ -57,15 +71,18 @@ main(argc, argv)
        exit(rc);
 usage:
        fprintf(stderr,
        exit(rc);
 usage:
        fprintf(stderr,
-           "Usage: cp [-ip] f1 f2; or: cp [-irp] f1 ... fn d2");
+           "Usage: cp [-ip] f1 f2; or: cp [-irp] f1 ... fn d2\n");
        exit(1);
 }
 
        exit(1);
 }
 
+                       /* I/O buffer; guarantee long-word alignment */
+static char    buf[MAXBSIZE];
+
 copy(from, to)
        char *from, *to;
 {
 copy(from, to)
        char *from, *to;
 {
-       int fold, fnew, n;
-       char *last, destname[MAXPATHLEN + 1], buf[MAXBSIZE];
+       int fold, fnew, n, exists;
+       char *last, destname[MAXPATHLEN + 1];
        struct stat stfrom, stto;
 
        fold = open(from, 0);
        struct stat stfrom, stto;
 
        fold = open(from, 0);
@@ -91,17 +108,24 @@ copy(from, to)
                to = destname;
        }
        if (rflag && (stfrom.st_mode&S_IFMT) == S_IFDIR) {
                to = destname;
        }
        if (rflag && (stfrom.st_mode&S_IFMT) == S_IFDIR) {
+               int fixmode = 0;        /* cleanup mode after rcopy */
+
                (void) close(fold);
                if (stat(to, &stto) < 0) {
                (void) close(fold);
                if (stat(to, &stto) < 0) {
-                       if (mkdir(to, stfrom.st_mode & 07777) < 0) {
+                       if (mkdir(to, (stfrom.st_mode & 07777) | 0700) < 0) {
                                Perror(to);
                                return (1);
                        }
                                Perror(to);
                                return (1);
                        }
+                       fixmode = 1;
                } else if ((stto.st_mode&S_IFMT) != S_IFDIR) {
                        fprintf(stderr, "cp: %s: Not a directory.\n", to);
                        return (1);
                } else if ((stto.st_mode&S_IFMT) != S_IFDIR) {
                        fprintf(stderr, "cp: %s: Not a directory.\n", to);
                        return (1);
-               }
-               return (rcopy(from, to));
+               } else if (pflag)
+                       fixmode = 1;
+               n = rcopy(from, to);
+               if (fixmode)
+                       (void) chmod(to, stfrom.st_mode & 07777);
+               return (n);
        }
 
        if ((stfrom.st_mode&S_IFMT) == S_IFDIR)
        }
 
        if ((stfrom.st_mode&S_IFMT) == S_IFDIR)
@@ -109,7 +133,8 @@ copy(from, to)
                        "cp: %s: Is a directory (copying as plain file).\n",
                                from);
 
                        "cp: %s: Is a directory (copying as plain file).\n",
                                from);
 
-       if (stat(to, &stto) >= 0) {
+       exists = stat(to, &stto) == 0;
+       if (exists) {
                if (stfrom.st_dev == stto.st_dev &&
                   stfrom.st_ino == stto.st_ino) {
                        fprintf(stderr,
                if (stfrom.st_dev == stto.st_dev &&
                   stfrom.st_ino == stto.st_ino) {
                        fprintf(stderr,
@@ -136,6 +161,9 @@ copy(from, to)
                Perror(to);
                (void) close(fold); return(1);
        }
                Perror(to);
                (void) close(fold); return(1);
        }
+       if (exists && pflag)
+               (void) fchmod(fnew, stfrom.st_mode & 07777);
+                       
        for (;;) {
                n = read(fold, buf, sizeof buf);
                if (n == 0)
        for (;;) {
                n = read(fold, buf, sizeof buf);
                if (n == 0)