Bug 346849: Add a "Save Image as..." entry to the context menu for <canvas>, Original...
[mozilla-central.git] / config / nsinstall.c
blobde9efdb2c0e821b08c05273d165c3ee0569ae15f
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
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 of the GNU General Public License Version 2 or later (the "GPL"),
26 * or 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 ***** */
38 ** Netscape portable install command.
40 ** Brendan Eich, 7/20/95
42 #include <stdio.h> /* OSF/1 requires this before grp.h, so put it first */
43 #include <assert.h>
44 #include <fcntl.h>
45 #include <errno.h>
46 #include <dirent.h>
47 #include <limits.h>
48 #include <grp.h>
49 #include <pwd.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <utime.h>
55 #include <sys/types.h>
56 #include <sys/stat.h>
57 #include "pathsub.h"
59 #ifdef HAVE_GETOPT_H
60 #include <getopt.h>
61 #endif
63 #ifdef SUNOS4
64 #include "sunos4.h"
65 #endif
67 #ifdef NEXTSTEP
68 #include <bsd/libc.h>
69 #endif
71 #ifdef __QNX__
72 #include <unix.h>
73 #endif
75 #ifdef NEED_S_ISLNK
76 #if !defined(S_ISLNK) && defined(S_IFLNK)
77 #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK)
78 #endif
79 #endif
81 #ifndef _DIRECTORY_SEPARATOR
82 #define _DIRECTORY_SEPARATOR "/"
83 #endif /* _DIRECTORY_SEPARATOR */
85 #ifdef NEED_FCHMOD_PROTO
86 extern int fchmod(int fildes, mode_t mode);
87 #endif
89 static void
90 usage(void)
92 fprintf(stderr,
93 "usage: %s [-C cwd] [-L linkprefix] [-m mode] [-o owner] [-g group]\n"
94 " %*s [-DdltR] file [file ...] directory\n",
95 program, (int) strlen(program), "");
96 exit(2);
99 static int
100 mkdirs(char *path, mode_t mode)
102 char *cp;
103 struct stat sb;
104 int res;
105 int l;
107 /* strip trailing "/." */
108 l = strlen(path);
109 if(l > 1 && path[l - 1] == '.' && path[l - 2] == '/')
110 path[l - 2] = 0;
112 while (*path == '/' && path[1] == '/')
113 path++;
114 while ((cp = strrchr(path, '/')) && cp[1] == '\0')
115 *cp = '\0';
116 if (cp && cp != path) {
117 *cp = '\0';
118 if ((lstat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
119 mkdirs(path, mode) < 0) {
120 return -1;
122 *cp = '/';
125 res = mkdir(path, mode);
126 if ((res != 0) && (errno == EEXIST))
127 return 0;
128 else
129 return res;
132 static uid_t
133 touid(char *owner)
135 struct passwd *pw;
136 uid_t uid;
137 char *cp;
139 pw = getpwnam(owner);
140 if (pw)
141 return pw->pw_uid;
142 uid = strtol(owner, &cp, 0);
143 if (uid == 0 && cp == owner)
144 fail("cannot find uid for %s", owner);
145 return uid;
148 static gid_t
149 togid(char *group)
151 struct group *gr;
152 gid_t gid;
153 char *cp;
155 gr = getgrnam(group);
156 if (gr)
157 return gr->gr_gid;
158 gid = strtol(group, &cp, 0);
159 if (gid == 0 && cp == group)
160 fail("cannot find gid for %s", group);
161 return gid;
164 static void
165 copyfile( char *name, char *toname, mode_t mode, char *group, char *owner,
166 int dotimes, uid_t uid, gid_t gid )
168 int fromfd, tofd, cc, wc, exists;
169 char buf[BUFSIZ], *bp;
170 struct stat sb, tosb;
171 struct utimbuf utb;
173 exists = (lstat(toname, &tosb) == 0);
175 fromfd = open(name, O_RDONLY);
176 if (fromfd < 0 || fstat(fromfd, &sb) < 0)
177 fail("cannot access %s", name);
178 if (exists && (!S_ISREG(tosb.st_mode) || access(toname, W_OK) < 0))
179 (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
180 tofd = open(toname, O_CREAT | O_WRONLY, 0666);
181 if (tofd < 0)
182 fail("cannot create %s", toname);
184 bp = buf;
185 while ((cc = read(fromfd, bp, sizeof buf)) > 0)
187 while ((wc = write(tofd, bp, (unsigned int)cc)) > 0)
189 if ((cc -= wc) == 0)
190 break;
191 bp += wc;
193 if (wc < 0)
194 fail("cannot write to %s", toname);
196 if (cc < 0)
197 fail("cannot read from %s", name);
199 if (ftruncate(tofd, sb.st_size) < 0)
200 fail("cannot truncate %s", toname);
201 #if !defined(VMS)
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 #ifdef HAVE_FCHMOD
210 if (fchmod(tofd, mode) < 0)
211 #else
212 if (chmod(toname, mode) < 0)
213 #endif
214 fail("cannot change mode of %s", toname);
215 #endif
216 if ((owner || group) && fchown(tofd, uid, gid) < 0)
217 fail("cannot change owner of %s", toname);
219 /* Must check for delayed (NFS) write errors on close. */
220 if (close(tofd) < 0)
221 fail("cannot write to %s", toname);
222 close(fromfd);
223 #if defined(VMS)
224 if (chmod(toname, (mode & (S_IREAD | S_IWRITE))) < 0)
225 fail("cannot change mode of %s", toname);
226 if (dotimes)
228 utb.actime = sb.st_atime;
229 utb.modtime = sb.st_mtime;
230 if (utime(toname, &utb) < 0)
231 fail("cannot set times of %s", toname);
233 #endif
236 static void
237 copydir( char *from, char *to, mode_t mode, char *group, char *owner,
238 int dotimes, uid_t uid, gid_t gid)
240 int i;
241 DIR *dir;
242 struct dirent *ep;
243 struct stat sb;
244 char *base, *destdir, *direntry, *destentry;
246 base = xbasename(from);
248 /* create destination directory */
249 destdir = xmalloc((unsigned int)(strlen(to) + 1 + strlen(base) + 1));
250 sprintf(destdir, "%s%s%s", to, _DIRECTORY_SEPARATOR, base);
251 if (mkdirs(destdir, mode) != 0) {
252 fail("cannot make directory %s\n", destdir);
253 return;
256 dir = opendir(from);
258 direntry = xmalloc((unsigned int)PATH_MAX);
259 destentry = xmalloc((unsigned int)PATH_MAX);
261 while ((ep = readdir(dir)))
263 if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
264 continue;
266 sprintf(direntry, "%s/%s", from, ep->d_name);
267 sprintf(destentry, "%s%s%s", destdir, _DIRECTORY_SEPARATOR, ep->d_name);
269 if (stat(direntry, &sb) == 0 && S_ISDIR(sb.st_mode))
270 copydir( direntry, destdir, mode, group, owner, dotimes, uid, gid );
271 else
272 copyfile( direntry, destentry, mode, group, owner, dotimes, uid, gid );
275 free(direntry);
276 free(destentry);
277 closedir(dir);
281 main(int argc, char **argv)
283 int onlydir, dodir, dolink, dorelsymlink, dotimes, opt, len, lplen, tdlen, bnlen, exists, fromfd, tofd, cc, wc;
284 mode_t mode = 0755;
285 char *linkprefix, *owner, *group, *cp, *cwd, *todir, *toname, *name, *base, *linkname, *bp, buf[BUFSIZ];
286 uid_t uid;
287 gid_t gid;
288 struct stat sb, tosb, fromsb;
289 struct utimbuf utb;
291 program = argv[0];
292 cwd = linkname = linkprefix = owner = group = 0;
293 onlydir = dodir = dolink = dorelsymlink = dotimes = lplen = 0;
295 while ((opt = getopt(argc, argv, "C:DdlL:Rm:o:g:t")) != EOF) {
296 switch (opt) {
297 case 'C':
298 cwd = optarg;
299 break;
300 case 'D':
301 onlydir = 1;
302 break;
303 case 'd':
304 dodir = 1;
305 break;
306 case 'l':
307 dolink = 1;
308 break;
309 case 'L':
310 linkprefix = optarg;
311 lplen = strlen(linkprefix);
312 dolink = 1;
313 break;
314 case 'R':
315 dolink = dorelsymlink = 1;
316 break;
317 case 'm':
318 mode = strtoul(optarg, &cp, 8);
319 if (mode == 0 && cp == optarg)
320 usage();
321 break;
322 case 'o':
323 owner = optarg;
324 break;
325 case 'g':
326 group = optarg;
327 break;
328 case 't':
329 dotimes = 1;
330 break;
331 default:
332 usage();
336 argc -= optind;
337 argv += optind;
338 if (argc < 2 - onlydir)
339 usage();
341 todir = argv[argc-1];
342 if ((stat(todir, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
343 mkdirs(todir, 0777) < 0) {
344 fail("cannot make directory %s", todir);
346 if (onlydir)
347 return 0;
349 if (!cwd) {
350 #ifndef NEEDS_GETCWD
351 #ifndef GETCWD_CANT_MALLOC
352 cwd = getcwd(0, PATH_MAX);
353 #else
354 cwd = malloc(PATH_MAX + 1);
355 cwd = getcwd(cwd, PATH_MAX);
356 #endif
357 #else
358 cwd = malloc(PATH_MAX + 1);
359 cwd = getwd(cwd);
360 #endif
363 xchdir(todir);
364 #ifndef NEEDS_GETCWD
365 #ifndef GETCWD_CANT_MALLOC
366 todir = getcwd(0, PATH_MAX);
367 #else
368 todir = malloc(PATH_MAX + 1);
369 todir = getcwd(todir, PATH_MAX);
370 #endif
371 #else
372 todir = malloc(PATH_MAX + 1);
373 todir = getwd(todir);
374 #endif
375 tdlen = strlen(todir);
376 xchdir(cwd);
377 tdlen = strlen(todir);
379 uid = owner ? touid(owner) : (uid_t)(-1);
380 gid = group ? togid(group) : (gid_t)(-1);
382 while (--argc > 0) {
383 name = *argv++;
384 len = strlen(name);
385 base = xbasename(name);
386 bnlen = strlen(base);
387 toname = xmalloc((unsigned int)(tdlen + 1 + bnlen + 1));
388 sprintf(toname, "%s%s%s", todir, _DIRECTORY_SEPARATOR, base);
389 exists = (lstat(toname, &tosb) == 0);
391 if (dodir) {
392 /* -d means create a directory, always */
393 if (exists && !S_ISDIR(tosb.st_mode)) {
394 (void) unlink(toname);
395 exists = 0;
397 if (!exists && mkdir(toname, mode) < 0)
398 fail("cannot make directory %s", toname);
399 if ((owner || group) && chown(toname, uid, gid) < 0)
400 fail("cannot change owner of %s", toname);
401 } else if (dolink) {
402 if (access(name, R_OK) != 0) {
403 fail("cannot access %s", name);
405 if (*name == '/') {
406 /* source is absolute pathname, link to it directly */
407 linkname = 0;
408 } else {
409 if (linkprefix) {
410 /* -L implies -l and prefixes names with a $cwd arg. */
411 len += lplen + 1;
412 linkname = xmalloc((unsigned int)(len + 1));
413 sprintf(linkname, "%s/%s", linkprefix, name);
414 } else if (dorelsymlink) {
415 /* Symlink the relative path from todir to source name. */
416 linkname = xmalloc(PATH_MAX);
418 if (*todir == '/') {
419 /* todir is absolute: skip over common prefix. */
420 lplen = relatepaths(todir, cwd, linkname);
421 strcpy(linkname + lplen, name);
422 } else {
423 /* todir is named by a relative path: reverse it. */
424 reversepath(todir, name, len, linkname);
425 xchdir(cwd);
428 len = strlen(linkname);
430 name = linkname;
433 /* Check for a pre-existing symlink with identical content. */
434 if ((exists && (!S_ISLNK(tosb.st_mode) ||
435 readlink(toname, buf, sizeof buf) != len ||
436 strncmp(buf, name, (unsigned int)len) != 0)) ||
437 ((stat(name, &fromsb) == 0) &&
438 (fromsb.st_mtime > tosb.st_mtime))) {
439 (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
440 exists = 0;
442 if (!exists && symlink(name, toname) < 0)
443 fail("cannot make symbolic link %s", toname);
444 #ifdef HAVE_LCHOWN
445 if ((owner || group) && lchown(toname, uid, gid) < 0)
446 fail("cannot change owner of %s", toname);
447 #endif
449 if (linkname) {
450 free(linkname);
451 linkname = 0;
453 } else {
454 /* Copy from name to toname, which might be the same file. */
455 if( stat(name, &sb) == 0 && S_IFDIR & sb.st_mode )
457 /* then is directory: must explicitly create destination dir */
458 /* and manually copy files over */
459 copydir( name, todir, mode, group, owner, dotimes, uid, gid );
461 else
463 copyfile(name, toname, mode, group, owner, dotimes, uid, gid);
467 free(toname);
470 free(cwd);
471 free(todir);
472 return 0;