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