Backout a74bd5095902, Bug 959405 - Please update the Buri Moz-central, 1.3, 1.2 with...
[gecko.git] / config / nsinstall.c
blob4b53f6bba07c1dcb507e4df0755bda7812cba8c1
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/. */
5 /*
6 ** Netscape portable install command.
7 **
8 ** Brendan Eich, 7/20/95
9 */
10 #include <stdio.h> /* OSF/1 requires this before grp.h, so put it first */
11 #include <assert.h>
12 #include <fcntl.h>
13 #include <errno.h>
14 #include <dirent.h>
15 #include <limits.h>
16 #include <grp.h>
17 #include <pwd.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <utime.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include "pathsub.h"
27 #ifdef HAVE_GETOPT_H
28 #include <getopt.h>
29 #endif
31 #ifdef SUNOS4
32 #include "sunos4.h"
33 #endif
35 #ifdef NEXTSTEP
36 #include <bsd/libc.h>
37 #endif
39 #ifdef __QNX__
40 #include <unix.h>
41 #endif
43 #ifdef NEED_S_ISLNK
44 #if !defined(S_ISLNK) && defined(S_IFLNK)
45 #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK)
46 #endif
47 #endif
49 #ifndef _DIRECTORY_SEPARATOR
50 #define _DIRECTORY_SEPARATOR "/"
51 #endif /* _DIRECTORY_SEPARATOR */
53 #ifdef NEED_FCHMOD_PROTO
54 extern int fchmod(int fildes, mode_t mode);
55 #endif
57 static void
58 usage(void)
60 fprintf(stderr,
61 "usage: %s [-C cwd] [-L linkprefix] [-m mode] [-o owner] [-g group]\n"
62 " %*s [-DdltR] file [file ...] directory\n",
63 program, (int) strlen(program), "");
64 exit(2);
67 static int
68 mkdirs(char *path, mode_t mode)
70 char *cp;
71 struct stat sb;
72 int res;
73 int l;
75 /* strip trailing "/." */
76 l = strlen(path);
77 if(l > 1 && path[l - 1] == '.' && path[l - 2] == '/')
78 path[l - 2] = 0;
80 while (*path == '/' && path[1] == '/')
81 path++;
82 for (cp = strrchr(path, '/'); cp && cp != path && *(cp - 1) == '/'; cp--);
83 if (cp && cp != path) {
84 *cp = '\0';
85 if ((lstat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
86 mkdirs(path, mode) < 0) {
87 return -1;
89 *cp = '/';
92 res = mkdir(path, mode);
93 if ((res != 0) && (errno == EEXIST))
94 return 0;
95 else
96 return res;
99 static uid_t
100 touid(char *owner)
102 struct passwd *pw;
103 uid_t uid;
104 char *cp;
106 pw = getpwnam(owner);
107 if (pw)
108 return pw->pw_uid;
109 uid = strtol(owner, &cp, 0);
110 if (uid == 0 && cp == owner)
111 fail("cannot find uid for %s", owner);
112 return uid;
115 static gid_t
116 togid(char *group)
118 struct group *gr;
119 gid_t gid;
120 char *cp;
122 gr = getgrnam(group);
123 if (gr)
124 return gr->gr_gid;
125 gid = strtol(group, &cp, 0);
126 if (gid == 0 && cp == group)
127 fail("cannot find gid for %s", group);
128 return gid;
131 static void
132 copyfile( char *name, char *toname, mode_t mode, char *group, char *owner,
133 int dotimes, uid_t uid, gid_t gid )
135 int fromfd, tofd = -1, cc, wc, exists;
136 char buf[BUFSIZ], *bp;
137 struct stat sb, tosb;
138 struct utimbuf utb;
140 exists = (lstat(toname, &tosb) == 0);
142 fromfd = open(name, O_RDONLY);
143 if (fromfd < 0 || fstat(fromfd, &sb) < 0)
144 fail("cannot access %s", name);
145 if (exists) {
146 if (S_ISREG(tosb.st_mode)) {
147 /* See if we can open it. This is more reliable than 'access'. */
148 tofd = open(toname, O_CREAT | O_WRONLY, 0666);
150 if (tofd < 0) {
151 (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
154 if (tofd < 0) {
155 tofd = open(toname, O_CREAT | O_WRONLY, 0666);
156 if (tofd < 0)
157 fail("cannot create %s", toname);
160 bp = buf;
161 while ((cc = read(fromfd, bp, sizeof buf)) > 0)
163 while ((wc = write(tofd, bp, (unsigned int)cc)) > 0)
165 if ((cc -= wc) == 0)
166 break;
167 bp += wc;
169 if (wc < 0)
170 fail("cannot write to %s", toname);
172 if (cc < 0)
173 fail("cannot read from %s", name);
175 if (ftruncate(tofd, sb.st_size) < 0)
176 fail("cannot truncate %s", toname);
177 #if !defined(VMS)
178 if (dotimes)
180 utb.actime = sb.st_atime;
181 utb.modtime = sb.st_mtime;
182 if (utime(toname, &utb) < 0)
183 fail("cannot set times of %s", toname);
185 #ifdef HAVE_FCHMOD
186 if (fchmod(tofd, mode) < 0)
187 #else
188 if (chmod(toname, mode) < 0)
189 #endif
190 fail("cannot change mode of %s", toname);
191 #endif
192 if ((owner || group) && fchown(tofd, uid, gid) < 0)
193 fail("cannot change owner of %s", toname);
195 /* Must check for delayed (NFS) write errors on close. */
196 if (close(tofd) < 0)
197 fail("cannot write to %s", toname);
198 close(fromfd);
199 #if defined(VMS)
200 if (chmod(toname, (mode & (S_IREAD | S_IWRITE))) < 0)
201 fail("cannot change mode of %s", toname);
202 if (dotimes)
204 utb.actime = sb.st_atime;
205 utb.modtime = sb.st_mtime;
206 if (utime(toname, &utb) < 0)
207 fail("cannot set times of %s", toname);
209 #endif
212 static void
213 copydir( char *from, char *to, mode_t mode, char *group, char *owner,
214 int dotimes, uid_t uid, gid_t gid)
216 int i;
217 DIR *dir;
218 struct dirent *ep;
219 struct stat sb;
220 char *base, *destdir, *direntry, *destentry;
222 base = xbasename(from);
224 /* create destination directory */
225 destdir = xmalloc((unsigned int)(strlen(to) + 1 + strlen(base) + 1));
226 sprintf(destdir, "%s%s%s", to, _DIRECTORY_SEPARATOR, base);
227 if (mkdirs(destdir, mode) != 0) {
228 fail("cannot make directory %s\n", destdir);
229 free(destdir);
230 return;
233 if (!(dir = opendir(from))) {
234 fail("cannot open directory %s\n", from);
235 free(destdir);
236 return;
239 direntry = xmalloc((unsigned int)PATH_MAX);
240 destentry = xmalloc((unsigned int)PATH_MAX);
242 while ((ep = readdir(dir)))
244 if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
245 continue;
247 sprintf(direntry, "%s/%s", from, ep->d_name);
248 sprintf(destentry, "%s%s%s", destdir, _DIRECTORY_SEPARATOR, ep->d_name);
250 if (stat(direntry, &sb) == 0 && S_ISDIR(sb.st_mode))
251 copydir( direntry, destdir, mode, group, owner, dotimes, uid, gid );
252 else
253 copyfile( direntry, destentry, mode, group, owner, dotimes, uid, gid );
256 free(destdir);
257 free(direntry);
258 free(destentry);
259 closedir(dir);
263 main(int argc, char **argv)
265 int onlydir, dodir, dolink, dorelsymlink, dotimes, opt, len, lplen, tdlen, bnlen, exists, fromfd, tofd, cc, wc;
266 mode_t mode = 0755;
267 char *linkprefix, *owner, *group, *cp, *cwd, *todir, *toname, *name, *base, *linkname, *bp, buf[BUFSIZ];
268 uid_t uid;
269 gid_t gid;
270 struct stat sb, tosb, fromsb;
271 struct utimbuf utb;
273 program = argv[0];
274 cwd = linkname = linkprefix = owner = group = 0;
275 onlydir = dodir = dolink = dorelsymlink = dotimes = lplen = 0;
277 while ((opt = getopt(argc, argv, "C:DdlL:Rm:o:g:t")) != EOF) {
278 switch (opt) {
279 case 'C':
280 cwd = optarg;
281 break;
282 case 'D':
283 onlydir = 1;
284 break;
285 case 'd':
286 dodir = 1;
287 break;
288 case 'L':
289 linkprefix = optarg;
290 lplen = strlen(linkprefix);
291 dolink = 1;
292 break;
293 case 'R':
294 dolink = dorelsymlink = 1;
295 break;
296 case 'm':
297 mode = strtoul(optarg, &cp, 8);
298 if (mode == 0 && cp == optarg)
299 usage();
300 break;
301 case 'o':
302 owner = optarg;
303 break;
304 case 'g':
305 group = optarg;
306 break;
307 case 't':
308 dotimes = 1;
309 break;
310 default:
311 usage();
315 argc -= optind;
316 argv += optind;
317 if (argc < 2 - onlydir)
318 usage();
320 todir = argv[argc-1];
321 if ((stat(todir, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
322 mkdirs(todir, 0777) < 0) {
323 fail("cannot make directory %s", todir);
325 if (onlydir)
326 return 0;
328 if (!cwd) {
329 #ifndef NEEDS_GETCWD
330 #ifndef GETCWD_CANT_MALLOC
331 cwd = getcwd(0, PATH_MAX);
332 #else
333 cwd = malloc(PATH_MAX + 1);
334 cwd = getcwd(cwd, PATH_MAX);
335 #endif
336 #else
337 cwd = malloc(PATH_MAX + 1);
338 cwd = getwd(cwd);
339 #endif
342 xchdir(todir);
343 #ifndef NEEDS_GETCWD
344 #ifndef GETCWD_CANT_MALLOC
345 todir = getcwd(0, PATH_MAX);
346 #else
347 todir = malloc(PATH_MAX + 1);
348 todir = getcwd(todir, PATH_MAX);
349 #endif
350 #else
351 todir = malloc(PATH_MAX + 1);
352 todir = getwd(todir);
353 #endif
354 tdlen = strlen(todir);
355 xchdir(cwd);
356 tdlen = strlen(todir);
358 uid = owner ? touid(owner) : (uid_t)(-1);
359 gid = group ? togid(group) : (gid_t)(-1);
361 while (--argc > 0) {
362 name = *argv++;
363 len = strlen(name);
364 base = xbasename(name);
365 bnlen = strlen(base);
366 toname = xmalloc((unsigned int)(tdlen + 1 + bnlen + 1));
367 sprintf(toname, "%s%s%s", todir, _DIRECTORY_SEPARATOR, base);
368 exists = (lstat(toname, &tosb) == 0);
370 if (dodir) {
371 /* -d means create a directory, always */
372 if (exists && !S_ISDIR(tosb.st_mode)) {
373 (void) unlink(toname);
374 exists = 0;
376 if (!exists && mkdir(toname, mode) < 0)
377 fail("cannot make directory %s", toname);
378 if ((owner || group) && chown(toname, uid, gid) < 0)
379 fail("cannot change owner of %s", toname);
380 } else if (dolink) {
381 if (access(name, R_OK) != 0) {
382 fail("cannot access %s", name);
384 if (*name == '/') {
385 /* source is absolute pathname, link to it directly */
386 linkname = 0;
387 } else {
388 if (linkprefix) {
389 /* -L prefixes names with a $cwd arg. */
390 len += lplen + 1;
391 linkname = xmalloc((unsigned int)(len + 1));
392 sprintf(linkname, "%s/%s", linkprefix, name);
393 } else if (dorelsymlink) {
394 /* Symlink the relative path from todir to source name. */
395 linkname = xmalloc(PATH_MAX);
397 if (*todir == '/') {
398 /* todir is absolute: skip over common prefix. */
399 lplen = relatepaths(todir, cwd, linkname);
400 strcpy(linkname + lplen, name);
401 } else {
402 /* todir is named by a relative path: reverse it. */
403 reversepath(todir, name, len, linkname);
404 xchdir(cwd);
407 len = strlen(linkname);
409 name = linkname;
412 /* Check for a pre-existing symlink with identical content. */
413 if ((exists && (!S_ISLNK(tosb.st_mode) ||
414 readlink(toname, buf, sizeof buf) != len ||
415 strncmp(buf, name, (unsigned int)len) != 0)) ||
416 ((stat(name, &fromsb) == 0) &&
417 (fromsb.st_mtime > tosb.st_mtime))) {
418 (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
419 exists = 0;
421 if (!exists && symlink(name, toname) < 0)
422 fail("cannot make symbolic link %s", toname);
423 #ifdef HAVE_LCHOWN
424 if ((owner || group) && lchown(toname, uid, gid) < 0)
425 fail("cannot change owner of %s", toname);
426 #endif
428 if (linkname) {
429 free(linkname);
430 linkname = 0;
432 } else {
433 /* Copy from name to toname, which might be the same file. */
434 if( stat(name, &sb) == 0 && S_IFDIR & sb.st_mode )
436 /* then is directory: must explicitly create destination dir */
437 /* and manually copy files over */
438 copydir( name, todir, mode, group, owner, dotimes, uid, gid );
440 else
442 copyfile(name, toname, mode, group, owner, dotimes, uid, gid);
446 free(toname);
449 free(cwd);
450 free(todir);
451 return 0;