xref: /dragonfly/usr.bin/xinstall/xinstall.c (revision 4e7eb5cc)
1 /*
2  * Copyright (c) 1987, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1987, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)xinstall.c	8.1 (Berkeley) 7/21/93
35  * $FreeBSD: src/usr.bin/xinstall/xinstall.c,v 1.38.2.8 2002/08/07 16:29:48 ru Exp $
36  * $DragonFly: src/usr.bin/xinstall/xinstall.c,v 1.3 2003/10/04 20:36:55 hmp Exp $
37  */
38 
39 #include <sys/param.h>
40 #include <sys/mman.h>
41 #include <sys/mount.h>
42 #include <sys/stat.h>
43 #include <sys/wait.h>
44 
45 #include <ctype.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <grp.h>
50 #include <paths.h>
51 #include <pwd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <sysexits.h>
56 #include <unistd.h>
57 #include <utime.h>
58 
59 #include "pathnames.h"
60 
61 /* Bootstrap aid - this doesn't exist in most older releases */
62 #ifndef MAP_FAILED
63 #define MAP_FAILED ((void *)-1)	/* from <sys/mman.h> */
64 #endif
65 
66 #define MAX_CMP_SIZE	(16 * 1024 * 1024)
67 
68 #define	DIRECTORY	0x01		/* Tell install it's a directory. */
69 #define	SETFLAGS	0x02		/* Tell install to set flags. */
70 #define	NOCHANGEBITS	(UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
71 #define	BACKUP_SUFFIX	".old"
72 
73 struct passwd *pp;
74 struct group *gp;
75 gid_t gid;
76 uid_t uid;
77 int dobackup, docompare, dodir, dopreserve, dostrip, nommap, safecopy, verbose;
78 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
79 const char *suffix = BACKUP_SUFFIX;
80 
81 void	copy(int, const char *, int, const char *, off_t);
82 int	compare(int, const char *, size_t, int, const char *, size_t);
83 int	create_newfile(const char *, int, struct stat *);
84 int	create_tempfile(const char *, char *, size_t);
85 void	install(const char *, const char *, u_long, u_int);
86 void	install_dir(char *);
87 u_long	numeric_id(const char *, const char *);
88 void	strip(const char *);
89 int	trymmap(int);
90 void	usage(void);
91 
92 int
93 main(int argc, char **argv)
94 {
95 	struct stat from_sb, to_sb;
96 	mode_t *set;
97 	u_long fset;
98 	int ch, no_target;
99 	u_int iflags;
100 	char *flags;
101 	const char *group, *owner, *to_name;
102 
103 	iflags = 0;
104 	group = owner = NULL;
105 	while ((ch = getopt(argc, argv, "B:bCcdf:g:Mm:o:pSsv")) != -1)
106 		switch((char)ch) {
107 		case 'B':
108 			suffix = optarg;
109 			/* FALLTHROUGH */
110 		case 'b':
111 			dobackup = 1;
112 			break;
113 		case 'C':
114 			docompare = 1;
115 			break;
116 		case 'c':
117 			/* For backwards compatibility. */
118 			break;
119 		case 'd':
120 			dodir = 1;
121 			break;
122 		case 'f':
123 			flags = optarg;
124 			if (strtofflags(&flags, &fset, NULL))
125 				errx(EX_USAGE, "%s: invalid flag", flags);
126 			iflags |= SETFLAGS;
127 			break;
128 		case 'g':
129 			group = optarg;
130 			break;
131 		case 'M':
132 			nommap = 1;
133 			break;
134 		case 'm':
135 			if (!(set = setmode(optarg)))
136 				errx(EX_USAGE, "invalid file mode: %s",
137 				     optarg);
138 			mode = getmode(set, 0);
139 			free(set);
140 			break;
141 		case 'o':
142 			owner = optarg;
143 			break;
144 		case 'p':
145 			docompare = dopreserve = 1;
146 			break;
147 		case 'S':
148 			safecopy = 1;
149 			break;
150 		case 's':
151 			dostrip = 1;
152 			break;
153 		case 'v':
154 			verbose = 1;
155 			break;
156 		case '?':
157 		default:
158 			usage();
159 		}
160 	argc -= optind;
161 	argv += optind;
162 
163 	/* some options make no sense when creating directories */
164 	if (dostrip && dodir)
165 		usage();
166 
167 	/* must have at least two arguments, except when creating directories */
168 	if (argc < 2 && !dodir)
169 		usage();
170 
171 	/* need to make a temp copy so we can compare stripped version */
172 	if (docompare && dostrip)
173 		safecopy = 1;
174 
175 	/* get group and owner id's */
176 	if (group != NULL) {
177 		if ((gp = getgrnam(group)) != NULL)
178 			gid = gp->gr_gid;
179 		else
180 			gid = (gid_t)numeric_id(group, "group");
181 	} else
182 		gid = (gid_t)-1;
183 
184 	if (owner != NULL) {
185 		if ((pp = getpwnam(owner)) != NULL)
186 			uid = pp->pw_uid;
187 		else
188 			uid = (uid_t)numeric_id(owner, "user");
189 	} else
190 		uid = (uid_t)-1;
191 
192 	if (dodir) {
193 		for (; *argv != NULL; ++argv)
194 			install_dir(*argv);
195 		exit(EX_OK);
196 		/* NOTREACHED */
197 	}
198 
199 	no_target = stat(to_name = argv[argc - 1], &to_sb);
200 	if (!no_target && S_ISDIR(to_sb.st_mode)) {
201 		for (; *argv != to_name; ++argv)
202 			install(*argv, to_name, fset, iflags | DIRECTORY);
203 		exit(EX_OK);
204 		/* NOTREACHED */
205 	}
206 
207 	/* can't do file1 file2 directory/file */
208 	if (argc != 2)
209 		usage();
210 
211 	if (!no_target) {
212 		if (stat(*argv, &from_sb))
213 			err(EX_OSERR, "%s", *argv);
214 		if (!S_ISREG(to_sb.st_mode)) {
215 			errno = EFTYPE;
216 			err(EX_OSERR, "%s", to_name);
217 		}
218 		if (to_sb.st_dev == from_sb.st_dev &&
219 		    to_sb.st_ino == from_sb.st_ino)
220 			errx(EX_USAGE,
221 			    "%s and %s are the same file", *argv, to_name);
222 	}
223 	install(*argv, to_name, fset, iflags);
224 	exit(EX_OK);
225 	/* NOTREACHED */
226 }
227 
228 u_long
229 numeric_id(const char *name, const char *type)
230 {
231 	u_long val;
232 	char *ep;
233 
234 	/*
235 	 * XXX
236 	 * We know that uid_t's and gid_t's are unsigned longs.
237 	 */
238 	errno = 0;
239 	val = strtoul(name, &ep, 10);
240 	if (errno)
241 		err(EX_NOUSER, "%s", name);
242 	if (*ep != '\0')
243 		errx(EX_NOUSER, "unknown %s %s", type, name);
244 	return (val);
245 }
246 
247 /*
248  * install --
249  *	build a path name and install the file
250  */
251 void
252 install(const char *from_name, const char *to_name, u_long fset, u_int flags)
253 {
254 	struct stat from_sb, temp_sb, to_sb;
255 	struct utimbuf utb;
256 	int devnull, files_match, from_fd, serrno, target;
257 	int tempcopy, temp_fd, to_fd;
258 	char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
259 
260 	files_match = 0;
261 
262 	/* If try to install NULL file to a directory, fails. */
263 	if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
264 		if (stat(from_name, &from_sb))
265 			err(EX_OSERR, "%s", from_name);
266 		if (!S_ISREG(from_sb.st_mode)) {
267 			errno = EFTYPE;
268 			err(EX_OSERR, "%s", from_name);
269 		}
270 		/* Build the target path. */
271 		if (flags & DIRECTORY) {
272 			(void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
273 			    to_name,
274 			    (p = strrchr(from_name, '/')) ? ++p : from_name);
275 			to_name = pathbuf;
276 		}
277 		devnull = 0;
278 	} else {
279 		devnull = 1;
280 	}
281 
282 	target = stat(to_name, &to_sb) == 0;
283 
284 	/* Only install to regular files. */
285 	if (target && !S_ISREG(to_sb.st_mode)) {
286 		errno = EFTYPE;
287 		warn("%s", to_name);
288 		return;
289 	}
290 
291 	/* Only copy safe if the target exists. */
292 	tempcopy = safecopy && target;
293 
294 	if (!devnull && (from_fd = open(from_name, O_RDONLY, 0)) < 0)
295 		err(EX_OSERR, "%s", from_name);
296 
297 	/* If we don't strip, we can compare first. */
298 	if (docompare && !dostrip && target) {
299 		if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
300 			err(EX_OSERR, "%s", to_name);
301 		if (devnull)
302 			files_match = to_sb.st_size == 0;
303 		else
304 			files_match = !(compare(from_fd, from_name,
305 			    (size_t)from_sb.st_size, to_fd,
306 			    to_name, (size_t)to_sb.st_size));
307 
308 		/* Close "to" file unless we match. */
309 		if (!files_match)
310 			(void)close(to_fd);
311 	}
312 
313 	if (!files_match) {
314 		if (tempcopy) {
315 			to_fd = create_tempfile(to_name, tempfile,
316 			    sizeof(tempfile));
317 			if (to_fd < 0)
318 				err(EX_OSERR, "%s", tempfile);
319 		} else {
320 			if ((to_fd = create_newfile(to_name, target,
321 			    &to_sb)) < 0)
322 				err(EX_OSERR, "%s", to_name);
323 			if (verbose)
324 				(void)printf("install: %s -> %s\n",
325 				    from_name, to_name);
326 		}
327 		if (!devnull)
328 			copy(from_fd, from_name, to_fd,
329 			     tempcopy ? tempfile : to_name, from_sb.st_size);
330 	}
331 
332 	if (dostrip) {
333 		strip(tempcopy ? tempfile : to_name);
334 
335 		/*
336 		 * Re-open our fd on the target, in case we used a strip
337 		 * that does not work in-place -- like GNU binutils strip.
338 		 */
339 		close(to_fd);
340 		to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY, 0);
341 		if (to_fd < 0)
342 			err(EX_OSERR, "stripping %s", to_name);
343 	}
344 
345 	/*
346 	 * Compare the stripped temp file with the target.
347 	 */
348 	if (docompare && dostrip && target) {
349 		temp_fd = to_fd;
350 
351 		/* Re-open to_fd using the real target name. */
352 		if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
353 			err(EX_OSERR, "%s", to_name);
354 
355 		if (fstat(temp_fd, &temp_sb)) {
356 			serrno = errno;
357 			(void)unlink(tempfile);
358 			errno = serrno;
359 			err(EX_OSERR, "%s", tempfile);
360 		}
361 
362 		if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
363 			    to_name, (size_t)to_sb.st_size) == 0) {
364 			/*
365 			 * If target has more than one link we need to
366 			 * replace it in order to snap the extra links.
367 			 * Need to preserve target file times, though.
368 			 */
369 			if (to_sb.st_nlink != 1) {
370 				utb.actime = to_sb.st_atime;
371 				utb.modtime = to_sb.st_mtime;
372 				(void)utime(tempfile, &utb);
373 			} else {
374 				files_match = 1;
375 				(void)unlink(tempfile);
376 			}
377 			(void) close(temp_fd);
378 		}
379 	}
380 
381 	/*
382 	 * Move the new file into place if doing a safe copy
383 	 * and the files are different (or just not compared).
384 	 */
385 	if (tempcopy && !files_match) {
386 		/* Try to turn off the immutable bits. */
387 		if (to_sb.st_flags & NOCHANGEBITS)
388 			(void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
389 		if (dobackup) {
390 			if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
391 			    suffix) != strlen(to_name) + strlen(suffix)) {
392 				unlink(tempfile);
393 				errx(EX_OSERR, "%s: backup filename too long",
394 				    to_name);
395 			}
396 			if (verbose)
397 				(void)printf("install: %s -> %s\n", to_name, backup);
398 			if (rename(to_name, backup) < 0) {
399 				serrno = errno;
400 				unlink(tempfile);
401 				errno = serrno;
402 				err(EX_OSERR, "rename: %s to %s", to_name,
403 				     backup);
404 			}
405 		}
406 		if (verbose)
407 			(void)printf("install: %s -> %s\n", from_name, to_name);
408 		if (rename(tempfile, to_name) < 0) {
409 			serrno = errno;
410 			unlink(tempfile);
411 			errno = serrno;
412 			err(EX_OSERR, "rename: %s to %s",
413 			    tempfile, to_name);
414 		}
415 
416 		/* Re-open to_fd so we aren't hosed by the rename(2). */
417 		(void) close(to_fd);
418 		if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
419 			err(EX_OSERR, "%s", to_name);
420 	}
421 
422 	/*
423 	 * Preserve the timestamp of the source file if necessary.
424 	 */
425 	if (dopreserve && !files_match && !devnull) {
426 		utb.actime = from_sb.st_atime;
427 		utb.modtime = from_sb.st_mtime;
428 		(void)utime(to_name, &utb);
429 	}
430 
431 	if (fstat(to_fd, &to_sb) == -1) {
432 		serrno = errno;
433 		(void)unlink(to_name);
434 		errno = serrno;
435 		err(EX_OSERR, "%s", to_name);
436 	}
437 
438 	/*
439 	 * Set owner, group, mode for target; do the chown first,
440 	 * chown may lose the setuid bits.
441 	 */
442 	if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
443 	    (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
444 	    (mode != to_sb.st_mode)) {
445 		/* Try to turn off the immutable bits. */
446 		if (to_sb.st_flags & NOCHANGEBITS)
447 			(void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
448 	}
449 
450 	if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
451 	    (uid != (uid_t)-1 && uid != to_sb.st_uid))
452 		if (fchown(to_fd, uid, gid) == -1) {
453 			serrno = errno;
454 			(void)unlink(to_name);
455 			errno = serrno;
456 			err(EX_OSERR,"%s: chown/chgrp", to_name);
457 		}
458 
459 	if (mode != to_sb.st_mode)
460 		if (fchmod(to_fd, mode)) {
461 			serrno = errno;
462 			(void)unlink(to_name);
463 			errno = serrno;
464 			err(EX_OSERR, "%s: chmod", to_name);
465 		}
466 
467 	/*
468 	 * If provided a set of flags, set them, otherwise, preserve the
469 	 * flags, except for the dump flag.
470 	 * NFS does not support flags.  Ignore EOPNOTSUPP flags if we're just
471 	 * trying to turn off UF_NODUMP.  If we're trying to set real flags,
472 	 * then warn if the the fs doesn't support it, otherwise fail.
473 	 */
474 	if (!devnull && fchflags(to_fd,
475 	    flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
476 		if (flags & SETFLAGS) {
477 			if (errno == EOPNOTSUPP)
478 				warn("%s: chflags", to_name);
479 			else {
480 				serrno = errno;
481 				(void)unlink(to_name);
482 				errno = serrno;
483 				err(EX_OSERR, "%s: chflags", to_name);
484 			}
485 		}
486 	}
487 
488 	(void)close(to_fd);
489 	if (!devnull)
490 		(void)close(from_fd);
491 }
492 
493 /*
494  * compare --
495  *	compare two files; non-zero means files differ
496  */
497 int
498 compare(int from_fd, const char *from_name __unused, size_t from_len,
499 	int to_fd, const char *to_name __unused, size_t to_len)
500 {
501 	char *p, *q;
502 	int rv;
503 	int done_compare;
504 
505 	rv = 0;
506 	if (from_len != to_len)
507 		return 1;
508 
509 	if (from_len <= MAX_CMP_SIZE) {
510 		done_compare = 0;
511 		if (trymmap(from_fd) && trymmap(to_fd)) {
512 			p = mmap(NULL, from_len, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
513 			if (p == (char *)MAP_FAILED)
514 				goto out;
515 			q = mmap(NULL, from_len, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
516 			if (q == (char *)MAP_FAILED) {
517 				munmap(p, from_len);
518 				goto out;
519 			}
520 
521 			rv = memcmp(p, q, from_len);
522 			munmap(p, from_len);
523 			munmap(q, from_len);
524 			done_compare = 1;
525 		}
526 	out:
527 		if (!done_compare) {
528 			char buf1[MAXBSIZE];
529 			char buf2[MAXBSIZE];
530 			int n1, n2;
531 
532 			rv = 0;
533 			lseek(from_fd, 0, SEEK_SET);
534 			lseek(to_fd, 0, SEEK_SET);
535 			while (rv == 0) {
536 				n1 = read(from_fd, buf1, sizeof(buf1));
537 				if (n1 == 0)
538 					break;		/* EOF */
539 				else if (n1 > 0) {
540 					n2 = read(to_fd, buf2, n1);
541 					if (n2 == n1)
542 						rv = memcmp(buf1, buf2, n1);
543 					else
544 						rv = 1;	/* out of sync */
545 				} else
546 					rv = 1;		/* read failure */
547 			}
548 			lseek(from_fd, 0, SEEK_SET);
549 			lseek(to_fd, 0, SEEK_SET);
550 		}
551 	} else
552 		rv = 1;	/* don't bother in this case */
553 
554 	return rv;
555 }
556 
557 /*
558  * create_tempfile --
559  *	create a temporary file based on path and open it
560  */
561 int
562 create_tempfile(const char *path, char *temp, size_t tsize)
563 {
564 	char *p;
565 
566 	(void)strncpy(temp, path, tsize);
567 	temp[tsize - 1] = '\0';
568 	if ((p = strrchr(temp, '/')) != NULL)
569 		p++;
570 	else
571 		p = temp;
572 	(void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
573 	temp[tsize - 1] = '\0';
574 	return (mkstemp(temp));
575 }
576 
577 /*
578  * create_newfile --
579  *	create a new file, overwriting an existing one if necessary
580  */
581 int
582 create_newfile(const char *path, int target, struct stat *sbp)
583 {
584 	char backup[MAXPATHLEN];
585 
586 	if (target) {
587 		/*
588 		 * Unlink now... avoid ETXTBSY errors later.  Try to turn
589 		 * off the append/immutable bits -- if we fail, go ahead,
590 		 * it might work.
591 		 */
592 		if (sbp->st_flags & NOCHANGEBITS)
593 			(void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
594 
595 		if (dobackup) {
596 			if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
597 			    path, suffix) != strlen(path) + strlen(suffix))
598 				errx(EX_OSERR, "%s: backup filename too long",
599 				    path);
600 			(void)snprintf(backup, MAXPATHLEN, "%s%s",
601 			    path, suffix);
602 			if (verbose)
603 				(void)printf("install: %s -> %s\n",
604 				    path, backup);
605 			if (rename(path, backup) < 0)
606 				err(EX_OSERR, "rename: %s to %s", path, backup);
607 		} else
608 			(void)unlink(path);
609 	}
610 
611 	return (open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR));
612 }
613 
614 /*
615  * copy --
616  *	copy from one file to another
617  */
618 void
619 copy(register int from_fd, const char *from_name, register int to_fd,
620      const char *to_name, off_t size)
621 {
622 	register int nr, nw;
623 	int serrno;
624 	char *p, buf[MAXBSIZE];
625 	int done_copy;
626 
627 	/* Rewind file descriptors. */
628 	if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
629 		err(EX_OSERR, "lseek: %s", from_name);
630 	if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
631 		err(EX_OSERR, "lseek: %s", to_name);
632 
633 	/*
634 	 * Mmap and write if less than 8M (the limit is so we don't totally
635 	 * trash memory on big files.  This is really a minor hack, but it
636 	 * wins some CPU back.
637 	 */
638 	done_copy = 0;
639 	if (size <= 8 * 1048576 && trymmap(from_fd) &&
640 	    (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
641 		    from_fd, (off_t)0)) != (char *)MAP_FAILED) {
642 		if ((nw = write(to_fd, p, size)) != size) {
643 			serrno = errno;
644 			(void)unlink(to_name);
645 			errno = nw > 0 ? EIO : serrno;
646 			err(EX_OSERR, "%s", to_name);
647 		}
648 		done_copy = 1;
649 	}
650 	if (!done_copy) {
651 		while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
652 			if ((nw = write(to_fd, buf, nr)) != nr) {
653 				serrno = errno;
654 				(void)unlink(to_name);
655 				errno = nw > 0 ? EIO : serrno;
656 				err(EX_OSERR, "%s", to_name);
657 			}
658 		if (nr != 0) {
659 			serrno = errno;
660 			(void)unlink(to_name);
661 			errno = serrno;
662 			err(EX_OSERR, "%s", from_name);
663 		}
664 	}
665 }
666 
667 /*
668  * strip --
669  *	use strip(1) to strip the target file
670  */
671 void
672 strip(const char *to_name)
673 {
674 	const char *stripbin;
675 	int serrno, status;
676 
677 	switch (fork()) {
678 	case -1:
679 		serrno = errno;
680 		(void)unlink(to_name);
681 		errno = serrno;
682 		err(EX_TEMPFAIL, "fork");
683 	case 0:
684 		stripbin = getenv("STRIPBIN");
685 		if (stripbin == NULL)
686 			stripbin = "strip";
687 		execlp(stripbin, stripbin, to_name, (char *)NULL);
688 		err(EX_OSERR, "exec(%s)", stripbin);
689 	default:
690 		if (wait(&status) == -1 || status) {
691 			serrno = errno;
692 			(void)unlink(to_name);
693 			errc(EX_SOFTWARE, serrno, "wait");
694 			/* NOTREACHED */
695 		}
696 	}
697 }
698 
699 /*
700  * install_dir --
701  *	build directory heirarchy
702  */
703 void
704 install_dir(char *path)
705 {
706 	register char *p;
707 	struct stat sb;
708 	int ch;
709 
710 	for (p = path;; ++p)
711 		if (!*p || (p != path && *p  == '/')) {
712 			ch = *p;
713 			*p = '\0';
714 			if (stat(path, &sb)) {
715 				if (errno != ENOENT || mkdir(path, 0755) < 0) {
716 					err(EX_OSERR, "mkdir %s", path);
717 					/* NOTREACHED */
718 				} else if (verbose)
719 					(void)printf("install: mkdir %s\n",
720 						     path);
721 			} else if (!S_ISDIR(sb.st_mode))
722 				errx(EX_OSERR, "%s exists but is not a directory", path);
723 			if (!(*p = ch))
724 				break;
725  		}
726 
727 	if ((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid))
728 		warn("chown %u:%u %s", uid, gid, path);
729 	if (chmod(path, mode))
730 		warn("chmod %o %s", mode, path);
731 }
732 
733 /*
734  * usage --
735  *	print a usage message and die
736  */
737 void
738 usage(void)
739 {
740 	(void)fprintf(stderr, "\
741 usage: install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n\
742                [-o owner] file1 file2\n\
743        install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n\
744                [-o owner] file1 ... fileN directory\n\
745        install -d [-v] [-g group] [-m mode] [-o owner] directory ...\n");
746 	exit(EX_USAGE);
747 	/* NOTREACHED */
748 }
749 
750 /*
751  * trymmap --
752  *	return true (1) if mmap should be tried, false (0) if not.
753  */
754 int
755 trymmap(int fd)
756 {
757 /*
758  * The ifdef is for bootstrapping - f_fstypename doesn't exist in
759  * pre-Lite2-merge systems.
760  */
761 #ifdef MFSNAMELEN
762 	struct statfs stfs;
763 
764 	if (nommap || fstatfs(fd, &stfs) != 0)
765 		return (0);
766 	if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
767 	    strcmp(stfs.f_fstypename, "cd9660") == 0)
768 		return (1);
769 #endif
770 	return (0);
771 }
772