make consistent with POSIX 1003.2, draft 10. Now have two recursive
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Sat, 30 Jun 1990 07:40:58 +0000 (23:40 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Sat, 30 Jun 1990 07:40:58 +0000 (23:40 -0800)
flags, -r and -R; -r is unchanged and deprecated; -R is recursive done right.
Add ability to do FIFO's in recursive copies.  Disallow any setuid/gid bits
if either owner or group has changed.

SCCS-vsn: bin/cp/cp.c 5.20
SCCS-vsn: bin/cp/cp.1 6.10

usr/src/bin/cp/cp.1
usr/src/bin/cp/cp.c

index fa98865..afb838b 100644 (file)
@@ -3,7 +3,7 @@
 .\"
 .\" %sccs.include.redist.man%
 .\"
 .\"
 .\" %sccs.include.redist.man%
 .\"
-.\"     @(#)cp.1       6.9 (Berkeley) %G%
+.\"     @(#)cp.1       6.10 (Berkeley) %G%
 .\"
 .Dd 
 .Dt CP 1
 .\"
 .Dd 
 .Dt CP 1
@@ -17,7 +17,7 @@
 .Ar source_file target_file
 .br
 .Nm cp
 .Ar source_file target_file
 .br
 .Nm cp
-.Op Fl fhipr
+.Op Fl Rfhip
 .Ar source_file ... target_directory
 .Sh DESCRIPTION
 In the first synopsis form,
 .Ar source_file ... target_directory
 .Sh DESCRIPTION
 In the first synopsis form,
@@ -41,23 +41,29 @@ Forces
 .Nm cp
 to follow symbolic links.
 Provided for the
 .Nm cp
 to follow symbolic links.
 Provided for the
-.Fl r
+.Fl R
 option which does not follow symbolic links by default.
 .Tp Fl f
 option which does not follow symbolic links by default.
 .Tp Fl f
-Force existing destination pathnames to be truncated before copying,
-without prompting for confirmation.
-(The
+Turns off the
 .Fl i
 .Fl i
-option is ignored if the
+option.
+(The last
 .Fl f
 .Fl f
-option is specified.)
+or
+.Fl i
+option overrides any previous specification of either option.)
 .Tp Fl i
 Causes
 .Nm cp
 to write a prompt to standard error before copying a file that would
 overwrite an existing file.
 If the response from the standard input begins with the character ``y'',
 .Tp Fl i
 Causes
 .Nm cp
 to write a prompt to standard error before copying a file that would
 overwrite an existing file.
 If the response from the standard input begins with the character ``y'',
-the file is copied if permissions allow the copy.
+the file is copied if permissions allow.
+(The last
+.Fl f
+or
+.Fl i
+option overrides any previous specification of either option.)
 .Tp Fl p
 Causes
 .Nm cp
 .Tp Fl p
 Causes
 .Nm cp
@@ -68,51 +74,46 @@ file mode, user ID, and group ID as allowed by permissions.
 If the user ID and group ID cannot be preserved, no error message
 is displayed and the exit value is not altered.
 .Pp
 If the user ID and group ID cannot be preserved, no error message
 is displayed and the exit value is not altered.
 .Pp
-If the source file has its set user ID bit on and the user ID cannot
-be preserved, the set user ID bit is not preserved
-in the copy's permissions.
-If the source file has its set group ID bit on and the group ID cannot
-be preserved, the set group ID bit is not preserved
-in the copy's permissions.
-If the source file has both the set user ID and set group ID bits
-on and either the user ID or group ID cannot be preserved, neither
-the set user ID or set group ID bits are preserved in the copy's
-permissions.
-.Tp Fl r
+If the source file is set user ID or set group ID, and either the user
+ID or the group ID cannot be preserved, the set user ID and set group
+ID bits are not preserved in the copy's permissions.
+.Tp Fl R
 If
 .Ar source_file
 designates a directory,
 .Nm cp
 copies the directory and the entire subtree connected at that point.
 If
 .Ar source_file
 designates a directory,
 .Nm cp
 copies the directory and the entire subtree connected at that point.
-This option also causes symbolic links to be copied, rather than
-indirected through, and for
-.Nm cp
-to create special files rather than copying them as normal files.
+Special file types, such as symbolic links and block and character
+devices, are recreated instead of being copied.
 Created directories have the same mode as the corresponding source
 Created directories have the same mode as the corresponding source
-directory, unmodified by the process' umask.
+directory, unmodified by the process' file mode creation mask (umask).
+.Tp Fl r
+The
+.Fl r
+option is identical to the
+.Fl R
+option with the exception that it does not treat special files
+differently from regular files.
+Symbolic links are always followed.
+This option has been deprecated.
 .Tp
 .Pp
 For each destination file that already exists, its contents are
 .Tp
 .Pp
 For each destination file that already exists, its contents are
-overwritten if permissions allow, but its mode, user ID, and group
-ID are unchanged.
+overwritten if permissions allow, but
+.Nm cp
+will not change its mode, user ID, or group ID.
+However, if the file is not being copied by the super-user,
+writing the file may clear the set user ID or set group ID
+permission bits.
 .Pp
 If the destination file does not exist, the mode of the source file is
 used as modified by the file mode creation mask (umask).
 .Pp
 If the destination file does not exist, the mode of the source file is
 used as modified by the file mode creation mask (umask).
-If the source file has its set user ID bit on, that bit is removed
-unless both the source file and the destination file are owned by the
-same user.
-If the source file has its set group ID bit on, that bit is removed
-unless both the source file and the destination file are in the same
-group and the user is a member of that group.
-If both the set user ID and set group ID bits are set, all of the above
-conditions must be fulfilled or both bits are removed.
+If the source file is either set user ID or set group ID, those
+bits are removed unless the source file and the destination
+file are owned by the same user and group.
 .Pp
 Appropriate permissions are required for file creation or overwriting.
 .Pp
 .Pp
 Appropriate permissions are required for file creation or overwriting.
 .Pp
-Symbolic links are followed unless the
-.Fl r
-option is specified, in which case the link itself is copied.
-.Pp
 .Nm Cp
 exits 0 on success, >0 if an error occurred.
 .Sh SEE ALSO
 .Nm Cp
 exits 0 on success, >0 if an error occurred.
 .Sh SEE ALSO
index 1340fdd..aef3374 100644 (file)
@@ -15,7 +15,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)cp.c       5.19 (Berkeley) %G%";
+static char sccsid[] = "@(#)cp.c       5.20 (Berkeley) %G%";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -54,7 +54,7 @@ PATH_T to = { "", to.p_path };
 
 uid_t myuid;
 int exit_val, myumask;
 
 uid_t myuid;
 int exit_val, myumask;
-int iflag, pflag, rflag;
+int iflag, pflag, orflag, rflag;
 int (*statfcn)();
 char *buf, *pname;
 char *path_append(), *path_basename();
 int (*statfcn)();
 char *buf, *pname;
 char *path_append(), *path_basename();
@@ -90,10 +90,12 @@ main(argc, argv)
                case 'p':
                        pflag = 1;
                        break;
                case 'p':
                        pflag = 1;
                        break;
-               case 'r':
                case 'R':
                        rflag = 1;
                        break;
                case 'R':
                        rflag = 1;
                        break;
+               case 'r':
+                       orflag = 1;
+                       break;
                case '?':
                default:
                        usage();
                case '?':
                default:
                        usage();
@@ -106,6 +108,12 @@ main(argc, argv)
        if (argc < 2)
                usage();
 
        if (argc < 2)
                usage();
 
+       if (rflag && orflag) {
+               (void)fprintf(stderr,
+                   "cp: the -R and -r options are mutually exclusive.\n");
+               exit(1);
+       }
+
        buf = (char *)malloc(MAXBSIZE);
        if (!buf) {
                (void)fprintf(stderr, "%s: out of space.\n", pname);
        buf = (char *)malloc(MAXBSIZE);
        if (!buf) {
                (void)fprintf(stderr, "%s: out of space.\n", pname);
@@ -208,7 +216,7 @@ copy()
                copy_link(!dne);
                return;
        case S_IFDIR:
                copy_link(!dne);
                return;
        case S_IFDIR:
-               if (!rflag) {
+               if (!rflag && !orflag) {
                        (void)fprintf(stderr,
                            "%s: %s is a directory (not copied).\n",
                            pname, from.p_path);
                        (void)fprintf(stderr,
                            "%s: %s is a directory (not copied).\n",
                            pname, from.p_path);
@@ -244,23 +252,22 @@ copy()
                        setfile(&from_stat, 0);
                else if (dne)
                        (void)chmod(to.p_path, from_stat.st_mode);
                        setfile(&from_stat, 0);
                else if (dne)
                        (void)chmod(to.p_path, from_stat.st_mode);
-               break;
+               return;
        case S_IFCHR:
        case S_IFBLK:
        case S_IFCHR:
        case S_IFBLK:
-               /*
-                * if recursive flag on, try and create the special device
-                * otherwise copy the contents.
-                */
                if (rflag) {
                        copy_special(&from_stat, !dne);
                if (rflag) {
                        copy_special(&from_stat, !dne);
-                       if (pflag)
-                               setfile(&from_stat, 0);
                        return;
                }
                        return;
                }
-               /* FALLTHROUGH */
-       default:
-               copy_file(&from_stat, dne);
+               break;
+       case S_IFIFO:
+               if (rflag) {
+                       copy_fifo(&from_stat, !dne);
+                       return;
+               }
+               break;
        }
        }
+       copy_file(&from_stat, dne);
 }
 
 copy_file(fs, dne)
 }
 
 copy_file(fs, dne)
@@ -319,30 +326,16 @@ copy_file(fs, dne)
        if (pflag)
                setfile(fs, to_fd);
        /*
        if (pflag)
                setfile(fs, to_fd);
        /*
-        * If the source was setuid, set the bits on the copy if the copy
-        * was created and is owned by the same uid.  If the source was
-        * setgid, set the bits on the copy if the copy was created and is
-        * owned by the same gid and the user is a member of that group.
-        * If both setuid and setgid, lose both bits unless all the above
-        * conditions are met.
+        * If the source was setuid or setgid, lose the bits unless the
+        * copy is owned by the same user and group.
         */
         */
-       else if (fs->st_mode & (S_ISUID|S_ISGID)) {
-               if (fs->st_mode & S_ISUID && myuid != fs->st_uid)
-                       fs->st_mode &= ~(S_ISUID|S_ISGID);
-               if (fs->st_mode & S_ISGID) {
-                       if (fstat(to_fd, &to_stat)) {
-                               error(to.p_path);
-                               fs->st_mode &= ~(S_ISUID|S_ISGID);
-                       }
-                       else if (fs->st_gid != to_stat.st_gid ||
-                           !ismember(fs->st_gid))
-                               fs->st_mode &= ~(S_ISUID|S_ISGID);
-               }
-               if (fs->st_mode & (S_ISUID|S_ISGID) && fchmod(to_fd,
-                   fs->st_mode & (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) &
-                   ~myumask))
+       else if (fs->st_mode & (S_ISUID|S_ISGID) && fs->st_uid == myuid)
+               if (fstat(to_fd, &to_stat))
+                       error(to.p_path);
+#define        RETAINBITS      (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
+               else if (fs->st_gid == to_stat.st_gid && fchmod(to_fd,
+                   fs->st_mode & RETAINBITS & ~myumask))
                        error(to.p_path);
                        error(to.p_path);
-       }
        (void)close(from_fd);
        (void)close(to_fd);
 }
        (void)close(from_fd);
        (void)close(to_fd);
 }
@@ -445,6 +438,22 @@ copy_link(exists)
        }
 }
 
        }
 }
 
+copy_fifo(from_stat, exists)
+       struct stat *from_stat;
+       int exists;
+{
+       if (exists && unlink(to.p_path)) {
+               error(to.p_path);
+               return;
+       }
+       if (mkfifo(to.p_path, from_stat->st_mode)) {
+               error(to.p_path);
+               return;
+       }
+       if (pflag)
+               setfile(from_stat, 0);
+}
+
 copy_special(from_stat, exists)
        struct stat *from_stat;
        int exists;
 copy_special(from_stat, exists)
        struct stat *from_stat;
        int exists;
@@ -457,6 +466,8 @@ copy_special(from_stat, exists)
                error(to.p_path);
                return;
        }
                error(to.p_path);
                return;
        }
+       if (pflag)
+               setfile(from_stat, 0);
 }
 
 setfile(fs, fd)
 }
 
 setfile(fs, fd)
@@ -632,6 +643,6 @@ path_basename(p)
 usage()
 {
        (void)fprintf(stderr,
 usage()
 {
        (void)fprintf(stderr,
-"usage: cp [-fhipr] src target;\n   or: cp [-fhipr] src1 ... srcN directory\n");
+"usage: cp [-Rfhip] src target;\n   or: cp [-Rfhip] src1 ... srcN directory\n");
        exit(1);
 }
        exit(1);
 }