Home | History | Annotate | Line # | Download | only in xinstall
xinstall.c revision 1.26
      1  1.26  christos /*	$NetBSD: xinstall.c,v 1.26 1998/09/28 08:16:15 christos Exp $	*/
      2   1.5       jtc 
      3   1.1       cgd /*
      4   1.5       jtc  * Copyright (c) 1987, 1993
      5   1.5       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.20       mrg #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.20       mrg __COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
     39  1.20       mrg 	The Regents of the University of California.  All rights reserved.\n");
     40   1.1       cgd #endif /* not lint */
     41   1.1       cgd 
     42   1.1       cgd #ifndef lint
     43   1.5       jtc #if 0
     44   1.5       jtc static char sccsid[] = "@(#)xinstall.c	8.1 (Berkeley) 7/21/93";
     45  1.20       mrg #else
     46  1.26  christos __RCSID("$NetBSD: xinstall.c,v 1.26 1998/09/28 08:16:15 christos Exp $");
     47   1.5       jtc #endif
     48   1.1       cgd #endif /* not lint */
     49   1.1       cgd 
     50   1.1       cgd #include <sys/param.h>
     51   1.5       jtc #include <sys/wait.h>
     52   1.5       jtc #include <sys/mman.h>
     53   1.1       cgd #include <sys/stat.h>
     54   1.5       jtc 
     55   1.5       jtc #include <ctype.h>
     56   1.5       jtc #include <errno.h>
     57   1.5       jtc #include <fcntl.h>
     58   1.1       cgd #include <grp.h>
     59   1.5       jtc #include <paths.h>
     60   1.1       cgd #include <pwd.h>
     61   1.1       cgd #include <stdio.h>
     62   1.5       jtc #include <stdlib.h>
     63   1.5       jtc #include <string.h>
     64   1.5       jtc #include <unistd.h>
     65   1.5       jtc #include <err.h>
     66   1.5       jtc 
     67   1.1       cgd #include "pathnames.h"
     68   1.1       cgd 
     69   1.5       jtc struct passwd *pp;
     70   1.5       jtc struct group *gp;
     71  1.26  christos int docopy, dodir, dostrip, dolink, dopreserve;
     72   1.5       jtc int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
     73   1.8   ghudson char pathbuf[MAXPATHLEN];
     74   1.8   ghudson uid_t uid;
     75   1.8   ghudson gid_t gid;
     76   1.5       jtc 
     77  1.16  christos #define LN_ABSOLUTE	0x01
     78  1.16  christos #define LN_RELATIVE	0x02
     79  1.16  christos #define LN_HARD		0x04
     80  1.16  christos #define LN_SYMBOLIC	0x08
     81  1.16  christos #define LN_MIXED	0x10
     82  1.16  christos 
     83   1.5       jtc #define	DIRECTORY	0x01		/* Tell install it's a directory. */
     84   1.5       jtc #define	SETFLAGS	0x02		/* Tell install to set flags. */
     85   1.5       jtc 
     86   1.5       jtc void	copy __P((int, char *, int, char *, off_t));
     87  1.16  christos void	makelink __P((char *, char *));
     88   1.5       jtc void	install __P((char *, char *, u_long, u_int));
     89   1.7       jtc void	install_dir __P((char *));
     90   1.5       jtc u_long	string_to_flags __P((char **, u_long *, u_long *));
     91   1.5       jtc void	strip __P((char *));
     92   1.5       jtc void	usage __P((void));
     93  1.20       mrg int	main __P((int, char *[]));
     94   1.1       cgd 
     95   1.5       jtc int
     96   1.1       cgd main(argc, argv)
     97   1.1       cgd 	int argc;
     98   1.5       jtc 	char *argv[];
     99   1.1       cgd {
    100   1.1       cgd 	struct stat from_sb, to_sb;
    101   1.5       jtc 	mode_t *set;
    102   1.5       jtc 	u_long fset;
    103   1.5       jtc 	u_int iflags;
    104   1.1       cgd 	int ch, no_target;
    105  1.16  christos 	char *p;
    106  1.16  christos 	char *flags = NULL, *to_name, *group = NULL, *owner = NULL;
    107   1.1       cgd 
    108   1.5       jtc 	iflags = 0;
    109  1.26  christos 	while ((ch = getopt(argc, argv, "cdf:g:l:m:o:ps")) != -1)
    110   1.1       cgd 		switch((char)ch) {
    111   1.1       cgd 		case 'c':
    112   1.1       cgd 			docopy = 1;
    113   1.1       cgd 			break;
    114  1.26  christos 		case 'd':
    115  1.26  christos 			dodir = 1;
    116  1.26  christos 			break;
    117   1.5       jtc 		case 'f':
    118   1.5       jtc 			flags = optarg;
    119   1.5       jtc 			if (string_to_flags(&flags, &fset, NULL))
    120   1.5       jtc 				errx(1, "%s: invalid flag", flags);
    121   1.5       jtc 			iflags |= SETFLAGS;
    122   1.5       jtc 			break;
    123   1.1       cgd 		case 'g':
    124   1.1       cgd 			group = optarg;
    125   1.1       cgd 			break;
    126  1.16  christos 		case 'l':
    127  1.16  christos 			for (p = optarg; *p; p++)
    128  1.16  christos 				switch (*p) {
    129  1.16  christos 				case 's':
    130  1.16  christos 					dolink &= ~(LN_HARD|LN_MIXED);
    131  1.16  christos 					dolink |= LN_SYMBOLIC;
    132  1.16  christos 					break;
    133  1.16  christos 				case 'h':
    134  1.16  christos 					dolink &= ~(LN_SYMBOLIC|LN_MIXED);
    135  1.16  christos 					dolink |= LN_HARD;
    136  1.16  christos 					break;
    137  1.16  christos 				case 'm':
    138  1.16  christos 					dolink &= ~(LN_SYMBOLIC|LN_HARD);
    139  1.16  christos 					dolink |= LN_MIXED;
    140  1.16  christos 					break;
    141  1.16  christos 				case 'a':
    142  1.16  christos 					dolink &= ~LN_RELATIVE;
    143  1.16  christos 					dolink |= LN_ABSOLUTE;
    144  1.16  christos 					break;
    145  1.16  christos 				case 'r':
    146  1.16  christos 					dolink &= ~LN_ABSOLUTE;
    147  1.16  christos 					dolink |= LN_RELATIVE;
    148  1.16  christos 					break;
    149  1.16  christos 				default:
    150  1.16  christos 					errx(1, "%c: invalid link type", *p);
    151  1.16  christos 					break;
    152  1.16  christos 				}
    153  1.16  christos 			break;
    154  1.26  christos 		case 'm':
    155  1.26  christos 			if (!(set = setmode(optarg)))
    156  1.26  christos 				errx(1, "%s: invalid file mode", optarg);
    157  1.26  christos 			mode = getmode(set, 0);
    158  1.26  christos 			break;
    159  1.26  christos 		case 'o':
    160  1.26  christos 			owner = optarg;
    161  1.26  christos 			break;
    162  1.26  christos 		case 'p':
    163  1.26  christos 			dopreserve = 1;
    164  1.26  christos 			break;
    165  1.26  christos 		case 's':
    166  1.26  christos 			dostrip = 1;
    167  1.26  christos 			break;
    168   1.1       cgd 		case '?':
    169   1.1       cgd 		default:
    170   1.1       cgd 			usage();
    171   1.1       cgd 		}
    172   1.1       cgd 	argc -= optind;
    173   1.1       cgd 	argv += optind;
    174   1.2       jtc 
    175  1.23        tv 	/* strip and link options make no sense when creating directories */
    176  1.23        tv 	if ((dostrip || dolink) && dodir)
    177  1.16  christos 		usage();
    178  1.16  christos 
    179  1.16  christos 	/* strip and flags make no sense with links */
    180  1.16  christos 	if ((dostrip || flags) && dolink)
    181   1.2       jtc 		usage();
    182   1.2       jtc 
    183   1.2       jtc 	/* must have at least two arguments, except when creating directories */
    184   1.2       jtc 	if (argc < 2 && !dodir)
    185   1.1       cgd 		usage();
    186   1.1       cgd 
    187   1.1       cgd 	/* get group and owner id's */
    188   1.8   ghudson 	if (group && !(gp = getgrnam(group)) && !isdigit(*group))
    189   1.5       jtc 		errx(1, "unknown group %s", group);
    190   1.8   ghudson 	gid = (group) ? ((gp) ? gp->gr_gid : atoi(group)) : -1;
    191   1.8   ghudson 	if (owner && !(pp = getpwnam(owner)) && !isdigit(*owner))
    192   1.5       jtc 		errx(1, "unknown user %s", owner);
    193   1.8   ghudson 	uid = (owner) ? ((pp) ? pp->pw_uid : atoi(owner)) : -1;
    194   1.1       cgd 
    195   1.2       jtc 	if (dodir) {
    196   1.2       jtc 		for (; *argv != NULL; ++argv)
    197   1.5       jtc 			install_dir(*argv);
    198   1.2       jtc 		exit (0);
    199   1.2       jtc 		/* NOTREACHED */
    200   1.5       jtc 	}
    201   1.2       jtc 
    202   1.1       cgd 	no_target = stat(to_name = argv[argc - 1], &to_sb);
    203   1.2       jtc 	if (!no_target && S_ISDIR(to_sb.st_mode)) {
    204   1.1       cgd 		for (; *argv != to_name; ++argv)
    205   1.5       jtc 			install(*argv, to_name, fset, iflags | DIRECTORY);
    206   1.1       cgd 		exit(0);
    207   1.1       cgd 	}
    208   1.1       cgd 
    209   1.1       cgd 	/* can't do file1 file2 directory/file */
    210   1.1       cgd 	if (argc != 2)
    211   1.1       cgd 		usage();
    212   1.1       cgd 
    213   1.1       cgd 	if (!no_target) {
    214   1.5       jtc 		if (stat(*argv, &from_sb))
    215   1.5       jtc 			err(1, "%s", *argv);
    216   1.5       jtc 		if (!S_ISREG(to_sb.st_mode))
    217   1.5       jtc 			errx(1, "%s: %s", to_name, strerror(EFTYPE));
    218  1.16  christos 		if (!dolink && to_sb.st_dev == from_sb.st_dev &&
    219   1.5       jtc 		    to_sb.st_ino == from_sb.st_ino)
    220   1.5       jtc 			errx(1, "%s and %s are the same file", *argv, to_name);
    221   1.5       jtc 		/*
    222   1.5       jtc 		 * Unlink now... avoid ETXTBSY errors later.  Try and turn
    223   1.5       jtc 		 * off the append/immutable bits -- if we fail, go ahead,
    224   1.5       jtc 		 * it might work.
    225   1.5       jtc 		 */
    226   1.5       jtc #define	NOCHANGEBITS	(UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
    227   1.5       jtc 		if (to_sb.st_flags & NOCHANGEBITS)
    228   1.5       jtc 			(void)chflags(to_name,
    229   1.5       jtc 			    to_sb.st_flags & ~(NOCHANGEBITS));
    230   1.1       cgd 		(void)unlink(to_name);
    231   1.1       cgd 	}
    232   1.5       jtc 	install(*argv, to_name, fset, iflags);
    233   1.1       cgd 	exit(0);
    234   1.1       cgd }
    235   1.1       cgd 
    236   1.1       cgd /*
    237  1.16  christos  * makelink --
    238  1.16  christos  *	make a link from source to destination
    239  1.16  christos  */
    240  1.16  christos void
    241  1.16  christos makelink(from_name, to_name)
    242  1.16  christos 	char *from_name;
    243  1.16  christos 	char *to_name;
    244  1.16  christos {
    245  1.16  christos 	char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
    246  1.16  christos 
    247  1.16  christos 	/* Try hard links first */
    248  1.16  christos 	if (dolink & (LN_HARD|LN_MIXED)) {
    249  1.16  christos 		if (link(from_name, to_name) == -1) {
    250  1.16  christos 			if ((dolink & LN_HARD) || errno != EXDEV)
    251  1.16  christos 				err(1, "link %s -> %s", from_name, to_name);
    252  1.16  christos 		}
    253  1.16  christos 		else
    254  1.16  christos 			return;
    255  1.16  christos 	}
    256  1.16  christos 
    257  1.16  christos 	/* Symbolic links */
    258  1.16  christos 	if (dolink & LN_ABSOLUTE) {
    259  1.16  christos 		/* Convert source path to absolute */
    260  1.16  christos 		if (realpath(from_name, src) == NULL)
    261  1.16  christos 			err(1, "%s", src);
    262  1.16  christos 		if (symlink(src, to_name) == -1)
    263  1.16  christos 			err(1, "symlink %s -> %s", src, to_name);
    264  1.16  christos 		return;
    265  1.16  christos 	}
    266  1.16  christos 
    267  1.16  christos 	if (dolink & LN_RELATIVE) {
    268  1.16  christos 		char *s, *d;
    269  1.16  christos 
    270  1.16  christos 		/* Resolve pathnames */
    271  1.16  christos 		if (realpath(from_name, src) == NULL)
    272  1.16  christos 			err(1, "%s", src);
    273  1.16  christos 		if (realpath(to_name, dst) == NULL)
    274  1.16  christos 			err(1, "%s", dst);
    275  1.16  christos 
    276  1.16  christos 		/* trim common path components */
    277  1.16  christos 		for (s = src, d = dst; *s == *d; s++, d++)
    278  1.16  christos 			continue;
    279  1.16  christos 		while (*s != '/')
    280  1.16  christos 			s--, d--;
    281  1.16  christos 
    282  1.16  christos 		/* count the number of directories we need to backtrack */
    283  1.16  christos 		for (++d, lnk[0] = '\0'; *d; d++)
    284  1.16  christos 			if (*d == '/')
    285  1.16  christos 				(void) strcat(lnk, "../");
    286  1.16  christos 
    287  1.16  christos 		(void) strcat(lnk, ++s);
    288  1.16  christos 
    289  1.16  christos 		if (symlink(lnk, dst) == -1)
    290  1.16  christos 			err(1, "symlink %s -> %s", lnk, dst);
    291  1.16  christos 		return;
    292  1.16  christos 	}
    293  1.16  christos 
    294  1.16  christos 	/*
    295  1.16  christos 	 * If absolute or relative was not specified,
    296  1.16  christos 	 * try the names the user provided
    297  1.16  christos 	 */
    298  1.16  christos 	if (symlink(from_name, to_name) == -1)
    299  1.17  christos 		err(1, "symlink %s -> %s", from_name, to_name);
    300  1.16  christos 
    301  1.16  christos }
    302  1.16  christos 
    303  1.16  christos /*
    304   1.1       cgd  * install --
    305   1.1       cgd  *	build a path name and install the file
    306   1.1       cgd  */
    307   1.5       jtc void
    308   1.5       jtc install(from_name, to_name, fset, flags)
    309   1.1       cgd 	char *from_name, *to_name;
    310   1.5       jtc 	u_long fset;
    311   1.5       jtc 	u_int flags;
    312   1.1       cgd {
    313   1.5       jtc 	struct stat from_sb, to_sb;
    314   1.5       jtc 	int devnull, from_fd, to_fd, serrno;
    315   1.5       jtc 	char *p;
    316   1.5       jtc 
    317   1.5       jtc 	/* If try to install NULL file to a directory, fails. */
    318   1.5       jtc 	if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
    319   1.5       jtc 		if (stat(from_name, &from_sb))
    320   1.5       jtc 			err(1, "%s", from_name);
    321   1.5       jtc 		if (!S_ISREG(from_sb.st_mode))
    322   1.5       jtc 			errx(1, "%s: %s", from_name, strerror(EFTYPE));
    323   1.5       jtc 		/* Build the target path. */
    324   1.5       jtc 		if (flags & DIRECTORY) {
    325   1.5       jtc 			(void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
    326   1.5       jtc 			    to_name,
    327  1.21     lukem 			    (p = strrchr(from_name, '/')) ? ++p : from_name);
    328   1.1       cgd 			to_name = pathbuf;
    329   1.1       cgd 		}
    330   1.1       cgd 		devnull = 0;
    331   1.5       jtc 	} else {
    332   1.5       jtc 		from_sb.st_flags = 0;	/* XXX */
    333   1.1       cgd 		devnull = 1;
    334   1.5       jtc 	}
    335   1.1       cgd 
    336   1.5       jtc 	/*
    337   1.5       jtc 	 * Unlink now... avoid ETXTBSY errors later.  Try and turn
    338   1.5       jtc 	 * off the append/immutable bits -- if we fail, go ahead,
    339   1.5       jtc 	 * it might work.
    340   1.5       jtc 	 */
    341   1.5       jtc 	if (stat(to_name, &to_sb) == 0 &&
    342   1.5       jtc 	    to_sb.st_flags & (NOCHANGEBITS))
    343   1.5       jtc 		(void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
    344   1.1       cgd 	(void)unlink(to_name);
    345  1.16  christos 
    346  1.16  christos 	if (dolink) {
    347  1.16  christos 		makelink(from_name, to_name);
    348  1.16  christos 		return;
    349  1.16  christos 	}
    350   1.1       cgd 
    351   1.5       jtc 	/* Create target. */
    352   1.5       jtc 	if ((to_fd = open(to_name,
    353   1.5       jtc 	    O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
    354   1.5       jtc 		err(1, "%s", to_name);
    355   1.1       cgd 	if (!devnull) {
    356   1.1       cgd 		if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
    357   1.1       cgd 			(void)unlink(to_name);
    358   1.5       jtc 			err(1, "%s", from_name);
    359   1.1       cgd 		}
    360   1.5       jtc 		copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
    361   1.1       cgd 		(void)close(from_fd);
    362   1.1       cgd 	}
    363   1.9  jonathan 
    364   1.9  jonathan 	if (dostrip) {
    365   1.1       cgd 		strip(to_name);
    366   1.9  jonathan 
    367   1.9  jonathan 		/*
    368   1.9  jonathan 		 * Re-open our fd on the target, in case we used a strip
    369   1.9  jonathan 		 *  that does not work in-place -- like gnu binutils strip.
    370   1.9  jonathan 		 */
    371   1.9  jonathan 		close(to_fd);
    372   1.9  jonathan 		if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
    373   1.9  jonathan 		  err(1, "stripping %s", to_name);
    374   1.9  jonathan 	}
    375   1.9  jonathan 
    376   1.1       cgd 	/*
    377   1.5       jtc 	 * Set owner, group, mode for target; do the chown first,
    378   1.1       cgd 	 * chown may lose the setuid bits.
    379   1.1       cgd 	 */
    380   1.8   ghudson 	if ((gid != -1 || uid != -1) && fchown(to_fd, uid, gid)) {
    381   1.5       jtc 		serrno = errno;
    382   1.5       jtc 		(void)unlink(to_name);
    383   1.5       jtc 		errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
    384   1.1       cgd 	}
    385   1.5       jtc 	if (fchmod(to_fd, mode)) {
    386   1.5       jtc 		serrno = errno;
    387   1.5       jtc 		(void)unlink(to_name);
    388   1.5       jtc 		errx(1, "%s: chmod: %s", to_name, strerror(serrno));
    389   1.5       jtc 	}
    390   1.5       jtc 
    391   1.5       jtc 	/*
    392   1.5       jtc 	 * If provided a set of flags, set them, otherwise, preserve the
    393   1.5       jtc 	 * flags, except for the dump flag.
    394   1.5       jtc 	 */
    395   1.5       jtc 	if (fchflags(to_fd,
    396   1.5       jtc 	    flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
    397  1.11      fvdl 		if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
    398  1.13      fvdl 			warn("%s: chflags", to_name);
    399  1.26  christos 	}
    400  1.26  christos 
    401  1.26  christos 	/*
    402  1.26  christos 	 * Preserve the date of the source file.
    403  1.26  christos 	 */
    404  1.26  christos 	if (dopreserve) {
    405  1.26  christos 		struct timeval tv[2];
    406  1.26  christos 
    407  1.26  christos 		TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
    408  1.26  christos 		TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
    409  1.26  christos 		if (futimes(to_fd, tv) == -1)
    410  1.26  christos 			warn("%s: futimes", to_name);
    411   1.5       jtc 	}
    412   1.5       jtc 
    413   1.1       cgd 	(void)close(to_fd);
    414   1.5       jtc 	if (!docopy && !devnull && unlink(from_name))
    415   1.5       jtc 		err(1, "%s", from_name);
    416   1.1       cgd }
    417   1.1       cgd 
    418   1.1       cgd /*
    419   1.1       cgd  * copy --
    420   1.1       cgd  *	copy from one file to another
    421   1.1       cgd  */
    422   1.5       jtc void
    423   1.5       jtc copy(from_fd, from_name, to_fd, to_name, size)
    424  1.20       mrg 	int from_fd, to_fd;
    425   1.1       cgd 	char *from_name, *to_name;
    426   1.5       jtc 	off_t size;
    427   1.1       cgd {
    428  1.20       mrg 	int nr, nw;
    429   1.5       jtc 	int serrno;
    430  1.14   thorpej 	char *p;
    431  1.14   thorpej 	char buf[MAXBSIZE];
    432   1.1       cgd 
    433   1.5       jtc 	/*
    434   1.5       jtc 	 * Mmap and write if less than 8M (the limit is so we don't totally
    435   1.5       jtc 	 * trash memory on big files.  This is really a minor hack, but it
    436   1.5       jtc 	 * wins some CPU back.
    437   1.5       jtc 	 */
    438   1.5       jtc 	if (size <= 8 * 1048576) {
    439   1.5       jtc 		if ((p = mmap(NULL, (size_t)size, PROT_READ,
    440  1.25   mycroft 		    MAP_FILE|MAP_SHARED, from_fd, (off_t)0)) == (char *)-1)
    441   1.5       jtc 			err(1, "%s", from_name);
    442   1.5       jtc 		if (write(to_fd, p, size) != size)
    443   1.5       jtc 			err(1, "%s", to_name);
    444   1.5       jtc 	} else {
    445   1.5       jtc 		while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
    446   1.5       jtc 			if ((nw = write(to_fd, buf, nr)) != nr) {
    447   1.5       jtc 				serrno = errno;
    448   1.5       jtc 				(void)unlink(to_name);
    449   1.5       jtc 				errx(1, "%s: %s",
    450   1.5       jtc 				    to_name, strerror(nw > 0 ? EIO : serrno));
    451   1.5       jtc 			}
    452   1.5       jtc 		if (nr != 0) {
    453   1.5       jtc 			serrno = errno;
    454   1.5       jtc 			(void)unlink(to_name);
    455   1.5       jtc 			errx(1, "%s: %s", from_name, strerror(serrno));
    456   1.1       cgd 		}
    457   1.1       cgd 	}
    458   1.1       cgd }
    459   1.1       cgd 
    460   1.1       cgd /*
    461   1.1       cgd  * strip --
    462   1.1       cgd  *	use strip(1) to strip the target file
    463   1.1       cgd  */
    464   1.5       jtc void
    465   1.1       cgd strip(to_name)
    466   1.1       cgd 	char *to_name;
    467   1.1       cgd {
    468   1.5       jtc 	int serrno, status;
    469  1.18   thorpej 	char *stripprog;
    470   1.1       cgd 
    471   1.1       cgd 	switch (vfork()) {
    472   1.1       cgd 	case -1:
    473   1.5       jtc 		serrno = errno;
    474   1.5       jtc 		(void)unlink(to_name);
    475  1.22   thorpej 		errx(1, "vfork: %s", strerror(serrno));
    476   1.1       cgd 	case 0:
    477  1.18   thorpej 		stripprog = getenv("STRIP");
    478  1.18   thorpej 		if (stripprog == NULL)
    479  1.18   thorpej 			stripprog = _PATH_STRIP;
    480  1.18   thorpej 		execl(stripprog, "strip", to_name, NULL);
    481  1.22   thorpej 		warn("%s", stripprog);
    482  1.22   thorpej 		_exit(1);
    483   1.1       cgd 	default:
    484   1.1       cgd 		if (wait(&status) == -1 || status)
    485   1.5       jtc 			(void)unlink(to_name);
    486   1.1       cgd 	}
    487   1.1       cgd }
    488   1.1       cgd 
    489   1.1       cgd /*
    490   1.5       jtc  * install_dir --
    491   1.2       jtc  *	build directory heirarchy
    492   1.2       jtc  */
    493   1.7       jtc void
    494   1.5       jtc install_dir(path)
    495   1.2       jtc         char *path;
    496   1.2       jtc {
    497  1.20       mrg         char *p;
    498   1.2       jtc         struct stat sb;
    499   1.4       cgd         int ch;
    500   1.2       jtc 
    501   1.4       cgd         for (p = path;; ++p)
    502   1.4       cgd                 if (!*p || (p != path && *p  == '/')) {
    503   1.2       jtc                         ch = *p;
    504   1.2       jtc                         *p = '\0';
    505   1.2       jtc                         if (stat(path, &sb)) {
    506   1.2       jtc                                 if (errno != ENOENT || mkdir(path, 0777) < 0) {
    507   1.7       jtc 					err(1, "%s", path);
    508   1.7       jtc 					/* NOTREACHED */
    509   1.2       jtc                                 }
    510   1.2       jtc                         }
    511   1.2       jtc                         if (!(*p = ch))
    512   1.4       cgd 				break;
    513   1.2       jtc                 }
    514   1.2       jtc 
    515   1.8   ghudson 	if (((gid != -1 || uid != -1) && chown(path, uid, gid)) ||
    516   1.2       jtc             chmod(path, mode)) {
    517   1.5       jtc                 warn("%s", path);
    518   1.2       jtc         }
    519   1.2       jtc }
    520   1.2       jtc 
    521   1.2       jtc /*
    522   1.1       cgd  * usage --
    523   1.1       cgd  *	print a usage message and die
    524   1.1       cgd  */
    525   1.5       jtc void
    526   1.1       cgd usage()
    527   1.1       cgd {
    528   1.2       jtc 	(void)fprintf(stderr, "\
    529   1.5       jtc usage: install [-cs] [-f flags] [-g group] [-m mode] [-o owner] file1 file2\n\
    530   1.5       jtc        install [-cs] [-f flags] [-g group] [-m mode] [-o owner] file1 ... fileN directory\n\
    531   1.2       jtc        install  -d   [-g group] [-m mode] [-o owner] directory ...\n");
    532   1.6       jtc 	exit(1);
    533   1.1       cgd }
    534