Home | History | Annotate | Line # | Download | only in xinstall
xinstall.c revision 1.68
      1  1.68     lukem /*	$NetBSD: xinstall.c,v 1.68 2002/02/28 00:22:51 lukem 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.65        tv #if HAVE_CONFIG_H
     37  1.65        tv #include "config.h"
     38  1.65        tv #else
     39  1.65        tv #define HAVE_FUTIMES 1
     40  1.65        tv #define HAVE_STRUCT_STAT_ST_FLAGS 1
     41  1.65        tv #endif
     42  1.65        tv 
     43  1.20       mrg #include <sys/cdefs.h>
     44  1.67        tv #if defined(__COPYRIGHT) && !defined(lint)
     45  1.20       mrg __COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
     46  1.20       mrg 	The Regents of the University of California.  All rights reserved.\n");
     47   1.1       cgd #endif /* not lint */
     48   1.1       cgd 
     49  1.67        tv #if defined(__RCSID) && !defined(lint)
     50   1.5       jtc #if 0
     51   1.5       jtc static char sccsid[] = "@(#)xinstall.c	8.1 (Berkeley) 7/21/93";
     52  1.20       mrg #else
     53  1.68     lukem __RCSID("$NetBSD: xinstall.c,v 1.68 2002/02/28 00:22:51 lukem Exp $");
     54   1.5       jtc #endif
     55   1.1       cgd #endif /* not lint */
     56   1.1       cgd 
     57   1.1       cgd #include <sys/param.h>
     58   1.5       jtc #include <sys/mman.h>
     59   1.1       cgd #include <sys/stat.h>
     60  1.61    simonb #include <sys/wait.h>
     61   1.5       jtc 
     62   1.5       jtc #include <ctype.h>
     63  1.67        tv #include <err.h>
     64   1.5       jtc #include <errno.h>
     65   1.5       jtc #include <fcntl.h>
     66   1.1       cgd #include <grp.h>
     67  1.67        tv #include <libgen.h>
     68   1.5       jtc #include <paths.h>
     69   1.1       cgd #include <pwd.h>
     70   1.1       cgd #include <stdio.h>
     71   1.5       jtc #include <stdlib.h>
     72   1.5       jtc #include <string.h>
     73   1.5       jtc #include <unistd.h>
     74  1.50     lukem #include <vis.h>
     75   1.5       jtc 
     76   1.1       cgd #include "pathnames.h"
     77  1.29       mrg #include "stat_flags.h"
     78   1.1       cgd 
     79  1.28  wsanchez #define STRIP_ARGS_MAX 32
     80  1.31   hubertf #define BACKUP_SUFFIX ".old"
     81  1.28  wsanchez 
     82  1.50     lukem int	dobackup, docopy, dodir, dostrip, dolink, dopreserve, dorename,
     83  1.50     lukem 	    dounpriv;
     84  1.50     lukem int	numberedbackup;
     85  1.50     lukem int	mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
     86  1.50     lukem char	pathbuf[MAXPATHLEN];
     87  1.65        tv id_t	uid, gid;
     88  1.54     lukem char	*group, *owner, *fflags, *tags;
     89  1.50     lukem FILE	*metafp;
     90  1.50     lukem char	*metafile;
     91  1.50     lukem u_long	fileflags;
     92  1.50     lukem char	*stripArgs;
     93  1.50     lukem char	*suffix = BACKUP_SUFFIX;
     94   1.5       jtc 
     95  1.16  christos #define LN_ABSOLUTE	0x01
     96  1.16  christos #define LN_RELATIVE	0x02
     97  1.16  christos #define LN_HARD		0x04
     98  1.16  christos #define LN_SYMBOLIC	0x08
     99  1.16  christos #define LN_MIXED	0x10
    100  1.16  christos 
    101   1.5       jtc #define	DIRECTORY	0x01		/* Tell install it's a directory. */
    102   1.5       jtc #define	SETFLAGS	0x02		/* Tell install to set flags. */
    103  1.50     lukem #define	HASUID		0x04		/* Tell install the uid was given */
    104  1.50     lukem #define	HASGID		0x08		/* Tell install the gid was given */
    105   1.5       jtc 
    106  1.50     lukem void	backup(const char *);
    107  1.48    simonb void	copy(int, char *, int, char *, off_t);
    108  1.61    simonb int	do_link(char *, char *);
    109  1.61    simonb void	do_symlink(char *, char *);
    110  1.50     lukem void	install(char *, char *, u_int);
    111  1.50     lukem void	install_dir(char *, u_int);
    112  1.50     lukem int	main(int, char *[]);
    113  1.48    simonb void	makelink(char *, char *);
    114  1.61    simonb void	metadata_log(const char *, const char *, struct timeval *, const char *);
    115  1.50     lukem int	parseid(char *, id_t *);
    116  1.48    simonb void	strip(char *);
    117  1.48    simonb void	usage(void);
    118  1.61    simonb char   *xbasename(char *);
    119  1.61    simonb char   *xdirname(char *);
    120   1.1       cgd 
    121   1.5       jtc int
    122  1.48    simonb main(int argc, char *argv[])
    123   1.1       cgd {
    124  1.53     lukem 	struct stat	from_sb, to_sb;
    125  1.53     lukem 	void		*set;
    126  1.53     lukem 	u_int		iflags;
    127  1.53     lukem 	int		ch, no_target;
    128  1.53     lukem 	char		*p, *to_name;
    129  1.43       cgd 
    130  1.43       cgd 	setprogname(argv[0]);
    131   1.1       cgd 
    132   1.5       jtc 	iflags = 0;
    133  1.54     lukem 	while ((ch = getopt(argc, argv, "cbB:df:g:l:m:M:o:prsS:T:U")) != -1)
    134   1.1       cgd 		switch((char)ch) {
    135  1.31   hubertf 		case 'B':
    136  1.31   hubertf 			suffix = optarg;
    137  1.35   hubertf 			numberedbackup = 0;
    138  1.35   hubertf 			{
    139  1.35   hubertf 				/* Check if given suffix really generates
    140  1.35   hubertf 				   different suffixes - catch e.g. ".%" */
    141  1.35   hubertf 				char suffix_expanded0[FILENAME_MAX],
    142  1.35   hubertf 				     suffix_expanded1[FILENAME_MAX];
    143  1.35   hubertf 				(void)snprintf(suffix_expanded0, FILENAME_MAX,
    144  1.35   hubertf 					       suffix, 0);
    145  1.35   hubertf 				(void)snprintf(suffix_expanded1, FILENAME_MAX,
    146  1.35   hubertf 					       suffix, 1);
    147  1.35   hubertf 				if (strcmp(suffix_expanded0, suffix_expanded1)
    148  1.35   hubertf 				    != 0)
    149  1.35   hubertf 					numberedbackup = 1;
    150  1.35   hubertf 			}
    151  1.31   hubertf 			/* fall through; -B implies -b */
    152  1.61    simonb 			/*FALLTHROUGH*/
    153  1.31   hubertf 		case 'b':
    154  1.31   hubertf 			dobackup = 1;
    155  1.31   hubertf 			break;
    156   1.1       cgd 		case 'c':
    157   1.1       cgd 			docopy = 1;
    158   1.1       cgd 			break;
    159  1.26  christos 		case 'd':
    160  1.26  christos 			dodir = 1;
    161  1.26  christos 			break;
    162  1.65        tv #if !HAVE_CONFIG_H
    163   1.5       jtc 		case 'f':
    164  1.52        tv 			fflags = optarg;
    165   1.5       jtc 			break;
    166  1.65        tv #endif
    167   1.1       cgd 		case 'g':
    168   1.1       cgd 			group = optarg;
    169   1.1       cgd 			break;
    170  1.16  christos 		case 'l':
    171  1.16  christos 			for (p = optarg; *p; p++)
    172  1.16  christos 				switch (*p) {
    173  1.16  christos 				case 's':
    174  1.16  christos 					dolink &= ~(LN_HARD|LN_MIXED);
    175  1.16  christos 					dolink |= LN_SYMBOLIC;
    176  1.16  christos 					break;
    177  1.16  christos 				case 'h':
    178  1.16  christos 					dolink &= ~(LN_SYMBOLIC|LN_MIXED);
    179  1.16  christos 					dolink |= LN_HARD;
    180  1.16  christos 					break;
    181  1.16  christos 				case 'm':
    182  1.16  christos 					dolink &= ~(LN_SYMBOLIC|LN_HARD);
    183  1.16  christos 					dolink |= LN_MIXED;
    184  1.16  christos 					break;
    185  1.16  christos 				case 'a':
    186  1.16  christos 					dolink &= ~LN_RELATIVE;
    187  1.16  christos 					dolink |= LN_ABSOLUTE;
    188  1.16  christos 					break;
    189  1.16  christos 				case 'r':
    190  1.16  christos 					dolink &= ~LN_ABSOLUTE;
    191  1.16  christos 					dolink |= LN_RELATIVE;
    192  1.16  christos 					break;
    193  1.16  christos 				default:
    194  1.16  christos 					errx(1, "%c: invalid link type", *p);
    195  1.61    simonb 					/* NOTREACHED */
    196  1.16  christos 				}
    197  1.16  christos 			break;
    198  1.26  christos 		case 'm':
    199  1.26  christos 			if (!(set = setmode(optarg)))
    200  1.26  christos 				errx(1, "%s: invalid file mode", optarg);
    201  1.26  christos 			mode = getmode(set, 0);
    202  1.42     enami 			free(set);
    203  1.26  christos 			break;
    204  1.50     lukem 		case 'M':
    205  1.50     lukem 			metafile = optarg;
    206  1.50     lukem 			break;
    207  1.26  christos 		case 'o':
    208  1.26  christos 			owner = optarg;
    209  1.26  christos 			break;
    210  1.26  christos 		case 'p':
    211  1.26  christos 			dopreserve = 1;
    212  1.26  christos 			break;
    213  1.33  christos 		case 'r':
    214  1.33  christos 			dorename = 1;
    215  1.33  christos 			break;
    216  1.28  wsanchez 		case 'S':
    217  1.49    simonb 			stripArgs = strdup(optarg);
    218  1.49    simonb 			if (stripArgs == NULL)
    219  1.49    simonb 				errx(1, "%s", strerror(ENOMEM));
    220  1.28  wsanchez 			/* fall through; -S implies -s */
    221  1.61    simonb 			/*FALLTHROUGH*/
    222  1.26  christos 		case 's':
    223  1.26  christos 			dostrip = 1;
    224  1.26  christos 			break;
    225  1.54     lukem 		case 'T':
    226  1.54     lukem 			tags = optarg;
    227  1.54     lukem 			break;
    228  1.38  sommerfe 		case 'U':
    229  1.50     lukem 			dounpriv = 1;
    230  1.38  sommerfe 			break;
    231   1.1       cgd 		case '?':
    232   1.1       cgd 		default:
    233   1.1       cgd 			usage();
    234   1.1       cgd 		}
    235   1.1       cgd 	argc -= optind;
    236   1.1       cgd 	argv += optind;
    237   1.2       jtc 
    238  1.23        tv 	/* strip and link options make no sense when creating directories */
    239  1.23        tv 	if ((dostrip || dolink) && dodir)
    240  1.16  christos 		usage();
    241  1.16  christos 
    242  1.16  christos 	/* strip and flags make no sense with links */
    243  1.52        tv 	if ((dostrip || fflags) && dolink)
    244   1.2       jtc 		usage();
    245   1.2       jtc 
    246   1.2       jtc 	/* must have at least two arguments, except when creating directories */
    247   1.2       jtc 	if (argc < 2 && !dodir)
    248   1.1       cgd 		usage();
    249   1.1       cgd 
    250   1.1       cgd 	/* get group and owner id's */
    251  1.52        tv 	if (group && !dounpriv) {
    252  1.50     lukem 		struct group *gp;
    253  1.50     lukem 
    254  1.50     lukem 		if ((gp = getgrnam(group)) != NULL)
    255  1.50     lukem 			gid = gp->gr_gid;
    256  1.50     lukem 		else if (! parseid(group, &gid))
    257  1.41       cgd 			errx(1, "unknown group %s", group);
    258  1.50     lukem 		iflags |= HASGID;
    259  1.50     lukem 	}
    260  1.52        tv 	if (owner && !dounpriv) {
    261  1.50     lukem 		struct passwd *pp;
    262  1.50     lukem 
    263  1.50     lukem 		if ((pp = getpwnam(owner)) != NULL)
    264  1.50     lukem 			uid = pp->pw_uid;
    265  1.50     lukem 		else if (! parseid(owner, &uid))
    266  1.41       cgd 			errx(1, "unknown user %s", owner);
    267  1.50     lukem 		iflags |= HASUID;
    268  1.50     lukem 	}
    269  1.50     lukem 
    270  1.65        tv #if !HAVE_CONFIG_H
    271  1.52        tv 	if (fflags && !dounpriv) {
    272  1.52        tv 		if (string_to_flags(&fflags, &fileflags, NULL))
    273  1.52        tv 			errx(1, "%s: invalid flag", fflags);
    274  1.52        tv 		iflags |= SETFLAGS;
    275  1.52        tv 	}
    276  1.65        tv #endif
    277  1.52        tv 
    278  1.50     lukem 	if (metafile) {
    279  1.50     lukem 		if ((metafp = fopen(metafile, "a")) == NULL)
    280  1.50     lukem 			warn("open %s", metafile);
    281  1.41       cgd 	}
    282   1.1       cgd 
    283   1.2       jtc 	if (dodir) {
    284   1.2       jtc 		for (; *argv != NULL; ++argv)
    285  1.50     lukem 			install_dir(*argv, iflags);
    286   1.2       jtc 		exit (0);
    287   1.5       jtc 	}
    288   1.2       jtc 
    289   1.1       cgd 	no_target = stat(to_name = argv[argc - 1], &to_sb);
    290   1.2       jtc 	if (!no_target && S_ISDIR(to_sb.st_mode)) {
    291   1.1       cgd 		for (; *argv != to_name; ++argv)
    292  1.50     lukem 			install(*argv, to_name, iflags | DIRECTORY);
    293   1.1       cgd 		exit(0);
    294   1.1       cgd 	}
    295   1.1       cgd 
    296   1.1       cgd 	/* can't do file1 file2 directory/file */
    297   1.1       cgd 	if (argc != 2)
    298   1.1       cgd 		usage();
    299   1.1       cgd 
    300   1.1       cgd 	if (!no_target) {
    301  1.61    simonb 		/* makelink() handles checks for links */
    302  1.61    simonb 		if (!dolink) {
    303  1.61    simonb 			if (stat(*argv, &from_sb))
    304  1.66        tv 				err(1, "%s: stat", *argv);
    305  1.61    simonb 			if (!S_ISREG(to_sb.st_mode))
    306  1.61    simonb 				errx(1, "%s: not a regular file", to_name);
    307  1.61    simonb 			if (to_sb.st_dev == from_sb.st_dev &&
    308  1.61    simonb 			    to_sb.st_ino == from_sb.st_ino)
    309  1.61    simonb 				errx(1, "%s and %s are the same file", *argv,
    310  1.61    simonb 				    to_name);
    311  1.61    simonb 		}
    312   1.5       jtc 		/*
    313   1.5       jtc 		 * Unlink now... avoid ETXTBSY errors later.  Try and turn
    314   1.5       jtc 		 * off the append/immutable bits -- if we fail, go ahead,
    315   1.5       jtc 		 * it might work.
    316   1.5       jtc 		 */
    317  1.65        tv #if !HAVE_CONFIG_H
    318   1.5       jtc #define	NOCHANGEBITS	(UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
    319   1.5       jtc 		if (to_sb.st_flags & NOCHANGEBITS)
    320   1.5       jtc 			(void)chflags(to_name,
    321   1.5       jtc 			    to_sb.st_flags & ~(NOCHANGEBITS));
    322  1.65        tv #endif
    323  1.32   hubertf 		if (dobackup)
    324  1.32   hubertf 			backup(to_name);
    325  1.34  christos 		else if (!dorename)
    326  1.31   hubertf 			(void)unlink(to_name);
    327   1.1       cgd 	}
    328  1.50     lukem 	install(*argv, to_name, iflags);
    329   1.1       cgd 	exit(0);
    330   1.1       cgd }
    331   1.1       cgd 
    332   1.1       cgd /*
    333  1.50     lukem  * parseid --
    334  1.50     lukem  *	parse uid or gid from arg into id, returning non-zero if successful
    335  1.50     lukem  */
    336  1.50     lukem int
    337  1.50     lukem parseid(char *name, id_t *id)
    338  1.50     lukem {
    339  1.50     lukem 	char	*ep;
    340  1.50     lukem 
    341  1.50     lukem 	errno = 0;
    342  1.50     lukem 	*id = (id_t)strtoul(name, &ep, 10);
    343  1.50     lukem 	if (errno || *ep != '\0')
    344  1.50     lukem 		return (0);
    345  1.50     lukem 	return (1);
    346  1.50     lukem }
    347  1.50     lukem 
    348  1.50     lukem /*
    349  1.61    simonb  * do_link --
    350  1.61    simonb  *	make a hard link, obeying dorename if set
    351  1.61    simonb  *	return -1 on failure
    352  1.61    simonb  */
    353  1.61    simonb int
    354  1.61    simonb do_link(char *from_name, char *to_name)
    355  1.61    simonb {
    356  1.61    simonb 	char tmpl[MAXPATHLEN];
    357  1.61    simonb 	int ret;
    358  1.61    simonb 
    359  1.61    simonb 	if (dorename) {
    360  1.61    simonb 		(void)snprintf(tmpl, sizeof(tmpl), "%s/inst.XXXXXX",
    361  1.61    simonb 		    xdirname(to_name));
    362  1.62     perry 		/* This usage is safe. The linker will bitch anyway. */
    363  1.61    simonb 		if (mktemp(tmpl) == NULL)
    364  1.66        tv 			err(1, "%s: mktemp", tmpl);
    365  1.61    simonb 		ret = link(from_name, tmpl);
    366  1.61    simonb 		if (ret == 0) {
    367  1.61    simonb 			ret = rename(tmpl, to_name);
    368  1.61    simonb 			if (ret < 0)
    369  1.61    simonb 				/* remove temporary link before exiting */
    370  1.61    simonb 				(void)unlink(tmpl);
    371  1.61    simonb 		}
    372  1.61    simonb 		return (ret);
    373  1.61    simonb 	} else
    374  1.61    simonb 		return (link(from_name, to_name));
    375  1.61    simonb }
    376  1.61    simonb 
    377  1.61    simonb /*
    378  1.61    simonb  * do_symlink --
    379  1.61    simonb  *	make a symbolic link, obeying dorename if set
    380  1.61    simonb  *	exit on failure
    381  1.61    simonb  */
    382  1.61    simonb void
    383  1.61    simonb do_symlink(char *from_name, char *to_name)
    384  1.61    simonb {
    385  1.61    simonb 	char tmpl[MAXPATHLEN];
    386  1.61    simonb 
    387  1.61    simonb 	if (dorename) {
    388  1.61    simonb 		(void)snprintf(tmpl, sizeof(tmpl), "%s/inst.XXXXXX",
    389  1.61    simonb 		    xdirname(to_name));
    390  1.62     perry 		/* This usage is safe. The linker will bitch anyway. */
    391  1.61    simonb 		if (mktemp(tmpl) == NULL)
    392  1.66        tv 			err(1, "%s: mktemp", tmpl);
    393  1.61    simonb 
    394  1.61    simonb 		if (symlink(from_name, tmpl) == -1)
    395  1.61    simonb 			err(1, "symlink %s -> %s", from_name, tmpl);
    396  1.61    simonb 		if (rename(tmpl, to_name) == -1) {
    397  1.61    simonb 			/* remove temporary link before exiting */
    398  1.61    simonb 			(void)unlink(tmpl);
    399  1.61    simonb 			err(1, "%s: rename", to_name);
    400  1.61    simonb 		}
    401  1.61    simonb 	} else {
    402  1.61    simonb 		if (symlink(from_name, to_name) == -1)
    403  1.61    simonb 			err(1, "symlink %s -> %s", from_name, to_name);
    404  1.61    simonb 	}
    405  1.61    simonb }
    406  1.61    simonb 
    407  1.61    simonb /*
    408  1.16  christos  * makelink --
    409  1.16  christos  *	make a link from source to destination
    410  1.16  christos  */
    411  1.16  christos void
    412  1.48    simonb makelink(char *from_name, char *to_name)
    413  1.16  christos {
    414  1.53     lukem 	char	src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
    415  1.68     lukem 	struct stat	to_sb;
    416  1.16  christos 
    417  1.16  christos 	/* Try hard links first */
    418  1.16  christos 	if (dolink & (LN_HARD|LN_MIXED)) {
    419  1.61    simonb 		if (do_link(from_name, to_name) == -1) {
    420  1.16  christos 			if ((dolink & LN_HARD) || errno != EXDEV)
    421  1.16  christos 				err(1, "link %s -> %s", from_name, to_name);
    422  1.68     lukem 		} else {
    423  1.68     lukem 			if (stat(to_name, &to_sb))
    424  1.68     lukem 				err(1, "%s: stat", to_name);
    425  1.68     lukem 			if (S_ISREG(to_sb.st_mode))
    426  1.68     lukem 				metadata_log(to_name, "file", NULL, NULL);
    427  1.68     lukem 					/* XXX: only metalog hardlinked files */
    428  1.16  christos 			return;
    429  1.57     lukem 		}
    430  1.16  christos 	}
    431  1.16  christos 
    432  1.16  christos 	/* Symbolic links */
    433  1.16  christos 	if (dolink & LN_ABSOLUTE) {
    434  1.16  christos 		/* Convert source path to absolute */
    435  1.16  christos 		if (realpath(from_name, src) == NULL)
    436  1.66        tv 			err(1, "%s: realpath", from_name);
    437  1.61    simonb 		do_symlink(src, to_name);
    438  1.59     perry 		metadata_log(to_name, "link", NULL, src);
    439  1.16  christos 		return;
    440  1.16  christos 	}
    441  1.16  christos 
    442  1.16  christos 	if (dolink & LN_RELATIVE) {
    443  1.61    simonb 		char *cp, *d, *s;
    444  1.16  christos 
    445  1.16  christos 		/* Resolve pathnames */
    446  1.16  christos 		if (realpath(from_name, src) == NULL)
    447  1.66        tv 			err(1, "%s: realpath", from_name);
    448  1.61    simonb 
    449  1.61    simonb 		/*
    450  1.61    simonb 		 * The last component of to_name may be a symlink,
    451  1.61    simonb 		 * so use realpath to resolve only the directory.
    452  1.61    simonb 		 */
    453  1.63     perry 		cp = xdirname(to_name);
    454  1.61    simonb 		if (realpath(cp, dst) == NULL)
    455  1.66        tv 			err(1, "%s: realpath", cp);
    456  1.61    simonb 		/* .. and add the last component */
    457  1.61    simonb 		if (strcmp(dst, "/") != 0) {
    458  1.61    simonb 			if (strlcat(dst, "/", sizeof(dst)) > sizeof(dst))
    459  1.61    simonb 				errx(1, "resolved pathname too long");
    460  1.61    simonb 		}
    461  1.61    simonb 		cp = xbasename(to_name);
    462  1.61    simonb 		if (strlcat(dst, cp, sizeof(dst)) > sizeof(dst))
    463  1.61    simonb 			errx(1, "resolved pathname too long");
    464  1.16  christos 
    465  1.16  christos 		/* trim common path components */
    466  1.16  christos 		for (s = src, d = dst; *s == *d; s++, d++)
    467  1.16  christos 			continue;
    468  1.16  christos 		while (*s != '/')
    469  1.16  christos 			s--, d--;
    470  1.16  christos 
    471  1.16  christos 		/* count the number of directories we need to backtrack */
    472  1.16  christos 		for (++d, lnk[0] = '\0'; *d; d++)
    473  1.16  christos 			if (*d == '/')
    474  1.61    simonb 				(void)strcat(lnk, "../");
    475  1.16  christos 
    476  1.61    simonb 		(void)strcat(lnk, ++s);
    477  1.16  christos 
    478  1.61    simonb 		do_symlink(lnk, dst);
    479  1.59     perry 		metadata_log(dst, "link", NULL, lnk);
    480  1.16  christos 		return;
    481  1.16  christos 	}
    482  1.16  christos 
    483  1.16  christos 	/*
    484  1.16  christos 	 * If absolute or relative was not specified,
    485  1.16  christos 	 * try the names the user provided
    486  1.16  christos 	 */
    487  1.61    simonb 	do_symlink(from_name, to_name);
    488  1.59     perry 	metadata_log(to_name, "link", NULL, from_name);
    489  1.16  christos }
    490  1.16  christos 
    491  1.16  christos /*
    492   1.1       cgd  * install --
    493   1.1       cgd  *	build a path name and install the file
    494   1.1       cgd  */
    495   1.5       jtc void
    496  1.50     lukem install(char *from_name, char *to_name, u_int flags)
    497   1.1       cgd {
    498  1.65        tv 	struct stat	from_sb;
    499  1.65        tv #if !HAVE_CONFIG_H
    500  1.65        tv 	struct stat	to_sb;
    501  1.65        tv #endif
    502  1.53     lukem 	struct timeval	tv[2];
    503  1.60     dillo 	int		devnull, from_fd, to_fd, serrno, tmpmode;
    504  1.53     lukem 	char		*p, tmpl[MAXPATHLEN], *oto_name;
    505   1.5       jtc 
    506   1.5       jtc 	if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
    507  1.56     perry 		if (!dolink) {
    508  1.56     perry 			if (stat(from_name, &from_sb))
    509  1.66        tv 				err(1, "%s: stat", from_name);
    510  1.56     perry 			if (!S_ISREG(from_sb.st_mode))
    511  1.56     perry 				errx(1, "%s: not a regular file", from_name);
    512  1.56     perry 		}
    513   1.5       jtc 		/* Build the target path. */
    514   1.5       jtc 		if (flags & DIRECTORY) {
    515   1.5       jtc 			(void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
    516   1.5       jtc 			    to_name,
    517  1.21     lukem 			    (p = strrchr(from_name, '/')) ? ++p : from_name);
    518   1.1       cgd 			to_name = pathbuf;
    519   1.1       cgd 		}
    520   1.1       cgd 		devnull = 0;
    521   1.5       jtc 	} else {
    522  1.65        tv #if HAVE_STRUCT_STAT_ST_FLAGS
    523   1.5       jtc 		from_sb.st_flags = 0;	/* XXX */
    524  1.65        tv #endif
    525   1.1       cgd 		devnull = 1;
    526   1.5       jtc 	}
    527   1.1       cgd 
    528   1.5       jtc 	/*
    529   1.5       jtc 	 * Unlink now... avoid ETXTBSY errors later.  Try and turn
    530   1.5       jtc 	 * off the append/immutable bits -- if we fail, go ahead,
    531   1.5       jtc 	 * it might work.
    532   1.5       jtc 	 */
    533  1.65        tv #if !HAVE_CONFIG_H
    534   1.5       jtc 	if (stat(to_name, &to_sb) == 0 &&
    535   1.5       jtc 	    to_sb.st_flags & (NOCHANGEBITS))
    536   1.5       jtc 		(void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
    537  1.65        tv #endif
    538  1.33  christos 	if (dorename) {
    539  1.64   msaitoh 		(void)snprintf(tmpl, sizeof(tmpl), "%s/inst.XXXXXX",
    540  1.61    simonb 		    xdirname(to_name));
    541  1.33  christos 		oto_name = to_name;
    542  1.33  christos 		to_name = tmpl;
    543  1.33  christos 	} else {
    544  1.37  christos 		oto_name = NULL;	/* pacify gcc */
    545  1.33  christos 		if (dobackup)
    546  1.33  christos 			backup(to_name);
    547  1.33  christos 		else
    548  1.33  christos 			(void)unlink(to_name);
    549  1.33  christos 	}
    550  1.16  christos 
    551  1.16  christos 	if (dolink) {
    552  1.61    simonb 		makelink(from_name, dorename ? oto_name : to_name);
    553  1.16  christos 		return;
    554  1.16  christos 	}
    555   1.1       cgd 
    556   1.5       jtc 	/* Create target. */
    557  1.33  christos 	if (dorename) {
    558  1.33  christos 		if ((to_fd = mkstemp(to_name)) == -1)
    559  1.66        tv 			err(1, "%s: mkstemp", to_name);
    560  1.33  christos 	} else {
    561  1.33  christos 		if ((to_fd = open(to_name,
    562  1.33  christos 		    O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
    563  1.66        tv 			err(1, "%s: open", to_name);
    564  1.33  christos 	}
    565   1.1       cgd 	if (!devnull) {
    566   1.1       cgd 		if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
    567   1.1       cgd 			(void)unlink(to_name);
    568  1.66        tv 			err(1, "%s: open", from_name);
    569   1.1       cgd 		}
    570   1.5       jtc 		copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
    571   1.1       cgd 		(void)close(from_fd);
    572   1.1       cgd 	}
    573   1.9  jonathan 
    574   1.9  jonathan 	if (dostrip) {
    575   1.1       cgd 		strip(to_name);
    576   1.9  jonathan 
    577   1.9  jonathan 		/*
    578   1.9  jonathan 		 * Re-open our fd on the target, in case we used a strip
    579   1.9  jonathan 		 *  that does not work in-place -- like gnu binutils strip.
    580   1.9  jonathan 		 */
    581   1.9  jonathan 		close(to_fd);
    582   1.9  jonathan 		if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
    583   1.9  jonathan 		  err(1, "stripping %s", to_name);
    584   1.9  jonathan 	}
    585   1.9  jonathan 
    586  1.41       cgd 	/*
    587  1.41       cgd 	 * Set owner, group, mode for target; do the chown first,
    588  1.41       cgd 	 * chown may lose the setuid bits.
    589  1.41       cgd 	 */
    590  1.50     lukem 	if (!dounpriv &&
    591  1.50     lukem 	    (flags & (HASUID | HASGID)) && fchown(to_fd, uid, gid) == -1) {
    592  1.41       cgd 		serrno = errno;
    593  1.41       cgd 		(void)unlink(to_name);
    594  1.41       cgd 		errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
    595   1.1       cgd 	}
    596  1.60     dillo 	tmpmode = mode;
    597  1.58        tv 	if (dounpriv)
    598  1.60     dillo 		tmpmode &= S_IRWXU|S_IRWXG|S_IRWXO;
    599  1.60     dillo 	if (fchmod(to_fd, tmpmode) == -1) {
    600   1.5       jtc 		serrno = errno;
    601   1.5       jtc 		(void)unlink(to_name);
    602   1.5       jtc 		errx(1, "%s: chmod: %s", to_name, strerror(serrno));
    603   1.5       jtc 	}
    604   1.5       jtc 
    605   1.5       jtc 	/*
    606  1.26  christos 	 * Preserve the date of the source file.
    607  1.26  christos 	 */
    608  1.26  christos 	if (dopreserve) {
    609  1.37  christos #ifdef BSD4_4
    610  1.26  christos 		TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
    611  1.26  christos 		TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
    612  1.37  christos #else
    613  1.37  christos 		tv[0].tv_sec = from_sb.st_atime;
    614  1.37  christos 		tv[0].tv_usec = 0;
    615  1.37  christos 		tv[1].tv_sec = from_sb.st_mtime;
    616  1.37  christos 		tv[1].tv_usec = 0;
    617  1.37  christos #endif
    618  1.65        tv #if HAVE_FUTIMES
    619  1.65        tv 		if (futimes(to_fd, tv) == -1)
    620  1.65        tv 			warn("%s: futimes", to_name);
    621  1.65        tv #else
    622  1.65        tv 		if (utimes(to_name, tv) == -1)
    623  1.66        tv 			warn("%s: utimes", to_name);
    624  1.65        tv #endif
    625   1.5       jtc 	}
    626   1.5       jtc 
    627   1.1       cgd 	(void)close(to_fd);
    628  1.33  christos 
    629  1.50     lukem 	if (dorename) {
    630  1.33  christos 		if (rename(to_name, oto_name) == -1)
    631  1.33  christos 			err(1, "%s: rename", to_name);
    632  1.50     lukem 		to_name = oto_name;
    633  1.50     lukem 	}
    634  1.33  christos 
    635   1.5       jtc 	if (!docopy && !devnull && unlink(from_name))
    636  1.66        tv 		err(1, "%s: unlink", from_name);
    637  1.50     lukem 
    638  1.50     lukem 	/*
    639  1.50     lukem 	 * If provided a set of flags, set them, otherwise, preserve the
    640  1.50     lukem 	 * flags, except for the dump flag.
    641  1.50     lukem 	 */
    642  1.65        tv #if !HAVE_CONFIG_H
    643  1.50     lukem 	if (!dounpriv && chflags(to_name,
    644  1.51     lukem 	    flags & SETFLAGS ? fileflags : from_sb.st_flags & ~UF_NODUMP) == -1)
    645  1.51     lukem 	{
    646  1.50     lukem 		if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
    647  1.50     lukem 			warn("%s: chflags", to_name);
    648  1.50     lukem 	}
    649  1.65        tv #endif
    650  1.50     lukem 
    651  1.59     perry 	metadata_log(to_name, "file", tv, NULL);
    652   1.1       cgd }
    653   1.1       cgd 
    654   1.1       cgd /*
    655   1.1       cgd  * copy --
    656   1.1       cgd  *	copy from one file to another
    657   1.1       cgd  */
    658   1.5       jtc void
    659  1.48    simonb copy(int from_fd, char *from_name, int to_fd, char *to_name, off_t size)
    660   1.1       cgd {
    661  1.53     lukem 	ssize_t	nr, nw;
    662  1.53     lukem 	int	serrno;
    663  1.53     lukem 	char	*p;
    664  1.53     lukem 	char	buf[MAXBSIZE];
    665   1.1       cgd 
    666   1.5       jtc 	/*
    667  1.28  wsanchez 	 * There's no reason to do anything other than close the file
    668  1.28  wsanchez 	 * now if it's empty, so let's not bother.
    669   1.5       jtc 	 */
    670  1.28  wsanchez 	if (size > 0) {
    671  1.45       chs 
    672  1.28  wsanchez 		/*
    673  1.45       chs 		 * Mmap and write if less than 8M (the limit is so we
    674  1.45       chs 		 * don't totally trash memory on big files).  This is
    675  1.45       chs 		 * really a minor hack, but it wins some CPU back.
    676  1.28  wsanchez 		 */
    677  1.45       chs 
    678  1.28  wsanchez 		if (size <= 8 * 1048576) {
    679  1.28  wsanchez 			if ((p = mmap(NULL, (size_t)size, PROT_READ,
    680  1.45       chs 			    MAP_FILE|MAP_SHARED, from_fd, (off_t)0))
    681  1.45       chs 			    == MAP_FAILED) {
    682  1.45       chs 				goto mmap_failed;
    683  1.36   thorpej 			}
    684  1.44       cgd #ifdef MADV_SEQUENTIAL
    685  1.39  christos 			if (madvise(p, (size_t)size, MADV_SEQUENTIAL) == -1
    686  1.39  christos 			    && errno != EOPNOTSUPP)
    687  1.38  sommerfe 				warnx("madvise: %s", strerror(errno));
    688  1.44       cgd #endif
    689  1.38  sommerfe 
    690  1.36   thorpej 			if (write(to_fd, p, size) != size) {
    691  1.36   thorpej 				serrno = errno;
    692  1.36   thorpej 				(void)unlink(to_name);
    693  1.66        tv 				errx(1, "%s: write: %s",
    694  1.36   thorpej 				    to_name, strerror(serrno));
    695  1.36   thorpej 			}
    696  1.28  wsanchez 		} else {
    697  1.45       chs mmap_failed:
    698  1.36   thorpej 			while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
    699  1.28  wsanchez 				if ((nw = write(to_fd, buf, nr)) != nr) {
    700  1.28  wsanchez 					serrno = errno;
    701  1.28  wsanchez 					(void)unlink(to_name);
    702  1.66        tv 					errx(1, "%s: write: %s", to_name,
    703  1.45       chs 					    strerror(nw > 0 ? EIO : serrno));
    704  1.28  wsanchez 				}
    705  1.36   thorpej 			}
    706  1.28  wsanchez 			if (nr != 0) {
    707   1.5       jtc 				serrno = errno;
    708   1.5       jtc 				(void)unlink(to_name);
    709  1.66        tv 				errx(1, "%s: read: %s", from_name, strerror(serrno));
    710   1.5       jtc 			}
    711   1.1       cgd 		}
    712   1.1       cgd 	}
    713   1.1       cgd }
    714   1.1       cgd 
    715   1.1       cgd /*
    716   1.1       cgd  * strip --
    717   1.1       cgd  *	use strip(1) to strip the target file
    718   1.1       cgd  */
    719   1.5       jtc void
    720  1.48    simonb strip(char *to_name)
    721   1.1       cgd {
    722  1.53     lukem 	int	serrno, status;
    723  1.53     lukem 	char	*stripprog;
    724   1.1       cgd 
    725   1.1       cgd 	switch (vfork()) {
    726   1.1       cgd 	case -1:
    727   1.5       jtc 		serrno = errno;
    728   1.5       jtc 		(void)unlink(to_name);
    729  1.22   thorpej 		errx(1, "vfork: %s", strerror(serrno));
    730  1.61    simonb 		/*NOTREACHED*/
    731   1.1       cgd 	case 0:
    732  1.18   thorpej 		stripprog = getenv("STRIP");
    733  1.18   thorpej 		if (stripprog == NULL)
    734  1.18   thorpej 			stripprog = _PATH_STRIP;
    735  1.28  wsanchez 
    736  1.28  wsanchez 		if (stripArgs) {
    737  1.50     lukem 			/*
    738  1.50     lukem 			 * build up a command line and let /bin/sh
    739  1.50     lukem 			 * parse the arguments
    740  1.50     lukem 			 */
    741  1.28  wsanchez 			char* cmd = (char*)malloc(sizeof(char)*
    742  1.28  wsanchez 						  (3+strlen(stripprog)+
    743  1.28  wsanchez 						     strlen(stripArgs)+
    744  1.28  wsanchez 						     strlen(to_name)));
    745  1.49    simonb 
    746  1.49    simonb 			if (cmd == NULL)
    747  1.49    simonb 				errx(1, "%s", strerror(ENOMEM));
    748  1.28  wsanchez 
    749  1.28  wsanchez 			sprintf(cmd, "%s %s %s", stripprog, stripArgs, to_name);
    750  1.28  wsanchez 
    751  1.28  wsanchez 			execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
    752  1.28  wsanchez 		} else
    753  1.40       cgd 			execlp(stripprog, "strip", to_name, NULL);
    754  1.28  wsanchez 
    755  1.66        tv 		warn("%s: exec of strip", stripprog);
    756  1.22   thorpej 		_exit(1);
    757  1.61    simonb 		/*NOTREACHED*/
    758   1.1       cgd 	default:
    759   1.1       cgd 		if (wait(&status) == -1 || status)
    760   1.5       jtc 			(void)unlink(to_name);
    761   1.1       cgd 	}
    762  1.32   hubertf }
    763  1.32   hubertf 
    764  1.32   hubertf /*
    765  1.61    simonb  * backup --
    766  1.61    simonb  *	backup file "to_name" to to_name.suffix
    767  1.61    simonb  *	if suffix contains a "%", it's taken as a printf(3) pattern
    768  1.61    simonb  *	used for a numbered backup.
    769  1.32   hubertf  */
    770  1.32   hubertf void
    771  1.48    simonb backup(const char *to_name)
    772  1.32   hubertf {
    773  1.61    simonb 	char	bname[FILENAME_MAX];
    774  1.32   hubertf 
    775  1.35   hubertf 	if (numberedbackup) {
    776  1.32   hubertf 		/* Do numbered backup */
    777  1.32   hubertf 		int cnt;
    778  1.32   hubertf 		char suffix_expanded[FILENAME_MAX];
    779  1.32   hubertf 
    780  1.32   hubertf 		cnt=0;
    781  1.32   hubertf 		do {
    782  1.50     lukem 			(void)snprintf(suffix_expanded, FILENAME_MAX, suffix,
    783  1.50     lukem 			    cnt);
    784  1.61    simonb 			(void)snprintf(bname, FILENAME_MAX, "%s%s", to_name,
    785  1.61    simonb 			    suffix_expanded);
    786  1.32   hubertf 			cnt++;
    787  1.61    simonb 		} while (access(bname, F_OK) == 0);
    788  1.32   hubertf 	} else {
    789  1.32   hubertf 		/* Do simple backup */
    790  1.61    simonb 		(void)snprintf(bname, FILENAME_MAX, "%s%s", to_name, suffix);
    791  1.32   hubertf 	}
    792  1.32   hubertf 
    793  1.61    simonb 	(void)rename(to_name, bname);
    794   1.1       cgd }
    795   1.1       cgd 
    796   1.1       cgd /*
    797   1.5       jtc  * install_dir --
    798  1.47       wiz  *	build directory hierarchy
    799   1.2       jtc  */
    800   1.7       jtc void
    801  1.50     lukem install_dir(char *path, u_int flags)
    802   1.2       jtc {
    803  1.53     lukem         char		*p;
    804  1.53     lukem         struct stat	sb;
    805  1.53     lukem         int		ch;
    806   1.2       jtc 
    807   1.4       cgd         for (p = path;; ++p)
    808   1.4       cgd                 if (!*p || (p != path && *p  == '/')) {
    809   1.2       jtc                         ch = *p;
    810   1.2       jtc                         *p = '\0';
    811   1.2       jtc                         if (stat(path, &sb)) {
    812   1.2       jtc                                 if (errno != ENOENT || mkdir(path, 0777) < 0) {
    813  1.66        tv 					err(1, "%s: mkdir", path);
    814   1.2       jtc                                 }
    815   1.2       jtc                         }
    816   1.2       jtc                         if (!(*p = ch))
    817   1.4       cgd 				break;
    818   1.2       jtc                 }
    819   1.2       jtc 
    820  1.50     lukem 	if (!dounpriv && (
    821  1.50     lukem 	    ((flags & (HASUID | HASGID)) && chown(path, uid, gid) == -1)
    822  1.50     lukem 	    || chmod(path, mode) == -1 )) {
    823  1.66        tv                 warn("%s: chown/chmod", path);
    824  1.41       cgd 	}
    825  1.59     perry 	metadata_log(path, "dir", NULL, NULL);
    826   1.2       jtc }
    827   1.2       jtc 
    828   1.2       jtc /*
    829  1.50     lukem  * metadata_log --
    830  1.50     lukem  *	if metafp is not NULL, output mtree(8) full path name and settings to
    831  1.50     lukem  *	metafp, to allow permissions to be set correctly by other tools.
    832  1.50     lukem  */
    833  1.50     lukem void
    834  1.59     perry metadata_log(const char *path, const char *type, struct timeval *tv,
    835  1.57     lukem 	const char *link)
    836  1.50     lukem {
    837  1.50     lukem 	const char	extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
    838  1.53     lukem 	char		*buf;
    839  1.50     lukem 
    840  1.50     lukem 	if (!metafp)
    841  1.50     lukem 		return;
    842  1.50     lukem 	buf = (char *)malloc(4 * strlen(path) + 1);	/* buf for strsvis(3) */
    843  1.50     lukem 	if (buf == NULL) {
    844  1.50     lukem 		warnx("%s", strerror(ENOMEM));
    845  1.50     lukem 		return;
    846  1.50     lukem 	}
    847  1.50     lukem 	if (flock(fileno(metafp), LOCK_EX) == -1) {	/* lock log file */
    848  1.50     lukem 		warn("can't lock %s", metafile);
    849  1.50     lukem 		return;
    850  1.50     lukem 	}
    851  1.50     lukem 
    852  1.50     lukem 	strsvis(buf, path, VIS_CSTYLE, extra);		/* encode name */
    853  1.50     lukem 	fprintf(metafp, ".%s%s type=%s mode=%#o",	/* print details */
    854  1.59     perry 	    buf[0] == '/' ? "" : "/", buf, type, mode);
    855  1.57     lukem 	if (link)
    856  1.57     lukem 		fprintf(metafp, " link=%s", link);
    857  1.52        tv 	if (owner)
    858  1.52        tv 		fprintf(metafp, " uname=%s", owner);
    859  1.52        tv 	if (group)
    860  1.52        tv 		fprintf(metafp, " gname=%s", group);
    861  1.52        tv 	if (fflags)
    862  1.52        tv 		fprintf(metafp, " flags=%s", fflags);
    863  1.54     lukem 	if (tags)
    864  1.54     lukem 		fprintf(metafp, " tags=%s", tags);
    865  1.57     lukem 	if (tv != NULL && dopreserve)
    866  1.50     lukem 		fprintf(metafp, " time=%ld.%ld", tv[1].tv_sec, tv[1].tv_usec);
    867  1.50     lukem 	fputc('\n', metafp);
    868  1.50     lukem 	fflush(metafp);					/* flush output */
    869  1.50     lukem 	if (flock(fileno(metafp), LOCK_UN) == -1) {	/* unlock log file */
    870  1.50     lukem 		warn("can't unlock %s", metafile);
    871  1.50     lukem 	}
    872  1.50     lukem 	free(buf);
    873  1.50     lukem }
    874  1.50     lukem 
    875  1.61    simonb /*
    876  1.61    simonb  * xbasename --
    877  1.61    simonb  *	libc basename(3) that returns a pointer to a static buffer
    878  1.61    simonb  *	instead of overwriting that passed-in string.
    879  1.61    simonb  */
    880  1.61    simonb char *
    881  1.61    simonb xbasename(char *path)
    882  1.61    simonb {
    883  1.61    simonb 	static char tmp[MAXPATHLEN];
    884  1.61    simonb 
    885  1.61    simonb 	(void)strlcpy(tmp, path, sizeof(tmp));
    886  1.61    simonb 	return (basename(tmp));
    887  1.61    simonb }
    888  1.61    simonb 
    889  1.61    simonb /*
    890  1.61    simonb  * xdirname --
    891  1.61    simonb  *	libc dirname(3) that returns a pointer to a static buffer
    892  1.61    simonb  *	instead of overwriting that passed-in string.
    893  1.61    simonb  */
    894  1.61    simonb char *
    895  1.61    simonb xdirname(char *path)
    896  1.61    simonb {
    897  1.61    simonb 	static char tmp[MAXPATHLEN];
    898  1.50     lukem 
    899  1.61    simonb 	(void)strlcpy(tmp, path, sizeof(tmp));
    900  1.61    simonb 	return (dirname(tmp));
    901  1.61    simonb }
    902  1.50     lukem 
    903  1.50     lukem /*
    904   1.1       cgd  * usage --
    905   1.1       cgd  *	print a usage message and die
    906   1.1       cgd  */
    907   1.5       jtc void
    908  1.48    simonb usage(void)
    909   1.1       cgd {
    910  1.53     lukem 
    911   1.2       jtc 	(void)fprintf(stderr, "\
    912  1.54     lukem usage: install [-Ubcprs] [-M log] [-T tags] [-B suffix] [-f flags] [-m mode]\n\
    913  1.50     lukem 	    [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 file2\n\
    914  1.54     lukem        install [-Ubcprs] [-M log] [-T tags] [-B suffix] [-f flags] [-m mode]\n\
    915  1.54     lukem 	    [-o owner] [-g group] [-l linkflags] [-S stripflags]\n\
    916  1.50     lukem 	    file1 ... fileN directory\n\
    917  1.54     lukem        install [-Up] [-M log] [-T tags] -d [-m mode]\n\
    918  1.54     lukem 	    [-o owner] [-g group] directory ...\n");
    919   1.6       jtc 	exit(1);
    920   1.1       cgd }
    921