1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 ** Netscape portable install command.
9 ** Brendan Eich, 7/20/95
11 #include <stdio.h> /* OSF/1 requires this before grp.h, so put it first */
20 #include <sys/types.h>
25 #ifdef USE_REENTRANT_LIBC
27 #endif /* USE_REENTRANT_LIBC */
38 * Does getcwd() take NULL as the first argument and malloc
42 #define GETCWD_CAN_MALLOC
45 #if defined(LINUX) || defined(__GNU__) || defined(__GLIBC__)
49 #if defined(SCO) || defined(UNIXWARE)
50 #if !defined(S_ISLNK) && defined(S_IFLNK)
51 #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK)
56 #define d_ino d_stat.st_ino
63 "usage: %s [-C cwd] [-L linkprefix] [-m mode] [-o owner] [-g group]\n"
64 " %*s [-DdltR] file [file ...] directory\n",
65 program, (int)strlen(program), "");
70 mkdirs(char *path, mode_t mode)
76 while (*path == '/' && path[1] == '/')
78 for (cp = strrchr(path, '/'); cp && cp != path && cp[-1] == '/'; cp--)
80 if (cp && cp != path) {
82 if ((stat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
83 mkdirs(path, mode) < 0) {
88 res = mkdir(path, mode);
89 if ((res != 0) && (errno == EEXIST))
102 pw = getpwnam(owner);
105 uid = strtol(owner, &cp, 0);
106 if (uid == 0 && cp == owner)
107 fail("cannot find uid for %s", owner);
118 gr = getgrnam(group);
121 gid = strtol(group, &cp, 0);
122 if (gid == 0 && cp == group)
123 fail("cannot find gid for %s", group);
128 main(int argc, char **argv)
130 int onlydir, dodir, dolink, dorelsymlink, dotimes, opt, len, lplen, tdlen, bnlen, exists, fromfd, tofd, cc, wc;
132 char *linkprefix, *owner, *group, *cp, *cwd, *todir, *toname, *name, *base, *linkname, *bp, buf[BUFSIZ];
135 struct stat sb, tosb;
139 cwd = linkname = linkprefix = owner = group = 0;
140 onlydir = dodir = dolink = dorelsymlink = dotimes = lplen = 0;
142 while ((opt = getopt(argc, argv, "C:DdlL:Rm:o:g:t")) != EOF) {
158 lplen = strlen(linkprefix);
162 dolink = dorelsymlink = 1;
165 mode = strtoul(optarg, &cp, 8);
166 if (mode == 0 && cp == optarg)
185 if (argc < 2 - onlydir)
188 todir = argv[argc-1];
189 if ((stat(todir, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
190 mkdirs(todir, 0777) < 0) {
191 fail("cannot make directory %s", todir);
197 #ifdef GETCWD_CAN_MALLOC
198 cwd = getcwd(0, PATH_MAX);
200 cwd = malloc(PATH_MAX + 1);
201 cwd = getcwd(cwd, PATH_MAX);
205 #ifdef GETCWD_CAN_MALLOC
206 todir = getcwd(0, PATH_MAX);
208 todir = malloc(PATH_MAX + 1);
209 todir = getcwd(todir, PATH_MAX);
211 tdlen = strlen(todir);
213 tdlen = strlen(todir);
215 uid = owner ? touid(owner) : -1;
216 gid = group ? togid(group) : -1;
221 base = xbasename(name);
222 bnlen = strlen(base);
223 toname = (char*)xmalloc(tdlen + 1 + bnlen + 1);
224 sprintf(toname, "%s/%s", todir, base);
225 exists = (lstat(toname, &tosb) == 0);
228 /* -d means create a directory, always */
229 if (exists && !S_ISDIR(tosb.st_mode)) {
230 (void) unlink(toname);
233 if (!exists && mkdir(toname, mode) < 0)
234 fail("cannot make directory %s", toname);
235 if ((owner || group) && chown(toname, uid, gid) < 0)
236 fail("cannot change owner of %s", toname);
239 /* source is absolute pathname, link to it directly */
243 /* -L implies -l and prefixes names with a $cwd arg. */
245 linkname = (char*)xmalloc(len + 1);
246 sprintf(linkname, "%s/%s", linkprefix, name);
247 } else if (dorelsymlink) {
248 /* Symlink the relative path from todir to source name. */
249 linkname = (char*)xmalloc(PATH_MAX);
252 /* todir is absolute: skip over common prefix. */
253 lplen = relatepaths(todir, cwd, linkname);
254 strcpy(linkname + lplen, name);
256 /* todir is named by a relative path: reverse it. */
257 reversepath(todir, name, len, linkname);
261 len = strlen(linkname);
266 /* Check for a pre-existing symlink with identical content. */
268 (!S_ISLNK(tosb.st_mode) ||
269 readlink(toname, buf, sizeof buf) != len ||
270 strncmp(buf, name, len) != 0)) {
271 (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
274 if (!exists && symlink(name, toname) < 0)
275 fail("cannot make symbolic link %s", toname);
277 if ((owner || group) && lchown(toname, uid, gid) < 0)
278 fail("cannot change owner of %s", toname);
286 /* Copy from name to toname, which might be the same file. */
287 fromfd = open(name, O_RDONLY);
288 if (fromfd < 0 || fstat(fromfd, &sb) < 0)
289 fail("cannot access %s", name);
290 if (exists && (!S_ISREG(tosb.st_mode) || access(toname, W_OK) < 0))
291 (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
292 tofd = open(toname, O_CREAT | O_WRONLY, 0666);
294 fail("cannot create %s", toname);
297 while ((cc = read(fromfd, bp, sizeof buf)) > 0) {
298 while ((wc = write(tofd, bp, cc)) > 0) {
304 fail("cannot write to %s", toname);
307 fail("cannot read from %s", name);
309 if (ftruncate(tofd, sb.st_size) < 0)
310 fail("cannot truncate %s", toname);
312 utb.actime = sb.st_atime;
313 utb.modtime = sb.st_mtime;
314 if (utime(toname, &utb) < 0)
315 fail("cannot set times of %s", toname);
318 if (fchmod(tofd, mode) < 0)
320 if (chmod(toname, mode) < 0)
322 fail("cannot change mode of %s", toname);
323 if ((owner || group) && fchown(tofd, uid, gid) < 0)
324 fail("cannot change owner of %s", toname);
326 /* Must check for delayed (NFS) write errors on close. */
328 fail("cannot write to %s", toname);
341 ** Pathname subroutines.
343 ** Brendan Eich, 8/29/95
349 fail(char *format, ...)
354 #ifdef USE_REENTRANT_LIBC
359 fprintf(stderr, "%s: ", program);
360 va_start(ap, format);
361 vfprintf(stderr, format, ap);
365 #ifdef USE_REENTRANT_LIBC
367 fprintf(stderr, ": %s", r_strerror_r);
369 fprintf(stderr, ": %s", strerror(errno));
377 getcomponent(char *path, char *name)
386 } while (*path != '/' && *path != '\0');
394 #ifdef UNIXWARE_READDIR_BUFFER_TOO_SMALL
395 /* Sigh. The static buffer in Unixware's readdir is too small. */
396 struct dirent * readdir(DIR *d)
398 static struct dirent *buf = NULL;
399 #define MAX_PATH_LEN 1024
403 buf = (struct dirent *) malloc(sizeof(struct dirent) + MAX_PATH_LEN)
405 return(readdir_r(d, buf));
410 ino2name(ino_t ino, char *dir)
418 fail("cannot read parent directory");
420 if (!(ep = readdir(dp)))
421 fail("cannot find current directory");
422 if (ep->d_ino == ino)
425 name = xstrdup(ep->d_name);
433 void *p = malloc(size);
435 fail("cannot allocate %u bytes", size);
442 return strcpy((char*)xmalloc(strlen(s) + 1), s);
446 xbasename(char *path)
450 while ((cp = strrchr(path, '/')) && cp[1] == '\0')
452 if (!cp) return path;
460 fail("cannot change directory to %s", dir);
464 relatepaths(char *from, char *to, char *outpath)
470 assert(*from == '/' && *to == '/');
471 for (cp = to, cp2 = from; *cp == *cp2; cp++, cp2++)
474 while (cp[-1] != '/')
477 /* closest common ancestor is /, so use full pathname */
478 len = strlen(strcpy(outpath, to));
479 if (outpath[len] != '/') {
480 outpath[len++] = '/';
485 while ((cp2 = getcomponent(cp2, buf)) != 0) {
486 strcpy(outpath + len, "../");
489 while ((cp = getcomponent(cp, buf)) != 0) {
490 sprintf(outpath + len, "%s/", buf);
491 len += strlen(outpath + len);
498 reversepath(char *inpath, char *name, int len, char *outpath)
504 cp = strcpy(outpath + PATH_MAX - (len + 1), name);
506 while ((cp2 = getcomponent(cp2, buf)) != 0) {
507 if (strcmp(buf, ".") == 0)
509 if (strcmp(buf, "..") == 0) {
510 if (stat(".", &sb) < 0)
511 fail("cannot stat current directory");
512 name = ino2name(sb.st_ino, "..");
521 strncpy(cp, "../", 3);