break out special local mail processing (e.g., mapping to the
[unix-history] / usr / src / usr.bin / xinstall / xinstall.c
index 8eb73f9..5c08ba5 100644 (file)
@@ -1,67 +1,91 @@
 /*
 /*
- * Copyright (c) 1987 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1987, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1987 Regents of the University of California.\n\
- All rights reserved.\n";
-#endif not lint
+static char copyright[] =
+"@(#) Copyright (c) 1987, 1993\n\
      The Regents of the University of California.  All rights reserved.\n";
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)xinstall.c 5.6 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) %G%";
+#endif /* not lint */
 
 #include <sys/param.h>
 
 #include <sys/param.h>
+#include <sys/wait.h>
+#include <sys/mman.h>
 #include <sys/stat.h>
 #include <sys/stat.h>
-#include <sys/file.h>
-#include <a.out.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
 #include <grp.h>
 #include <grp.h>
+#include <paths.h>
 #include <pwd.h>
 #include <stdio.h>
 #include <pwd.h>
 #include <stdio.h>
-#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "pathnames.h"
 
 
-#define        YES             1                       /* yes/true */
-#define        NO              0                       /* no/false */
+struct passwd *pp;
+struct group *gp;
+int docopy, dostrip;
+int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
+char *group, *owner, pathbuf[MAXPATHLEN];
 
 
-extern int     errno;
-extern char    *sys_errlist[];
+#define        DIRECTORY       0x01            /* Tell install it's a directory. */
+#define        SETFLAGS        0x02            /* Tell install to set flags. */
 
 
-static struct passwd   *pp;
-static struct group    *gp;
-static int     docopy, dostrip,
-               mode = 0755;
-static char    *group, *owner,
-               pathbuf[MAXPATHLEN];
+void   copy __P((int, char *, int, char *, off_t));
+void   err __P((const char *, ...));
+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));
 
 
+int
 main(argc, argv)
 main(argc, argv)
-       int     argc;
-       char    **argv;
+       int argc;
+       char *argv[];
 {
 {
-       extern char     *optarg;
-       extern int      optind;
-       struct stat     from_sb, to_sb;
-       int     ch, no_target;
-       char    *to_name;
+       struct stat from_sb, to_sb;
+       mode_t *set;
+       u_long fset;
+       u_int iflags;
+       int ch, no_target;
+       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':
                switch((char)ch) {
                case 'c':
-                       docopy = YES;
+                       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 'm':
                        break;
                case 'g':
                        group = optarg;
                        break;
                case 'm':
-                       mode = atoo(optarg);
+                       if (!(set = setmode(optarg)))
+                               err("%s: invalid file mode", optarg);
+                       mode = getmode(set, 0);
                        break;
                case 'o':
                        owner = optarg;
                        break;
                case 's':
                        break;
                case 'o':
                        owner = optarg;
                        break;
                case 's':
-                       dostrip = YES;
+                       dostrip = 1;
                        break;
                case '?':
                default:
                        break;
                case '?':
                default:
@@ -73,19 +97,15 @@ main(argc, argv)
                usage();
 
        /* get group and owner id's */
                usage();
 
        /* get group and owner id's */
-       if (group && !(gp = getgrnam(group))) {
-               fprintf(stderr, "install: unknown group %s.\n", group);
-               exit(1);
-       }
-       if (owner && !(pp = getpwnam(owner))) {
-               fprintf(stderr, "install: unknown user %s.\n", owner);
-               exit(1);
-       }
+       if (group && !(gp = getgrnam(group)))
+               err("unknown group %s", group);
+       if (owner && !(pp = getpwnam(owner)))
+               err("unknown user %s", owner);
 
        no_target = stat(to_name = argv[argc - 1], &to_sb);
 
        no_target = stat(to_name = argv[argc - 1], &to_sb);
-       if (!no_target && to_sb.st_mode & S_IFDIR) {
+       if (!no_target && (to_sb.st_mode & S_IFMT) == S_IFDIR) {
                for (; *argv != to_name; ++argv)
                for (; *argv != to_name; ++argv)
-                       install(*argv, to_name, YES);
+                       install(*argv, to_name, fset, iflags | DIRECTORY);
                exit(0);
        }
 
                exit(0);
        }
 
@@ -94,22 +114,25 @@ main(argc, argv)
                usage();
 
        if (!no_target) {
                usage();
 
        if (!no_target) {
-               if (stat(*argv, &from_sb)) {
-                       fprintf(stderr, "install: can't find %s.\n", *argv);
-                       exit(1);
-               }
-               if (!(to_sb.st_mode & S_IFREG)) {
-                       fprintf(stderr, "install: %s isn't a regular file.\n", to_name);
-                       exit(1);
-               }
-               if (to_sb.st_dev == from_sb.st_dev && to_sb.st_ino == from_sb.st_ino) {
-                       fprintf(stderr, "install: %s and %s are the same file.\n", *argv, to_name);
-                       exit(1);
-               }
-               /* unlink now... avoid ETXTBSY errors later */
+               if (stat(*argv, &from_sb))
+                       err("%s: %s", *argv, strerror(errno));
+               if (!S_ISREG(to_sb.st_mode))
+                       err("%s: %s", to_name, strerror(EFTYPE));
+               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.  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, NO);
+       install(*argv, to_name, fset, iflags);
        exit(0);
 }
 
        exit(0);
 }
 
@@ -117,190 +140,193 @@ main(argc, argv)
  * install --
  *     build a path name and install the file
  */
  * install --
  *     build a path name and install the file
  */
-static
-install(from_name, to_name, isdir)
-       char    *from_name, *to_name;
-       int     isdir;
+void
+install(from_name, to_name, fset, flags)
+       char *from_name, *to_name;
+       u_long fset;
+       u_int flags;
 {
 {
-       struct stat     from_sb;
-       int     devnull, from_fd, to_fd;
-       char    *C,
-               *rindex();
+       struct stat from_sb, to_sb;
+       int devnull, from_fd, to_fd, serrno;
+       char *p;
 
 
-       if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
-               fprintf(stderr, "install: open: %s: %s\n", from_name, sys_errlist[errno]);
-               exit(1);
-       }
-
-       /* if try to install "/dev/null" to a directory, fails */
-       devnull = isdir ? NO : !strcmp(from_name, "/dev/null");
-       if (!devnull) {
-               if (fstat(from_fd, &from_sb)) {
-                       fprintf(stderr, "install: can't find %s.\n", from_name);
-                       exit(1);
-               }
-               if (!(from_sb.st_mode & S_IFREG)) {
-                       fprintf(stderr, "install: %s isn't a regular file.\n", from_name);
-                       exit(1);
+       /* If try to install NULL file to a directory, fails. */
+       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 (flags & DIRECTORY) {
+                       (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
+                           to_name,
+                           (p = rindex(from_name, '/')) ? ++p : from_name);
+                       to_name = pathbuf;
                }
                }
+               devnull = 0;
+       } else {
+               from_sb.st_flags = 0;   /* XXX */
+               devnull = 1;
        }
 
        }
 
-       /* build the path */
-       if (isdir) {
-               (void)sprintf(pathbuf, "%s/%s", to_name, (C = rindex(*from_name, '/')) ? ++C : from_name);
-               to_name = pathbuf;
-       }
-
-       /* 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);
 
        (void)unlink(to_name);
 
-       /* open target, set owner, group, mode */
-       if ((to_fd = open(to_name, O_CREAT|O_WRONLY|O_TRUNC, 0)) < 0) {
-               fprintf(stderr, "install: %s: %s\n", to_name, sys_errlist[errno]);
-               exit(1);
-       }
-       if (fchmod(to_fd, mode)) {
-               fprintf(stderr, "install: fchmod: %s: %s\n", to_name, sys_errlist[errno]);
-               bad();
+       /* Create target. */
+       if ((to_fd = open(to_name,
+           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) {
+                       (void)unlink(to_name);
+                       err("%s: %s", from_name, strerror(errno));
+               }
+               copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
+               (void)close(from_fd);
        }
        }
-       if ((group || owner) && fchown(to_fd, owner ? pp->pw_uid : -1, group ? gp->gr_gid : -1)) {
-               fprintf(stderr, "install: fchown: %s: %s\n", to_name, sys_errlist[errno]);
-               bad();
+       if (dostrip)
+               strip(to_name);
+       /*
+        * Set owner, group, mode for target; do the chown first,
+        * chown may lose the setuid bits.
+        */
+       if ((group || owner) &&
+           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 (devnull) {
-               (void)close(to_fd);
-               return;
+       if (fchmod(to_fd, mode)) {
+               serrno = errno;
+               (void)unlink(to_name);
+               err("%s: chmod: %s", to_name, strerror(serrno));
        }
 
        }
 
-       if (dostrip) {
-               strip(from_fd, from_name, to_fd, to_name);
-               if (docopy)
-                       goto done;
-       }
-       else if (docopy) {
-               copy(from_fd, from_name, to_fd, to_name);
-               goto done;
-       }
-       else if (rename(from_name, to_name))
-               copy(from_fd, from_name, to_fd, to_name);
-       else if (chmod(to_name, mode)) {
-               fprintf(stderr, "install: chmod: %s: %s\n", to_name, sys_errlist[errno]);
-               bad();
+       /*
+        * 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);
+               err("%s: chflags: %s", to_name, strerror(serrno));
        }
        }
-       (void)unlink(from_name);
 
 
-done:  (void)close(from_fd);
        (void)close(to_fd);
        (void)close(to_fd);
-}
-
-/*
- * strip --
- *     copy file, strip(1)'ing it at the same time
- */
-static
-strip(from_fd, from_name, to_fd, to_name)
-       register int    from_fd, to_fd;
-       char    *from_name, *to_name;
-{
-       typedef struct exec     EXEC;
-       register long   size;
-       register int    n;
-       EXEC    head;
-       char    buf[MAXBSIZE];
-       off_t   lseek();
-
-       if (read(from_fd, (char *)&head, sizeof(head)) < 0 || N_BADMAG(head)) {
-               fprintf(stderr, "install: %s not in a.out format.\n", from_name);
-               bad();
-       }
-       if (head.a_syms || head.a_trsize || head.a_drsize) {
-               size = (long)head.a_text + head.a_data;
-               head.a_syms = head.a_trsize = head.a_drsize = 0;
-               if (head.a_magic == ZMAGIC)
-                       size += getpagesize() - sizeof(EXEC);
-               if (write(to_fd, (char *)&head, sizeof(EXEC)) != sizeof(EXEC)) {
-                       fprintf(stderr, "install: write: %s: %s\n", to_name, sys_errlist[errno]);
-                       bad();
-               }
-               for (; size; size -= n)
-                       if ((n = read(from_fd, buf, (int)MIN(size, sizeof(buf)))) <= 0)
-                               break;
-                       else if (write(to_fd, buf, n) != n) {
-                               fprintf(stderr, "install: write: %s: %s\n", to_name, sys_errlist[errno]);
-                               bad();
-                       }
-               if (size) {
-                       fprintf(stderr, "install: read: %s: premature EOF.\n", from_name);
-                       bad();
-               }
-               if (n == -1) {
-                       fprintf(stderr, "install: read: %s: %s\n", from_name, sys_errlist[errno]);
-                       bad();
-               }
-       }
-       else {
-               (void)lseek(from_fd, 0L, L_SET);
-               copy(from_fd, from_name, to_fd, to_name);
-       }
+       if (!docopy && !devnull && unlink(from_name))
+               err("%s: %s", from_name, strerror(errno));
 }
 
 /*
  * copy --
  *     copy from one file to another
  */
 }
 
 /*
  * copy --
  *     copy from one file to another
  */
-static
-copy(from_fd, from_name, to_fd, to_name)
-       register int    from_fd, to_fd;
-       char    *from_name, *to_name;
+void
+copy(from_fd, from_name, to_fd, to_name, size)
+       register int from_fd, to_fd;
+       char *from_name, *to_name;
+       off_t size;
 {
 {
-       register int    n;
-       char    buf[MAXBSIZE];
+       register int nr, nw;
+       int serrno;
+       char *p, buf[MAXBSIZE];
 
 
-       while ((n = read(from_fd, buf, sizeof(buf))) > 0)
-               if (write(to_fd, buf, n) != n) {
-                       fprintf(stderr, "install: write: %s: %s\n", to_name, sys_errlist[errno]);
-                       bad();
+       /*
+        * Mmap and write if less than 8M (the limit is so we don't totally
+        * trash memory on big files.  This is really a minor hack, but it
+        * wins some CPU back.
+        */
+       if (size <= 8 * 1048576) {
+               if ((p = mmap(NULL, (size_t)size, PROT_READ,
+                   0, from_fd, (off_t)0)) == (char *)-1)
+                       err("%s: %s", from_name, strerror(errno));
+               if (write(to_fd, p, size) != size)
+                       err("%s: %s", to_name, strerror(errno));
+       } else {
+               while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
+                       if ((nw = write(to_fd, buf, nr)) != nr) {
+                               serrno = errno;
+                               (void)unlink(to_name);
+                               err("%s: %s",
+                                   to_name, strerror(nw > 0 ? EIO : serrno));
+                       }
+               if (nr != 0) {
+                       serrno = errno;
+                       (void)unlink(to_name);
+                       err("%s: %s", from_name, strerror(serrno));
                }
                }
-       if (n == -1) {
-               fprintf(stderr, "install: read: %s: %s\n", from_name, sys_errlist[errno]);
-               bad();
        }
 }
 
 /*
        }
 }
 
 /*
- * atoo --
- *     octal string to int
+ * strip --
+ *     use strip(1) to strip the target file
  */
  */
-static
-atoo(str)
-       register char   *str;
+void
+strip(to_name)
+       char *to_name;
 {
 {
-       register int    val;
+       int serrno, status;
 
 
-       for (val = 0; isdigit(*str); ++str)
-               val = val * 8 + *str - '0';
-       return(val);
+       switch (vfork()) {
+       case -1:
+               serrno = errno;
+               (void)unlink(to_name);
+               err("forks: %s", strerror(errno));
+       case 0:
+               execl(_PATH_STRIP, "strip", to_name, NULL);
+               err("%s: %s", _PATH_STRIP, strerror(errno));
+       default:
+               if (wait(&status) == -1 || status)
+                       (void)unlink(to_name);
+       }
 }
 
 /*
 }
 
 /*
- * bad --
- *     remove created target and die
+ * usage --
+ *     print a usage message and die
  */
  */
-static
-bad()
+void
+usage()
 {
 {
-       (void)unlink(pathbuf);
+       (void)fprintf(stderr,
+"usage: install [-cs] [-f flags] [-g group] [-m mode] [-o owner] file1 file2;\n\tor file1 ... fileN directory\n");
        exit(1);
 }
 
        exit(1);
 }
 
-/*
- * usage --
- *     print a usage message and die
- */
-static
-usage()
+#if __STDC__
+#include <stdarg.h>
+#else
+#include <varargs.h>
+#endif
+
+void
+#if __STDC__
+err(const char *fmt, ...)
+#else
+err(fmt, va_alist)
+       char *fmt;
+        va_dcl
+#endif
 {
 {
-       fputs("usage: install [-cs] [-g group] [-m mode] [-o owner] f1 f2;\n\tor f1 ... fn directory\n", stderr);
+       va_list ap;
+#if __STDC__
+       va_start(ap, fmt);
+#else
+       va_start(ap);
+#endif
+       (void)fprintf(stderr, "install: ");
+       (void)vfprintf(stderr, fmt, ap);
+       va_end(ap);
+       (void)fprintf(stderr, "\n");
        exit(1);
        exit(1);
+       /* NOTREACHED */
 }
 }