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
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.
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 */
55 #include <sys/types.h>
76 #if !defined(S_ISLNK) && defined(S_IFLNK)
77 #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK)
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
);
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
), "");
100 mkdirs(char *path
, mode_t mode
)
107 /* strip trailing "/." */
109 if(l
> 1 && path
[l
- 1] == '.' && path
[l
- 2] == '/')
112 while (*path
== '/' && path
[1] == '/')
114 while ((cp
= strrchr(path
, '/')) && cp
[1] == '\0')
116 if (cp
&& cp
!= path
) {
118 if ((lstat(path
, &sb
) < 0 || !S_ISDIR(sb
.st_mode
)) &&
119 mkdirs(path
, mode
) < 0) {
125 res
= mkdir(path
, mode
);
126 if ((res
!= 0) && (errno
== EEXIST
))
139 pw
= getpwnam(owner
);
142 uid
= strtol(owner
, &cp
, 0);
143 if (uid
== 0 && cp
== owner
)
144 fail("cannot find uid for %s", owner
);
155 gr
= getgrnam(group
);
158 gid
= strtol(group
, &cp
, 0);
159 if (gid
== 0 && cp
== group
)
160 fail("cannot find gid for %s", group
);
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
;
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);
182 fail("cannot create %s", toname
);
185 while ((cc
= read(fromfd
, bp
, sizeof buf
)) > 0)
187 while ((wc
= write(tofd
, bp
, (unsigned int)cc
)) > 0)
194 fail("cannot write to %s", toname
);
197 fail("cannot read from %s", name
);
199 if (ftruncate(tofd
, sb
.st_size
) < 0)
200 fail("cannot truncate %s", toname
);
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
);
210 if (fchmod(tofd
, mode
) < 0)
212 if (chmod(toname
, mode
) < 0)
214 fail("cannot change mode of %s", toname
);
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. */
221 fail("cannot write to %s", toname
);
224 if (chmod(toname
, (mode
& (S_IREAD
| S_IWRITE
))) < 0)
225 fail("cannot change mode of %s", toname
);
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
);
237 copydir( char *from
, char *to
, mode_t mode
, char *group
, char *owner
,
238 int dotimes
, uid_t uid
, gid_t gid
)
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
);
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)
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
);
272 copyfile( direntry
, destentry
, mode
, group
, owner
, dotimes
, uid
, gid
);
281 main(int argc
, char **argv
)
283 int onlydir
, dodir
, dolink
, dorelsymlink
, dotimes
, opt
, len
, lplen
, tdlen
, bnlen
, exists
, fromfd
, tofd
, cc
, wc
;
285 char *linkprefix
, *owner
, *group
, *cp
, *cwd
, *todir
, *toname
, *name
, *base
, *linkname
, *bp
, buf
[BUFSIZ
];
288 struct stat sb
, tosb
, fromsb
;
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
) {
311 lplen
= strlen(linkprefix
);
315 dolink
= dorelsymlink
= 1;
318 mode
= strtoul(optarg
, &cp
, 8);
319 if (mode
== 0 && cp
== optarg
)
338 if (argc
< 2 - onlydir
)
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
);
351 #ifndef GETCWD_CANT_MALLOC
352 cwd
= getcwd(0, PATH_MAX
);
354 cwd
= malloc(PATH_MAX
+ 1);
355 cwd
= getcwd(cwd
, PATH_MAX
);
358 cwd
= malloc(PATH_MAX
+ 1);
365 #ifndef GETCWD_CANT_MALLOC
366 todir
= getcwd(0, PATH_MAX
);
368 todir
= malloc(PATH_MAX
+ 1);
369 todir
= getcwd(todir
, PATH_MAX
);
372 todir
= malloc(PATH_MAX
+ 1);
373 todir
= getwd(todir
);
375 tdlen
= strlen(todir
);
377 tdlen
= strlen(todir
);
379 uid
= owner
? touid(owner
) : (uid_t
)(-1);
380 gid
= group
? togid(group
) : (gid_t
)(-1);
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);
392 /* -d means create a directory, always */
393 if (exists
&& !S_ISDIR(tosb
.st_mode
)) {
394 (void) unlink(toname
);
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
);
402 if (access(name
, R_OK
) != 0) {
403 fail("cannot access %s", name
);
406 /* source is absolute pathname, link to it directly */
410 /* -L implies -l and prefixes names with a $cwd arg. */
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
);
419 /* todir is absolute: skip over common prefix. */
420 lplen
= relatepaths(todir
, cwd
, linkname
);
421 strcpy(linkname
+ lplen
, name
);
423 /* todir is named by a relative path: reverse it. */
424 reversepath(todir
, name
, len
, linkname
);
428 len
= strlen(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
);
442 if (!exists
&& symlink(name
, toname
) < 0)
443 fail("cannot make symbolic link %s", toname
);
445 if ((owner
|| group
) && lchown(toname
, uid
, gid
) < 0)
446 fail("cannot change owner of %s", toname
);
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
);
463 copyfile(name
, toname
, mode
, group
, owner
, dotimes
, uid
, gid
);