michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: ** Netscape portable install command. michael@0: ** michael@0: ** Brendan Eich, 7/20/95 michael@0: */ michael@0: #include /* OSF/1 requires this before grp.h, so put it first */ michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #ifdef USE_REENTRANT_LIBC michael@0: #include "libc_r.h" michael@0: #endif /* USE_REENTRANT_LIBC */ michael@0: michael@0: #include "pathsub.h" michael@0: michael@0: #define HAVE_FCHMOD michael@0: michael@0: #if defined(BEOS) michael@0: #undef HAVE_FCHMOD michael@0: #endif michael@0: michael@0: /* michael@0: * Does getcwd() take NULL as the first argument and malloc michael@0: * the result buffer? michael@0: */ michael@0: #if !defined(DARWIN) michael@0: #define GETCWD_CAN_MALLOC michael@0: #endif michael@0: michael@0: #if defined(LINUX) || defined(__GNU__) || defined(__GLIBC__) michael@0: #include michael@0: #endif michael@0: michael@0: #if defined(SCO) || defined(UNIXWARE) michael@0: #if !defined(S_ISLNK) && defined(S_IFLNK) michael@0: #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK) michael@0: #endif michael@0: #endif michael@0: michael@0: #ifdef QNX michael@0: #define d_ino d_stat.st_ino michael@0: #endif michael@0: michael@0: static void michael@0: usage(void) michael@0: { michael@0: fprintf(stderr, michael@0: "usage: %s [-C cwd] [-L linkprefix] [-m mode] [-o owner] [-g group]\n" michael@0: " %*s [-DdltR] file [file ...] directory\n", michael@0: program, (int)strlen(program), ""); michael@0: exit(2); michael@0: } michael@0: michael@0: static int michael@0: mkdirs(char *path, mode_t mode) michael@0: { michael@0: char *cp; michael@0: struct stat sb; michael@0: int res; michael@0: michael@0: while (*path == '/' && path[1] == '/') michael@0: path++; michael@0: for (cp = strrchr(path, '/'); cp && cp != path && cp[-1] == '/'; cp--) michael@0: ; michael@0: if (cp && cp != path) { michael@0: *cp = '\0'; michael@0: if ((stat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) && michael@0: mkdirs(path, mode) < 0) { michael@0: return -1; michael@0: } michael@0: *cp = '/'; michael@0: } michael@0: res = mkdir(path, mode); michael@0: if ((res != 0) && (errno == EEXIST)) michael@0: return 0; michael@0: else michael@0: return res; michael@0: } michael@0: michael@0: static uid_t michael@0: touid(char *owner) michael@0: { michael@0: struct passwd *pw; michael@0: uid_t uid; michael@0: char *cp; michael@0: michael@0: pw = getpwnam(owner); michael@0: if (pw) michael@0: return pw->pw_uid; michael@0: uid = strtol(owner, &cp, 0); michael@0: if (uid == 0 && cp == owner) michael@0: fail("cannot find uid for %s", owner); michael@0: return uid; michael@0: } michael@0: michael@0: static gid_t michael@0: togid(char *group) michael@0: { michael@0: struct group *gr; michael@0: gid_t gid; michael@0: char *cp; michael@0: michael@0: gr = getgrnam(group); michael@0: if (gr) michael@0: return gr->gr_gid; michael@0: gid = strtol(group, &cp, 0); michael@0: if (gid == 0 && cp == group) michael@0: fail("cannot find gid for %s", group); michael@0: return gid; michael@0: } michael@0: michael@0: int michael@0: main(int argc, char **argv) michael@0: { michael@0: int onlydir, dodir, dolink, dorelsymlink, dotimes, opt, len, lplen, tdlen, bnlen, exists, fromfd, tofd, cc, wc; michael@0: mode_t mode = 0755; michael@0: char *linkprefix, *owner, *group, *cp, *cwd, *todir, *toname, *name, *base, *linkname, *bp, buf[BUFSIZ]; michael@0: uid_t uid; michael@0: gid_t gid; michael@0: struct stat sb, tosb; michael@0: struct utimbuf utb; michael@0: michael@0: program = argv[0]; michael@0: cwd = linkname = linkprefix = owner = group = 0; michael@0: onlydir = dodir = dolink = dorelsymlink = dotimes = lplen = 0; michael@0: michael@0: while ((opt = getopt(argc, argv, "C:DdlL:Rm:o:g:t")) != EOF) { michael@0: switch (opt) { michael@0: case 'C': michael@0: cwd = optarg; michael@0: break; michael@0: case 'D': michael@0: onlydir = 1; michael@0: break; michael@0: case 'd': michael@0: dodir = 1; michael@0: break; michael@0: case 'l': michael@0: dolink = 1; michael@0: break; michael@0: case 'L': michael@0: linkprefix = optarg; michael@0: lplen = strlen(linkprefix); michael@0: dolink = 1; michael@0: break; michael@0: case 'R': michael@0: dolink = dorelsymlink = 1; michael@0: break; michael@0: case 'm': michael@0: mode = strtoul(optarg, &cp, 8); michael@0: if (mode == 0 && cp == optarg) michael@0: usage(); michael@0: break; michael@0: case 'o': michael@0: owner = optarg; michael@0: break; michael@0: case 'g': michael@0: group = optarg; michael@0: break; michael@0: case 't': michael@0: dotimes = 1; michael@0: break; michael@0: default: michael@0: usage(); michael@0: } michael@0: } michael@0: michael@0: argc -= optind; michael@0: argv += optind; michael@0: if (argc < 2 - onlydir) michael@0: usage(); michael@0: michael@0: todir = argv[argc-1]; michael@0: if ((stat(todir, &sb) < 0 || !S_ISDIR(sb.st_mode)) && michael@0: mkdirs(todir, 0777) < 0) { michael@0: fail("cannot make directory %s", todir); michael@0: } michael@0: if (onlydir) michael@0: return 0; michael@0: michael@0: if (!cwd) { michael@0: #ifdef GETCWD_CAN_MALLOC michael@0: cwd = getcwd(0, PATH_MAX); michael@0: #else michael@0: cwd = malloc(PATH_MAX + 1); michael@0: cwd = getcwd(cwd, PATH_MAX); michael@0: #endif michael@0: } michael@0: xchdir(todir); michael@0: #ifdef GETCWD_CAN_MALLOC michael@0: todir = getcwd(0, PATH_MAX); michael@0: #else michael@0: todir = malloc(PATH_MAX + 1); michael@0: todir = getcwd(todir, PATH_MAX); michael@0: #endif michael@0: xchdir(cwd); michael@0: tdlen = strlen(todir); michael@0: michael@0: uid = owner ? touid(owner) : -1; michael@0: gid = group ? togid(group) : -1; michael@0: michael@0: while (--argc > 0) { michael@0: name = *argv++; michael@0: len = strlen(name); michael@0: base = xbasename(name); michael@0: bnlen = strlen(base); michael@0: toname = (char*)xmalloc(tdlen + 1 + bnlen + 1); michael@0: sprintf(toname, "%s/%s", todir, base); michael@0: exists = (lstat(toname, &tosb) == 0); michael@0: michael@0: if (dodir) { michael@0: /* -d means create a directory, always */ michael@0: if (exists && !S_ISDIR(tosb.st_mode)) { michael@0: (void) unlink(toname); michael@0: exists = 0; michael@0: } michael@0: if (!exists && mkdir(toname, mode) < 0) michael@0: fail("cannot make directory %s", toname); michael@0: if ((owner || group) && chown(toname, uid, gid) < 0) michael@0: fail("cannot change owner of %s", toname); michael@0: } else if (dolink) { michael@0: if (*name == '/') { michael@0: /* source is absolute pathname, link to it directly */ michael@0: linkname = 0; michael@0: } else { michael@0: if (linkprefix) { michael@0: /* -L implies -l and prefixes names with a $cwd arg. */ michael@0: len += lplen + 1; michael@0: linkname = (char*)xmalloc(len + 1); michael@0: sprintf(linkname, "%s/%s", linkprefix, name); michael@0: } else if (dorelsymlink) { michael@0: /* Symlink the relative path from todir to source name. */ michael@0: linkname = (char*)xmalloc(PATH_MAX); michael@0: michael@0: if (*todir == '/') { michael@0: /* todir is absolute: skip over common prefix. */ michael@0: lplen = relatepaths(todir, cwd, linkname); michael@0: strcpy(linkname + lplen, name); michael@0: } else { michael@0: /* todir is named by a relative path: reverse it. */ michael@0: reversepath(todir, name, len, linkname); michael@0: xchdir(cwd); michael@0: } michael@0: michael@0: len = strlen(linkname); michael@0: } michael@0: name = linkname; michael@0: } michael@0: michael@0: /* Check for a pre-existing symlink with identical content. */ michael@0: if (exists && michael@0: (!S_ISLNK(tosb.st_mode) || michael@0: readlink(toname, buf, sizeof buf) != len || michael@0: strncmp(buf, name, len) != 0)) { michael@0: (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname); michael@0: exists = 0; michael@0: } michael@0: if (!exists && symlink(name, toname) < 0) michael@0: fail("cannot make symbolic link %s", toname); michael@0: #ifdef HAVE_LCHOWN michael@0: if ((owner || group) && lchown(toname, uid, gid) < 0) michael@0: fail("cannot change owner of %s", toname); michael@0: #endif michael@0: michael@0: if (linkname) { michael@0: free(linkname); michael@0: linkname = 0; michael@0: } michael@0: } else { michael@0: /* Copy from name to toname, which might be the same file. */ michael@0: fromfd = open(name, O_RDONLY); michael@0: if (fromfd < 0 || fstat(fromfd, &sb) < 0) michael@0: fail("cannot access %s", name); michael@0: if (exists && (!S_ISREG(tosb.st_mode) || access(toname, W_OK) < 0)) michael@0: (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname); michael@0: tofd = open(toname, O_CREAT | O_WRONLY, 0666); michael@0: if (tofd < 0) michael@0: fail("cannot create %s", toname); michael@0: michael@0: bp = buf; michael@0: while ((cc = read(fromfd, bp, sizeof buf)) > 0) { michael@0: while ((wc = write(tofd, bp, cc)) > 0) { michael@0: if ((cc -= wc) == 0) michael@0: break; michael@0: bp += wc; michael@0: } michael@0: if (wc < 0) michael@0: fail("cannot write to %s", toname); michael@0: } michael@0: if (cc < 0) michael@0: fail("cannot read from %s", name); michael@0: michael@0: if (ftruncate(tofd, sb.st_size) < 0) michael@0: fail("cannot truncate %s", toname); michael@0: if (dotimes) { michael@0: utb.actime = sb.st_atime; michael@0: utb.modtime = sb.st_mtime; michael@0: if (utime(toname, &utb) < 0) michael@0: fail("cannot set times of %s", toname); michael@0: } michael@0: #ifdef HAVE_FCHMOD michael@0: if (fchmod(tofd, mode) < 0) michael@0: #else michael@0: if (chmod(toname, mode) < 0) michael@0: #endif michael@0: fail("cannot change mode of %s", toname); michael@0: if ((owner || group) && fchown(tofd, uid, gid) < 0) michael@0: fail("cannot change owner of %s", toname); michael@0: michael@0: /* Must check for delayed (NFS) write errors on close. */ michael@0: if (close(tofd) < 0) michael@0: fail("cannot write to %s", toname); michael@0: close(fromfd); michael@0: } michael@0: michael@0: free(toname); michael@0: } michael@0: michael@0: free(cwd); michael@0: free(todir); michael@0: return 0; michael@0: } michael@0: michael@0: /* michael@0: ** Pathname subroutines. michael@0: ** michael@0: ** Brendan Eich, 8/29/95 michael@0: */ michael@0: michael@0: char *program; michael@0: michael@0: void michael@0: fail(char *format, ...) michael@0: { michael@0: int error; michael@0: va_list ap; michael@0: michael@0: #ifdef USE_REENTRANT_LIBC michael@0: R_STRERROR_INIT_R(); michael@0: #endif michael@0: michael@0: error = errno; michael@0: fprintf(stderr, "%s: ", program); michael@0: va_start(ap, format); michael@0: vfprintf(stderr, format, ap); michael@0: va_end(ap); michael@0: if (error) michael@0: michael@0: #ifdef USE_REENTRANT_LIBC michael@0: R_STRERROR_R(errno); michael@0: fprintf(stderr, ": %s", r_strerror_r); michael@0: #else michael@0: fprintf(stderr, ": %s", strerror(errno)); michael@0: #endif michael@0: michael@0: putc('\n', stderr); michael@0: exit(1); michael@0: } michael@0: michael@0: char * michael@0: getcomponent(char *path, char *name) michael@0: { michael@0: if (*path == '\0') michael@0: return 0; michael@0: if (*path == '/') { michael@0: *name++ = '/'; michael@0: } else { michael@0: do { michael@0: *name++ = *path++; michael@0: } while (*path != '/' && *path != '\0'); michael@0: } michael@0: *name = '\0'; michael@0: while (*path == '/') michael@0: path++; michael@0: return path; michael@0: } michael@0: michael@0: #ifdef UNIXWARE_READDIR_BUFFER_TOO_SMALL michael@0: /* Sigh. The static buffer in Unixware's readdir is too small. */ michael@0: struct dirent * readdir(DIR *d) michael@0: { michael@0: static struct dirent *buf = NULL; michael@0: #define MAX_PATH_LEN 1024 michael@0: michael@0: michael@0: if(buf == NULL) michael@0: buf = (struct dirent *) malloc(sizeof(struct dirent) + MAX_PATH_LEN) michael@0: ; michael@0: return(readdir_r(d, buf)); michael@0: } michael@0: #endif michael@0: michael@0: char * michael@0: ino2name(ino_t ino, char *dir) michael@0: { michael@0: DIR *dp; michael@0: struct dirent *ep; michael@0: char *name; michael@0: michael@0: dp = opendir(".."); michael@0: if (!dp) michael@0: fail("cannot read parent directory"); michael@0: for (;;) { michael@0: if (!(ep = readdir(dp))) michael@0: fail("cannot find current directory"); michael@0: if (ep->d_ino == ino) michael@0: break; michael@0: } michael@0: name = xstrdup(ep->d_name); michael@0: closedir(dp); michael@0: return name; michael@0: } michael@0: michael@0: void * michael@0: xmalloc(size_t size) michael@0: { michael@0: void *p = malloc(size); michael@0: if (!p) michael@0: fail("cannot allocate %u bytes", size); michael@0: return p; michael@0: } michael@0: michael@0: char * michael@0: xstrdup(char *s) michael@0: { michael@0: return strcpy((char*)xmalloc(strlen(s) + 1), s); michael@0: } michael@0: michael@0: char * michael@0: xbasename(char *path) michael@0: { michael@0: char *cp; michael@0: michael@0: while ((cp = strrchr(path, '/')) && cp[1] == '\0') michael@0: *cp = '\0'; michael@0: if (!cp) return path; michael@0: return cp + 1; michael@0: } michael@0: michael@0: void michael@0: xchdir(char *dir) michael@0: { michael@0: if (chdir(dir) < 0) michael@0: fail("cannot change directory to %s", dir); michael@0: } michael@0: michael@0: int michael@0: relatepaths(char *from, char *to, char *outpath) michael@0: { michael@0: char *cp, *cp2; michael@0: int len; michael@0: char buf[NAME_MAX]; michael@0: michael@0: assert(*from == '/' && *to == '/'); michael@0: for (cp = to, cp2 = from; *cp == *cp2; cp++, cp2++) michael@0: if (*cp == '\0') michael@0: break; michael@0: while (cp[-1] != '/') michael@0: cp--, cp2--; michael@0: if (cp - 1 == to) { michael@0: /* closest common ancestor is /, so use full pathname */ michael@0: len = strlen(strcpy(outpath, to)); michael@0: if (outpath[len] != '/') { michael@0: outpath[len++] = '/'; michael@0: outpath[len] = '\0'; michael@0: } michael@0: } else { michael@0: len = 0; michael@0: while ((cp2 = getcomponent(cp2, buf)) != 0) { michael@0: strcpy(outpath + len, "../"); michael@0: len += 3; michael@0: } michael@0: while ((cp = getcomponent(cp, buf)) != 0) { michael@0: sprintf(outpath + len, "%s/", buf); michael@0: len += strlen(outpath + len); michael@0: } michael@0: } michael@0: return len; michael@0: } michael@0: michael@0: void michael@0: reversepath(char *inpath, char *name, int len, char *outpath) michael@0: { michael@0: char *cp, *cp2; michael@0: char buf[NAME_MAX]; michael@0: struct stat sb; michael@0: michael@0: cp = strcpy(outpath + PATH_MAX - (len + 1), name); michael@0: cp2 = inpath; michael@0: while ((cp2 = getcomponent(cp2, buf)) != 0) { michael@0: if (strcmp(buf, ".") == 0) michael@0: continue; michael@0: if (strcmp(buf, "..") == 0) { michael@0: if (stat(".", &sb) < 0) michael@0: fail("cannot stat current directory"); michael@0: name = ino2name(sb.st_ino, ".."); michael@0: len = strlen(name); michael@0: cp -= len + 1; michael@0: strcpy(cp, name); michael@0: cp[len] = '/'; michael@0: free(name); michael@0: xchdir(".."); michael@0: } else { michael@0: cp -= 3; michael@0: strncpy(cp, "../", 3); michael@0: xchdir(buf); michael@0: } michael@0: } michael@0: strcpy(outpath, cp); michael@0: }