Bug 550643 - Minor improvements to parsemark.py. r=jorendorff.
[mozilla-central.git] / nsprpub / config / nsinstall.c
blobf18e1166ca4a1e7824755aaf78c29716403e4e4f
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 ** Netscape portable install command.
41 ** Brendan Eich, 7/20/95
43 #include <stdio.h> /* OSF/1 requires this before grp.h, so put it first */
44 #include <assert.h>
45 #include <fcntl.h>
46 #include <grp.h>
47 #include <pwd.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <utime.h>
52 #include <sys/types.h>
53 #include <sys/stat.h>
54 #include <dirent.h>
55 #include <errno.h>
56 #include <stdarg.h>
57 #ifdef USE_REENTRANT_LIBC
58 #include "libc_r.h"
59 #endif /* USE_REENTRANT_LIBC */
61 #include "pathsub.h"
63 #define HAVE_FCHMOD
65 #if defined(BEOS)
66 #undef HAVE_FCHMOD
67 #endif
70 * Does getcwd() take NULL as the first argument and malloc
71 * the result buffer?
73 #if !defined(DARWIN) && !defined(NEXTSTEP)
74 #define GETCWD_CAN_MALLOC
75 #endif
77 #ifdef NEXTSTEP
78 #include <bsd/libc.h>
81 ** balazs.pataki@sztaki.hu: The getcwd is broken in NEXTSTEP (returns 0),
82 ** when called on a mounted fs. Did anyone notice this? Here's an ugly
83 ** workaround ...
85 #define getcwd(b,s) my_getcwd(b,s)
87 static char *
88 my_getcwd (char *buf, size_t size)
90 FILE *pwd = popen("pwd", "r");
91 char *result = fgets(buf, size, pwd);
93 if (result) {
94 buf[strlen(buf)-1] = '\0';
96 pclose (pwd);
97 return buf;
99 #endif /* NEXTSTEP */
101 #if defined(LINUX) || defined(__GNU__) || defined(__GLIBC__)
102 #include <getopt.h>
103 #endif
105 #if defined(SCO) || defined(UNIXWARE) || defined(SNI) || defined(NCR) || defined(NEC) || defined(NEXTSTEP)
106 #if !defined(S_ISLNK) && defined(S_IFLNK)
107 #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK)
108 #endif
109 #endif
111 #if defined(SNI)
112 extern int fchmod(int fildes, mode_t mode);
113 #endif
115 #ifdef QNX
116 #define d_ino d_stat.st_ino
117 #endif
119 static void
120 usage(void)
122 fprintf(stderr,
123 "usage: %s [-C cwd] [-L linkprefix] [-m mode] [-o owner] [-g group]\n"
124 " %*s [-DdltR] file [file ...] directory\n",
125 program, (int)strlen(program), "");
126 exit(2);
129 static int
130 mkdirs(char *path, mode_t mode)
132 char *cp;
133 struct stat sb;
134 int res;
136 while (*path == '/' && path[1] == '/')
137 path++;
138 for (cp = strrchr(path, '/'); cp && cp != path && cp[-1] == '/'; cp--)
140 if (cp && cp != path) {
141 *cp = '\0';
142 if ((stat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
143 mkdirs(path, mode) < 0) {
144 return -1;
146 *cp = '/';
148 res = mkdir(path, mode);
149 if ((res != 0) && (errno == EEXIST))
150 return 0;
151 else
152 return res;
155 static uid_t
156 touid(char *owner)
158 struct passwd *pw;
159 uid_t uid;
160 char *cp;
162 pw = getpwnam(owner);
163 if (pw)
164 return pw->pw_uid;
165 uid = strtol(owner, &cp, 0);
166 if (uid == 0 && cp == owner)
167 fail("cannot find uid for %s", owner);
168 return uid;
171 static gid_t
172 togid(char *group)
174 struct group *gr;
175 gid_t gid;
176 char *cp;
178 gr = getgrnam(group);
179 if (gr)
180 return gr->gr_gid;
181 gid = strtol(group, &cp, 0);
182 if (gid == 0 && cp == group)
183 fail("cannot find gid for %s", group);
184 return gid;
188 main(int argc, char **argv)
190 int onlydir, dodir, dolink, dorelsymlink, dotimes, opt, len, lplen, tdlen, bnlen, exists, fromfd, tofd, cc, wc;
191 mode_t mode = 0755;
192 char *linkprefix, *owner, *group, *cp, *cwd, *todir, *toname, *name, *base, *linkname, *bp, buf[BUFSIZ];
193 uid_t uid;
194 gid_t gid;
195 struct stat sb, tosb;
196 struct utimbuf utb;
198 program = argv[0];
199 cwd = linkname = linkprefix = owner = group = 0;
200 onlydir = dodir = dolink = dorelsymlink = dotimes = lplen = 0;
202 while ((opt = getopt(argc, argv, "C:DdlL:Rm:o:g:t")) != EOF) {
203 switch (opt) {
204 case 'C':
205 cwd = optarg;
206 break;
207 case 'D':
208 onlydir = 1;
209 break;
210 case 'd':
211 dodir = 1;
212 break;
213 case 'l':
214 dolink = 1;
215 break;
216 case 'L':
217 linkprefix = optarg;
218 lplen = strlen(linkprefix);
219 dolink = 1;
220 break;
221 case 'R':
222 dolink = dorelsymlink = 1;
223 break;
224 case 'm':
225 mode = strtoul(optarg, &cp, 8);
226 if (mode == 0 && cp == optarg)
227 usage();
228 break;
229 case 'o':
230 owner = optarg;
231 break;
232 case 'g':
233 group = optarg;
234 break;
235 case 't':
236 dotimes = 1;
237 break;
238 default:
239 usage();
243 argc -= optind;
244 argv += optind;
245 if (argc < 2 - onlydir)
246 usage();
248 todir = argv[argc-1];
249 if ((stat(todir, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
250 mkdirs(todir, 0777) < 0) {
251 fail("cannot make directory %s", todir);
253 if (onlydir)
254 return 0;
256 if (!cwd) {
257 #ifdef GETCWD_CAN_MALLOC
258 cwd = getcwd(0, PATH_MAX);
259 #else
260 cwd = malloc(PATH_MAX + 1);
261 cwd = getcwd(cwd, PATH_MAX);
262 #endif
264 xchdir(todir);
265 #ifdef GETCWD_CAN_MALLOC
266 todir = getcwd(0, PATH_MAX);
267 #else
268 todir = malloc(PATH_MAX + 1);
269 todir = getcwd(todir, PATH_MAX);
270 #endif
271 tdlen = strlen(todir);
272 xchdir(cwd);
273 tdlen = strlen(todir);
275 uid = owner ? touid(owner) : -1;
276 gid = group ? togid(group) : -1;
278 while (--argc > 0) {
279 name = *argv++;
280 len = strlen(name);
281 base = xbasename(name);
282 bnlen = strlen(base);
283 toname = (char*)xmalloc(tdlen + 1 + bnlen + 1);
284 sprintf(toname, "%s/%s", todir, base);
285 exists = (lstat(toname, &tosb) == 0);
287 if (dodir) {
288 /* -d means create a directory, always */
289 if (exists && !S_ISDIR(tosb.st_mode)) {
290 (void) unlink(toname);
291 exists = 0;
293 if (!exists && mkdir(toname, mode) < 0)
294 fail("cannot make directory %s", toname);
295 if ((owner || group) && chown(toname, uid, gid) < 0)
296 fail("cannot change owner of %s", toname);
297 } else if (dolink) {
298 if (*name == '/') {
299 /* source is absolute pathname, link to it directly */
300 linkname = 0;
301 } else {
302 if (linkprefix) {
303 /* -L implies -l and prefixes names with a $cwd arg. */
304 len += lplen + 1;
305 linkname = (char*)xmalloc(len + 1);
306 sprintf(linkname, "%s/%s", linkprefix, name);
307 } else if (dorelsymlink) {
308 /* Symlink the relative path from todir to source name. */
309 linkname = (char*)xmalloc(PATH_MAX);
311 if (*todir == '/') {
312 /* todir is absolute: skip over common prefix. */
313 lplen = relatepaths(todir, cwd, linkname);
314 strcpy(linkname + lplen, name);
315 } else {
316 /* todir is named by a relative path: reverse it. */
317 reversepath(todir, name, len, linkname);
318 xchdir(cwd);
321 len = strlen(linkname);
323 name = linkname;
326 /* Check for a pre-existing symlink with identical content. */
327 if (exists &&
328 (!S_ISLNK(tosb.st_mode) ||
329 readlink(toname, buf, sizeof buf) != len ||
330 strncmp(buf, name, len) != 0)) {
331 (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
332 exists = 0;
334 if (!exists && symlink(name, toname) < 0)
335 fail("cannot make symbolic link %s", toname);
336 #ifdef HAVE_LCHOWN
337 if ((owner || group) && lchown(toname, uid, gid) < 0)
338 fail("cannot change owner of %s", toname);
339 #endif
341 if (linkname) {
342 free(linkname);
343 linkname = 0;
345 } else {
346 /* Copy from name to toname, which might be the same file. */
347 fromfd = open(name, O_RDONLY);
348 if (fromfd < 0 || fstat(fromfd, &sb) < 0)
349 fail("cannot access %s", name);
350 if (exists && (!S_ISREG(tosb.st_mode) || access(toname, W_OK) < 0))
351 (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
352 tofd = open(toname, O_CREAT | O_WRONLY, 0666);
353 if (tofd < 0)
354 fail("cannot create %s", toname);
356 bp = buf;
357 while ((cc = read(fromfd, bp, sizeof buf)) > 0) {
358 while ((wc = write(tofd, bp, cc)) > 0) {
359 if ((cc -= wc) == 0)
360 break;
361 bp += wc;
363 if (wc < 0)
364 fail("cannot write to %s", toname);
366 if (cc < 0)
367 fail("cannot read from %s", name);
369 if (ftruncate(tofd, sb.st_size) < 0)
370 fail("cannot truncate %s", toname);
371 if (dotimes) {
372 utb.actime = sb.st_atime;
373 utb.modtime = sb.st_mtime;
374 if (utime(toname, &utb) < 0)
375 fail("cannot set times of %s", toname);
377 #ifdef HAVE_FCHMOD
378 if (fchmod(tofd, mode) < 0)
379 #else
380 if (chmod(toname, mode) < 0)
381 #endif
382 fail("cannot change mode of %s", toname);
383 if ((owner || group) && fchown(tofd, uid, gid) < 0)
384 fail("cannot change owner of %s", toname);
386 /* Must check for delayed (NFS) write errors on close. */
387 if (close(tofd) < 0)
388 fail("cannot write to %s", toname);
389 close(fromfd);
392 free(toname);
395 free(cwd);
396 free(todir);
397 return 0;
401 ** Pathname subroutines.
403 ** Brendan Eich, 8/29/95
406 char *program;
408 void
409 fail(char *format, ...)
411 int error;
412 va_list ap;
414 #ifdef USE_REENTRANT_LIBC
415 R_STRERROR_INIT_R();
416 #endif
418 error = errno;
419 fprintf(stderr, "%s: ", program);
420 va_start(ap, format);
421 vfprintf(stderr, format, ap);
422 va_end(ap);
423 if (error)
425 #ifdef USE_REENTRANT_LIBC
426 R_STRERROR_R(errno);
427 fprintf(stderr, ": %s", r_strerror_r);
428 #else
429 fprintf(stderr, ": %s", strerror(errno));
430 #endif
432 putc('\n', stderr);
433 exit(1);
436 char *
437 getcomponent(char *path, char *name)
439 if (*path == '\0')
440 return 0;
441 if (*path == '/') {
442 *name++ = '/';
443 } else {
444 do {
445 *name++ = *path++;
446 } while (*path != '/' && *path != '\0');
448 *name = '\0';
449 while (*path == '/')
450 path++;
451 return path;
454 #ifdef UNIXWARE_READDIR_BUFFER_TOO_SMALL
455 /* Sigh. The static buffer in Unixware's readdir is too small. */
456 struct dirent * readdir(DIR *d)
458 static struct dirent *buf = NULL;
459 #define MAX_PATH_LEN 1024
462 if(buf == NULL)
463 buf = (struct dirent *) malloc(sizeof(struct dirent) + MAX_PATH_LEN)
465 return(readdir_r(d, buf));
467 #endif
469 char *
470 ino2name(ino_t ino, char *dir)
472 DIR *dp;
473 struct dirent *ep;
474 char *name;
476 dp = opendir("..");
477 if (!dp)
478 fail("cannot read parent directory");
479 for (;;) {
480 if (!(ep = readdir(dp)))
481 fail("cannot find current directory");
482 if (ep->d_ino == ino)
483 break;
485 name = xstrdup(ep->d_name);
486 closedir(dp);
487 return name;
490 void *
491 xmalloc(size_t size)
493 void *p = malloc(size);
494 if (!p)
495 fail("cannot allocate %u bytes", size);
496 return p;
499 char *
500 xstrdup(char *s)
502 return strcpy((char*)xmalloc(strlen(s) + 1), s);
505 char *
506 xbasename(char *path)
508 char *cp;
510 while ((cp = strrchr(path, '/')) && cp[1] == '\0')
511 *cp = '\0';
512 if (!cp) return path;
513 return cp + 1;
516 void
517 xchdir(char *dir)
519 if (chdir(dir) < 0)
520 fail("cannot change directory to %s", dir);
524 relatepaths(char *from, char *to, char *outpath)
526 char *cp, *cp2;
527 int len;
528 char buf[NAME_MAX];
530 assert(*from == '/' && *to == '/');
531 for (cp = to, cp2 = from; *cp == *cp2; cp++, cp2++)
532 if (*cp == '\0')
533 break;
534 while (cp[-1] != '/')
535 cp--, cp2--;
536 if (cp - 1 == to) {
537 /* closest common ancestor is /, so use full pathname */
538 len = strlen(strcpy(outpath, to));
539 if (outpath[len] != '/') {
540 outpath[len++] = '/';
541 outpath[len] = '\0';
543 } else {
544 len = 0;
545 while ((cp2 = getcomponent(cp2, buf)) != 0) {
546 strcpy(outpath + len, "../");
547 len += 3;
549 while ((cp = getcomponent(cp, buf)) != 0) {
550 sprintf(outpath + len, "%s/", buf);
551 len += strlen(outpath + len);
554 return len;
557 void
558 reversepath(char *inpath, char *name, int len, char *outpath)
560 char *cp, *cp2;
561 char buf[NAME_MAX];
562 struct stat sb;
564 cp = strcpy(outpath + PATH_MAX - (len + 1), name);
565 cp2 = inpath;
566 while ((cp2 = getcomponent(cp2, buf)) != 0) {
567 if (strcmp(buf, ".") == 0)
568 continue;
569 if (strcmp(buf, "..") == 0) {
570 if (stat(".", &sb) < 0)
571 fail("cannot stat current directory");
572 name = ino2name(sb.st_ino, "..");
573 len = strlen(name);
574 cp -= len + 1;
575 strcpy(cp, name);
576 cp[len] = '/';
577 free(name);
578 xchdir("..");
579 } else {
580 cp -= 3;
581 strncpy(cp, "../", 3);
582 xchdir(buf);
585 strcpy(outpath, cp);