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