1999-08-01 09:02:06 +04:00
|
|
|
/* $NetBSD: xinstall.c,v 1.38 1999/08/01 05:02:06 sommerfeld Exp $ */
|
1994-11-14 07:57:16 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-11-14 07:57:16 +03:00
|
|
|
* Copyright (c) 1987, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-19 18:42:36 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-10-19 18:42:36 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1994-11-14 07:57:16 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93";
|
1997-10-19 18:42:36 +04:00
|
|
|
#else
|
1999-08-01 09:02:06 +04:00
|
|
|
__RCSID("$NetBSD: xinstall.c,v 1.38 1999/08/01 05:02:06 sommerfeld Exp $");
|
1994-11-14 07:57:16 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
1994-11-14 07:57:16 +03:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/mman.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <sys/stat.h>
|
1994-11-14 07:57:16 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <ctype.h>
|
1993-06-11 22:30:46 +04:00
|
|
|
#include <errno.h>
|
1994-11-14 07:57:16 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <grp.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <paths.h>
|
1994-11-14 07:57:16 +03:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <err.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "pathnames.h"
|
1998-10-10 11:38:22 +04:00
|
|
|
#include "stat_flags.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-10-08 06:12:51 +04:00
|
|
|
#define STRIP_ARGS_MAX 32
|
1999-01-25 04:42:57 +03:00
|
|
|
#define BACKUP_SUFFIX ".old"
|
1998-10-08 06:12:51 +04:00
|
|
|
|
1994-11-14 07:57:16 +03:00
|
|
|
struct passwd *pp;
|
|
|
|
struct group *gp;
|
1999-02-04 14:56:48 +03:00
|
|
|
int dobackup=0, docopy=0, dodir=0, dostrip=0, dolink=0, dopreserve=0,
|
1999-08-01 09:02:06 +04:00
|
|
|
dorename = 0, unpriv = 0;
|
1999-03-29 21:01:49 +04:00
|
|
|
static int numberedbackup = 0;
|
1994-11-14 07:57:16 +03:00
|
|
|
int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
|
1995-08-10 08:20:57 +04:00
|
|
|
char pathbuf[MAXPATHLEN];
|
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
1998-10-08 06:12:51 +04:00
|
|
|
char *stripArgs=NULL;
|
1999-01-25 04:42:57 +03:00
|
|
|
char *suffix=BACKUP_SUFFIX;
|
1994-11-14 07:57:16 +03:00
|
|
|
|
1997-03-16 22:43:19 +03:00
|
|
|
#define LN_ABSOLUTE 0x01
|
|
|
|
#define LN_RELATIVE 0x02
|
|
|
|
#define LN_HARD 0x04
|
|
|
|
#define LN_SYMBOLIC 0x08
|
|
|
|
#define LN_MIXED 0x10
|
|
|
|
|
1994-11-14 07:57:16 +03:00
|
|
|
#define DIRECTORY 0x01 /* Tell install it's a directory. */
|
|
|
|
#define SETFLAGS 0x02 /* Tell install to set flags. */
|
|
|
|
|
|
|
|
void copy __P((int, char *, int, char *, off_t));
|
1997-03-16 22:43:19 +03:00
|
|
|
void makelink __P((char *, char *));
|
1994-11-14 07:57:16 +03:00
|
|
|
void install __P((char *, char *, u_long, u_int));
|
1994-12-20 04:24:38 +03:00
|
|
|
void install_dir __P((char *));
|
1994-11-14 07:57:16 +03:00
|
|
|
void strip __P((char *));
|
1999-01-26 04:34:25 +03:00
|
|
|
void backup __P((const char *));
|
1994-11-14 07:57:16 +03:00
|
|
|
void usage __P((void));
|
1997-10-19 18:42:36 +04:00
|
|
|
int main __P((int, char *[]));
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-11-14 07:57:16 +03:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
1994-11-14 07:57:16 +03:00
|
|
|
char *argv[];
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
struct stat from_sb, to_sb;
|
1994-11-14 07:57:16 +03:00
|
|
|
mode_t *set;
|
|
|
|
u_long fset;
|
|
|
|
u_int iflags;
|
1993-03-21 12:45:37 +03:00
|
|
|
int ch, no_target;
|
1997-03-16 22:43:19 +03:00
|
|
|
char *p;
|
|
|
|
char *flags = NULL, *to_name, *group = NULL, *owner = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-11-14 07:57:16 +03:00
|
|
|
iflags = 0;
|
1999-08-01 09:02:06 +04:00
|
|
|
while ((ch = getopt(argc, argv, "cbB:df:g:l:m:o:prsS:U")) != -1)
|
1993-03-21 12:45:37 +03:00
|
|
|
switch((char)ch) {
|
1999-01-25 04:42:57 +03:00
|
|
|
case 'B':
|
|
|
|
suffix = optarg;
|
1999-03-29 21:01:49 +04:00
|
|
|
numberedbackup = 0;
|
|
|
|
{
|
|
|
|
/* Check if given suffix really generates
|
|
|
|
different suffixes - catch e.g. ".%" */
|
|
|
|
char suffix_expanded0[FILENAME_MAX],
|
|
|
|
suffix_expanded1[FILENAME_MAX];
|
|
|
|
(void)snprintf(suffix_expanded0, FILENAME_MAX,
|
|
|
|
suffix, 0);
|
|
|
|
(void)snprintf(suffix_expanded1, FILENAME_MAX,
|
|
|
|
suffix, 1);
|
|
|
|
if (strcmp(suffix_expanded0, suffix_expanded1)
|
|
|
|
!= 0)
|
|
|
|
numberedbackup = 1;
|
|
|
|
}
|
1999-01-25 04:42:57 +03:00
|
|
|
/* fall through; -B implies -b */
|
|
|
|
case 'b':
|
|
|
|
dobackup = 1;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'c':
|
|
|
|
docopy = 1;
|
|
|
|
break;
|
1998-09-28 12:16:15 +04:00
|
|
|
case 'd':
|
|
|
|
dodir = 1;
|
|
|
|
break;
|
1994-11-14 07:57:16 +03:00
|
|
|
case 'f':
|
|
|
|
flags = optarg;
|
|
|
|
if (string_to_flags(&flags, &fset, NULL))
|
|
|
|
errx(1, "%s: invalid flag", flags);
|
|
|
|
iflags |= SETFLAGS;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'g':
|
|
|
|
group = optarg;
|
|
|
|
break;
|
1997-03-16 22:43:19 +03:00
|
|
|
case 'l':
|
|
|
|
for (p = optarg; *p; p++)
|
|
|
|
switch (*p) {
|
|
|
|
case 's':
|
|
|
|
dolink &= ~(LN_HARD|LN_MIXED);
|
|
|
|
dolink |= LN_SYMBOLIC;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
dolink &= ~(LN_SYMBOLIC|LN_MIXED);
|
|
|
|
dolink |= LN_HARD;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
dolink &= ~(LN_SYMBOLIC|LN_HARD);
|
|
|
|
dolink |= LN_MIXED;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
dolink &= ~LN_RELATIVE;
|
|
|
|
dolink |= LN_ABSOLUTE;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
dolink &= ~LN_ABSOLUTE;
|
|
|
|
dolink |= LN_RELATIVE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errx(1, "%c: invalid link type", *p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
1998-09-28 12:16:15 +04:00
|
|
|
case 'm':
|
|
|
|
if (!(set = setmode(optarg)))
|
|
|
|
errx(1, "%s: invalid file mode", optarg);
|
|
|
|
mode = getmode(set, 0);
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
owner = optarg;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
dopreserve = 1;
|
|
|
|
break;
|
1999-02-04 14:56:48 +03:00
|
|
|
case 'r':
|
|
|
|
dorename = 1;
|
|
|
|
break;
|
1998-10-08 06:12:51 +04:00
|
|
|
case 'S':
|
|
|
|
stripArgs = (char*)malloc(sizeof(char)*(strlen(optarg)+1));
|
|
|
|
strcpy(stripArgs,optarg);
|
|
|
|
/* fall through; -S implies -s */
|
1998-09-28 12:16:15 +04:00
|
|
|
case 's':
|
|
|
|
dostrip = 1;
|
|
|
|
break;
|
1999-08-01 09:02:06 +04:00
|
|
|
case 'U':
|
|
|
|
unpriv = 1;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
1993-06-11 22:30:46 +04:00
|
|
|
|
1997-12-01 01:20:00 +03:00
|
|
|
/* strip and link options make no sense when creating directories */
|
|
|
|
if ((dostrip || dolink) && dodir)
|
1997-03-16 22:43:19 +03:00
|
|
|
usage();
|
|
|
|
|
|
|
|
/* strip and flags make no sense with links */
|
|
|
|
if ((dostrip || flags) && dolink)
|
1993-06-11 22:30:46 +04:00
|
|
|
usage();
|
|
|
|
|
|
|
|
/* must have at least two arguments, except when creating directories */
|
|
|
|
if (argc < 2 && !dodir)
|
1993-03-21 12:45:37 +03:00
|
|
|
usage();
|
|
|
|
|
|
|
|
/* get group and owner id's */
|
1998-12-20 18:07:46 +03:00
|
|
|
if (group && !(gp = getgrnam(group)) && !isdigit((unsigned char)*group))
|
1994-11-14 07:57:16 +03:00
|
|
|
errx(1, "unknown group %s", group);
|
1995-08-10 08:20:57 +04:00
|
|
|
gid = (group) ? ((gp) ? gp->gr_gid : atoi(group)) : -1;
|
1998-12-20 18:07:46 +03:00
|
|
|
if (owner && !(pp = getpwnam(owner)) && !isdigit((unsigned char)*owner))
|
1994-11-14 07:57:16 +03:00
|
|
|
errx(1, "unknown user %s", owner);
|
1995-08-10 08:20:57 +04:00
|
|
|
uid = (owner) ? ((pp) ? pp->pw_uid : atoi(owner)) : -1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1993-06-11 22:30:46 +04:00
|
|
|
if (dodir) {
|
|
|
|
for (; *argv != NULL; ++argv)
|
1994-11-14 07:57:16 +03:00
|
|
|
install_dir(*argv);
|
1993-06-11 22:30:46 +04:00
|
|
|
exit (0);
|
1994-11-14 07:57:16 +03:00
|
|
|
}
|
1993-06-11 22:30:46 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
no_target = stat(to_name = argv[argc - 1], &to_sb);
|
1993-06-11 22:30:46 +04:00
|
|
|
if (!no_target && S_ISDIR(to_sb.st_mode)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
for (; *argv != to_name; ++argv)
|
1994-11-14 07:57:16 +03:00
|
|
|
install(*argv, to_name, fset, iflags | DIRECTORY);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* can't do file1 file2 directory/file */
|
|
|
|
if (argc != 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if (!no_target) {
|
1994-11-14 07:57:16 +03:00
|
|
|
if (stat(*argv, &from_sb))
|
|
|
|
err(1, "%s", *argv);
|
|
|
|
if (!S_ISREG(to_sb.st_mode))
|
|
|
|
errx(1, "%s: %s", to_name, strerror(EFTYPE));
|
1997-03-16 22:43:19 +03:00
|
|
|
if (!dolink && to_sb.st_dev == from_sb.st_dev &&
|
1994-11-14 07:57:16 +03:00
|
|
|
to_sb.st_ino == from_sb.st_ino)
|
|
|
|
errx(1, "%s and %s are the same file", *argv, to_name);
|
|
|
|
/*
|
|
|
|
* Unlink now... avoid ETXTBSY errors later. Try and turn
|
|
|
|
* off the append/immutable bits -- if we fail, go ahead,
|
|
|
|
* it might work.
|
|
|
|
*/
|
|
|
|
#define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
|
|
|
|
if (to_sb.st_flags & NOCHANGEBITS)
|
|
|
|
(void)chflags(to_name,
|
|
|
|
to_sb.st_flags & ~(NOCHANGEBITS));
|
1999-01-26 04:34:25 +03:00
|
|
|
if (dobackup)
|
|
|
|
backup(to_name);
|
1999-03-24 18:13:46 +03:00
|
|
|
else if (!dorename)
|
1999-01-25 04:42:57 +03:00
|
|
|
(void)unlink(to_name);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-11-14 07:57:16 +03:00
|
|
|
install(*argv, to_name, fset, iflags);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
1997-03-16 22:43:19 +03:00
|
|
|
/*
|
|
|
|
* makelink --
|
|
|
|
* make a link from source to destination
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
makelink(from_name, to_name)
|
|
|
|
char *from_name;
|
|
|
|
char *to_name;
|
|
|
|
{
|
|
|
|
char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
|
|
|
|
|
|
|
|
/* Try hard links first */
|
|
|
|
if (dolink & (LN_HARD|LN_MIXED)) {
|
|
|
|
if (link(from_name, to_name) == -1) {
|
|
|
|
if ((dolink & LN_HARD) || errno != EXDEV)
|
|
|
|
err(1, "link %s -> %s", from_name, to_name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Symbolic links */
|
|
|
|
if (dolink & LN_ABSOLUTE) {
|
|
|
|
/* Convert source path to absolute */
|
|
|
|
if (realpath(from_name, src) == NULL)
|
|
|
|
err(1, "%s", src);
|
|
|
|
if (symlink(src, to_name) == -1)
|
|
|
|
err(1, "symlink %s -> %s", src, to_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dolink & LN_RELATIVE) {
|
|
|
|
char *s, *d;
|
|
|
|
|
|
|
|
/* Resolve pathnames */
|
|
|
|
if (realpath(from_name, src) == NULL)
|
|
|
|
err(1, "%s", src);
|
|
|
|
if (realpath(to_name, dst) == NULL)
|
|
|
|
err(1, "%s", dst);
|
|
|
|
|
|
|
|
/* trim common path components */
|
|
|
|
for (s = src, d = dst; *s == *d; s++, d++)
|
|
|
|
continue;
|
|
|
|
while (*s != '/')
|
|
|
|
s--, d--;
|
|
|
|
|
|
|
|
/* count the number of directories we need to backtrack */
|
|
|
|
for (++d, lnk[0] = '\0'; *d; d++)
|
|
|
|
if (*d == '/')
|
|
|
|
(void) strcat(lnk, "../");
|
|
|
|
|
|
|
|
(void) strcat(lnk, ++s);
|
|
|
|
|
|
|
|
if (symlink(lnk, dst) == -1)
|
|
|
|
err(1, "symlink %s -> %s", lnk, dst);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If absolute or relative was not specified,
|
|
|
|
* try the names the user provided
|
|
|
|
*/
|
|
|
|
if (symlink(from_name, to_name) == -1)
|
1997-03-16 22:46:02 +03:00
|
|
|
err(1, "symlink %s -> %s", from_name, to_name);
|
1997-03-16 22:43:19 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* install --
|
|
|
|
* build a path name and install the file
|
|
|
|
*/
|
1994-11-14 07:57:16 +03:00
|
|
|
void
|
|
|
|
install(from_name, to_name, fset, flags)
|
1993-03-21 12:45:37 +03:00
|
|
|
char *from_name, *to_name;
|
1994-11-14 07:57:16 +03:00
|
|
|
u_long fset;
|
|
|
|
u_int flags;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-11-14 07:57:16 +03:00
|
|
|
struct stat from_sb, to_sb;
|
|
|
|
int devnull, from_fd, to_fd, serrno;
|
1999-02-04 14:56:48 +03:00
|
|
|
char *p, tmpl[MAXPATHLEN], *oto_name;
|
1994-11-14 07:57:16 +03:00
|
|
|
|
|
|
|
/* If try to install NULL file to a directory, fails. */
|
|
|
|
if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
|
|
|
|
if (stat(from_name, &from_sb))
|
|
|
|
err(1, "%s", from_name);
|
|
|
|
if (!S_ISREG(from_sb.st_mode))
|
|
|
|
errx(1, "%s: %s", from_name, strerror(EFTYPE));
|
|
|
|
/* Build the target path. */
|
|
|
|
if (flags & DIRECTORY) {
|
|
|
|
(void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
|
|
|
|
to_name,
|
1997-10-20 07:32:25 +04:00
|
|
|
(p = strrchr(from_name, '/')) ? ++p : from_name);
|
1993-03-21 12:45:37 +03:00
|
|
|
to_name = pathbuf;
|
|
|
|
}
|
|
|
|
devnull = 0;
|
1994-11-14 07:57:16 +03:00
|
|
|
} else {
|
|
|
|
from_sb.st_flags = 0; /* XXX */
|
1993-03-21 12:45:37 +03:00
|
|
|
devnull = 1;
|
1994-11-14 07:57:16 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-11-14 07:57:16 +03:00
|
|
|
/*
|
|
|
|
* Unlink now... avoid ETXTBSY errors later. Try and turn
|
|
|
|
* off the append/immutable bits -- if we fail, go ahead,
|
|
|
|
* it might work.
|
|
|
|
*/
|
|
|
|
if (stat(to_name, &to_sb) == 0 &&
|
|
|
|
to_sb.st_flags & (NOCHANGEBITS))
|
|
|
|
(void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
|
1999-02-04 14:56:48 +03:00
|
|
|
if (dorename) {
|
|
|
|
char *ptr, c, *dir;
|
|
|
|
|
|
|
|
if ((ptr = strrchr(to_name, '/')) != NULL) {
|
|
|
|
c = *++ptr;
|
|
|
|
*ptr = '\0';
|
|
|
|
dir = to_name;
|
|
|
|
} else {
|
1999-07-06 18:45:31 +04:00
|
|
|
c = '\0'; /* pacify gcc */
|
1999-02-04 14:56:48 +03:00
|
|
|
dir = tmpl;
|
|
|
|
*dir = '\0';
|
|
|
|
}
|
|
|
|
(void)snprintf(tmpl, sizeof(tmpl), "%sinst.XXXXXX", dir);
|
|
|
|
if (ptr)
|
|
|
|
*ptr = c;
|
|
|
|
oto_name = to_name;
|
|
|
|
to_name = tmpl;
|
|
|
|
|
|
|
|
} else {
|
1999-07-06 18:45:31 +04:00
|
|
|
oto_name = NULL; /* pacify gcc */
|
1999-02-04 14:56:48 +03:00
|
|
|
if (dobackup)
|
|
|
|
backup(to_name);
|
|
|
|
else
|
|
|
|
(void)unlink(to_name);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-03-16 22:43:19 +03:00
|
|
|
if (dolink) {
|
|
|
|
makelink(from_name, to_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1994-11-14 07:57:16 +03:00
|
|
|
/* Create target. */
|
1999-02-04 14:56:48 +03:00
|
|
|
if (dorename) {
|
|
|
|
if ((to_fd = mkstemp(to_name)) == -1)
|
|
|
|
err(1, "%s", to_name);
|
|
|
|
} else {
|
|
|
|
if ((to_fd = open(to_name,
|
|
|
|
O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
|
|
|
|
err(1, "%s", to_name);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!devnull) {
|
|
|
|
if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
|
|
|
|
(void)unlink(to_name);
|
1994-11-14 07:57:16 +03:00
|
|
|
err(1, "%s", from_name);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-11-14 07:57:16 +03:00
|
|
|
copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)close(from_fd);
|
|
|
|
}
|
1995-12-20 13:25:17 +03:00
|
|
|
|
|
|
|
if (dostrip) {
|
1993-03-21 12:45:37 +03:00
|
|
|
strip(to_name);
|
1995-12-20 13:25:17 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Re-open our fd on the target, in case we used a strip
|
|
|
|
* that does not work in-place -- like gnu binutils strip.
|
|
|
|
*/
|
|
|
|
close(to_fd);
|
|
|
|
if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
|
|
|
|
err(1, "stripping %s", to_name);
|
|
|
|
}
|
|
|
|
|
1999-08-01 09:02:06 +04:00
|
|
|
if (!unpriv) {
|
|
|
|
/*
|
|
|
|
* Set owner, group, mode for target; do the chown first,
|
|
|
|
* chown may lose the setuid bits.
|
|
|
|
*/
|
|
|
|
if ((gid != -1 || uid != -1) && fchown(to_fd, uid, gid)) {
|
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
|
|
|
errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-11-14 07:57:16 +03:00
|
|
|
if (fchmod(to_fd, mode)) {
|
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
|
|
|
errx(1, "%s: chmod: %s", to_name, strerror(serrno));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If provided a set of flags, set them, otherwise, preserve the
|
|
|
|
* flags, except for the dump flag.
|
|
|
|
*/
|
|
|
|
if (fchflags(to_fd,
|
|
|
|
flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
|
1997-03-06 02:48:47 +03:00
|
|
|
if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
|
1997-03-10 17:14:37 +03:00
|
|
|
warn("%s: chflags", to_name);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-11-14 07:57:16 +03:00
|
|
|
|
1998-09-28 12:16:15 +04:00
|
|
|
/*
|
|
|
|
* Preserve the date of the source file.
|
|
|
|
*/
|
|
|
|
if (dopreserve) {
|
|
|
|
struct timeval tv[2];
|
|
|
|
|
1999-07-06 18:45:31 +04:00
|
|
|
#ifdef BSD4_4
|
1998-09-28 12:16:15 +04:00
|
|
|
TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
|
|
|
|
TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
|
1999-07-06 18:45:31 +04:00
|
|
|
#else
|
|
|
|
tv[0].tv_sec = from_sb.st_atime;
|
|
|
|
tv[0].tv_usec = 0;
|
|
|
|
tv[1].tv_sec = from_sb.st_mtime;
|
|
|
|
tv[1].tv_usec = 0;
|
|
|
|
#endif
|
1998-09-28 12:16:15 +04:00
|
|
|
if (futimes(to_fd, tv) == -1)
|
|
|
|
warn("%s: futimes", to_name);
|
|
|
|
}
|
|
|
|
|
1994-11-14 07:57:16 +03:00
|
|
|
(void)close(to_fd);
|
1999-02-04 14:56:48 +03:00
|
|
|
|
|
|
|
if (dorename)
|
|
|
|
if (rename(to_name, oto_name) == -1)
|
|
|
|
err(1, "%s: rename", to_name);
|
|
|
|
|
1994-11-14 07:57:16 +03:00
|
|
|
if (!docopy && !devnull && unlink(from_name))
|
|
|
|
err(1, "%s", from_name);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copy --
|
|
|
|
* copy from one file to another
|
|
|
|
*/
|
1994-11-14 07:57:16 +03:00
|
|
|
void
|
|
|
|
copy(from_fd, from_name, to_fd, to_name, size)
|
1997-10-19 18:42:36 +04:00
|
|
|
int from_fd, to_fd;
|
1993-03-21 12:45:37 +03:00
|
|
|
char *from_name, *to_name;
|
1994-11-14 07:57:16 +03:00
|
|
|
off_t size;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1999-06-26 04:41:39 +04:00
|
|
|
ssize_t nr, nw;
|
1994-11-14 07:57:16 +03:00
|
|
|
int serrno;
|
1997-03-10 20:29:05 +03:00
|
|
|
char *p;
|
|
|
|
char buf[MAXBSIZE];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-11-14 07:57:16 +03:00
|
|
|
/*
|
1998-10-08 06:12:51 +04:00
|
|
|
* There's no reason to do anything other than close the file
|
|
|
|
* now if it's empty, so let's not bother.
|
1994-11-14 07:57:16 +03:00
|
|
|
*/
|
1998-10-08 06:12:51 +04:00
|
|
|
if (size > 0) {
|
|
|
|
/*
|
|
|
|
* Mmap and write if less than 8M (the limit is so we don't totally
|
|
|
|
* trash memory on big files). This is really a minor hack, but it
|
|
|
|
* wins some CPU back.
|
|
|
|
*/
|
|
|
|
if (size <= 8 * 1048576) {
|
|
|
|
if ((p = mmap(NULL, (size_t)size, PROT_READ,
|
1999-06-26 04:41:39 +04:00
|
|
|
MAP_FILE|MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) {
|
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
|
|
|
errx(1, "%s: %s", from_name, strerror(serrno));
|
|
|
|
}
|
1999-08-01 09:02:06 +04:00
|
|
|
if (madvise(p, (size_t)size, MADV_SEQUENTIAL) == -1)
|
|
|
|
warnx("madvise: %s", strerror(errno));
|
|
|
|
|
1999-06-26 04:41:39 +04:00
|
|
|
if (write(to_fd, p, size) != size) {
|
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
|
|
|
errx(1, "%s: %s",
|
|
|
|
to_name, strerror(serrno));
|
|
|
|
}
|
1998-10-08 06:12:51 +04:00
|
|
|
} else {
|
1999-06-26 04:41:39 +04:00
|
|
|
while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
|
1998-10-08 06:12:51 +04:00
|
|
|
if ((nw = write(to_fd, buf, nr)) != nr) {
|
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
|
|
|
errx(1, "%s: %s",
|
|
|
|
to_name, strerror(nw > 0 ? EIO : serrno));
|
|
|
|
}
|
1999-06-26 04:41:39 +04:00
|
|
|
}
|
1998-10-08 06:12:51 +04:00
|
|
|
if (nr != 0) {
|
1994-11-14 07:57:16 +03:00
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
1998-10-08 06:12:51 +04:00
|
|
|
errx(1, "%s: %s", from_name, strerror(serrno));
|
1994-11-14 07:57:16 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* strip --
|
|
|
|
* use strip(1) to strip the target file
|
|
|
|
*/
|
1994-11-14 07:57:16 +03:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
strip(to_name)
|
|
|
|
char *to_name;
|
|
|
|
{
|
1994-11-14 07:57:16 +03:00
|
|
|
int serrno, status;
|
1997-04-17 11:56:55 +04:00
|
|
|
char *stripprog;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
switch (vfork()) {
|
|
|
|
case -1:
|
1994-11-14 07:57:16 +03:00
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
1997-10-28 02:16:34 +03:00
|
|
|
errx(1, "vfork: %s", strerror(serrno));
|
1993-03-21 12:45:37 +03:00
|
|
|
case 0:
|
1997-04-17 11:56:55 +04:00
|
|
|
stripprog = getenv("STRIP");
|
|
|
|
if (stripprog == NULL)
|
|
|
|
stripprog = _PATH_STRIP;
|
1998-10-08 06:12:51 +04:00
|
|
|
|
|
|
|
if (stripArgs) {
|
|
|
|
/* build up a command line and let /bin/sh parse the arguments */
|
|
|
|
char* cmd = (char*)malloc(sizeof(char)*
|
|
|
|
(3+strlen(stripprog)+
|
|
|
|
strlen(stripArgs)+
|
|
|
|
strlen(to_name)));
|
|
|
|
|
|
|
|
sprintf(cmd, "%s %s %s", stripprog, stripArgs, to_name);
|
|
|
|
|
|
|
|
execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
|
|
|
|
} else
|
|
|
|
execl(stripprog, "strip", to_name, NULL);
|
|
|
|
|
1997-10-28 02:16:34 +03:00
|
|
|
warn("%s", stripprog);
|
|
|
|
_exit(1);
|
1993-03-21 12:45:37 +03:00
|
|
|
default:
|
|
|
|
if (wait(&status) == -1 || status)
|
1994-11-14 07:57:16 +03:00
|
|
|
(void)unlink(to_name);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-26 04:34:25 +03:00
|
|
|
/*
|
|
|
|
* backup file "to_name" to to_name.suffix
|
|
|
|
* if suffix contains a "%", it's taken as a printf(3) pattern
|
|
|
|
* used for a numbered backup.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
backup(to_name)
|
|
|
|
const char *to_name;
|
|
|
|
{
|
|
|
|
char backup[FILENAME_MAX];
|
|
|
|
|
1999-03-29 21:01:49 +04:00
|
|
|
if (numberedbackup) {
|
1999-01-26 04:34:25 +03:00
|
|
|
/* Do numbered backup */
|
|
|
|
int cnt;
|
|
|
|
char suffix_expanded[FILENAME_MAX];
|
|
|
|
|
|
|
|
cnt=0;
|
|
|
|
do {
|
|
|
|
(void)snprintf(suffix_expanded, FILENAME_MAX, suffix, cnt);
|
|
|
|
(void)snprintf(backup, FILENAME_MAX, "%s%s",
|
|
|
|
to_name, suffix_expanded);
|
|
|
|
cnt++;
|
|
|
|
} while (access(backup, F_OK)==0);
|
|
|
|
} else {
|
|
|
|
/* Do simple backup */
|
|
|
|
(void)snprintf(backup, FILENAME_MAX, "%s%s", to_name, suffix);
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)rename(to_name, backup);
|
|
|
|
}
|
|
|
|
|
1993-06-11 22:30:46 +04:00
|
|
|
/*
|
1994-11-14 07:57:16 +03:00
|
|
|
* install_dir --
|
1993-06-11 22:30:46 +04:00
|
|
|
* build directory heirarchy
|
|
|
|
*/
|
1994-12-20 04:24:38 +03:00
|
|
|
void
|
1994-11-14 07:57:16 +03:00
|
|
|
install_dir(path)
|
1993-06-11 22:30:46 +04:00
|
|
|
char *path;
|
|
|
|
{
|
1997-10-19 18:42:36 +04:00
|
|
|
char *p;
|
1993-06-11 22:30:46 +04:00
|
|
|
struct stat sb;
|
1994-10-03 00:32:31 +03:00
|
|
|
int ch;
|
1993-06-11 22:30:46 +04:00
|
|
|
|
1994-10-03 00:32:31 +03:00
|
|
|
for (p = path;; ++p)
|
|
|
|
if (!*p || (p != path && *p == '/')) {
|
1993-06-11 22:30:46 +04:00
|
|
|
ch = *p;
|
|
|
|
*p = '\0';
|
|
|
|
if (stat(path, &sb)) {
|
|
|
|
if (errno != ENOENT || mkdir(path, 0777) < 0) {
|
1994-12-20 04:24:38 +03:00
|
|
|
err(1, "%s", path);
|
|
|
|
/* NOTREACHED */
|
1993-06-11 22:30:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(*p = ch))
|
1994-10-03 00:32:31 +03:00
|
|
|
break;
|
1993-06-11 22:30:46 +04:00
|
|
|
}
|
|
|
|
|
1995-08-10 08:20:57 +04:00
|
|
|
if (((gid != -1 || uid != -1) && chown(path, uid, gid)) ||
|
1993-06-11 22:30:46 +04:00
|
|
|
chmod(path, mode)) {
|
1994-11-14 07:57:16 +03:00
|
|
|
warn("%s", path);
|
1993-06-11 22:30:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* usage --
|
|
|
|
* print a usage message and die
|
|
|
|
*/
|
1994-11-14 07:57:16 +03:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
usage()
|
|
|
|
{
|
1993-06-11 22:30:46 +04:00
|
|
|
(void)fprintf(stderr, "\
|
1999-08-01 09:02:06 +04:00
|
|
|
usage: install [-Ubcps] [-B suffix] [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 file2\n\
|
|
|
|
install [-Ubcps] [-B suffix] [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 ... fileN directory\n\
|
|
|
|
install [-Up] -d [-m mode] [-o owner] [-g group] directory ...\n");
|
1994-12-19 01:05:40 +03:00
|
|
|
exit(1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|