BSD 4_3_Net_2 release
[unix-history] / usr / src / bin / cp / cp.c
index 77d3135..8124e8d 100644 (file)
@@ -5,17 +5,33 @@
  * This code is derived from software contributed to Berkeley by
  * David Hitz of Auspex Systems Inc.
  *
  * This code is derived from software contributed to Berkeley by
  * David Hitz of Auspex Systems Inc.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -25,7 +41,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)cp.c       5.15 (Berkeley) %G%";
+static char sccsid[] = "@(#)cp.c       5.24 (Berkeley) 5/6/91";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -44,30 +60,24 @@ static char sccsid[] = "@(#)cp.c    5.15 (Berkeley) %G%";
 
 #include <sys/param.h>
 #include <sys/stat.h>
 
 #include <sys/param.h>
 #include <sys/stat.h>
-#include <sys/file.h>
-#include <sys/dir.h>
 #include <sys/time.h>
 #include <sys/time.h>
-#include <stdio.h>
+#include <dirent.h>
+#include <fcntl.h>
 #include <errno.h>
 #include <errno.h>
-#include <strings.h>
-
-#define        type(st)        ((st).st_mode & S_IFMT)
-
-typedef struct {
-       char    p_path[MAXPATHLEN + 1]; /* pointer to the start of a path. */
-       char    *p_end;                 /* pointer to NULL at end of path. */
-} PATH_T;
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "cp.h"
 
 
-PATH_T from = { "", from.p_path };
-PATH_T to = { "", to.p_path };
+PATH_T from = { from.p_path, "" };
+PATH_T to = { to.p_path, "" };
 
 
-extern int errno;
 uid_t myuid;
 int exit_val, myumask;
 uid_t myuid;
 int exit_val, myumask;
-int interactive_flag, preserve_flag, recursive_flag;
+int iflag, pflag, orflag, rflag;
 int (*statfcn)();
 int (*statfcn)();
-char *buf, *pname;
-char *path_append(), *path_basename();
+char *buf, *progname;
 
 main(argc, argv)
        int argc;
 
 main(argc, argv)
        int argc;
@@ -76,33 +86,35 @@ main(argc, argv)
        extern int optind;
        struct stat to_stat;
        register int c, r;
        extern int optind;
        struct stat to_stat;
        register int c, r;
-       int force_flag, symfollow, lstat(), stat();
-       char *old_to, *p, *malloc();
+       int symfollow, lstat(), stat();
+       char *old_to, *p;
 
        /*
 
        /*
-        * cp is used by mv(1) -- except for usage statements, print
-        * the "called as" program name.
+        * The utility cp(1) is used by mv(1) -- except for usage statements,
+        * print the "called as" program name.
         */
         */
-       pname = (p = rindex(*argv,'/')) ? ++p : *argv;
+       progname = (p = rindex(*argv,'/')) ? ++p : *argv;
 
 
-       force_flag = symfollow = 0;
+       symfollow = 0;
        while ((c = getopt(argc, argv, "Rfhipr")) != EOF) {
        switch ((char)c) {
                case 'f':
        while ((c = getopt(argc, argv, "Rfhipr")) != EOF) {
        switch ((char)c) {
                case 'f':
-                       force_flag = 1;
+                       iflag = 0;
                        break;
                case 'h':
                        symfollow = 1;
                        break;
                case 'i':
                        break;
                case 'h':
                        symfollow = 1;
                        break;
                case 'i':
-                       interactive_flag = isatty(fileno(stdin));
+                       iflag = isatty(fileno(stdin));
                        break;
                case 'p':
                        break;
                case 'p':
-                       preserve_flag = 1;
+                       pflag = 1;
                        break;
                        break;
-               case 'r':
                case 'R':
                case 'R':
-                       recursive_flag = 1;
+                       rflag = 1;
+                       break;
+               case 'r':
+                       orflag = 1;
                        break;
                case '?':
                default:
                        break;
                case '?':
                default:
@@ -116,12 +128,15 @@ main(argc, argv)
        if (argc < 2)
                usage();
 
        if (argc < 2)
                usage();
 
-       if (force_flag)
-               interactive_flag = 0;
+       if (rflag && orflag) {
+               (void)fprintf(stderr,
+                   "cp: the -R and -r options are mutually exclusive.\n");
+               exit(1);
+       }
 
        buf = (char *)malloc(MAXBSIZE);
        if (!buf) {
 
        buf = (char *)malloc(MAXBSIZE);
        if (!buf) {
-               (void)fprintf(stderr, "%s: out of space.\n", pname);
+               (void)fprintf(stderr, "%s: out of space.\n", progname);
                exit(1);
        }
 
                exit(1);
        }
 
@@ -133,16 +148,15 @@ main(argc, argv)
 
        /* consume last argument first. */
        if (!path_set(&to, argv[--argc]))
 
        /* consume last argument first. */
        if (!path_set(&to, argv[--argc]))
-               exit(exit_val);
+               exit(1);
 
 
-       statfcn = symfollow || !recursive_flag ? stat : lstat;
+       statfcn = symfollow || !rflag ? stat : lstat;
 
        /*
         * Cp has two distinct cases:
         *
 
        /*
         * Cp has two distinct cases:
         *
-        * Case (1)       $ cp [-rip] source target
-        *
-        * Case (2)       $ cp [-rip] source1 ... directory
+        * % cp [-rip] source target
+        * % cp [-rip] source1 ... directory
         *
         * In both cases, source can be either a file or a directory.
         *
         *
         * In both cases, source can be either a file or a directory.
         *
@@ -158,7 +172,7 @@ main(argc, argv)
                error(to.p_path);
                exit(1);
        }
                error(to.p_path);
                exit(1);
        }
-       if (r == -1 || type(to_stat) != S_IFDIR) {
+       if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
                /*
                 * Case (1).  Target is not a directory.
                 */
                /*
                 * Case (1).  Target is not a directory.
                 */
@@ -167,7 +181,7 @@ main(argc, argv)
                        exit(1);
                }
                if (!path_set(&from, *argv))
                        exit(1);
                }
                if (!path_set(&from, *argv))
-                       exit(exit_val);
+                       exit(1);
                copy();
        }
        else {
                copy();
        }
        else {
@@ -175,11 +189,15 @@ main(argc, argv)
                 * Case (2).  Target is a directory.
                 */
                for (;; ++argv) {
                 * Case (2).  Target is a directory.
                 */
                for (;; ++argv) {
-                       if (!path_set(&from, *argv))
+                       if (!path_set(&from, *argv)) {
+                               exit_val = 1;
                                continue;
                                continue;
+                       }
                        old_to = path_append(&to, path_basename(&from), -1);
                        old_to = path_append(&to, path_basename(&from), -1);
-                       if (!old_to)
+                       if (!old_to) {
+                               exit_val = 1;
                                continue;
                                continue;
+                       }
                        copy();
                        if (!--argc)
                                break;
                        copy();
                        if (!--argc)
                                break;
@@ -209,22 +227,22 @@ copy()
                    to_stat.st_ino == from_stat.st_ino) {
                        (void)fprintf(stderr,
                            "%s: %s and %s are identical (not copied).\n",
                    to_stat.st_ino == from_stat.st_ino) {
                        (void)fprintf(stderr,
                            "%s: %s and %s are identical (not copied).\n",
-                           pname, to.p_path, from.p_path);
+                           progname, to.p_path, from.p_path);
                        exit_val = 1;
                        return;
                }
                dne = 0;
        }
 
                        exit_val = 1;
                        return;
                }
                dne = 0;
        }
 
-       switch(type(from_stat)) {
+       switch(from_stat.st_mode & S_IFMT) {
        case S_IFLNK:
                copy_link(!dne);
                return;
        case S_IFDIR:
        case S_IFLNK:
                copy_link(!dne);
                return;
        case S_IFDIR:
-               if (!recursive_flag) {
+               if (!rflag && !orflag) {
                        (void)fprintf(stderr,
                            "%s: %s is a directory (not copied).\n",
                        (void)fprintf(stderr,
                            "%s: %s is a directory (not copied).\n",
-                           pname, from.p_path);
+                           progname, from.p_path);
                        exit_val = 1;
                        return;
                }
                        exit_val = 1;
                        return;
                }
@@ -242,9 +260,9 @@ copy()
                                return;
                        }
                }
                                return;
                        }
                }
-               else if (type(to_stat) != S_IFDIR) {
+               else if (!S_ISDIR(to_stat.st_mode) != S_IFDIR) {
                        (void)fprintf(stderr, "%s: %s: not a directory.\n",
                        (void)fprintf(stderr, "%s: %s: not a directory.\n",
-                           pname, to.p_path);
+                           progname, to.p_path);
                        return;
                }
                copy_dir();
                        return;
                }
                copy_dir();
@@ -253,27 +271,26 @@ copy()
                 * same as the from directory, umodified by the umask;
                 * arguably wrong, but it's been that way forever.
                 */
                 * same as the from directory, umodified by the umask;
                 * arguably wrong, but it's been that way forever.
                 */
-               if (preserve_flag)
+               if (pflag)
                        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 (recursive_flag) {
+               if (rflag) {
                        copy_special(&from_stat, !dne);
                        copy_special(&from_stat, !dne);
-                       if (preserve_flag)
-                               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)
@@ -297,7 +314,7 @@ copy_file(fs, dne)
         * modified by the umask.)
         */
        if (!dne) {
         * modified by the umask.)
         */
        if (!dne) {
-               if (interactive_flag) {
+               if (iflag) {
                        int checkch, ch;
 
                        (void)fprintf(stderr, "overwrite %s? ", to.p_path);
                        int checkch, ch;
 
                        (void)fprintf(stderr, "overwrite %s? ", to.p_path);
@@ -329,48 +346,35 @@ copy_file(fs, dne)
        }
        if (rcount < 0)
                error(from.p_path);
        }
        if (rcount < 0)
                error(from.p_path);
-       if (preserve_flag)
+       if (pflag)
                setfile(fs, to_fd);
        /*
                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(from_fd);
-       (void)close(to_fd);
+       if (close(to_fd))
+               error(to.p_path);
 }
 
 copy_dir()
 {
        struct stat from_stat;
 }
 
 copy_dir()
 {
        struct stat from_stat;
-       struct direct *dp, **dir_list;
+       struct dirent *dp, **dir_list;
        register int dir_cnt, i;
        char *old_from, *old_to;
 
        dir_cnt = scandir(from.p_path, &dir_list, NULL, NULL);
        if (dir_cnt == -1) {
                (void)fprintf(stderr, "%s: can't read directory %s.\n",
        register int dir_cnt, i;
        char *old_from, *old_to;
 
        dir_cnt = scandir(from.p_path, &dir_list, NULL, NULL);
        if (dir_cnt == -1) {
                (void)fprintf(stderr, "%s: can't read directory %s.\n",
-                   pname, from.p_path);
+                   progname, from.p_path);
                exit_val = 1;
        }
 
                exit_val = 1;
        }
 
@@ -391,15 +395,17 @@ copy_dir()
                    && (dp->d_name[1] == NULL || dp->d_name[1] == '.'))
                        goto done;
                old_from = path_append(&from, dp->d_name, (int)dp->d_namlen);
                    && (dp->d_name[1] == NULL || dp->d_name[1] == '.'))
                        goto done;
                old_from = path_append(&from, dp->d_name, (int)dp->d_namlen);
-               if (!old_from)
+               if (!old_from) {
+                       exit_val = 1;
                        goto done;
                        goto done;
+               }
 
                if (statfcn(from.p_path, &from_stat) < 0) {
                        error(dp->d_name);
                        path_restore(&from, old_from);
                        goto done;
                }
 
                if (statfcn(from.p_path, &from_stat) < 0) {
                        error(dp->d_name);
                        path_restore(&from, old_from);
                        goto done;
                }
-               if (type(from_stat) == S_IFDIR) {
+               if (S_ISDIR(from_stat.st_mode)) {
                        path_restore(&from, old_from);
                        continue;
                }
                        path_restore(&from, old_from);
                        continue;
                }
@@ -407,10 +413,11 @@ copy_dir()
                if (old_to) {
                        copy();
                        path_restore(&to, old_to);
                if (old_to) {
                        copy();
                        path_restore(&to, old_to);
-               }
+               } else
+                       exit_val = 1;
                path_restore(&from, old_from);
 done:          dir_list[i] = NULL;
                path_restore(&from, old_from);
 done:          dir_list[i] = NULL;
-               (void)free((char *)dp);
+               (void)free((void *)dp);
        }
 
        /* copy directories */
        }
 
        /* copy directories */
@@ -420,21 +427,23 @@ done:             dir_list[i] = NULL;
                        continue;
                old_from = path_append(&from, dp->d_name, (int) dp->d_namlen);
                if (!old_from) {
                        continue;
                old_from = path_append(&from, dp->d_name, (int) dp->d_namlen);
                if (!old_from) {
-                       (void)free((char *)dp);
+                       exit_val = 1;
+                       (void)free((void *)dp);
                        continue;
                }
                old_to = path_append(&to, dp->d_name, (int) dp->d_namlen);
                if (!old_to) {
                        continue;
                }
                old_to = path_append(&to, dp->d_name, (int) dp->d_namlen);
                if (!old_to) {
-                       (void)free((char *)dp);
+                       exit_val = 1;
+                       (void)free((void *)dp);
                        path_restore(&from, old_from);
                        continue;
                }
                copy();
                        path_restore(&from, old_from);
                        continue;
                }
                copy();
-               free((char *)dp);
+               free((void *)dp);
                path_restore(&from, old_from);
                path_restore(&to, old_to);
        }
                path_restore(&from, old_from);
                path_restore(&to, old_to);
        }
-       free((char *)dir_list);
+       free((void *)dir_list);
 }
 
 copy_link(exists)
 }
 
 copy_link(exists)
@@ -458,6 +467,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;
@@ -470,6 +495,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)
@@ -477,183 +504,47 @@ setfile(fs, fd)
        int fd;
 {
        static struct timeval tv[2];
        int fd;
 {
        static struct timeval tv[2];
+       char path[100];
+
+       fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
 
        tv[0].tv_sec = fs->st_atime;
        tv[1].tv_sec = fs->st_mtime;
 
        tv[0].tv_sec = fs->st_atime;
        tv[1].tv_sec = fs->st_mtime;
-       if (utimes(to.p_path, tv))
-               error(to.p_path);
+       if (utimes(to.p_path, tv)) {
+               (void)snprintf(path, sizeof(path), "utimes: %s", to.p_path);
+               error(path);
+       }
        /*
         * Changing the ownership probably won't succeed, unless we're root
        /*
         * Changing the ownership probably won't succeed, unless we're root
-        * or POSIX_CHOWN_RESTRICTED is not set.  Set uid before setting the
-        * mode; current BSD behavior is to remove all setuid bits on chown.
-        * If setuid, lose the bits if chown fails.
-        * If setgid, lose the bits if chgrp fails.
-        * If both, lose the bits if either fails.
+        * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
+        * the mode; current BSD behavior is to remove all setuid bits on
+        * chown.  If chown fails, lose setuid/setgid bits.
         */
         */
-       if ((fd ?
-           fchown(fd, fs->st_uid, -1) : chown(to.p_path, fs->st_uid, -1))) {
-               if (errno != EPERM)
-                       error(to.p_path);
-               if (fs->st_mode & S_ISUID)
-                       fs->st_mode &= ~(S_ISUID|S_ISGID);
-       }
-       if ((fd ?
-           fchown(fd, -1, fs->st_gid) : chown(to.p_path, -1, fs->st_gid))) {
-               if (errno != EPERM)
-                       error(to.p_path);
-               if (fs->st_mode & S_ISGID)
-                       fs->st_mode &= ~(S_ISUID|S_ISGID);
-       }
-       fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
-       if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode))
-               error(to.p_path);
-}
-
-ismember(gid)
-       gid_t gid;
-{
-       register int cnt;
-       static int ngroups, groups[NGROUPS];
-
-       if (!ngroups) {
-               ngroups = getgroups(NGROUPS, groups);
-               if (ngroups == -1) {
-                       ngroups = 0;
-                       exit_val = 1;
-                       (void)fprintf(stderr, "%s: %s\n",
-                           pname, strerror(errno));
-                       return(0);
+       if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
+           chown(to.p_path, fs->st_uid, fs->st_gid)) {
+               if (errno != EPERM) {
+                       (void)snprintf(path, sizeof(path),
+                           "chown: %s", to.p_path);
+                       error(path);
                }
                }
+               fs->st_mode &= ~(S_ISUID|S_ISGID);
+       }
+       if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) {
+               (void)snprintf(path, sizeof(path), "chown: %s", to.p_path);
+               error(path);
        }
        }
-       for (cnt = ngroups; cnt--;)
-               if (gid == groups[cnt])
-                       return(1);
-       return(0);
 }
 
 error(s)
        char *s;
 {
        exit_val = 1;
 }
 
 error(s)
        char *s;
 {
        exit_val = 1;
-       (void)fprintf(stderr, "%s: %s: %s\n", pname, s, strerror(errno));
-}
-
-/********************************************************************
- * Path Manipulation Routines.
- ********************************************************************/
-
-/*
- * These functions manipulate paths in PATH_T structures.
- * 
- * They eliminate multiple slashes in paths when they notice them, and keep
- * the path non-slash terminated.
- *
- * Both path_set() and path_append() return 0 if the requested name
- * would be too long.
- */
-
-#define        STRIP_TRAILING_SLASH(p) { \
-       while ((p)->p_end > (p)->p_path && (p)->p_end[-1] == '/') \
-               *--(p)->p_end = 0; \
-       }
-
-/*
- * Move specified string into path.  Convert "" to "." to handle BSD
- * semantics for a null path.  Strip trailing slashes.
- */
-path_set(p, string)
-       register PATH_T *p;
-       char *string;
-{
-       if (strlen(string) > MAXPATHLEN) {
-               (void)fprintf(stderr, "%s: %s: name too long.\n",
-                   pname, string);
-               exit_val = 1;
-               return(0);
-       }
-
-       (void)strcpy(p->p_path, string);
-       p->p_end = p->p_path + strlen(p->p_path);
-
-       if (p->p_path == p->p_end) {
-               *p->p_end++ = '.';
-               *p->p_end = 0;
-       }
-
-       STRIP_TRAILING_SLASH(p);
-       return(1);
-}
-
-/*
- * Append specified string to path, inserting '/' if necessary.  Return a
- * pointer to the old end of path for restoration.
- */
-char *
-path_append(p, name, len)
-       register PATH_T *p;
-       char *name;
-       int len;
-{
-       char *old;
-
-       old = p->p_end;
-       if (len == -1)
-               len = strlen(name);
-
-       /*
-        * The final "+ 1" accounts for the '/' between old path and name.
-        */
-       if ((len + p->p_end - p->p_path + 1) > MAXPATHLEN) {
-               (void)fprintf(stderr,
-                   "%s: %s/%s: name too long.\n", pname, p->p_path, name);
-               exit_val = 1;
-               return(0);
-       }
-
-       /*
-        * This code should always be executed, since paths shouldn't
-        * end in '/'.
-        */
-       if (p->p_end[-1] != '/') {
-               *p->p_end++ = '/';
-               *p->p_end = 0;
-       }
-
-       (void)strncat(p->p_end, name, len);
-       p->p_end += len;
-       *p->p_end = 0;
-
-       STRIP_TRAILING_SLASH(p);
-       return(old);
-}
-
-/*
- * Restore path to previous value.  (As returned by path_append.)
- */
-path_restore(p, old)
-       PATH_T *p;
-       char *old;
-{
-       p->p_end = old;
-       *p->p_end = 0;
-}
-
-/*
- * Return basename of path.  (Like basename(1).)
- */
-char *
-path_basename(p)
-       PATH_T *p;
-{
-       char *basename;
-
-       basename = rindex(p->p_path, '/');
-       return(basename ? ++basename : p->p_path);
+       (void)fprintf(stderr, "%s: %s: %s\n", progname, s, strerror(errno));
 }
 
 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);
 }