xinstall.c revision 77412
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#ifndef lint
41#if 0
42static char sccsid[] = "From: @(#)xinstall.c	8.1 (Berkeley) 7/21/93";
43#endif
44static const char rcsid[] =
45  "$FreeBSD: head/usr.bin/xinstall/xinstall.c 77412 2001-05-29 18:41:16Z imp $";
46#endif /* not lint */
47
48#include <sys/param.h>
49#include <sys/wait.h>
50#include <sys/mman.h>
51#include <sys/stat.h>
52#include <sys/mount.h>
53
54#include <ctype.h>
55#include <err.h>
56#include <errno.h>
57#include <fcntl.h>
58#include <grp.h>
59#include <paths.h>
60#include <pwd.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64#include <unistd.h>
65#include <sysexits.h>
66#include <utime.h>
67
68#include "pathnames.h"
69
70/* Bootstrap aid - this doesn't exist in most older releases */
71#ifndef MAP_FAILED
72#define MAP_FAILED ((void *)-1)	/* from <sys/mman.h> */
73#endif
74
75#define	DIRECTORY	0x01		/* Tell install it's a directory. */
76#define	SETFLAGS	0x02		/* Tell install to set flags. */
77#define	NOCHANGEBITS	(UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
78#define	BACKUP_SUFFIX	".old"
79
80struct passwd *pp;
81struct group *gp;
82gid_t gid;
83uid_t uid;
84int dobackup, docompare, dodir, dopreserve, dostrip, nommap, safecopy, verbose;
85mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
86char *suffix = BACKUP_SUFFIX;
87
88void	copy __P((int, char *, int, char *, off_t));
89int	compare __P((int, const char *, size_t, int, const char *, size_t));
90int	create_newfile __P((char *, int, struct stat *));
91int	create_tempfile __P((char *, char *, size_t));
92void	install __P((char *, char *, u_long, u_int));
93void	install_dir __P((char *));
94u_long	numeric_id __P((char *, char *));
95void	strip __P((char *));
96int	trymmap __P((int));
97void	usage __P((void));
98
99int
100main(argc, argv)
101	int argc;
102	char *argv[];
103{
104	struct stat from_sb, to_sb;
105	mode_t *set;
106	u_long fset;
107	int ch, no_target;
108	u_int iflags;
109	char *flags, *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		usage();
174
175	/* must have at least two arguments, except when creating directories */
176	if (argc < 2 && !dodir)
177		usage();
178
179	/* need to make a temp copy so we can compare stripped version */
180	if (docompare && dostrip)
181		safecopy = 1;
182
183	/* get group and owner id's */
184	if (group != NULL) {
185		if ((gp = getgrnam(group)) != NULL)
186			gid = gp->gr_gid;
187		else
188			gid = (uid_t)numeric_id(group, "group");
189	} else
190		gid = (gid_t)-1;
191
192	if (owner != NULL) {
193		if ((pp = getpwnam(owner)) != NULL)
194			uid = pp->pw_uid;
195		else
196			uid = (uid_t)numeric_id(owner, "user");
197	} else
198		uid = (uid_t)-1;
199
200	if (dodir) {
201		for (; *argv != NULL; ++argv)
202			install_dir(*argv);
203		exit(EX_OK);
204		/* NOTREACHED */
205	}
206
207	no_target = stat(to_name = argv[argc - 1], &to_sb);
208	if (!no_target && S_ISDIR(to_sb.st_mode)) {
209		for (; *argv != to_name; ++argv)
210			install(*argv, to_name, fset, iflags | DIRECTORY);
211		exit(EX_OK);
212		/* NOTREACHED */
213	}
214
215	/* can't do file1 file2 directory/file */
216	if (argc != 2)
217		usage();
218
219	if (!no_target) {
220		if (stat(*argv, &from_sb))
221			err(EX_OSERR, "%s", *argv);
222		if (!S_ISREG(to_sb.st_mode)) {
223			errno = EFTYPE;
224			err(EX_OSERR, "%s", to_name);
225		}
226		if (to_sb.st_dev == from_sb.st_dev &&
227		    to_sb.st_ino == from_sb.st_ino)
228			errx(EX_USAGE,
229			    "%s and %s are the same file", *argv, to_name);
230	}
231	install(*argv, to_name, fset, iflags);
232	exit(EX_OK);
233	/* NOTREACHED */
234}
235
236u_long
237numeric_id(name, type)
238	char *name, *type;
239{
240	u_long val;
241	char *ep;
242
243	/*
244	 * XXX
245	 * We know that uid_t's and gid_t's are unsigned longs.
246	 */
247	errno = 0;
248	val = strtoul(name, &ep, 10);
249	if (errno)
250		err(EX_NOUSER, "%s", name);
251	if (*ep != '\0')
252		errx(EX_NOUSER, "unknown %s %s", type, name);
253	return (val);
254}
255
256/*
257 * install --
258 *	build a path name and install the file
259 */
260void
261install(from_name, to_name, fset, flags)
262	char *from_name, *to_name;
263	u_long fset;
264	u_int flags;
265{
266	struct stat from_sb, temp_sb, to_sb;
267	struct utimbuf utb;
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				utb.actime = to_sb.st_atime;
383				utb.modtime = to_sb.st_mtime;
384				(void)utime(tempfile, &utb);
385			} else {
386				files_match = 1;
387				(void)unlink(tempfile);
388			}
389			(void) close(temp_fd);
390		}
391	}
392
393	/*
394	 * Move the new file into place if doing a safe copy
395	 * and the files are different (or just not compared).
396	 */
397	if (tempcopy && !files_match) {
398		/* Try to turn off the immutable bits. */
399		if (to_sb.st_flags & NOCHANGEBITS)
400			(void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
401		if (dobackup) {
402			if (snprintf(backup, MAXPATHLEN, "%s%s", to_name,
403			    suffix) != strlen(to_name) + strlen(suffix)) {
404				unlink(tempfile);
405				errx(EX_OSERR, "%s: backup filename too long",
406				    to_name);
407			}
408			if (verbose)
409				(void)printf("install: %s -> %s\n", to_name, backup);
410			if (rename(to_name, backup) < 0) {
411				serrno = errno;
412				unlink(tempfile);
413				errno = serrno;
414				err(EX_OSERR, "rename: %s to %s", to_name,
415				     backup);
416			}
417		}
418		if (verbose)
419			(void)printf("install: %s -> %s\n", from_name, to_name);
420		if (rename(tempfile, to_name) < 0) {
421			serrno = errno;
422			unlink(tempfile);
423			errno = serrno;
424			err(EX_OSERR, "rename: %s to %s",
425			    tempfile, to_name);
426		}
427
428		/* Re-open to_fd so we aren't hosed by the rename(2). */
429		(void) close(to_fd);
430		if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
431			err(EX_OSERR, "%s", to_name);
432	}
433
434	/*
435	 * Preserve the timestamp of the source file if necessary.
436	 */
437	if (dopreserve && !files_match && !devnull) {
438		utb.actime = from_sb.st_atime;
439		utb.modtime = from_sb.st_mtime;
440		(void)utime(to_name, &utb);
441	}
442
443	if (fstat(to_fd, &to_sb) == -1) {
444		serrno = errno;
445		(void)unlink(to_name);
446		errno = serrno;
447		err(EX_OSERR, "%s", to_name);
448	}
449
450	/*
451	 * Set owner, group, mode for target; do the chown first,
452	 * chown may lose the setuid bits.
453	 */
454	if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
455	    (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
456	    (mode != to_sb.st_mode)) {
457		/* Try to turn off the immutable bits. */
458		if (to_sb.st_flags & NOCHANGEBITS)
459			(void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
460	}
461
462	if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
463	    (uid != (uid_t)-1 && uid != to_sb.st_uid))
464		if (fchown(to_fd, uid, gid) == -1) {
465			serrno = errno;
466			(void)unlink(to_name);
467			errno = serrno;
468			err(EX_OSERR,"%s: chown/chgrp", to_name);
469		}
470
471	if (mode != to_sb.st_mode)
472		if (fchmod(to_fd, mode)) {
473			serrno = errno;
474			(void)unlink(to_name);
475			errno = serrno;
476			err(EX_OSERR, "%s: chmod", to_name);
477		}
478
479	/*
480	 * If provided a set of flags, set them, otherwise, preserve the
481	 * flags, except for the dump flag.
482	 * NFS does not support flags.  Ignore EOPNOTSUPP flags if we're just
483	 * trying to turn off UF_NODUMP.  If we're trying to set real flags,
484	 * then warn if the the fs doesn't support it, otherwise fail.
485	 */
486	if (!devnull && fchflags(to_fd,
487	    flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
488		if (flags & SETFLAGS) {
489			if (errno == EOPNOTSUPP)
490				warn("%s: chflags", to_name);
491			else {
492				serrno = errno;
493				(void)unlink(to_name);
494				errno = serrno;
495				err(EX_OSERR, "%s: chflags", to_name);
496			}
497		}
498	}
499
500	(void)close(to_fd);
501	if (!devnull)
502		(void)close(from_fd);
503}
504
505/*
506 * compare --
507 *	compare two files; non-zero means files differ
508 */
509int
510compare(int from_fd, const char *from_name, size_t from_len,
511	int to_fd, const char *to_name, size_t to_len)
512{
513	char *p, *q;
514	int rv;
515	int done_compare;
516
517	rv = 0;
518	if (from_len != to_len)
519		return 1;
520
521	if (from_len <= 8 * 1024 * 1024) {
522		done_compare = 0;
523		if (trymmap(from_fd) && trymmap(to_fd)) {
524			p = mmap(NULL, from_len, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
525			if (p == (char *)MAP_FAILED)
526				goto out;
527			q = mmap(NULL, from_len, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
528			if (q == (char *)MAP_FAILED) {
529				munmap(p, from_len);
530				goto out;
531			}
532
533			rv = memcmp(p, q, from_len);
534			munmap(p, from_len);
535			munmap(q, from_len);
536			done_compare = 1;
537		}
538	out:
539		if (!done_compare) {
540			char buf1[MAXBSIZE];
541			char buf2[MAXBSIZE];
542			int n1, n2;
543
544			rv = 0;
545			lseek(from_fd, 0, SEEK_SET);
546			lseek(to_fd, 0, SEEK_SET);
547			while (rv == 0) {
548				n1 = read(from_fd, buf1, sizeof(buf1));
549				if (n1 == 0)
550					break;		/* EOF */
551				else if (n1 > 0) {
552					n2 = read(to_fd, buf2, n1);
553					if (n2 == n1)
554						rv = memcmp(buf1, buf2, n1);
555					else
556						rv = 1;	/* out of sync */
557				} else
558					rv = 1;		/* read failure */
559			}
560			lseek(from_fd, 0, SEEK_SET);
561			lseek(to_fd, 0, SEEK_SET);
562		}
563	} else
564		rv = 1;	/* don't bother in this case */
565
566	return rv;
567}
568
569/*
570 * create_tempfile --
571 *	create a temporary file based on path and open it
572 */
573int
574create_tempfile(path, temp, tsize)
575	char *path;
576	char *temp;
577	size_t tsize;
578{
579	char *p;
580
581	(void)strncpy(temp, path, tsize);
582	temp[tsize - 1] = '\0';
583	if ((p = strrchr(temp, '/')) != NULL)
584		p++;
585	else
586		p = temp;
587	(void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
588	temp[tsize - 1] = '\0';
589	return (mkstemp(temp));
590}
591
592/*
593 * create_newfile --
594 *	create a new file, overwriting an existing one if necessary
595 */
596int
597create_newfile(path, target, sbp)
598	char *path;
599	int target;
600	struct stat *sbp;
601{
602	char backup[MAXPATHLEN];
603
604	if (target) {
605		/*
606		 * Unlink now... avoid ETXTBSY errors later.  Try to turn
607		 * off the append/immutable bits -- if we fail, go ahead,
608		 * it might work.
609		 */
610		if (sbp->st_flags & NOCHANGEBITS)
611			(void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
612
613		if (dobackup) {
614			if (snprintf(backup, MAXPATHLEN, "%s%s",
615			    path, suffix) != strlen(path) + strlen(suffix))
616				errx(EX_OSERR, "%s: backup filename too long",
617				    path);
618			(void)snprintf(backup, MAXPATHLEN, "%s%s",
619			    path, suffix);
620			if (verbose)
621				(void)printf("install: %s -> %s\n",
622				    path, backup);
623			if (rename(path, backup) < 0)
624				err(EX_OSERR, "rename: %s to %s", path, backup);
625		} else
626			(void)unlink(path);
627	}
628
629	return (open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR));
630}
631
632/*
633 * copy --
634 *	copy from one file to another
635 */
636void
637copy(from_fd, from_name, to_fd, to_name, size)
638	register int from_fd, to_fd;
639	char *from_name, *to_name;
640	off_t size;
641{
642	register int nr, nw;
643	int serrno;
644	char *p, buf[MAXBSIZE];
645	int done_copy;
646
647	/* Rewind file descriptors. */
648	if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
649		err(EX_OSERR, "lseek: %s", from_name);
650	if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
651		err(EX_OSERR, "lseek: %s", to_name);
652
653	/*
654	 * Mmap and write if less than 8M (the limit is so we don't totally
655	 * trash memory on big files.  This is really a minor hack, but it
656	 * wins some CPU back.
657	 */
658	done_copy = 0;
659	if (size <= 8 * 1048576 && trymmap(from_fd) &&
660	    (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
661		    from_fd, (off_t)0)) != (char *)MAP_FAILED) {
662		if ((nw = write(to_fd, p, size)) != size) {
663			serrno = errno;
664			(void)unlink(to_name);
665			errno = nw > 0 ? EIO : serrno;
666			err(EX_OSERR, "%s", to_name);
667		}
668		done_copy = 1;
669	}
670	if (!done_copy) {
671		while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
672			if ((nw = write(to_fd, buf, nr)) != nr) {
673				serrno = errno;
674				(void)unlink(to_name);
675				errno = nw > 0 ? EIO : serrno;
676				err(EX_OSERR, "%s", to_name);
677			}
678		if (nr != 0) {
679			serrno = errno;
680			(void)unlink(to_name);
681			errno = serrno;
682			err(EX_OSERR, "%s", from_name);
683		}
684	}
685}
686
687/*
688 * strip --
689 *	use strip(1) to strip the target file
690 */
691void
692strip(to_name)
693	char *to_name;
694{
695	int serrno, status;
696
697	switch (fork()) {
698	case -1:
699		serrno = errno;
700		(void)unlink(to_name);
701		errno = serrno;
702		err(EX_TEMPFAIL, "fork");
703	case 0:
704		execlp("strip", "strip", to_name, NULL);
705		err(EX_OSERR, "exec(strip)");
706	default:
707		if (wait(&status) == -1 || status) {
708			(void)unlink(to_name);
709			exit(EX_SOFTWARE);
710			/* NOTREACHED */
711		}
712	}
713}
714
715/*
716 * install_dir --
717 *	build directory heirarchy
718 */
719void
720install_dir(path)
721	char *path;
722{
723	register char *p;
724	struct stat sb;
725	int ch;
726
727	for (p = path;; ++p)
728		if (!*p || (p != path && *p  == '/')) {
729			ch = *p;
730			*p = '\0';
731			if (stat(path, &sb)) {
732				if (errno != ENOENT || mkdir(path, 0755) < 0) {
733					err(EX_OSERR, "mkdir %s", path);
734					/* NOTREACHED */
735				} else if (verbose)
736					(void)printf("install: mkdir %s\n",
737						     path);
738			} else if (!S_ISDIR(sb.st_mode))
739				errx(EX_OSERR, "%s exists but is not a directory", path);
740			if (!(*p = ch))
741				break;
742 		}
743
744	if ((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid))
745		warn("chown %u:%u %s", uid, gid, path);
746	if (chmod(path, mode))
747		warn("chmod %o %s", mode, path);
748}
749
750/*
751 * usage --
752 *	print a usage message and die
753 */
754void
755usage()
756{
757	(void)fprintf(stderr, "\
758usage: install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n\
759               [-o owner] file1 file2\n\
760       install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n\
761               [-o owner] file1 ... fileN directory\n\
762       install -d [-v] [-g group] [-m mode] [-o owner] directory ...\n");
763	exit(EX_USAGE);
764	/* NOTREACHED */
765}
766
767/*
768 * trymmap --
769 *	return true (1) if mmap should be tried, false (0) if not.
770 */
771int
772trymmap(fd)
773	int fd;
774{
775/*
776 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
777 * pre-Lite2-merge systems.
778 */
779#ifdef MFSNAMELEN
780	struct statfs stfs;
781
782	if (nommap || fstatfs(fd, &stfs) != 0)
783		return (0);
784	if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
785	    strcmp(stfs.f_fstypename, "cd9660") == 0)
786		return (1);
787#endif
788	return (0);
789}
790