Home | History | Annotate | Line # | Download | only in xinstall
xinstall.c revision 1.39
      1 /*	$NetBSD: xinstall.c,v 1.39 2000/01/15 01:10:12 christos 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.39 2000/01/15 01:10:12 christos 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 	mode_t *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 	iflags = 0;
    117 	while ((ch = getopt(argc, argv, "cbB:df:g:l:m:o:prsS:U")) != -1)
    118 		switch((char)ch) {
    119 		case 'B':
    120 			suffix = optarg;
    121 			numberedbackup = 0;
    122 			{
    123 				/* Check if given suffix really generates
    124 				   different suffixes - catch e.g. ".%" */
    125 				char suffix_expanded0[FILENAME_MAX],
    126 				     suffix_expanded1[FILENAME_MAX];
    127 				(void)snprintf(suffix_expanded0, FILENAME_MAX,
    128 					       suffix, 0);
    129 				(void)snprintf(suffix_expanded1, FILENAME_MAX,
    130 					       suffix, 1);
    131 				if (strcmp(suffix_expanded0, suffix_expanded1)
    132 				    != 0)
    133 					numberedbackup = 1;
    134 			}
    135 			/* fall through; -B implies -b */
    136 		case 'b':
    137 			dobackup = 1;
    138 			break;
    139 		case 'c':
    140 			docopy = 1;
    141 			break;
    142 		case 'd':
    143 			dodir = 1;
    144 			break;
    145 		case 'f':
    146 			flags = optarg;
    147 			if (string_to_flags(&flags, &fset, NULL))
    148 				errx(1, "%s: invalid flag", flags);
    149 			iflags |= SETFLAGS;
    150 			break;
    151 		case 'g':
    152 			group = optarg;
    153 			break;
    154 		case 'l':
    155 			for (p = optarg; *p; p++)
    156 				switch (*p) {
    157 				case 's':
    158 					dolink &= ~(LN_HARD|LN_MIXED);
    159 					dolink |= LN_SYMBOLIC;
    160 					break;
    161 				case 'h':
    162 					dolink &= ~(LN_SYMBOLIC|LN_MIXED);
    163 					dolink |= LN_HARD;
    164 					break;
    165 				case 'm':
    166 					dolink &= ~(LN_SYMBOLIC|LN_HARD);
    167 					dolink |= LN_MIXED;
    168 					break;
    169 				case 'a':
    170 					dolink &= ~LN_RELATIVE;
    171 					dolink |= LN_ABSOLUTE;
    172 					break;
    173 				case 'r':
    174 					dolink &= ~LN_ABSOLUTE;
    175 					dolink |= LN_RELATIVE;
    176 					break;
    177 				default:
    178 					errx(1, "%c: invalid link type", *p);
    179 					break;
    180 				}
    181 			break;
    182 		case 'm':
    183 			if (!(set = setmode(optarg)))
    184 				errx(1, "%s: invalid file mode", optarg);
    185 			mode = getmode(set, 0);
    186 			break;
    187 		case 'o':
    188 			owner = optarg;
    189 			break;
    190 		case 'p':
    191 			dopreserve = 1;
    192 			break;
    193 		case 'r':
    194 			dorename = 1;
    195 			break;
    196 		case 'S':
    197 			stripArgs = (char*)malloc(sizeof(char)*(strlen(optarg)+1));
    198 			strcpy(stripArgs,optarg);
    199 			/* fall through; -S implies -s */
    200 		case 's':
    201 			dostrip = 1;
    202 			break;
    203 		case 'U':
    204 			unpriv = 1;
    205 			break;
    206 		case '?':
    207 		default:
    208 			usage();
    209 		}
    210 	argc -= optind;
    211 	argv += optind;
    212 
    213 	/* strip and link options make no sense when creating directories */
    214 	if ((dostrip || dolink) && dodir)
    215 		usage();
    216 
    217 	/* strip and flags make no sense with links */
    218 	if ((dostrip || flags) && dolink)
    219 		usage();
    220 
    221 	/* must have at least two arguments, except when creating directories */
    222 	if (argc < 2 && !dodir)
    223 		usage();
    224 
    225 	/* get group and owner id's */
    226 	if (group && !(gp = getgrnam(group)) && !isdigit((unsigned char)*group))
    227 		errx(1, "unknown group %s", group);
    228 	gid = (group) ? ((gp) ? gp->gr_gid : atoi(group)) : -1;
    229 	if (owner && !(pp = getpwnam(owner)) && !isdigit((unsigned char)*owner))
    230 		errx(1, "unknown user %s", owner);
    231 	uid = (owner) ? ((pp) ? pp->pw_uid : atoi(owner)) : -1;
    232 
    233 	if (dodir) {
    234 		for (; *argv != NULL; ++argv)
    235 			install_dir(*argv);
    236 		exit (0);
    237 	}
    238 
    239 	no_target = stat(to_name = argv[argc - 1], &to_sb);
    240 	if (!no_target && S_ISDIR(to_sb.st_mode)) {
    241 		for (; *argv != to_name; ++argv)
    242 			install(*argv, to_name, fset, iflags | DIRECTORY);
    243 		exit(0);
    244 	}
    245 
    246 	/* can't do file1 file2 directory/file */
    247 	if (argc != 2)
    248 		usage();
    249 
    250 	if (!no_target) {
    251 		if (stat(*argv, &from_sb))
    252 			err(1, "%s", *argv);
    253 		if (!S_ISREG(to_sb.st_mode))
    254 			errx(1, "%s: %s", to_name, strerror(EFTYPE));
    255 		if (!dolink && to_sb.st_dev == from_sb.st_dev &&
    256 		    to_sb.st_ino == from_sb.st_ino)
    257 			errx(1, "%s and %s are the same file", *argv, to_name);
    258 		/*
    259 		 * Unlink now... avoid ETXTBSY errors later.  Try and turn
    260 		 * off the append/immutable bits -- if we fail, go ahead,
    261 		 * it might work.
    262 		 */
    263 #define	NOCHANGEBITS	(UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
    264 		if (to_sb.st_flags & NOCHANGEBITS)
    265 			(void)chflags(to_name,
    266 			    to_sb.st_flags & ~(NOCHANGEBITS));
    267 		if (dobackup)
    268 			backup(to_name);
    269 		else if (!dorename)
    270 			(void)unlink(to_name);
    271 	}
    272 	install(*argv, to_name, fset, iflags);
    273 	exit(0);
    274 }
    275 
    276 /*
    277  * makelink --
    278  *	make a link from source to destination
    279  */
    280 void
    281 makelink(from_name, to_name)
    282 	char *from_name;
    283 	char *to_name;
    284 {
    285 	char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
    286 
    287 	/* Try hard links first */
    288 	if (dolink & (LN_HARD|LN_MIXED)) {
    289 		if (link(from_name, to_name) == -1) {
    290 			if ((dolink & LN_HARD) || errno != EXDEV)
    291 				err(1, "link %s -> %s", from_name, to_name);
    292 		}
    293 		else
    294 			return;
    295 	}
    296 
    297 	/* Symbolic links */
    298 	if (dolink & LN_ABSOLUTE) {
    299 		/* Convert source path to absolute */
    300 		if (realpath(from_name, src) == NULL)
    301 			err(1, "%s", src);
    302 		if (symlink(src, to_name) == -1)
    303 			err(1, "symlink %s -> %s", src, to_name);
    304 		return;
    305 	}
    306 
    307 	if (dolink & LN_RELATIVE) {
    308 		char *s, *d;
    309 
    310 		/* Resolve pathnames */
    311 		if (realpath(from_name, src) == NULL)
    312 			err(1, "%s", src);
    313 		if (realpath(to_name, dst) == NULL)
    314 			err(1, "%s", dst);
    315 
    316 		/* trim common path components */
    317 		for (s = src, d = dst; *s == *d; s++, d++)
    318 			continue;
    319 		while (*s != '/')
    320 			s--, d--;
    321 
    322 		/* count the number of directories we need to backtrack */
    323 		for (++d, lnk[0] = '\0'; *d; d++)
    324 			if (*d == '/')
    325 				(void) strcat(lnk, "../");
    326 
    327 		(void) strcat(lnk, ++s);
    328 
    329 		if (symlink(lnk, dst) == -1)
    330 			err(1, "symlink %s -> %s", lnk, dst);
    331 		return;
    332 	}
    333 
    334 	/*
    335 	 * If absolute or relative was not specified,
    336 	 * try the names the user provided
    337 	 */
    338 	if (symlink(from_name, to_name) == -1)
    339 		err(1, "symlink %s -> %s", from_name, to_name);
    340 
    341 }
    342 
    343 /*
    344  * install --
    345  *	build a path name and install the file
    346  */
    347 void
    348 install(from_name, to_name, fset, flags)
    349 	char *from_name, *to_name;
    350 	u_long fset;
    351 	u_int flags;
    352 {
    353 	struct stat from_sb, to_sb;
    354 	int devnull, from_fd, to_fd, serrno;
    355 	char *p, tmpl[MAXPATHLEN], *oto_name;
    356 
    357 	/* If try to install NULL file to a directory, fails. */
    358 	if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
    359 		if (stat(from_name, &from_sb))
    360 			err(1, "%s", from_name);
    361 		if (!S_ISREG(from_sb.st_mode))
    362 			errx(1, "%s: %s", from_name, strerror(EFTYPE));
    363 		/* Build the target path. */
    364 		if (flags & DIRECTORY) {
    365 			(void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
    366 			    to_name,
    367 			    (p = strrchr(from_name, '/')) ? ++p : from_name);
    368 			to_name = pathbuf;
    369 		}
    370 		devnull = 0;
    371 	} else {
    372 		from_sb.st_flags = 0;	/* XXX */
    373 		devnull = 1;
    374 	}
    375 
    376 	/*
    377 	 * Unlink now... avoid ETXTBSY errors later.  Try and turn
    378 	 * off the append/immutable bits -- if we fail, go ahead,
    379 	 * it might work.
    380 	 */
    381 	if (stat(to_name, &to_sb) == 0 &&
    382 	    to_sb.st_flags & (NOCHANGEBITS))
    383 		(void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
    384 	if (dorename) {
    385 		char *ptr, c, *dir;
    386 
    387 		if ((ptr = strrchr(to_name, '/')) != NULL) {
    388 			c = *++ptr;
    389 			*ptr = '\0';
    390 			dir = to_name;
    391 		} else {
    392 			c = '\0';	/* pacify gcc */
    393 			dir = tmpl;
    394 			*dir = '\0';
    395 		}
    396 		(void)snprintf(tmpl, sizeof(tmpl), "%sinst.XXXXXX", dir);
    397 		if (ptr)
    398 			*ptr = c;
    399 		oto_name = to_name;
    400 		to_name = tmpl;
    401 
    402 	} else {
    403 		oto_name = NULL;	/* pacify gcc */
    404 		if (dobackup)
    405 			backup(to_name);
    406 		else
    407 			(void)unlink(to_name);
    408 	}
    409 
    410 	if (dolink) {
    411 		makelink(from_name, to_name);
    412 		return;
    413 	}
    414 
    415 	/* Create target. */
    416 	if (dorename) {
    417 		if ((to_fd = mkstemp(to_name)) == -1)
    418 			err(1, "%s", to_name);
    419 	} else {
    420 		if ((to_fd = open(to_name,
    421 		    O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
    422 			err(1, "%s", to_name);
    423 	}
    424 	if (!devnull) {
    425 		if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
    426 			(void)unlink(to_name);
    427 			err(1, "%s", from_name);
    428 		}
    429 		copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
    430 		(void)close(from_fd);
    431 	}
    432 
    433 	if (dostrip) {
    434 		strip(to_name);
    435 
    436 		/*
    437 		 * Re-open our fd on the target, in case we used a strip
    438 		 *  that does not work in-place -- like gnu binutils strip.
    439 		 */
    440 		close(to_fd);
    441 		if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
    442 		  err(1, "stripping %s", to_name);
    443 	}
    444 
    445 	if (!unpriv) {
    446 		/*
    447 		 * Set owner, group, mode for target; do the chown first,
    448 		 * chown may lose the setuid bits.
    449 		 */
    450 		if ((gid != -1 || uid != -1) && fchown(to_fd, uid, gid)) {
    451 			serrno = errno;
    452 			(void)unlink(to_name);
    453 			errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
    454 		}
    455 	}
    456 	if (fchmod(to_fd, mode)) {
    457 		serrno = errno;
    458 		(void)unlink(to_name);
    459 		errx(1, "%s: chmod: %s", to_name, strerror(serrno));
    460 	}
    461 
    462 	/*
    463 	 * If provided a set of flags, set them, otherwise, preserve the
    464 	 * flags, except for the dump flag.
    465 	 */
    466 	if (fchflags(to_fd,
    467 	    flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
    468 		if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
    469 			warn("%s: chflags", to_name);
    470 	}
    471 
    472 	/*
    473 	 * Preserve the date of the source file.
    474 	 */
    475 	if (dopreserve) {
    476 		struct timeval tv[2];
    477 
    478 #ifdef BSD4_4
    479 		TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
    480 		TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
    481 #else
    482 		tv[0].tv_sec = from_sb.st_atime;
    483 		tv[0].tv_usec = 0;
    484 		tv[1].tv_sec = from_sb.st_mtime;
    485 		tv[1].tv_usec = 0;
    486 #endif
    487 		if (futimes(to_fd, tv) == -1)
    488 			warn("%s: futimes", to_name);
    489 	}
    490 
    491 	(void)close(to_fd);
    492 
    493 	if (dorename)
    494 		if (rename(to_name, oto_name) == -1)
    495 			err(1, "%s: rename", to_name);
    496 
    497 	if (!docopy && !devnull && unlink(from_name))
    498 		err(1, "%s", from_name);
    499 }
    500 
    501 /*
    502  * copy --
    503  *	copy from one file to another
    504  */
    505 void
    506 copy(from_fd, from_name, to_fd, to_name, size)
    507 	int from_fd, to_fd;
    508 	char *from_name, *to_name;
    509 	off_t size;
    510 {
    511 	ssize_t nr, nw;
    512 	int serrno;
    513 	char *p;
    514 	char buf[MAXBSIZE];
    515 
    516 	/*
    517 	 * There's no reason to do anything other than close the file
    518 	 * now if it's empty, so let's not bother.
    519 	 */
    520 	if (size > 0) {
    521 		/*
    522 		 * Mmap and write if less than 8M (the limit is so we don't totally
    523 		 * trash memory on big files).  This is really a minor hack, but it
    524 		 * wins some CPU back.
    525 		 */
    526 		if (size <= 8 * 1048576) {
    527 			if ((p = mmap(NULL, (size_t)size, PROT_READ,
    528 			    MAP_FILE|MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) {
    529 				serrno = errno;
    530 				(void)unlink(to_name);
    531 				errx(1, "%s: %s", from_name, strerror(serrno));
    532 			}
    533 			if (madvise(p, (size_t)size, MADV_SEQUENTIAL) == -1
    534 			    && errno != EOPNOTSUPP)
    535 				warnx("madvise: %s", strerror(errno));
    536 
    537 			if (write(to_fd, p, size) != size) {
    538 				serrno = errno;
    539 				(void)unlink(to_name);
    540 				errx(1, "%s: %s",
    541 				    to_name, strerror(serrno));
    542 			}
    543 		} else {
    544 			while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
    545 				if ((nw = write(to_fd, buf, nr)) != nr) {
    546 					serrno = errno;
    547 					(void)unlink(to_name);
    548 					errx(1, "%s: %s",
    549 					    to_name, strerror(nw > 0 ? EIO : serrno));
    550 				}
    551 			}
    552 			if (nr != 0) {
    553 				serrno = errno;
    554 				(void)unlink(to_name);
    555 				errx(1, "%s: %s", from_name, strerror(serrno));
    556 			}
    557 		}
    558 	}
    559 }
    560 
    561 /*
    562  * strip --
    563  *	use strip(1) to strip the target file
    564  */
    565 void
    566 strip(to_name)
    567 	char *to_name;
    568 {
    569 	int serrno, status;
    570 	char *stripprog;
    571 
    572 	switch (vfork()) {
    573 	case -1:
    574 		serrno = errno;
    575 		(void)unlink(to_name);
    576 		errx(1, "vfork: %s", strerror(serrno));
    577 	case 0:
    578 		stripprog = getenv("STRIP");
    579 		if (stripprog == NULL)
    580 			stripprog = _PATH_STRIP;
    581 
    582 		if (stripArgs) {
    583 			/* build up a command line and let /bin/sh parse the arguments */
    584 			char* cmd = (char*)malloc(sizeof(char)*
    585 						  (3+strlen(stripprog)+
    586 						     strlen(stripArgs)+
    587 						     strlen(to_name)));
    588 
    589 			sprintf(cmd, "%s %s %s", stripprog, stripArgs, to_name);
    590 
    591 			execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
    592 		} else
    593 			execl(stripprog, "strip", to_name, NULL);
    594 
    595 		warn("%s", stripprog);
    596 		_exit(1);
    597 	default:
    598 		if (wait(&status) == -1 || status)
    599 			(void)unlink(to_name);
    600 	}
    601 }
    602 
    603 /*
    604  * backup file "to_name" to to_name.suffix
    605  * if suffix contains a "%", it's taken as a printf(3) pattern
    606  * used for a numbered backup.
    607  */
    608 void
    609 backup(to_name)
    610 	const char *to_name;
    611 {
    612 	char backup[FILENAME_MAX];
    613 
    614 	if (numberedbackup) {
    615 		/* Do numbered backup */
    616 		int cnt;
    617 		char suffix_expanded[FILENAME_MAX];
    618 
    619 		cnt=0;
    620 		do {
    621 			(void)snprintf(suffix_expanded, FILENAME_MAX, suffix, cnt);
    622 			(void)snprintf(backup, FILENAME_MAX, "%s%s",
    623 				       to_name, suffix_expanded);
    624 			cnt++;
    625 		} while (access(backup, F_OK)==0);
    626 	} else {
    627 		/* Do simple backup */
    628 		(void)snprintf(backup, FILENAME_MAX, "%s%s", to_name, suffix);
    629 	}
    630 
    631 	(void)rename(to_name, backup);
    632 }
    633 
    634 /*
    635  * install_dir --
    636  *	build directory heirarchy
    637  */
    638 void
    639 install_dir(path)
    640         char *path;
    641 {
    642         char *p;
    643         struct stat sb;
    644         int ch;
    645 
    646         for (p = path;; ++p)
    647                 if (!*p || (p != path && *p  == '/')) {
    648                         ch = *p;
    649                         *p = '\0';
    650                         if (stat(path, &sb)) {
    651                                 if (errno != ENOENT || mkdir(path, 0777) < 0) {
    652 					err(1, "%s", path);
    653 					/* NOTREACHED */
    654                                 }
    655                         }
    656                         if (!(*p = ch))
    657 				break;
    658                 }
    659 
    660 	if (((gid != -1 || uid != -1) && chown(path, uid, gid)) ||
    661             chmod(path, mode)) {
    662                 warn("%s", path);
    663         }
    664 }
    665 
    666 /*
    667  * usage --
    668  *	print a usage message and die
    669  */
    670 void
    671 usage()
    672 {
    673 	(void)fprintf(stderr, "\
    674 usage: install [-Ubcps] [-B suffix] [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 file2\n\
    675        install [-Ubcps] [-B suffix] [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 ... fileN directory\n\
    676        install [-Up] -d [-m mode] [-o owner] [-g group] directory ...\n");
    677 	exit(1);
    678 }
    679