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