Home | History | Annotate | Line # | Download | only in xinstall
xinstall.c revision 1.4
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1987 Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.1      cgd char copyright[] =
     36  1.1      cgd "@(#) Copyright (c) 1987 Regents of the University of California.\n\
     37  1.1      cgd  All rights reserved.\n";
     38  1.1      cgd #endif /* not lint */
     39  1.1      cgd 
     40  1.1      cgd #ifndef lint
     41  1.3  mycroft /*static char sccsid[] = "from: @(#)xinstall.c	5.24 (Berkeley) 7/1/90";*/
     42  1.4      cgd static char rcsid[] = "$Id: xinstall.c,v 1.4 1994/10/02 21:32:31 cgd Exp $";
     43  1.1      cgd #endif /* not lint */
     44  1.1      cgd 
     45  1.1      cgd #include <sys/param.h>
     46  1.1      cgd #include <sys/stat.h>
     47  1.1      cgd #include <sys/file.h>
     48  1.1      cgd #include <grp.h>
     49  1.1      cgd #include <pwd.h>
     50  1.1      cgd #include <stdio.h>
     51  1.1      cgd #include <ctype.h>
     52  1.2      jtc #include <errno.h>
     53  1.1      cgd #include <paths.h>
     54  1.1      cgd #include "pathnames.h"
     55  1.1      cgd 
     56  1.1      cgd static struct passwd *pp;
     57  1.1      cgd static struct group *gp;
     58  1.2      jtc static int docopy, dostrip, dodir, mode = 0755;
     59  1.1      cgd static char *group, *owner, pathbuf[MAXPATHLEN];
     60  1.1      cgd 
     61  1.1      cgd main(argc, argv)
     62  1.1      cgd 	int argc;
     63  1.1      cgd 	char **argv;
     64  1.1      cgd {
     65  1.1      cgd 	extern char *optarg;
     66  1.1      cgd 	extern int optind;
     67  1.1      cgd 	struct stat from_sb, to_sb;
     68  1.1      cgd 	mode_t *set, *setmode();
     69  1.1      cgd 	int ch, no_target;
     70  1.1      cgd 	char *to_name;
     71  1.1      cgd 
     72  1.2      jtc 	while ((ch = getopt(argc, argv, "cg:m:o:sd")) != EOF)
     73  1.1      cgd 		switch((char)ch) {
     74  1.1      cgd 		case 'c':
     75  1.1      cgd 			docopy = 1;
     76  1.1      cgd 			break;
     77  1.1      cgd 		case 'g':
     78  1.1      cgd 			group = optarg;
     79  1.1      cgd 			break;
     80  1.1      cgd 		case 'm':
     81  1.1      cgd 			if (!(set = setmode(optarg))) {
     82  1.1      cgd 				(void)fprintf(stderr,
     83  1.1      cgd 				    "install: invalid file mode.\n");
     84  1.1      cgd 				exit(1);
     85  1.1      cgd 			}
     86  1.1      cgd 			mode = getmode(set, 0);
     87  1.1      cgd 			break;
     88  1.1      cgd 		case 'o':
     89  1.1      cgd 			owner = optarg;
     90  1.1      cgd 			break;
     91  1.1      cgd 		case 's':
     92  1.1      cgd 			dostrip = 1;
     93  1.1      cgd 			break;
     94  1.2      jtc 		case 'd':
     95  1.2      jtc 			dodir = 1;
     96  1.2      jtc 			break;
     97  1.1      cgd 		case '?':
     98  1.1      cgd 		default:
     99  1.1      cgd 			usage();
    100  1.1      cgd 		}
    101  1.1      cgd 	argc -= optind;
    102  1.1      cgd 	argv += optind;
    103  1.2      jtc 
    104  1.2      jtc 	/* copy and strip options make no sense when creating directories */
    105  1.2      jtc 	if ((docopy || dostrip) && dodir)
    106  1.2      jtc 		usage();
    107  1.2      jtc 
    108  1.2      jtc 	/* must have at least two arguments, except when creating directories */
    109  1.2      jtc 	if (argc < 2 && !dodir)
    110  1.1      cgd 		usage();
    111  1.1      cgd 
    112  1.1      cgd 	/* get group and owner id's */
    113  1.1      cgd 	if (group && !(gp = getgrnam(group))) {
    114  1.1      cgd 		fprintf(stderr, "install: unknown group %s.\n", group);
    115  1.1      cgd 		exit(1);
    116  1.1      cgd 	}
    117  1.1      cgd 	if (owner && !(pp = getpwnam(owner))) {
    118  1.1      cgd 		fprintf(stderr, "install: unknown user %s.\n", owner);
    119  1.1      cgd 		exit(1);
    120  1.1      cgd 	}
    121  1.1      cgd 
    122  1.2      jtc 	if (dodir) {
    123  1.2      jtc 		for (; *argv != NULL; ++argv)
    124  1.2      jtc 			build(*argv);
    125  1.2      jtc 		exit (0);
    126  1.2      jtc 		/* NOTREACHED */
    127  1.2      jtc 	}
    128  1.2      jtc 
    129  1.1      cgd 	no_target = stat(to_name = argv[argc - 1], &to_sb);
    130  1.2      jtc 	if (!no_target && S_ISDIR(to_sb.st_mode)) {
    131  1.1      cgd 		for (; *argv != to_name; ++argv)
    132  1.1      cgd 			install(*argv, to_name, 1);
    133  1.1      cgd 		exit(0);
    134  1.1      cgd 	}
    135  1.1      cgd 
    136  1.1      cgd 	/* can't do file1 file2 directory/file */
    137  1.1      cgd 	if (argc != 2)
    138  1.1      cgd 		usage();
    139  1.1      cgd 
    140  1.1      cgd 	if (!no_target) {
    141  1.1      cgd 		if (stat(*argv, &from_sb)) {
    142  1.1      cgd 			fprintf(stderr, "install: can't find %s.\n", *argv);
    143  1.1      cgd 			exit(1);
    144  1.1      cgd 		}
    145  1.2      jtc 		if (!S_ISREG(to_sb.st_mode)) {
    146  1.1      cgd 			fprintf(stderr, "install: %s isn't a regular file.\n", to_name);
    147  1.1      cgd 			exit(1);
    148  1.1      cgd 		}
    149  1.1      cgd 		if (to_sb.st_dev == from_sb.st_dev && to_sb.st_ino == from_sb.st_ino) {
    150  1.1      cgd 			fprintf(stderr, "install: %s and %s are the same file.\n", *argv, to_name);
    151  1.1      cgd 			exit(1);
    152  1.1      cgd 		}
    153  1.1      cgd 		/* unlink now... avoid ETXTBSY errors later */
    154  1.1      cgd 		(void)unlink(to_name);
    155  1.1      cgd 	}
    156  1.1      cgd 	install(*argv, to_name, 0);
    157  1.1      cgd 	exit(0);
    158  1.1      cgd }
    159  1.1      cgd 
    160  1.1      cgd /*
    161  1.1      cgd  * install --
    162  1.1      cgd  *	build a path name and install the file
    163  1.1      cgd  */
    164  1.1      cgd install(from_name, to_name, isdir)
    165  1.1      cgd 	char *from_name, *to_name;
    166  1.1      cgd 	int isdir;
    167  1.1      cgd {
    168  1.1      cgd 	struct stat from_sb;
    169  1.1      cgd 	int devnull, from_fd, to_fd;
    170  1.1      cgd 	char *C, *rindex();
    171  1.1      cgd 
    172  1.1      cgd 	/* if try to install NULL file to a directory, fails */
    173  1.1      cgd 	if (isdir || strcmp(from_name, _PATH_DEVNULL)) {
    174  1.1      cgd 		if (stat(from_name, &from_sb)) {
    175  1.1      cgd 			fprintf(stderr, "install: can't find %s.\n", from_name);
    176  1.1      cgd 			exit(1);
    177  1.1      cgd 		}
    178  1.2      jtc 		if (!S_ISREG(from_sb.st_mode)) {
    179  1.1      cgd 			fprintf(stderr, "install: %s isn't a regular file.\n", from_name);
    180  1.1      cgd 			exit(1);
    181  1.1      cgd 		}
    182  1.1      cgd 		/* build the target path */
    183  1.1      cgd 		if (isdir) {
    184  1.1      cgd 			(void)sprintf(pathbuf, "%s/%s", to_name, (C = rindex(from_name, '/')) ? ++C : from_name);
    185  1.1      cgd 			to_name = pathbuf;
    186  1.1      cgd 		}
    187  1.1      cgd 		devnull = 0;
    188  1.1      cgd 	} else
    189  1.1      cgd 		devnull = 1;
    190  1.1      cgd 
    191  1.1      cgd 	/* unlink now... avoid ETXTBSY errors later */
    192  1.1      cgd 	(void)unlink(to_name);
    193  1.1      cgd 
    194  1.1      cgd 	/* create target */
    195  1.1      cgd 	if ((to_fd = open(to_name, O_CREAT|O_WRONLY|O_TRUNC, 0600)) < 0) {
    196  1.1      cgd 		error(to_name);
    197  1.1      cgd 		exit(1);
    198  1.1      cgd 	}
    199  1.1      cgd 	if (!devnull) {
    200  1.1      cgd 		if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
    201  1.1      cgd 			(void)unlink(to_name);
    202  1.1      cgd 			error(from_name);
    203  1.1      cgd 			exit(1);
    204  1.1      cgd 		}
    205  1.1      cgd 		copy(from_fd, from_name, to_fd, to_name);
    206  1.1      cgd 		(void)close(from_fd);
    207  1.1      cgd 	}
    208  1.1      cgd 	if (dostrip)
    209  1.1      cgd 		strip(to_name);
    210  1.1      cgd 	/*
    211  1.1      cgd 	 * set owner, group, mode for target; do the chown first,
    212  1.1      cgd 	 * chown may lose the setuid bits.
    213  1.1      cgd 	 */
    214  1.1      cgd 	if ((group || owner) &&
    215  1.1      cgd 	    fchown(to_fd, owner ? pp->pw_uid : -1, group ? gp->gr_gid : -1) ||
    216  1.1      cgd 	    fchmod(to_fd, mode)) {
    217  1.1      cgd 		error(to_name);
    218  1.1      cgd 		bad(to_name);
    219  1.1      cgd 	}
    220  1.1      cgd 	(void)close(to_fd);
    221  1.1      cgd 	if (!docopy && !devnull && unlink(from_name)) {
    222  1.1      cgd 		error(from_name);
    223  1.1      cgd 		exit(1);
    224  1.1      cgd 	}
    225  1.1      cgd }
    226  1.1      cgd 
    227  1.1      cgd /*
    228  1.1      cgd  * copy --
    229  1.1      cgd  *	copy from one file to another
    230  1.1      cgd  */
    231  1.1      cgd copy(from_fd, from_name, to_fd, to_name)
    232  1.1      cgd 	register int from_fd, to_fd;
    233  1.1      cgd 	char *from_name, *to_name;
    234  1.1      cgd {
    235  1.1      cgd 	register int n;
    236  1.1      cgd 	char buf[MAXBSIZE];
    237  1.1      cgd 
    238  1.1      cgd 	while ((n = read(from_fd, buf, sizeof(buf))) > 0)
    239  1.1      cgd 		if (write(to_fd, buf, n) != n) {
    240  1.1      cgd 			error(to_name);
    241  1.1      cgd 			bad(to_name);
    242  1.1      cgd 		}
    243  1.1      cgd 	if (n == -1) {
    244  1.1      cgd 		error(from_name);
    245  1.1      cgd 		bad(to_name);
    246  1.1      cgd 	}
    247  1.1      cgd }
    248  1.1      cgd 
    249  1.1      cgd /*
    250  1.1      cgd  * strip --
    251  1.1      cgd  *	use strip(1) to strip the target file
    252  1.1      cgd  */
    253  1.1      cgd strip(to_name)
    254  1.1      cgd 	char *to_name;
    255  1.1      cgd {
    256  1.1      cgd 	int status;
    257  1.1      cgd 
    258  1.1      cgd 	switch (vfork()) {
    259  1.1      cgd 	case -1:
    260  1.1      cgd 		error("fork");
    261  1.1      cgd 		bad(to_name);
    262  1.1      cgd 	case 0:
    263  1.1      cgd 		execl(_PATH_STRIP, "strip", to_name, (char *)NULL);
    264  1.1      cgd 		error(_PATH_STRIP);
    265  1.1      cgd 		_exit(1);
    266  1.1      cgd 	default:
    267  1.1      cgd 		if (wait(&status) == -1 || status)
    268  1.1      cgd 			bad(to_name);
    269  1.1      cgd 	}
    270  1.1      cgd }
    271  1.1      cgd 
    272  1.1      cgd /*
    273  1.2      jtc  * build --
    274  1.2      jtc  *	build directory heirarchy
    275  1.2      jtc  */
    276  1.2      jtc build(path)
    277  1.2      jtc         char *path;
    278  1.2      jtc {
    279  1.2      jtc         register char *p;
    280  1.2      jtc         struct stat sb;
    281  1.4      cgd         int ch;
    282  1.2      jtc 
    283  1.4      cgd         for (p = path;; ++p)
    284  1.4      cgd                 if (!*p || (p != path && *p  == '/')) {
    285  1.2      jtc                         ch = *p;
    286  1.2      jtc                         *p = '\0';
    287  1.2      jtc                         if (stat(path, &sb)) {
    288  1.2      jtc                                 if (errno != ENOENT || mkdir(path, 0777) < 0) {
    289  1.2      jtc                                         error(path);
    290  1.2      jtc                                         return(1);
    291  1.2      jtc                                 }
    292  1.2      jtc                         }
    293  1.2      jtc                         if (!(*p = ch))
    294  1.4      cgd 				break;
    295  1.2      jtc                 }
    296  1.2      jtc 
    297  1.2      jtc 	if ((group || owner) &&
    298  1.2      jtc             chown(path, owner ? pp->pw_uid : -1, group ? gp->gr_gid : -1) ||
    299  1.2      jtc             chmod(path, mode)) {
    300  1.2      jtc                 error(path);
    301  1.2      jtc         }
    302  1.2      jtc 
    303  1.2      jtc         return(0);
    304  1.2      jtc }
    305  1.2      jtc 
    306  1.2      jtc /*
    307  1.1      cgd  * error --
    308  1.1      cgd  *	print out an error message
    309  1.1      cgd  */
    310  1.1      cgd error(s)
    311  1.1      cgd 	char *s;
    312  1.1      cgd {
    313  1.1      cgd 	extern int errno;
    314  1.1      cgd 	char *strerror();
    315  1.1      cgd 
    316  1.1      cgd 	(void)fprintf(stderr, "install: %s: %s\n", s, strerror(errno));
    317  1.1      cgd }
    318  1.1      cgd 
    319  1.1      cgd /*
    320  1.1      cgd  * bad --
    321  1.1      cgd  *	remove created target and die
    322  1.1      cgd  */
    323  1.1      cgd bad(fname)
    324  1.1      cgd 	char *fname;
    325  1.1      cgd {
    326  1.1      cgd 	(void)unlink(fname);
    327  1.1      cgd 	exit(1);
    328  1.1      cgd }
    329  1.1      cgd 
    330  1.1      cgd /*
    331  1.1      cgd  * usage --
    332  1.1      cgd  *	print a usage message and die
    333  1.1      cgd  */
    334  1.1      cgd usage()
    335  1.1      cgd {
    336  1.2      jtc 	(void)fprintf(stderr, "\
    337  1.2      jtc usage: install [-cs] [-g group] [-m mode] [-o owner] file1 file2\n\
    338  1.2      jtc        install [-cs] [-g group] [-m mode] [-o owner] file1 ... fileN directory\n\
    339  1.2      jtc        install  -d   [-g group] [-m mode] [-o owner] directory ...\n");
    340  1.1      cgd 	exit(1);
    341  1.1      cgd }
    342