BSD 4_3_Net_2 release
[unix-history] / usr / src / bin / cp / cp.c
index 25ed6d0..8124e8d 100644 (file)
@@ -5,19 +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: (1) source distributions retain this entire copyright notice and
- * comment, and (2) distributions including binaries display the following
- * acknowledgement:  ``This product includes software developed by the
- * University of California, Berkeley and its contributors'' in the
- * documentation or other materials provided with the distribution and in
- * all advertising materials mentioning features or use of this software.
- * 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 ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY 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
@@ -27,7 +41,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)cp.c       5.20 (Berkeley) 6/29/90";
+static char sccsid[] = "@(#)cp.c       5.24 (Berkeley) 5/6/91";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -46,30 +60,24 @@ static char sccsid[] = "@(#)cp.c    5.20 (Berkeley) 6/29/90";
 
 #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 <unistd.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdlib.h>
 #include <string.h>
+#include "cp.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;
-
-PATH_T from = { "", from.p_path };
-PATH_T to = { "", to.p_path };
+PATH_T from = { from.p_path, "" };
+PATH_T to = { to.p_path, "" };
 
 uid_t myuid;
 int exit_val, myumask;
 int iflag, pflag, orflag, rflag;
 int (*statfcn)();
 
 uid_t myuid;
 int exit_val, myumask;
 int iflag, pflag, orflag, rflag;
 int (*statfcn)();
-char *buf, *pname;
-char *path_append(), *path_basename();
+char *buf, *progname;
 
 main(argc, argv)
        int argc;
 
 main(argc, argv)
        int argc;
@@ -82,10 +90,10 @@ main(argc, argv)
        char *old_to, *p;
 
        /*
        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;
 
        symfollow = 0;
        while ((c = getopt(argc, argv, "Rfhipr")) != EOF) {
 
        symfollow = 0;
        while ((c = getopt(argc, argv, "Rfhipr")) != EOF) {
@@ -128,7 +136,7 @@ main(argc, argv)
 
        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);
        }
 
@@ -140,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 || !rflag ? stat : lstat;
 
        /*
         * Cp has two distinct cases:
         *
 
        statfcn = symfollow || !rflag ? stat : lstat;
 
        /*
         * 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.
         *
@@ -165,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.
                 */
@@ -174,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 {
@@ -182,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;
@@ -216,14 +227,14 @@ 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_IFLNK:
                copy_link(!dne);
                return;
@@ -231,7 +242,7 @@ copy()
                if (!rflag && !orflag) {
                        (void)fprintf(stderr,
                            "%s: %s is a directory (not copied).\n",
                if (!rflag && !orflag) {
                        (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;
                }
@@ -249,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();
@@ -349,20 +360,21 @@ copy_file(fs, dne)
                    fs->st_mode & RETAINBITS & ~myumask))
                        error(to.p_path);
        (void)close(from_fd);
                    fs->st_mode & RETAINBITS & ~myumask))
                        error(to.p_path);
        (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;
        }
 
@@ -383,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;
                }
@@ -399,7 +413,8 @@ 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;
                (void)free((void *)dp);
                path_restore(&from, old_from);
 done:          dir_list[i] = NULL;
                (void)free((void *)dp);
@@ -412,11 +427,13 @@ 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) {
+                       exit_val = 1;
                        (void)free((void *)dp);
                        continue;
                }
                old_to = path_append(&to, dp->d_name, (int) dp->d_namlen);
                if (!old_to) {
                        (void)free((void *)dp);
                        continue;
                }
                old_to = path_append(&to, dp->d_name, (int) dp->d_namlen);
                if (!old_to) {
+                       exit_val = 1;
                        (void)free((void *)dp);
                        path_restore(&from, old_from);
                        continue;
                        (void)free((void *)dp);
                        path_restore(&from, old_from);
                        continue;
@@ -487,13 +504,16 @@ 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;
 
        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;
-       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
         * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
        /*
         * Changing the ownership probably won't succeed, unless we're root
         * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
@@ -502,154 +522,24 @@ setfile(fs, fd)
         */
        if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
            chown(to.p_path, fs->st_uid, fs->st_gid)) {
         */
        if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
            chown(to.p_path, fs->st_uid, fs->st_gid)) {
-               if (errno != EPERM)
-                       error(to.p_path);
+               if (errno != EPERM) {
+                       (void)snprintf(path, sizeof(path),
+                           "chown: %s", to.p_path);
+                       error(path);
+               }
                fs->st_mode &= ~(S_ISUID|S_ISGID);
        }
                fs->st_mode &= ~(S_ISUID|S_ISGID);
        }
-       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 ? 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()
 }
 
 usage()