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