xref: /freebsd/usr.bin/xinstall/xinstall.c (revision b52b9d56d4e96089873a75f9e29062eec19fabba)
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/wait.h>
54 
55 #include <ctype.h>
56 #include <err.h>
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <grp.h>
60 #include <paths.h>
61 #include <pwd.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <sysexits.h>
66 #include <unistd.h>
67 #include <utime.h>
68 
69 #include "pathnames.h"
70 
71 /* Bootstrap aid - this doesn't exist in most older releases */
72 #ifndef MAP_FAILED
73 #define MAP_FAILED ((void *)-1)	/* from <sys/mman.h> */
74 #endif
75 
76 #define MAX_CMP_SIZE	(16 * 1024 * 1024)
77 
78 #define	DIRECTORY	0x01		/* Tell install it's a directory. */
79 #define	SETFLAGS	0x02		/* Tell install to set flags. */
80 #define	NOCHANGEBITS	(UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
81 #define	BACKUP_SUFFIX	".old"
82 
83 struct passwd *pp;
84 struct group *gp;
85 gid_t gid;
86 uid_t uid;
87 int dobackup, docompare, dodir, dopreserve, dostrip, nommap, safecopy, verbose;
88 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
89 const char *suffix = BACKUP_SUFFIX;
90 
91 void	copy(int, const char *, int, const char *, off_t);
92 int	compare(int, const char *, size_t, int, const char *, size_t);
93 int	create_newfile(const char *, int, struct stat *);
94 int	create_tempfile(const char *, char *, size_t);
95 void	install(const char *, const char *, u_long, u_int);
96 void	install_dir(char *);
97 u_long	numeric_id(const char *, const char *);
98 void	strip(const char *);
99 int	trymmap(int);
100 void	usage(void);
101 
102 int
103 main(argc, argv)
104 	int argc;
105 	char *argv[];
106 {
107 	struct stat from_sb, to_sb;
108 	mode_t *set;
109 	u_long fset;
110 	int ch, no_target;
111 	u_int iflags;
112 	char *flags;
113 	const char *group, *owner, *to_name;
114 
115 	iflags = 0;
116 	group = owner = NULL;
117 	while ((ch = getopt(argc, argv, "B:bCcdf:g:Mm:o:pSsv")) != -1)
118 		switch((char)ch) {
119 		case 'B':
120 			suffix = optarg;
121 			/* FALLTHROUGH */
122 		case 'b':
123 			dobackup = 1;
124 			break;
125 		case 'C':
126 			docompare = 1;
127 			break;
128 		case 'c':
129 			/* For backwards compatibility. */
130 			break;
131 		case 'd':
132 			dodir = 1;
133 			break;
134 		case 'f':
135 			flags = optarg;
136 			if (strtofflags(&flags, &fset, NULL))
137 				errx(EX_USAGE, "%s: invalid flag", flags);
138 			iflags |= SETFLAGS;
139 			break;
140 		case 'g':
141 			group = optarg;
142 			break;
143 		case 'M':
144 			nommap = 1;
145 			break;
146 		case 'm':
147 			if (!(set = setmode(optarg)))
148 				errx(EX_USAGE, "invalid file mode: %s",
149 				     optarg);
150 			mode = getmode(set, 0);
151 			free(set);
152 			break;
153 		case 'o':
154 			owner = optarg;
155 			break;
156 		case 'p':
157 			docompare = dopreserve = 1;
158 			break;
159 		case 'S':
160 			safecopy = 1;
161 			break;
162 		case 's':
163 			dostrip = 1;
164 			break;
165 		case 'v':
166 			verbose = 1;
167 			break;
168 		case '?':
169 		default:
170 			usage();
171 		}
172 	argc -= optind;
173 	argv += optind;
174 
175 	/* some options make no sense when creating directories */
176 	if ((safecopy || docompare || dostrip) && dodir)
177 		usage();
178 
179 	/* must have at least two arguments, except when creating directories */
180 	if (argc < 2 && !dodir)
181 		usage();
182 
183 	/* need to make a temp copy so we can compare stripped version */
184 	if (docompare && dostrip)
185 		safecopy = 1;
186 
187 	/* get group and owner id's */
188 	if (group != NULL) {
189 		if ((gp = getgrnam(group)) != NULL)
190 			gid = gp->gr_gid;
191 		else
192 			gid = (gid_t)numeric_id(group, "group");
193 	} else
194 		gid = (gid_t)-1;
195 
196 	if (owner != NULL) {
197 		if ((pp = getpwnam(owner)) != NULL)
198 			uid = pp->pw_uid;
199 		else
200 			uid = (uid_t)numeric_id(owner, "user");
201 	} else
202 		uid = (uid_t)-1;
203 
204 	if (dodir) {
205 		for (; *argv != NULL; ++argv)
206 			install_dir(*argv);
207 		exit(EX_OK);
208 		/* NOTREACHED */
209 	}
210 
211 	no_target = stat(to_name = argv[argc - 1], &to_sb);
212 	if (!no_target && S_ISDIR(to_sb.st_mode)) {
213 		for (; *argv != to_name; ++argv)
214 			install(*argv, to_name, fset, iflags | DIRECTORY);
215 		exit(EX_OK);
216 		/* NOTREACHED */
217 	}
218 
219 	/* can't do file1 file2 directory/file */
220 	if (argc != 2)
221 		usage();
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(name, type)
242 	const char *name, *type;
243 {
244 	u_long val;
245 	char *ep;
246 
247 	/*
248 	 * XXX
249 	 * We know that uid_t's and gid_t's are unsigned longs.
250 	 */
251 	errno = 0;
252 	val = strtoul(name, &ep, 10);
253 	if (errno)
254 		err(EX_NOUSER, "%s", name);
255 	if (*ep != '\0')
256 		errx(EX_NOUSER, "unknown %s %s", type, name);
257 	return (val);
258 }
259 
260 /*
261  * install --
262  *	build a path name and install the file
263  */
264 void
265 install(from_name, to_name, fset, flags)
266 	const char *from_name, *to_name;
267 	u_long fset;
268 	u_int flags;
269 {
270 	struct stat from_sb, temp_sb, to_sb;
271 	struct utimbuf utb;
272 	int devnull, files_match, from_fd, serrno, target;
273 	int tempcopy, temp_fd, to_fd;
274 	char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
275 
276 	files_match = 0;
277 
278 	/* If try to install NULL file to a directory, fails. */
279 	if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
280 		if (stat(from_name, &from_sb))
281 			err(EX_OSERR, "%s", from_name);
282 		if (!S_ISREG(from_sb.st_mode)) {
283 			errno = EFTYPE;
284 			err(EX_OSERR, "%s", from_name);
285 		}
286 		/* Build the target path. */
287 		if (flags & DIRECTORY) {
288 			(void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
289 			    to_name,
290 			    (p = strrchr(from_name, '/')) ? ++p : from_name);
291 			to_name = pathbuf;
292 		}
293 		devnull = 0;
294 	} else {
295 		devnull = 1;
296 	}
297 
298 	target = stat(to_name, &to_sb) == 0;
299 
300 	/* Only install to regular files. */
301 	if (target && !S_ISREG(to_sb.st_mode)) {
302 		errno = EFTYPE;
303 		warn("%s", to_name);
304 		return;
305 	}
306 
307 	/* Only copy safe if the target exists. */
308 	tempcopy = safecopy && target;
309 
310 	if (!devnull && (from_fd = open(from_name, O_RDONLY, 0)) < 0)
311 		err(EX_OSERR, "%s", from_name);
312 
313 	/* If we don't strip, we can compare first. */
314 	if (docompare && !dostrip && target) {
315 		if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
316 			err(EX_OSERR, "%s", to_name);
317 		if (devnull)
318 			files_match = to_sb.st_size == 0;
319 		else
320 			files_match = !(compare(from_fd, from_name,
321 			    (size_t)from_sb.st_size, to_fd,
322 			    to_name, (size_t)to_sb.st_size));
323 
324 		/* Close "to" file unless we match. */
325 		if (!files_match)
326 			(void)close(to_fd);
327 	}
328 
329 	if (!files_match) {
330 		if (tempcopy) {
331 			to_fd = create_tempfile(to_name, tempfile,
332 			    sizeof(tempfile));
333 			if (to_fd < 0)
334 				err(EX_OSERR, "%s", tempfile);
335 		} else {
336 			if ((to_fd = create_newfile(to_name, target,
337 			    &to_sb)) < 0)
338 				err(EX_OSERR, "%s", to_name);
339 			if (verbose)
340 				(void)printf("install: %s -> %s\n",
341 				    from_name, to_name);
342 		}
343 		if (!devnull)
344 			copy(from_fd, from_name, to_fd,
345 			     tempcopy ? tempfile : to_name, from_sb.st_size);
346 	}
347 
348 	if (dostrip) {
349 		strip(tempcopy ? tempfile : to_name);
350 
351 		/*
352 		 * Re-open our fd on the target, in case we used a strip
353 		 * that does not work in-place -- like GNU binutils strip.
354 		 */
355 		close(to_fd);
356 		to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY, 0);
357 		if (to_fd < 0)
358 			err(EX_OSERR, "stripping %s", to_name);
359 	}
360 
361 	/*
362 	 * Compare the stripped temp file with the target.
363 	 */
364 	if (docompare && dostrip && target) {
365 		temp_fd = to_fd;
366 
367 		/* Re-open to_fd using the real target name. */
368 		if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
369 			err(EX_OSERR, "%s", to_name);
370 
371 		if (fstat(temp_fd, &temp_sb)) {
372 			serrno = errno;
373 			(void)unlink(tempfile);
374 			errno = serrno;
375 			err(EX_OSERR, "%s", tempfile);
376 		}
377 
378 		if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
379 			    to_name, (size_t)to_sb.st_size) == 0) {
380 			/*
381 			 * If target has more than one link we need to
382 			 * replace it in order to snap the extra links.
383 			 * Need to preserve target file times, though.
384 			 */
385 			if (to_sb.st_nlink != 1) {
386 				utb.actime = to_sb.st_atime;
387 				utb.modtime = to_sb.st_mtime;
388 				(void)utime(tempfile, &utb);
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 		utb.actime = from_sb.st_atime;
443 		utb.modtime = from_sb.st_mtime;
444 		(void)utime(to_name, &utb);
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)) {
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)
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 && fchflags(to_fd,
491 	    flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
492 		if (flags & SETFLAGS) {
493 			if (errno == EOPNOTSUPP)
494 				warn("%s: chflags", to_name);
495 			else {
496 				serrno = errno;
497 				(void)unlink(to_name);
498 				errno = serrno;
499 				err(EX_OSERR, "%s: chflags", to_name);
500 			}
501 		}
502 	}
503 
504 	(void)close(to_fd);
505 	if (!devnull)
506 		(void)close(from_fd);
507 }
508 
509 /*
510  * compare --
511  *	compare two files; non-zero means files differ
512  */
513 int
514 compare(int from_fd, const char *from_name __unused, size_t from_len,
515 	int to_fd, const char *to_name __unused, size_t to_len)
516 {
517 	char *p, *q;
518 	int rv;
519 	int done_compare;
520 
521 	rv = 0;
522 	if (from_len != to_len)
523 		return 1;
524 
525 	if (from_len <= MAX_CMP_SIZE) {
526 		done_compare = 0;
527 		if (trymmap(from_fd) && trymmap(to_fd)) {
528 			p = mmap(NULL, from_len, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
529 			if (p == (char *)MAP_FAILED)
530 				goto out;
531 			q = mmap(NULL, from_len, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
532 			if (q == (char *)MAP_FAILED) {
533 				munmap(p, from_len);
534 				goto out;
535 			}
536 
537 			rv = memcmp(p, q, from_len);
538 			munmap(p, from_len);
539 			munmap(q, from_len);
540 			done_compare = 1;
541 		}
542 	out:
543 		if (!done_compare) {
544 			char buf1[MAXBSIZE];
545 			char buf2[MAXBSIZE];
546 			int n1, n2;
547 
548 			rv = 0;
549 			lseek(from_fd, 0, SEEK_SET);
550 			lseek(to_fd, 0, SEEK_SET);
551 			while (rv == 0) {
552 				n1 = read(from_fd, buf1, sizeof(buf1));
553 				if (n1 == 0)
554 					break;		/* EOF */
555 				else if (n1 > 0) {
556 					n2 = read(to_fd, buf2, n1);
557 					if (n2 == n1)
558 						rv = memcmp(buf1, buf2, n1);
559 					else
560 						rv = 1;	/* out of sync */
561 				} else
562 					rv = 1;		/* read failure */
563 			}
564 			lseek(from_fd, 0, SEEK_SET);
565 			lseek(to_fd, 0, SEEK_SET);
566 		}
567 	} else
568 		rv = 1;	/* don't bother in this case */
569 
570 	return rv;
571 }
572 
573 /*
574  * create_tempfile --
575  *	create a temporary file based on path and open it
576  */
577 int
578 create_tempfile(path, temp, tsize)
579 	const char *path;
580 	char *temp;
581 	size_t tsize;
582 {
583 	char *p;
584 
585 	(void)strncpy(temp, path, tsize);
586 	temp[tsize - 1] = '\0';
587 	if ((p = strrchr(temp, '/')) != NULL)
588 		p++;
589 	else
590 		p = temp;
591 	(void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
592 	temp[tsize - 1] = '\0';
593 	return (mkstemp(temp));
594 }
595 
596 /*
597  * create_newfile --
598  *	create a new file, overwriting an existing one if necessary
599  */
600 int
601 create_newfile(path, target, sbp)
602 	const char *path;
603 	int target;
604 	struct stat *sbp;
605 {
606 	char backup[MAXPATHLEN];
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 			(void)unlink(path);
631 	}
632 
633 	return (open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR));
634 }
635 
636 /*
637  * copy --
638  *	copy from one file to another
639  */
640 void
641 copy(from_fd, from_name, to_fd, to_name, size)
642 	register int from_fd, to_fd;
643 	const char *from_name, *to_name;
644 	off_t size;
645 {
646 	register 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(to_name)
697 	const char *to_name;
698 {
699 	const char *stripbin;
700 	int serrno, status;
701 
702 	switch (fork()) {
703 	case -1:
704 		serrno = errno;
705 		(void)unlink(to_name);
706 		errno = serrno;
707 		err(EX_TEMPFAIL, "fork");
708 	case 0:
709 		stripbin = getenv("STRIPBIN");
710 		if (stripbin == NULL)
711 			stripbin = "strip";
712 		execlp(stripbin, stripbin, to_name, (char *)NULL);
713 		err(EX_OSERR, "exec(%s)", stripbin);
714 	default:
715 		if (wait(&status) == -1 || status) {
716 			serrno = errno;
717 			(void)unlink(to_name);
718 			errc(EX_SOFTWARE, serrno, "wait");
719 			/* NOTREACHED */
720 		}
721 	}
722 }
723 
724 /*
725  * install_dir --
726  *	build directory heirarchy
727  */
728 void
729 install_dir(path)
730 	char *path;
731 {
732 	register 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(fd)
782 	int fd;
783 {
784 /*
785  * The ifdef is for bootstrapping - f_fstypename doesn't exist in
786  * pre-Lite2-merge systems.
787  */
788 #ifdef MFSNAMELEN
789 	struct statfs stfs;
790 
791 	if (nommap || fstatfs(fd, &stfs) != 0)
792 		return (0);
793 	if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
794 	    strcmp(stfs.f_fstypename, "cd9660") == 0)
795 		return (1);
796 #endif
797 	return (0);
798 }
799