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