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