Home | History | Annotate | Line # | Download | only in xinstall
xinstall.c revision 1.46
      1 /*	$NetBSD: xinstall.c,v 1.46 2001/06/15 08:25:11 kleink Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1987, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)xinstall.c	8.1 (Berkeley) 7/21/93";
     45 #else
     46 __RCSID("$NetBSD: xinstall.c,v 1.46 2001/06/15 08:25:11 kleink Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/wait.h>
     52 #include <sys/mman.h>
     53 #include <sys/stat.h>
     54 
     55 #include <ctype.h>
     56 #include <errno.h>
     57 #include <fcntl.h>
     58 #include <grp.h>
     59 #include <paths.h>
     60 #include <pwd.h>
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 #include <string.h>
     64 #include <unistd.h>
     65 #include <err.h>
     66 
     67 #include "pathnames.h"
     68 #include "stat_flags.h"
     69 
     70 #define STRIP_ARGS_MAX 32
     71 #define BACKUP_SUFFIX ".old"
     72 
     73 struct passwd *pp;
     74 struct group *gp;
     75 int dobackup=0, docopy=0, dodir=0, dostrip=0, dolink=0, dopreserve=0,
     76     dorename = 0, unpriv = 0;
     77 static int numberedbackup = 0;
     78 int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
     79 char pathbuf[MAXPATHLEN];
     80 uid_t uid;
     81 gid_t gid;
     82 char *stripArgs=NULL;
     83 char *suffix=BACKUP_SUFFIX;
     84 
     85 #define LN_ABSOLUTE	0x01
     86 #define LN_RELATIVE	0x02
     87 #define LN_HARD		0x04
     88 #define LN_SYMBOLIC	0x08
     89 #define LN_MIXED	0x10
     90 
     91 #define	DIRECTORY	0x01		/* Tell install it's a directory. */
     92 #define	SETFLAGS	0x02		/* Tell install to set flags. */
     93 
     94 void	copy __P((int, char *, int, char *, off_t));
     95 void	makelink __P((char *, char *));
     96 void	install __P((char *, char *, u_long, u_int));
     97 void	install_dir __P((char *));
     98 void	strip __P((char *));
     99 void	backup __P((const char *));
    100 void	usage __P((void));
    101 int	main __P((int, char *[]));
    102 
    103 int
    104 main(argc, argv)
    105 	int argc;
    106 	char *argv[];
    107 {
    108 	struct stat from_sb, to_sb;
    109 	void *set;
    110 	u_long fset;
    111 	u_int iflags;
    112 	int ch, no_target;
    113 	char *p;
    114 	char *flags = NULL, *to_name, *group = NULL, *owner = NULL;
    115 
    116 	setprogname(argv[0]);
    117 
    118 	iflags = 0;
    119 	while ((ch = getopt(argc, argv, "cbB:df:g:l:m:o:prsS:U")) != -1)
    120 		switch((char)ch) {
    121 		case 'B':
    122 			suffix = optarg;
    123 			numberedbackup = 0;
    124 			{
    125 				/* Check if given suffix really generates
    126 				   different suffixes - catch e.g. ".%" */
    127 				char suffix_expanded0[FILENAME_MAX],
    128 				     suffix_expanded1[FILENAME_MAX];
    129 				(void)snprintf(suffix_expanded0, FILENAME_MAX,
    130 					       suffix, 0);
    131 				(void)snprintf(suffix_expanded1, FILENAME_MAX,
    132 					       suffix, 1);
    133 				if (strcmp(suffix_expanded0, suffix_expanded1)
    134 				    != 0)
    135 					numberedbackup = 1;
    136 			}
    137 			/* fall through; -B implies -b */
    138 		case 'b':
    139 			dobackup = 1;
    140 			break;
    141 		case 'c':
    142 			docopy = 1;
    143 			break;
    144 		case 'd':
    145 			dodir = 1;
    146 			break;
    147 		case 'f':
    148 			flags = optarg;
    149 			if (string_to_flags(&flags, &fset, NULL))
    150 				errx(1, "%s: invalid flag", flags);
    151 			iflags |= SETFLAGS;
    152 			break;
    153 		case 'g':
    154 			group = optarg;
    155 			break;
    156 		case 'l':
    157 			for (p = optarg; *p; p++)
    158 				switch (*p) {
    159 				case 's':
    160 					dolink &= ~(LN_HARD|LN_MIXED);
    161 					dolink |= LN_SYMBOLIC;
    162 					break;
    163 				case 'h':
    164 					dolink &= ~(LN_SYMBOLIC|LN_MIXED);
    165 					dolink |= LN_HARD;
    166 					break;
    167 				case 'm':
    168 					dolink &= ~(LN_SYMBOLIC|LN_HARD);
    169 					dolink |= LN_MIXED;
    170 					break;
    171 				case 'a':
    172 					dolink &= ~LN_RELATIVE;
    173 					dolink |= LN_ABSOLUTE;
    174 					break;
    175 				case 'r':
    176 					dolink &= ~LN_ABSOLUTE;
    177 					dolink |= LN_RELATIVE;
    178 					break;
    179 				default:
    180 					errx(1, "%c: invalid link type", *p);
    181 					break;
    182 				}
    183 			break;
    184 		case 'm':
    185 			if (!(set = setmode(optarg)))
    186 				errx(1, "%s: invalid file mode", optarg);
    187 			mode = getmode(set, 0);
    188 			free(set);
    189 			break;
    190 		case 'o':
    191 			owner = optarg;
    192 			break;
    193 		case 'p':
    194 			dopreserve = 1;
    195 			break;
    196 		case 'r':
    197 			dorename = 1;
    198 			break;
    199 		case 'S':
    200 			stripArgs = (char*)malloc(sizeof(char)*(strlen(optarg)+1));
    201 			strcpy(stripArgs,optarg);
    202 			/* fall through; -S implies -s */
    203 		case 's':
    204 			dostrip = 1;
    205 			break;
    206 		case 'U':
    207 			unpriv = 1;
    208 			break;
    209 		case '?':
    210 		default:
    211 			usage();
    212 		}
    213 	argc -= optind;
    214 	argv += optind;
    215 
    216 	/* strip and link options make no sense when creating directories */
    217 	if ((dostrip || dolink) && dodir)
    218 		usage();
    219 
    220 	/* strip and flags make no sense with links */
    221 	if ((dostrip || flags) && dolink)
    222 		usage();
    223 
    224 	/* must have at least two arguments, except when creating directories */
    225 	if (argc < 2 && !dodir)
    226 		usage();
    227 
    228 	/* get group and owner id's */
    229 	if (unpriv)
    230 		uid = gid = -1;
    231 	else {
    232 		if (group && !(gp = getgrnam(group)) &&
    233 		    !isdigit((unsigned char)*group))
    234 			errx(1, "unknown group %s", group);
    235 		gid = (group) ? ((gp) ? gp->gr_gid : atoi(group)) : -1;
    236 		if (owner && !(pp = getpwnam(owner)) &&
    237 		    !isdigit((unsigned char)*owner))
    238 			errx(1, "unknown user %s", owner);
    239 		uid = (owner) ? ((pp) ? pp->pw_uid : atoi(owner)) : -1;
    240 	}
    241 
    242 	if (dodir) {
    243 		for (; *argv != NULL; ++argv)
    244 			install_dir(*argv);
    245 		exit (0);
    246 	}
    247 
    248 	no_target = stat(to_name = argv[argc - 1], &to_sb);
    249 	if (!no_target && S_ISDIR(to_sb.st_mode)) {
    250 		for (; *argv != to_name; ++argv)
    251 			install(*argv, to_name, fset, iflags | DIRECTORY);
    252 		exit(0);
    253 	}
    254 
    255 	/* can't do file1 file2 directory/file */
    256 	if (argc != 2)
    257 		usage();
    258 
    259 	if (!no_target) {
    260 		if (stat(*argv, &from_sb))
    261 			err(1, "%s", *argv);
    262 		if (!S_ISREG(to_sb.st_mode))
    263 			errx(1, "%s: not a regular file", to_name);
    264 		if (!dolink && to_sb.st_dev == from_sb.st_dev &&
    265 		    to_sb.st_ino == from_sb.st_ino)
    266 			errx(1, "%s and %s are the same file", *argv, to_name);
    267 		/*
    268 		 * Unlink now... avoid ETXTBSY errors later.  Try and turn
    269 		 * off the append/immutable bits -- if we fail, go ahead,
    270 		 * it might work.
    271 		 */
    272 #define	NOCHANGEBITS	(UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
    273 		if (to_sb.st_flags & NOCHANGEBITS)
    274 			(void)chflags(to_name,
    275 			    to_sb.st_flags & ~(NOCHANGEBITS));
    276 		if (dobackup)
    277 			backup(to_name);
    278 		else if (!dorename)
    279 			(void)unlink(to_name);
    280 	}
    281 	install(*argv, to_name, fset, iflags);
    282 	exit(0);
    283 }
    284 
    285 /*
    286  * makelink --
    287  *	make a link from source to destination
    288  */
    289 void
    290 makelink(from_name, to_name)
    291 	char *from_name;
    292 	char *to_name;
    293 {
    294 	char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
    295 
    296 	/* Try hard links first */
    297 	if (dolink & (LN_HARD|LN_MIXED)) {
    298 		if (link(from_name, to_name) == -1) {
    299 			if ((dolink & LN_HARD) || errno != EXDEV)
    300 				err(1, "link %s -> %s", from_name, to_name);
    301 		}
    302 		else
    303 			return;
    304 	}
    305 
    306 	/* Symbolic links */
    307 	if (dolink & LN_ABSOLUTE) {
    308 		/* Convert source path to absolute */
    309 		if (realpath(from_name, src) == NULL)
    310 			err(1, "%s", src);
    311 		if (symlink(src, to_name) == -1)
    312 			err(1, "symlink %s -> %s", src, to_name);
    313 		return;
    314 	}
    315 
    316 	if (dolink & LN_RELATIVE) {
    317 		char *s, *d;
    318 
    319 		/* Resolve pathnames */
    320 		if (realpath(from_name, src) == NULL)
    321 			err(1, "%s", src);
    322 		if (realpath(to_name, dst) == NULL)
    323 			err(1, "%s", dst);
    324 
    325 		/* trim common path components */
    326 		for (s = src, d = dst; *s == *d; s++, d++)
    327 			continue;
    328 		while (*s != '/')
    329 			s--, d--;
    330 
    331 		/* count the number of directories we need to backtrack */
    332 		for (++d, lnk[0] = '\0'; *d; d++)
    333 			if (*d == '/')
    334 				(void) strcat(lnk, "../");
    335 
    336 		(void) strcat(lnk, ++s);
    337 
    338 		if (symlink(lnk, dst) == -1)
    339 			err(1, "symlink %s -> %s", lnk, dst);
    340 		return;
    341 	}
    342 
    343 	/*
    344 	 * If absolute or relative was not specified,
    345 	 * try the names the user provided
    346 	 */
    347 	if (symlink(from_name, to_name) == -1)
    348 		err(1, "symlink %s -> %s", from_name, to_name);
    349 
    350 }
    351 
    352 /*
    353  * install --
    354  *	build a path name and install the file
    355  */
    356 void
    357 install(from_name, to_name, fset, flags)
    358 	char *from_name, *to_name;
    359 	u_long fset;
    360 	u_int flags;
    361 {
    362 	struct stat from_sb, to_sb;
    363 	int devnull, from_fd, to_fd, serrno;
    364 	char *p, tmpl[MAXPATHLEN], *oto_name;
    365 
    366 	/* If try to install NULL file to a directory, fails. */
    367 	if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
    368 		if (stat(from_name, &from_sb))
    369 			err(1, "%s", from_name);
    370 		if (!S_ISREG(from_sb.st_mode))
    371 			errx(1, "%s: not a regular file", to_name);
    372 		/* Build the target path. */
    373 		if (flags & DIRECTORY) {
    374 			(void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
    375 			    to_name,
    376 			    (p = strrchr(from_name, '/')) ? ++p : from_name);
    377 			to_name = pathbuf;
    378 		}
    379 		devnull = 0;
    380 	} else {
    381 		from_sb.st_flags = 0;	/* XXX */
    382 		devnull = 1;
    383 	}
    384 
    385 	/*
    386 	 * Unlink now... avoid ETXTBSY errors later.  Try and turn
    387 	 * off the append/immutable bits -- if we fail, go ahead,
    388 	 * it might work.
    389 	 */
    390 	if (stat(to_name, &to_sb) == 0 &&
    391 	    to_sb.st_flags & (NOCHANGEBITS))
    392 		(void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
    393 	if (dorename) {
    394 		char *ptr, c, *dir;
    395 
    396 		if ((ptr = strrchr(to_name, '/')) != NULL) {
    397 			c = *++ptr;
    398 			*ptr = '\0';
    399 			dir = to_name;
    400 		} else {
    401 			c = '\0';	/* pacify gcc */
    402 			dir = tmpl;
    403 			*dir = '\0';
    404 		}
    405 		(void)snprintf(tmpl, sizeof(tmpl), "%sinst.XXXXXX", dir);
    406 		if (ptr)
    407 			*ptr = c;
    408 		oto_name = to_name;
    409 		to_name = tmpl;
    410 
    411 	} else {
    412 		oto_name = NULL;	/* pacify gcc */
    413 		if (dobackup)
    414 			backup(to_name);
    415 		else
    416 			(void)unlink(to_name);
    417 	}
    418 
    419 	if (dolink) {
    420 		makelink(from_name, to_name);
    421 		return;
    422 	}
    423 
    424 	/* Create target. */
    425 	if (dorename) {
    426 		if ((to_fd = mkstemp(to_name)) == -1)
    427 			err(1, "%s", to_name);
    428 	} else {
    429 		if ((to_fd = open(to_name,
    430 		    O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
    431 			err(1, "%s", to_name);
    432 	}
    433 	if (!devnull) {
    434 		if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
    435 			(void)unlink(to_name);
    436 			err(1, "%s", from_name);
    437 		}
    438 		copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
    439 		(void)close(from_fd);
    440 	}
    441 
    442 	if (dostrip) {
    443 		strip(to_name);
    444 
    445 		/*
    446 		 * Re-open our fd on the target, in case we used a strip
    447 		 *  that does not work in-place -- like gnu binutils strip.
    448 		 */
    449 		close(to_fd);
    450 		if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
    451 		  err(1, "stripping %s", to_name);
    452 	}
    453 
    454 	/*
    455 	 * Set owner, group, mode for target; do the chown first,
    456 	 * chown may lose the setuid bits.
    457 	 */
    458 	if ((gid != -1 || uid != -1) && fchown(to_fd, uid, gid)) {
    459 		serrno = errno;
    460 		(void)unlink(to_name);
    461 		errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
    462 	}
    463 	if (fchmod(to_fd, mode)) {
    464 		serrno = errno;
    465 		(void)unlink(to_name);
    466 		errx(1, "%s: chmod: %s", to_name, strerror(serrno));
    467 	}
    468 
    469 	/*
    470 	 * If provided a set of flags, set them, otherwise, preserve the
    471 	 * flags, except for the dump flag.
    472 	 */
    473 	if (fchflags(to_fd,
    474 	    flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
    475 		if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
    476 			warn("%s: chflags", to_name);
    477 	}
    478 
    479 	/*
    480 	 * Preserve the date of the source file.
    481 	 */
    482 	if (dopreserve) {
    483 		struct timeval tv[2];
    484 
    485 #ifdef BSD4_4
    486 		TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
    487 		TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
    488 #else
    489 		tv[0].tv_sec = from_sb.st_atime;
    490 		tv[0].tv_usec = 0;
    491 		tv[1].tv_sec = from_sb.st_mtime;
    492 		tv[1].tv_usec = 0;
    493 #endif
    494 		if (futimes(to_fd, tv) == -1)
    495 			warn("%s: futimes", to_name);
    496 	}
    497 
    498 	(void)close(to_fd);
    499 
    500 	if (dorename)
    501 		if (rename(to_name, oto_name) == -1)
    502 			err(1, "%s: rename", to_name);
    503 
    504 	if (!docopy && !devnull && unlink(from_name))
    505 		err(1, "%s", from_name);
    506 }
    507 
    508 /*
    509  * copy --
    510  *	copy from one file to another
    511  */
    512 void
    513 copy(from_fd, from_name, to_fd, to_name, size)
    514 	int from_fd, to_fd;
    515 	char *from_name, *to_name;
    516 	off_t size;
    517 {
    518 	ssize_t nr, nw;
    519 	int serrno;
    520 	char *p;
    521 	char buf[MAXBSIZE];
    522 
    523 	/*
    524 	 * There's no reason to do anything other than close the file
    525 	 * now if it's empty, so let's not bother.
    526 	 */
    527 	if (size > 0) {
    528 
    529 		/*
    530 		 * Mmap and write if less than 8M (the limit is so we
    531 		 * don't totally trash memory on big files).  This is
    532 		 * really a minor hack, but it wins some CPU back.
    533 		 */
    534 
    535 		if (size <= 8 * 1048576) {
    536 			if ((p = mmap(NULL, (size_t)size, PROT_READ,
    537 			    MAP_FILE|MAP_SHARED, from_fd, (off_t)0))
    538 			    == MAP_FAILED) {
    539 				goto mmap_failed;
    540 			}
    541 #ifdef MADV_SEQUENTIAL
    542 			if (madvise(p, (size_t)size, MADV_SEQUENTIAL) == -1
    543 			    && errno != EOPNOTSUPP)
    544 				warnx("madvise: %s", strerror(errno));
    545 #endif
    546 
    547 			if (write(to_fd, p, size) != size) {
    548 				serrno = errno;
    549 				(void)unlink(to_name);
    550 				errx(1, "%s: %s",
    551 				    to_name, strerror(serrno));
    552 			}
    553 		} else {
    554 mmap_failed:
    555 			while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
    556 				if ((nw = write(to_fd, buf, nr)) != nr) {
    557 					serrno = errno;
    558 					(void)unlink(to_name);
    559 					errx(1, "%s: %s", to_name,
    560 					    strerror(nw > 0 ? EIO : serrno));
    561 				}
    562 			}
    563 			if (nr != 0) {
    564 				serrno = errno;
    565 				(void)unlink(to_name);
    566 				errx(1, "%s: %s", from_name, strerror(serrno));
    567 			}
    568 		}
    569 	}
    570 }
    571 
    572 /*
    573  * strip --
    574  *	use strip(1) to strip the target file
    575  */
    576 void
    577 strip(to_name)
    578 	char *to_name;
    579 {
    580 	int serrno, status;
    581 	char *stripprog;
    582 
    583 	switch (vfork()) {
    584 	case -1:
    585 		serrno = errno;
    586 		(void)unlink(to_name);
    587 		errx(1, "vfork: %s", strerror(serrno));
    588 	case 0:
    589 		stripprog = getenv("STRIP");
    590 		if (stripprog == NULL)
    591 			stripprog = _PATH_STRIP;
    592 
    593 		if (stripArgs) {
    594 			/* build up a command line and let /bin/sh parse the arguments */
    595 			char* cmd = (char*)malloc(sizeof(char)*
    596 						  (3+strlen(stripprog)+
    597 						     strlen(stripArgs)+
    598 						     strlen(to_name)));
    599 
    600 			sprintf(cmd, "%s %s %s", stripprog, stripArgs, to_name);
    601 
    602 			execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
    603 		} else
    604 			execlp(stripprog, "strip", to_name, NULL);
    605 
    606 		warn("%s", stripprog);
    607 		_exit(1);
    608 	default:
    609 		if (wait(&status) == -1 || status)
    610 			(void)unlink(to_name);
    611 	}
    612 }
    613 
    614 /*
    615  * backup file "to_name" to to_name.suffix
    616  * if suffix contains a "%", it's taken as a printf(3) pattern
    617  * used for a numbered backup.
    618  */
    619 void
    620 backup(to_name)
    621 	const char *to_name;
    622 {
    623 	char backup[FILENAME_MAX];
    624 
    625 	if (numberedbackup) {
    626 		/* Do numbered backup */
    627 		int cnt;
    628 		char suffix_expanded[FILENAME_MAX];
    629 
    630 		cnt=0;
    631 		do {
    632 			(void)snprintf(suffix_expanded, FILENAME_MAX, suffix, cnt);
    633 			(void)snprintf(backup, FILENAME_MAX, "%s%s",
    634 				       to_name, suffix_expanded);
    635 			cnt++;
    636 		} while (access(backup, F_OK)==0);
    637 	} else {
    638 		/* Do simple backup */
    639 		(void)snprintf(backup, FILENAME_MAX, "%s%s", to_name, suffix);
    640 	}
    641 
    642 	(void)rename(to_name, backup);
    643 }
    644 
    645 /*
    646  * install_dir --
    647  *	build directory heirarchy
    648  */
    649 void
    650 install_dir(path)
    651         char *path;
    652 {
    653         char *p;
    654         struct stat sb;
    655         int ch;
    656 
    657         for (p = path;; ++p)
    658                 if (!*p || (p != path && *p  == '/')) {
    659                         ch = *p;
    660                         *p = '\0';
    661                         if (stat(path, &sb)) {
    662                                 if (errno != ENOENT || mkdir(path, 0777) < 0) {
    663 					err(1, "%s", path);
    664 					/* NOTREACHED */
    665                                 }
    666                         }
    667                         if (!(*p = ch))
    668 				break;
    669                 }
    670 
    671 	if (((gid != -1 || uid != -1) && chown(path, uid, gid)) ||
    672             chmod(path, mode)) {
    673                 warn("%s", path);
    674 	}
    675 }
    676 
    677 /*
    678  * usage --
    679  *	print a usage message and die
    680  */
    681 void
    682 usage()
    683 {
    684 	(void)fprintf(stderr, "\
    685 usage: install [-Ubcprs] [-B suffix] [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 file2\n\
    686        install [-Ubcprs] [-B suffix] [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 ... fileN directory\n\
    687        install [-Up] -d [-m mode] [-o owner] [-g group] directory ...\n");
    688 	exit(1);
    689 }
    690