Home | History | Annotate | Line # | Download | only in xinstall
xinstall.c revision 1.32
      1  1.32   hubertf /*	$NetBSD: xinstall.c,v 1.32 1999/01/26 01:34:25 hubertf 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.32   hubertf __RCSID("$NetBSD: xinstall.c,v 1.32 1999/01/26 01:34:25 hubertf 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.29       mrg #include "stat_flags.h"
     69   1.1       cgd 
     70  1.28  wsanchez #define STRIP_ARGS_MAX 32
     71  1.31   hubertf #define BACKUP_SUFFIX ".old"
     72  1.28  wsanchez 
     73   1.5       jtc struct passwd *pp;
     74   1.5       jtc struct group *gp;
     75  1.31   hubertf int dobackup=0, docopy=0, dodir=0, dostrip=0, dolink=0, dopreserve=0;
     76   1.5       jtc int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
     77   1.8   ghudson char pathbuf[MAXPATHLEN];
     78   1.8   ghudson uid_t uid;
     79   1.8   ghudson gid_t gid;
     80  1.28  wsanchez char *stripArgs=NULL;
     81  1.31   hubertf char *suffix=BACKUP_SUFFIX;
     82   1.5       jtc 
     83  1.16  christos #define LN_ABSOLUTE	0x01
     84  1.16  christos #define LN_RELATIVE	0x02
     85  1.16  christos #define LN_HARD		0x04
     86  1.16  christos #define LN_SYMBOLIC	0x08
     87  1.16  christos #define LN_MIXED	0x10
     88  1.16  christos 
     89   1.5       jtc #define	DIRECTORY	0x01		/* Tell install it's a directory. */
     90   1.5       jtc #define	SETFLAGS	0x02		/* Tell install to set flags. */
     91   1.5       jtc 
     92   1.5       jtc void	copy __P((int, char *, int, char *, off_t));
     93  1.16  christos void	makelink __P((char *, char *));
     94   1.5       jtc void	install __P((char *, char *, u_long, u_int));
     95   1.7       jtc void	install_dir __P((char *));
     96   1.5       jtc void	strip __P((char *));
     97  1.32   hubertf void	backup __P((const char *));
     98   1.5       jtc void	usage __P((void));
     99  1.20       mrg int	main __P((int, char *[]));
    100   1.1       cgd 
    101   1.5       jtc int
    102   1.1       cgd main(argc, argv)
    103   1.1       cgd 	int argc;
    104   1.5       jtc 	char *argv[];
    105   1.1       cgd {
    106   1.1       cgd 	struct stat from_sb, to_sb;
    107   1.5       jtc 	mode_t *set;
    108   1.5       jtc 	u_long fset;
    109   1.5       jtc 	u_int iflags;
    110   1.1       cgd 	int ch, no_target;
    111  1.16  christos 	char *p;
    112  1.16  christos 	char *flags = NULL, *to_name, *group = NULL, *owner = NULL;
    113   1.1       cgd 
    114   1.5       jtc 	iflags = 0;
    115  1.31   hubertf 	while ((ch = getopt(argc, argv, "cbB:df:g:l:m:o:psS:")) != -1)
    116   1.1       cgd 		switch((char)ch) {
    117  1.31   hubertf 		case 'B':
    118  1.31   hubertf 			suffix = optarg;
    119  1.31   hubertf 			/* fall through; -B implies -b */
    120  1.31   hubertf 		case 'b':
    121  1.31   hubertf 			dobackup = 1;
    122  1.31   hubertf 			break;
    123   1.1       cgd 		case 'c':
    124   1.1       cgd 			docopy = 1;
    125   1.1       cgd 			break;
    126  1.26  christos 		case 'd':
    127  1.26  christos 			dodir = 1;
    128  1.26  christos 			break;
    129   1.5       jtc 		case 'f':
    130   1.5       jtc 			flags = optarg;
    131   1.5       jtc 			if (string_to_flags(&flags, &fset, NULL))
    132   1.5       jtc 				errx(1, "%s: invalid flag", flags);
    133   1.5       jtc 			iflags |= SETFLAGS;
    134   1.5       jtc 			break;
    135   1.1       cgd 		case 'g':
    136   1.1       cgd 			group = optarg;
    137   1.1       cgd 			break;
    138  1.16  christos 		case 'l':
    139  1.16  christos 			for (p = optarg; *p; p++)
    140  1.16  christos 				switch (*p) {
    141  1.16  christos 				case 's':
    142  1.16  christos 					dolink &= ~(LN_HARD|LN_MIXED);
    143  1.16  christos 					dolink |= LN_SYMBOLIC;
    144  1.16  christos 					break;
    145  1.16  christos 				case 'h':
    146  1.16  christos 					dolink &= ~(LN_SYMBOLIC|LN_MIXED);
    147  1.16  christos 					dolink |= LN_HARD;
    148  1.16  christos 					break;
    149  1.16  christos 				case 'm':
    150  1.16  christos 					dolink &= ~(LN_SYMBOLIC|LN_HARD);
    151  1.16  christos 					dolink |= LN_MIXED;
    152  1.16  christos 					break;
    153  1.16  christos 				case 'a':
    154  1.16  christos 					dolink &= ~LN_RELATIVE;
    155  1.16  christos 					dolink |= LN_ABSOLUTE;
    156  1.16  christos 					break;
    157  1.16  christos 				case 'r':
    158  1.16  christos 					dolink &= ~LN_ABSOLUTE;
    159  1.16  christos 					dolink |= LN_RELATIVE;
    160  1.16  christos 					break;
    161  1.16  christos 				default:
    162  1.16  christos 					errx(1, "%c: invalid link type", *p);
    163  1.16  christos 					break;
    164  1.16  christos 				}
    165  1.16  christos 			break;
    166  1.26  christos 		case 'm':
    167  1.26  christos 			if (!(set = setmode(optarg)))
    168  1.26  christos 				errx(1, "%s: invalid file mode", optarg);
    169  1.26  christos 			mode = getmode(set, 0);
    170  1.26  christos 			break;
    171  1.26  christos 		case 'o':
    172  1.26  christos 			owner = optarg;
    173  1.26  christos 			break;
    174  1.26  christos 		case 'p':
    175  1.26  christos 			dopreserve = 1;
    176  1.26  christos 			break;
    177  1.28  wsanchez 		case 'S':
    178  1.28  wsanchez 			stripArgs = (char*)malloc(sizeof(char)*(strlen(optarg)+1));
    179  1.28  wsanchez 			strcpy(stripArgs,optarg);
    180  1.28  wsanchez 			/* fall through; -S implies -s */
    181  1.26  christos 		case 's':
    182  1.26  christos 			dostrip = 1;
    183  1.26  christos 			break;
    184   1.1       cgd 		case '?':
    185   1.1       cgd 		default:
    186   1.1       cgd 			usage();
    187   1.1       cgd 		}
    188   1.1       cgd 	argc -= optind;
    189   1.1       cgd 	argv += optind;
    190   1.2       jtc 
    191  1.23        tv 	/* strip and link options make no sense when creating directories */
    192  1.23        tv 	if ((dostrip || dolink) && dodir)
    193  1.16  christos 		usage();
    194  1.16  christos 
    195  1.16  christos 	/* strip and flags make no sense with links */
    196  1.16  christos 	if ((dostrip || flags) && dolink)
    197   1.2       jtc 		usage();
    198   1.2       jtc 
    199   1.2       jtc 	/* must have at least two arguments, except when creating directories */
    200   1.2       jtc 	if (argc < 2 && !dodir)
    201   1.1       cgd 		usage();
    202   1.1       cgd 
    203   1.1       cgd 	/* get group and owner id's */
    204  1.30  christos 	if (group && !(gp = getgrnam(group)) && !isdigit((unsigned char)*group))
    205   1.5       jtc 		errx(1, "unknown group %s", group);
    206   1.8   ghudson 	gid = (group) ? ((gp) ? gp->gr_gid : atoi(group)) : -1;
    207  1.30  christos 	if (owner && !(pp = getpwnam(owner)) && !isdigit((unsigned char)*owner))
    208   1.5       jtc 		errx(1, "unknown user %s", owner);
    209   1.8   ghudson 	uid = (owner) ? ((pp) ? pp->pw_uid : atoi(owner)) : -1;
    210   1.1       cgd 
    211   1.2       jtc 	if (dodir) {
    212   1.2       jtc 		for (; *argv != NULL; ++argv)
    213   1.5       jtc 			install_dir(*argv);
    214   1.2       jtc 		exit (0);
    215   1.5       jtc 	}
    216   1.2       jtc 
    217   1.1       cgd 	no_target = stat(to_name = argv[argc - 1], &to_sb);
    218   1.2       jtc 	if (!no_target && S_ISDIR(to_sb.st_mode)) {
    219   1.1       cgd 		for (; *argv != to_name; ++argv)
    220   1.5       jtc 			install(*argv, to_name, fset, iflags | DIRECTORY);
    221   1.1       cgd 		exit(0);
    222   1.1       cgd 	}
    223   1.1       cgd 
    224   1.1       cgd 	/* can't do file1 file2 directory/file */
    225   1.1       cgd 	if (argc != 2)
    226   1.1       cgd 		usage();
    227   1.1       cgd 
    228   1.1       cgd 	if (!no_target) {
    229   1.5       jtc 		if (stat(*argv, &from_sb))
    230   1.5       jtc 			err(1, "%s", *argv);
    231   1.5       jtc 		if (!S_ISREG(to_sb.st_mode))
    232   1.5       jtc 			errx(1, "%s: %s", to_name, strerror(EFTYPE));
    233  1.16  christos 		if (!dolink && to_sb.st_dev == from_sb.st_dev &&
    234   1.5       jtc 		    to_sb.st_ino == from_sb.st_ino)
    235   1.5       jtc 			errx(1, "%s and %s are the same file", *argv, to_name);
    236   1.5       jtc 		/*
    237   1.5       jtc 		 * Unlink now... avoid ETXTBSY errors later.  Try and turn
    238   1.5       jtc 		 * off the append/immutable bits -- if we fail, go ahead,
    239   1.5       jtc 		 * it might work.
    240   1.5       jtc 		 */
    241   1.5       jtc #define	NOCHANGEBITS	(UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
    242   1.5       jtc 		if (to_sb.st_flags & NOCHANGEBITS)
    243   1.5       jtc 			(void)chflags(to_name,
    244   1.5       jtc 			    to_sb.st_flags & ~(NOCHANGEBITS));
    245  1.32   hubertf 		if (dobackup)
    246  1.32   hubertf 			backup(to_name);
    247  1.32   hubertf 		else
    248  1.31   hubertf 			(void)unlink(to_name);
    249   1.1       cgd 	}
    250   1.5       jtc 	install(*argv, to_name, fset, iflags);
    251   1.1       cgd 	exit(0);
    252   1.1       cgd }
    253   1.1       cgd 
    254   1.1       cgd /*
    255  1.16  christos  * makelink --
    256  1.16  christos  *	make a link from source to destination
    257  1.16  christos  */
    258  1.16  christos void
    259  1.16  christos makelink(from_name, to_name)
    260  1.16  christos 	char *from_name;
    261  1.16  christos 	char *to_name;
    262  1.16  christos {
    263  1.16  christos 	char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
    264  1.16  christos 
    265  1.16  christos 	/* Try hard links first */
    266  1.16  christos 	if (dolink & (LN_HARD|LN_MIXED)) {
    267  1.16  christos 		if (link(from_name, to_name) == -1) {
    268  1.16  christos 			if ((dolink & LN_HARD) || errno != EXDEV)
    269  1.16  christos 				err(1, "link %s -> %s", from_name, to_name);
    270  1.16  christos 		}
    271  1.16  christos 		else
    272  1.16  christos 			return;
    273  1.16  christos 	}
    274  1.16  christos 
    275  1.16  christos 	/* Symbolic links */
    276  1.16  christos 	if (dolink & LN_ABSOLUTE) {
    277  1.16  christos 		/* Convert source path to absolute */
    278  1.16  christos 		if (realpath(from_name, src) == NULL)
    279  1.16  christos 			err(1, "%s", src);
    280  1.16  christos 		if (symlink(src, to_name) == -1)
    281  1.16  christos 			err(1, "symlink %s -> %s", src, to_name);
    282  1.16  christos 		return;
    283  1.16  christos 	}
    284  1.16  christos 
    285  1.16  christos 	if (dolink & LN_RELATIVE) {
    286  1.16  christos 		char *s, *d;
    287  1.16  christos 
    288  1.16  christos 		/* Resolve pathnames */
    289  1.16  christos 		if (realpath(from_name, src) == NULL)
    290  1.16  christos 			err(1, "%s", src);
    291  1.16  christos 		if (realpath(to_name, dst) == NULL)
    292  1.16  christos 			err(1, "%s", dst);
    293  1.16  christos 
    294  1.16  christos 		/* trim common path components */
    295  1.16  christos 		for (s = src, d = dst; *s == *d; s++, d++)
    296  1.16  christos 			continue;
    297  1.16  christos 		while (*s != '/')
    298  1.16  christos 			s--, d--;
    299  1.16  christos 
    300  1.16  christos 		/* count the number of directories we need to backtrack */
    301  1.16  christos 		for (++d, lnk[0] = '\0'; *d; d++)
    302  1.16  christos 			if (*d == '/')
    303  1.16  christos 				(void) strcat(lnk, "../");
    304  1.16  christos 
    305  1.16  christos 		(void) strcat(lnk, ++s);
    306  1.16  christos 
    307  1.16  christos 		if (symlink(lnk, dst) == -1)
    308  1.16  christos 			err(1, "symlink %s -> %s", lnk, dst);
    309  1.16  christos 		return;
    310  1.16  christos 	}
    311  1.16  christos 
    312  1.16  christos 	/*
    313  1.16  christos 	 * If absolute or relative was not specified,
    314  1.16  christos 	 * try the names the user provided
    315  1.16  christos 	 */
    316  1.16  christos 	if (symlink(from_name, to_name) == -1)
    317  1.17  christos 		err(1, "symlink %s -> %s", from_name, to_name);
    318  1.16  christos 
    319  1.16  christos }
    320  1.16  christos 
    321  1.16  christos /*
    322   1.1       cgd  * install --
    323   1.1       cgd  *	build a path name and install the file
    324   1.1       cgd  */
    325   1.5       jtc void
    326   1.5       jtc install(from_name, to_name, fset, flags)
    327   1.1       cgd 	char *from_name, *to_name;
    328   1.5       jtc 	u_long fset;
    329   1.5       jtc 	u_int flags;
    330   1.1       cgd {
    331   1.5       jtc 	struct stat from_sb, to_sb;
    332   1.5       jtc 	int devnull, from_fd, to_fd, serrno;
    333   1.5       jtc 	char *p;
    334   1.5       jtc 
    335   1.5       jtc 	/* If try to install NULL file to a directory, fails. */
    336   1.5       jtc 	if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
    337   1.5       jtc 		if (stat(from_name, &from_sb))
    338   1.5       jtc 			err(1, "%s", from_name);
    339   1.5       jtc 		if (!S_ISREG(from_sb.st_mode))
    340   1.5       jtc 			errx(1, "%s: %s", from_name, strerror(EFTYPE));
    341   1.5       jtc 		/* Build the target path. */
    342   1.5       jtc 		if (flags & DIRECTORY) {
    343   1.5       jtc 			(void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
    344   1.5       jtc 			    to_name,
    345  1.21     lukem 			    (p = strrchr(from_name, '/')) ? ++p : from_name);
    346   1.1       cgd 			to_name = pathbuf;
    347   1.1       cgd 		}
    348   1.1       cgd 		devnull = 0;
    349   1.5       jtc 	} else {
    350   1.5       jtc 		from_sb.st_flags = 0;	/* XXX */
    351   1.1       cgd 		devnull = 1;
    352   1.5       jtc 	}
    353   1.1       cgd 
    354   1.5       jtc 	/*
    355   1.5       jtc 	 * Unlink now... avoid ETXTBSY errors later.  Try and turn
    356   1.5       jtc 	 * off the append/immutable bits -- if we fail, go ahead,
    357   1.5       jtc 	 * it might work.
    358   1.5       jtc 	 */
    359   1.5       jtc 	if (stat(to_name, &to_sb) == 0 &&
    360   1.5       jtc 	    to_sb.st_flags & (NOCHANGEBITS))
    361   1.5       jtc 		(void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
    362  1.32   hubertf 	if (dobackup)
    363  1.32   hubertf 		backup(to_name);
    364  1.32   hubertf 	else
    365  1.31   hubertf 		(void)unlink(to_name);
    366  1.16  christos 
    367  1.16  christos 	if (dolink) {
    368  1.16  christos 		makelink(from_name, to_name);
    369  1.16  christos 		return;
    370  1.16  christos 	}
    371   1.1       cgd 
    372   1.5       jtc 	/* Create target. */
    373   1.5       jtc 	if ((to_fd = open(to_name,
    374   1.5       jtc 	    O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
    375   1.5       jtc 		err(1, "%s", to_name);
    376   1.1       cgd 	if (!devnull) {
    377   1.1       cgd 		if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
    378   1.1       cgd 			(void)unlink(to_name);
    379   1.5       jtc 			err(1, "%s", from_name);
    380   1.1       cgd 		}
    381   1.5       jtc 		copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
    382   1.1       cgd 		(void)close(from_fd);
    383   1.1       cgd 	}
    384   1.9  jonathan 
    385   1.9  jonathan 	if (dostrip) {
    386   1.1       cgd 		strip(to_name);
    387   1.9  jonathan 
    388   1.9  jonathan 		/*
    389   1.9  jonathan 		 * Re-open our fd on the target, in case we used a strip
    390   1.9  jonathan 		 *  that does not work in-place -- like gnu binutils strip.
    391   1.9  jonathan 		 */
    392   1.9  jonathan 		close(to_fd);
    393   1.9  jonathan 		if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
    394   1.9  jonathan 		  err(1, "stripping %s", to_name);
    395   1.9  jonathan 	}
    396   1.9  jonathan 
    397   1.1       cgd 	/*
    398   1.5       jtc 	 * Set owner, group, mode for target; do the chown first,
    399   1.1       cgd 	 * chown may lose the setuid bits.
    400   1.1       cgd 	 */
    401   1.8   ghudson 	if ((gid != -1 || uid != -1) && fchown(to_fd, uid, gid)) {
    402   1.5       jtc 		serrno = errno;
    403   1.5       jtc 		(void)unlink(to_name);
    404   1.5       jtc 		errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
    405   1.1       cgd 	}
    406   1.5       jtc 	if (fchmod(to_fd, mode)) {
    407   1.5       jtc 		serrno = errno;
    408   1.5       jtc 		(void)unlink(to_name);
    409   1.5       jtc 		errx(1, "%s: chmod: %s", to_name, strerror(serrno));
    410   1.5       jtc 	}
    411   1.5       jtc 
    412   1.5       jtc 	/*
    413   1.5       jtc 	 * If provided a set of flags, set them, otherwise, preserve the
    414   1.5       jtc 	 * flags, except for the dump flag.
    415   1.5       jtc 	 */
    416   1.5       jtc 	if (fchflags(to_fd,
    417   1.5       jtc 	    flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
    418  1.11      fvdl 		if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
    419  1.13      fvdl 			warn("%s: chflags", to_name);
    420  1.26  christos 	}
    421  1.26  christos 
    422  1.26  christos 	/*
    423  1.26  christos 	 * Preserve the date of the source file.
    424  1.26  christos 	 */
    425  1.26  christos 	if (dopreserve) {
    426  1.26  christos 		struct timeval tv[2];
    427  1.26  christos 
    428  1.26  christos 		TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
    429  1.26  christos 		TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
    430  1.26  christos 		if (futimes(to_fd, tv) == -1)
    431  1.26  christos 			warn("%s: futimes", to_name);
    432   1.5       jtc 	}
    433   1.5       jtc 
    434   1.1       cgd 	(void)close(to_fd);
    435   1.5       jtc 	if (!docopy && !devnull && unlink(from_name))
    436   1.5       jtc 		err(1, "%s", from_name);
    437   1.1       cgd }
    438   1.1       cgd 
    439   1.1       cgd /*
    440   1.1       cgd  * copy --
    441   1.1       cgd  *	copy from one file to another
    442   1.1       cgd  */
    443   1.5       jtc void
    444   1.5       jtc copy(from_fd, from_name, to_fd, to_name, size)
    445  1.20       mrg 	int from_fd, to_fd;
    446   1.1       cgd 	char *from_name, *to_name;
    447   1.5       jtc 	off_t size;
    448   1.1       cgd {
    449  1.20       mrg 	int nr, nw;
    450   1.5       jtc 	int serrno;
    451  1.14   thorpej 	char *p;
    452  1.14   thorpej 	char buf[MAXBSIZE];
    453   1.1       cgd 
    454   1.5       jtc 	/*
    455  1.28  wsanchez 	 * There's no reason to do anything other than close the file
    456  1.28  wsanchez 	 * now if it's empty, so let's not bother.
    457   1.5       jtc 	 */
    458  1.28  wsanchez 	if (size > 0) {
    459  1.28  wsanchez 		/*
    460  1.28  wsanchez 		 * Mmap and write if less than 8M (the limit is so we don't totally
    461  1.28  wsanchez 		 * trash memory on big files).  This is really a minor hack, but it
    462  1.28  wsanchez 		 * wins some CPU back.
    463  1.28  wsanchez 		 */
    464  1.28  wsanchez 		if (size <= 8 * 1048576) {
    465  1.28  wsanchez 			if ((p = mmap(NULL, (size_t)size, PROT_READ,
    466  1.28  wsanchez 			    MAP_FILE|MAP_SHARED, from_fd, (off_t)0)) == (char *)-1)
    467  1.28  wsanchez 				err(1, "%s", from_name);
    468  1.28  wsanchez 			if (write(to_fd, p, size) != size)
    469  1.28  wsanchez 				err(1, "%s", to_name);
    470  1.28  wsanchez 		} else {
    471  1.28  wsanchez 			while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
    472  1.28  wsanchez 				if ((nw = write(to_fd, buf, nr)) != nr) {
    473  1.28  wsanchez 					serrno = errno;
    474  1.28  wsanchez 					(void)unlink(to_name);
    475  1.28  wsanchez 					errx(1, "%s: %s",
    476  1.28  wsanchez 					    to_name, strerror(nw > 0 ? EIO : serrno));
    477  1.28  wsanchez 				}
    478  1.28  wsanchez 			if (nr != 0) {
    479   1.5       jtc 				serrno = errno;
    480   1.5       jtc 				(void)unlink(to_name);
    481  1.28  wsanchez 				errx(1, "%s: %s", from_name, strerror(serrno));
    482   1.5       jtc 			}
    483   1.1       cgd 		}
    484   1.1       cgd 	}
    485   1.1       cgd }
    486   1.1       cgd 
    487   1.1       cgd /*
    488   1.1       cgd  * strip --
    489   1.1       cgd  *	use strip(1) to strip the target file
    490   1.1       cgd  */
    491   1.5       jtc void
    492   1.1       cgd strip(to_name)
    493   1.1       cgd 	char *to_name;
    494   1.1       cgd {
    495   1.5       jtc 	int serrno, status;
    496  1.18   thorpej 	char *stripprog;
    497   1.1       cgd 
    498   1.1       cgd 	switch (vfork()) {
    499   1.1       cgd 	case -1:
    500   1.5       jtc 		serrno = errno;
    501   1.5       jtc 		(void)unlink(to_name);
    502  1.22   thorpej 		errx(1, "vfork: %s", strerror(serrno));
    503   1.1       cgd 	case 0:
    504  1.18   thorpej 		stripprog = getenv("STRIP");
    505  1.18   thorpej 		if (stripprog == NULL)
    506  1.18   thorpej 			stripprog = _PATH_STRIP;
    507  1.28  wsanchez 
    508  1.28  wsanchez 		if (stripArgs) {
    509  1.28  wsanchez 			/* build up a command line and let /bin/sh parse the arguments */
    510  1.28  wsanchez 			char* cmd = (char*)malloc(sizeof(char)*
    511  1.28  wsanchez 						  (3+strlen(stripprog)+
    512  1.28  wsanchez 						     strlen(stripArgs)+
    513  1.28  wsanchez 						     strlen(to_name)));
    514  1.28  wsanchez 
    515  1.28  wsanchez 			sprintf(cmd, "%s %s %s", stripprog, stripArgs, to_name);
    516  1.28  wsanchez 
    517  1.28  wsanchez 			execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
    518  1.28  wsanchez 		} else
    519  1.28  wsanchez 			execl(stripprog, "strip", to_name, NULL);
    520  1.28  wsanchez 
    521  1.22   thorpej 		warn("%s", stripprog);
    522  1.22   thorpej 		_exit(1);
    523   1.1       cgd 	default:
    524   1.1       cgd 		if (wait(&status) == -1 || status)
    525   1.5       jtc 			(void)unlink(to_name);
    526   1.1       cgd 	}
    527  1.32   hubertf }
    528  1.32   hubertf 
    529  1.32   hubertf /*
    530  1.32   hubertf  * backup file "to_name" to to_name.suffix
    531  1.32   hubertf  * if suffix contains a "%", it's taken as a printf(3) pattern
    532  1.32   hubertf  * used for a numbered backup.
    533  1.32   hubertf  */
    534  1.32   hubertf void
    535  1.32   hubertf backup(to_name)
    536  1.32   hubertf 	const char *to_name;
    537  1.32   hubertf {
    538  1.32   hubertf 	char backup[FILENAME_MAX];
    539  1.32   hubertf 
    540  1.32   hubertf 	if (strchr(suffix, '%')) {
    541  1.32   hubertf 		/* Do numbered backup */
    542  1.32   hubertf 		int cnt;
    543  1.32   hubertf 		char suffix_expanded[FILENAME_MAX];
    544  1.32   hubertf 
    545  1.32   hubertf 		cnt=0;
    546  1.32   hubertf 		do {
    547  1.32   hubertf 			(void)snprintf(suffix_expanded, FILENAME_MAX, suffix, cnt);
    548  1.32   hubertf 			(void)snprintf(backup, FILENAME_MAX, "%s%s",
    549  1.32   hubertf 				       to_name, suffix_expanded);
    550  1.32   hubertf 			cnt++;
    551  1.32   hubertf 		} while (access(backup, F_OK)==0);
    552  1.32   hubertf 	} else {
    553  1.32   hubertf 		/* Do simple backup */
    554  1.32   hubertf 		(void)snprintf(backup, FILENAME_MAX, "%s%s", to_name, suffix);
    555  1.32   hubertf 	}
    556  1.32   hubertf 
    557  1.32   hubertf 	(void)rename(to_name, backup);
    558   1.1       cgd }
    559   1.1       cgd 
    560   1.1       cgd /*
    561   1.5       jtc  * install_dir --
    562   1.2       jtc  *	build directory heirarchy
    563   1.2       jtc  */
    564   1.7       jtc void
    565   1.5       jtc install_dir(path)
    566   1.2       jtc         char *path;
    567   1.2       jtc {
    568  1.20       mrg         char *p;
    569   1.2       jtc         struct stat sb;
    570   1.4       cgd         int ch;
    571   1.2       jtc 
    572   1.4       cgd         for (p = path;; ++p)
    573   1.4       cgd                 if (!*p || (p != path && *p  == '/')) {
    574   1.2       jtc                         ch = *p;
    575   1.2       jtc                         *p = '\0';
    576   1.2       jtc                         if (stat(path, &sb)) {
    577   1.2       jtc                                 if (errno != ENOENT || mkdir(path, 0777) < 0) {
    578   1.7       jtc 					err(1, "%s", path);
    579   1.7       jtc 					/* NOTREACHED */
    580   1.2       jtc                                 }
    581   1.2       jtc                         }
    582   1.2       jtc                         if (!(*p = ch))
    583   1.4       cgd 				break;
    584   1.2       jtc                 }
    585   1.2       jtc 
    586   1.8   ghudson 	if (((gid != -1 || uid != -1) && chown(path, uid, gid)) ||
    587   1.2       jtc             chmod(path, mode)) {
    588   1.5       jtc                 warn("%s", path);
    589   1.2       jtc         }
    590   1.2       jtc }
    591   1.2       jtc 
    592   1.2       jtc /*
    593   1.1       cgd  * usage --
    594   1.1       cgd  *	print a usage message and die
    595   1.1       cgd  */
    596   1.5       jtc void
    597   1.1       cgd usage()
    598   1.1       cgd {
    599   1.2       jtc 	(void)fprintf(stderr, "\
    600  1.31   hubertf usage: install [-bcps] [-B suffix] [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 file2\n\
    601  1.31   hubertf        install [-bcps] [-B suffix] [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 ... fileN directory\n\
    602  1.28  wsanchez        install -pd [-m mode] [-o owner] [-g group] directory ...\n");
    603   1.6       jtc 	exit(1);
    604   1.1       cgd }
    605