break out special local mail processing (e.g., mapping to the
[unix-history] / usr / src / usr.bin / xinstall / xinstall.c
index 5e3d482..5c08ba5 100644 (file)
@@ -1,18 +1,18 @@
 /*
 /*
- * Copyright (c) 1987 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1987, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  * %sccs.include.redist.c%
  */
 
 #ifndef lint
  *
  * %sccs.include.redist.c%
  */
 
 #ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1987 Regents of the University of California.\n\
- All rights reserved.\n";
+static char copyright[] =
+"@(#) Copyright (c) 1987, 1993\n\
      The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)xinstall.c 5.28 (Berkeley) %G%";
+static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -20,16 +20,17 @@ static char sccsid[] = "@(#)xinstall.c      5.28 (Berkeley) %G%";
 #include <sys/mman.h>
 #include <sys/stat.h>
 
 #include <sys/mman.h>
 #include <sys/stat.h>
 
+#include <ctype.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <grp.h>
 #include <fcntl.h>
 #include <grp.h>
+#include <paths.h>
 #include <pwd.h>
 #include <pwd.h>
-#include <errno.h>
-#include <unistd.h>
-#include <stdlib.h>
 #include <stdio.h>
 #include <stdio.h>
-#include <ctype.h>
+#include <stdlib.h>
 #include <string.h>
 #include <string.h>
-#include <paths.h>
+#include <unistd.h>
+
 #include "pathnames.h"
 
 struct passwd *pp;
 #include "pathnames.h"
 
 struct passwd *pp;
@@ -38,9 +39,13 @@ int docopy, dostrip;
 int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
 char *group, *owner, pathbuf[MAXPATHLEN];
 
 int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
 char *group, *owner, pathbuf[MAXPATHLEN];
 
+#define        DIRECTORY       0x01            /* Tell install it's a directory. */
+#define        SETFLAGS        0x02            /* Tell install to set flags. */
+
 void   copy __P((int, char *, int, char *, off_t));
 void   err __P((const char *, ...));
 void   copy __P((int, char *, int, char *, off_t));
 void   err __P((const char *, ...));
-void   install __P((char *, char *, int));
+void   install __P((char *, char *, u_long, u_int));
+u_long string_to_flags __P((char **, u_long *, u_long *));
 void   strip __P((char *));
 void   usage __P((void));
 
 void   strip __P((char *));
 void   usage __P((void));
 
@@ -51,14 +56,23 @@ main(argc, argv)
 {
        struct stat from_sb, to_sb;
        mode_t *set;
 {
        struct stat from_sb, to_sb;
        mode_t *set;
+       u_long fset;
+       u_int iflags;
        int ch, no_target;
        int ch, no_target;
-       char *to_name;
+       char *flags, *to_name;
 
 
-       while ((ch = getopt(argc, argv, "cg:m:o:s")) != EOF)
+       iflags = 0;
+       while ((ch = getopt(argc, argv, "cf:g:m:o:s")) != EOF)
                switch((char)ch) {
                case 'c':
                        docopy = 1;
                        break;
                switch((char)ch) {
                case 'c':
                        docopy = 1;
                        break;
+               case 'f':
+                       flags = optarg;
+                       if (string_to_flags(&flags, &fset, NULL))
+                               err("%s: invalid flag", flags);
+                       iflags |= SETFLAGS;
+                       break;
                case 'g':
                        group = optarg;
                        break;
                case 'g':
                        group = optarg;
                        break;
@@ -91,7 +105,7 @@ main(argc, argv)
        no_target = stat(to_name = argv[argc - 1], &to_sb);
        if (!no_target && (to_sb.st_mode & S_IFMT) == S_IFDIR) {
                for (; *argv != to_name; ++argv)
        no_target = stat(to_name = argv[argc - 1], &to_sb);
        if (!no_target && (to_sb.st_mode & S_IFMT) == S_IFDIR) {
                for (; *argv != to_name; ++argv)
-                       install(*argv, to_name, 1);
+                       install(*argv, to_name, fset, iflags | DIRECTORY);
                exit(0);
        }
 
                exit(0);
        }
 
@@ -107,10 +121,18 @@ main(argc, argv)
                if (to_sb.st_dev == from_sb.st_dev &&
                    to_sb.st_ino == from_sb.st_ino)
                        err("%s and %s are the same file", *argv, to_name);
                if (to_sb.st_dev == from_sb.st_dev &&
                    to_sb.st_ino == from_sb.st_ino)
                        err("%s and %s are the same file", *argv, to_name);
-               /* unlink now... avoid ETXTBSY errors later */
+               /*
+                * Unlink now... avoid ETXTBSY errors later.  Try and turn
+                * off the append/immutable bits -- if we fail, go ahead,
+                * it might work.
+                */
+#define        NOCHANGEBITS    (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
+               if (to_sb.st_flags & NOCHANGEBITS)
+                       (void)chflags(to_name,
+                           to_sb.st_flags & ~(NOCHANGEBITS));
                (void)unlink(to_name);
        }
                (void)unlink(to_name);
        }
-       install(*argv, to_name, 0);
+       install(*argv, to_name, fset, iflags);
        exit(0);
 }
 
        exit(0);
 }
 
@@ -119,37 +141,47 @@ main(argc, argv)
  *     build a path name and install the file
  */
 void
  *     build a path name and install the file
  */
 void
-install(from_name, to_name, isdir)
+install(from_name, to_name, fset, flags)
        char *from_name, *to_name;
        char *from_name, *to_name;
-       int isdir;
+       u_long fset;
+       u_int flags;
 {
 {
-       struct stat from_sb;
+       struct stat from_sb, to_sb;
        int devnull, from_fd, to_fd, serrno;
        char *p;
 
        /* If try to install NULL file to a directory, fails. */
        int devnull, from_fd, to_fd, serrno;
        char *p;
 
        /* If try to install NULL file to a directory, fails. */
-       if (isdir || strcmp(from_name, _PATH_DEVNULL)) {
+       if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
                if (stat(from_name, &from_sb))
                        err("%s: %s", from_name, strerror(errno));
                if (!S_ISREG(from_sb.st_mode))
                        err("%s: %s", from_name, strerror(EFTYPE));
                /* Build the target path. */
                if (stat(from_name, &from_sb))
                        err("%s: %s", from_name, strerror(errno));
                if (!S_ISREG(from_sb.st_mode))
                        err("%s: %s", from_name, strerror(EFTYPE));
                /* Build the target path. */
-               if (isdir) {
+               if (flags & DIRECTORY) {
                        (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
                            to_name,
                            (p = rindex(from_name, '/')) ? ++p : from_name);
                        to_name = pathbuf;
                }
                devnull = 0;
                        (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
                            to_name,
                            (p = rindex(from_name, '/')) ? ++p : from_name);
                        to_name = pathbuf;
                }
                devnull = 0;
-       } else
+       } else {
+               from_sb.st_flags = 0;   /* XXX */
                devnull = 1;
                devnull = 1;
+       }
 
 
-       /* Unlink now... avoid ETXTBSY errors later. */
+       /*
+        * Unlink now... avoid ETXTBSY errors later.  Try and turn
+        * off the append/immutable bits -- if we fail, go ahead,
+        * it might work.
+        */
+       if (stat(to_name, &to_sb) == 0 &&
+           to_sb.st_flags & (NOCHANGEBITS))
+               (void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
        (void)unlink(to_name);
 
        /* Create target. */
        if ((to_fd = open(to_name,
        (void)unlink(to_name);
 
        /* Create target. */
        if ((to_fd = open(to_name,
-           O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR)) < 0)
+           O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
                err("%s: %s", to_name, strerror(errno));
        if (!devnull) {
                if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
                err("%s: %s", to_name, strerror(errno));
        if (!devnull) {
                if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
@@ -166,18 +198,26 @@ install(from_name, to_name, isdir)
         * chown may lose the setuid bits.
         */
        if ((group || owner) &&
         * chown may lose the setuid bits.
         */
        if ((group || owner) &&
-           fchown(to_fd, owner ? pp->pw_uid : -1, group ? gp->gr_gid : -1) ||
-           fchmod(to_fd, mode)) {
+           fchown(to_fd, owner ? pp->pw_uid : -1, group ? gp->gr_gid : -1)) {
+               serrno = errno;
+               (void)unlink(to_name);
+               err("%s: chown/chgrp: %s", to_name, strerror(serrno));
+       }
+       if (fchmod(to_fd, mode)) {
                serrno = errno;
                (void)unlink(to_name);
                serrno = errno;
                (void)unlink(to_name);
-               err("%s: %s", to_name, strerror(serrno));
+               err("%s: chmod: %s", to_name, strerror(serrno));
        }
 
        }
 
-       /* Always preserve the flags, except for the dump flag. */
-       if (fchflags(to_fd, from_sb.st_flags & ~NODUMP)) {
+       /*
+        * If provided a set of flags, set them, otherwise, preserve the
+        * flags, except for the dump flag.
+        */
+       if (fchflags(to_fd,
+           flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
                serrno = errno;
                (void)unlink(to_name);
                serrno = errno;
                (void)unlink(to_name);
-               err("%s: %s", to_name, strerror(serrno));
+               err("%s: chflags: %s", to_name, strerror(serrno));
        }
 
        (void)close(to_fd);
        }
 
        (void)close(to_fd);
@@ -258,7 +298,7 @@ void
 usage()
 {
        (void)fprintf(stderr,
 usage()
 {
        (void)fprintf(stderr,
-"usage: install [-cs] [-g group] [-m mode] [-o owner] file1 file2;\n\tor file1 ... fileN directory\n");
+"usage: install [-cs] [-f flags] [-g group] [-m mode] [-o owner] file1 file2;\n\tor file1 ... fileN directory\n");
        exit(1);
 }
 
        exit(1);
 }