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 for (cp
= strrchr(path
, '/'); cp
&& cp
!= path
&& *(cp
- 1) == '/'; cp
--);
115 if (cp
&& cp
!= path
) {
117 if ((lstat(path
, &sb
) < 0 || !S_ISDIR(sb
.st_mode
)) &&
118 mkdirs(path
, mode
) < 0) {
124 res
= mkdir(path
, mode
);
125 if ((res
!= 0) && (errno
== EEXIST
))
138 pw
= getpwnam(owner
);
141 uid
= strtol(owner
, &cp
, 0);
142 if (uid
== 0 && cp
== owner
)
143 fail("cannot find uid for %s", owner
);
154 gr
= getgrnam(group
);
157 gid
= strtol(group
, &cp
, 0);
158 if (gid
== 0 && cp
== group
)
159 fail("cannot find gid for %s", group
);
164 copyfile( char *name
, char *toname
, mode_t mode
, char *group
, char *owner
,
165 int dotimes
, uid_t uid
, gid_t gid
)
167 int fromfd
, tofd
= -1, cc
, wc
, exists
;
168 char buf
[BUFSIZ
], *bp
;
169 struct stat sb
, tosb
;
172 exists
= (lstat(toname
, &tosb
) == 0);
174 fromfd
= open(name
, O_RDONLY
);
175 if (fromfd
< 0 || fstat(fromfd
, &sb
) < 0)
176 fail("cannot access %s", name
);
178 if (S_ISREG(tosb
.st_mode
)) {
179 /* See if we can open it. This is more reliable than 'access'. */
180 tofd
= open(toname
, O_CREAT
| O_WRONLY
, 0666);
183 (void) (S_ISDIR(tosb
.st_mode
) ? rmdir
: unlink
)(toname
);
187 tofd
= open(toname
, O_CREAT
| O_WRONLY
, 0666);
189 fail("cannot create %s", toname
);
193 while ((cc
= read(fromfd
, bp
, sizeof buf
)) > 0)
195 while ((wc
= write(tofd
, bp
, (unsigned int)cc
)) > 0)
202 fail("cannot write to %s", toname
);
205 fail("cannot read from %s", name
);
207 if (ftruncate(tofd
, sb
.st_size
) < 0)
208 fail("cannot truncate %s", toname
);
212 utb
.actime
= sb
.st_atime
;
213 utb
.modtime
= sb
.st_mtime
;
214 if (utime(toname
, &utb
) < 0)
215 fail("cannot set times of %s", toname
);
218 if (fchmod(tofd
, mode
) < 0)
220 if (chmod(toname
, mode
) < 0)
222 fail("cannot change mode of %s", toname
);
224 if ((owner
|| group
) && fchown(tofd
, uid
, gid
) < 0)
225 fail("cannot change owner of %s", toname
);
227 /* Must check for delayed (NFS) write errors on close. */
229 fail("cannot write to %s", toname
);
232 if (chmod(toname
, (mode
& (S_IREAD
| S_IWRITE
))) < 0)
233 fail("cannot change mode of %s", toname
);
236 utb
.actime
= sb
.st_atime
;
237 utb
.modtime
= sb
.st_mtime
;
238 if (utime(toname
, &utb
) < 0)
239 fail("cannot set times of %s", toname
);
245 copydir( char *from
, char *to
, mode_t mode
, char *group
, char *owner
,
246 int dotimes
, uid_t uid
, gid_t gid
)
252 char *base
, *destdir
, *direntry
, *destentry
;
254 base
= xbasename(from
);
256 /* create destination directory */
257 destdir
= xmalloc((unsigned int)(strlen(to
) + 1 + strlen(base
) + 1));
258 sprintf(destdir
, "%s%s%s", to
, _DIRECTORY_SEPARATOR
, base
);
259 if (mkdirs(destdir
, mode
) != 0) {
260 fail("cannot make directory %s\n", destdir
);
266 direntry
= xmalloc((unsigned int)PATH_MAX
);
267 destentry
= xmalloc((unsigned int)PATH_MAX
);
269 while ((ep
= readdir(dir
)))
271 if (strcmp(ep
->d_name
, ".") == 0 || strcmp(ep
->d_name
, "..") == 0)
274 sprintf(direntry
, "%s/%s", from
, ep
->d_name
);
275 sprintf(destentry
, "%s%s%s", destdir
, _DIRECTORY_SEPARATOR
, ep
->d_name
);
277 if (stat(direntry
, &sb
) == 0 && S_ISDIR(sb
.st_mode
))
278 copydir( direntry
, destdir
, mode
, group
, owner
, dotimes
, uid
, gid
);
280 copyfile( direntry
, destentry
, mode
, group
, owner
, dotimes
, uid
, gid
);
289 main(int argc
, char **argv
)
291 int onlydir
, dodir
, dolink
, dorelsymlink
, dotimes
, opt
, len
, lplen
, tdlen
, bnlen
, exists
, fromfd
, tofd
, cc
, wc
;
293 char *linkprefix
, *owner
, *group
, *cp
, *cwd
, *todir
, *toname
, *name
, *base
, *linkname
, *bp
, buf
[BUFSIZ
];
296 struct stat sb
, tosb
, fromsb
;
300 cwd
= linkname
= linkprefix
= owner
= group
= 0;
301 onlydir
= dodir
= dolink
= dorelsymlink
= dotimes
= lplen
= 0;
303 while ((opt
= getopt(argc
, argv
, "C:DdlL:Rm:o:g:t")) != EOF
) {
319 lplen
= strlen(linkprefix
);
323 dolink
= dorelsymlink
= 1;
326 mode
= strtoul(optarg
, &cp
, 8);
327 if (mode
== 0 && cp
== optarg
)
346 if (argc
< 2 - onlydir
)
349 todir
= argv
[argc
-1];
350 if ((stat(todir
, &sb
) < 0 || !S_ISDIR(sb
.st_mode
)) &&
351 mkdirs(todir
, 0777) < 0) {
352 fail("cannot make directory %s", todir
);
359 #ifndef GETCWD_CANT_MALLOC
360 cwd
= getcwd(0, PATH_MAX
);
362 cwd
= malloc(PATH_MAX
+ 1);
363 cwd
= getcwd(cwd
, PATH_MAX
);
366 cwd
= malloc(PATH_MAX
+ 1);
373 #ifndef GETCWD_CANT_MALLOC
374 todir
= getcwd(0, PATH_MAX
);
376 todir
= malloc(PATH_MAX
+ 1);
377 todir
= getcwd(todir
, PATH_MAX
);
380 todir
= malloc(PATH_MAX
+ 1);
381 todir
= getwd(todir
);
383 tdlen
= strlen(todir
);
385 tdlen
= strlen(todir
);
387 uid
= owner
? touid(owner
) : (uid_t
)(-1);
388 gid
= group
? togid(group
) : (gid_t
)(-1);
393 base
= xbasename(name
);
394 bnlen
= strlen(base
);
395 toname
= xmalloc((unsigned int)(tdlen
+ 1 + bnlen
+ 1));
396 sprintf(toname
, "%s%s%s", todir
, _DIRECTORY_SEPARATOR
, base
);
397 exists
= (lstat(toname
, &tosb
) == 0);
400 /* -d means create a directory, always */
401 if (exists
&& !S_ISDIR(tosb
.st_mode
)) {
402 (void) unlink(toname
);
405 if (!exists
&& mkdir(toname
, mode
) < 0)
406 fail("cannot make directory %s", toname
);
407 if ((owner
|| group
) && chown(toname
, uid
, gid
) < 0)
408 fail("cannot change owner of %s", toname
);
410 if (access(name
, R_OK
) != 0) {
411 fail("cannot access %s", name
);
414 /* source is absolute pathname, link to it directly */
418 /* -L implies -l and prefixes names with a $cwd arg. */
420 linkname
= xmalloc((unsigned int)(len
+ 1));
421 sprintf(linkname
, "%s/%s", linkprefix
, name
);
422 } else if (dorelsymlink
) {
423 /* Symlink the relative path from todir to source name. */
424 linkname
= xmalloc(PATH_MAX
);
427 /* todir is absolute: skip over common prefix. */
428 lplen
= relatepaths(todir
, cwd
, linkname
);
429 strcpy(linkname
+ lplen
, name
);
431 /* todir is named by a relative path: reverse it. */
432 reversepath(todir
, name
, len
, linkname
);
436 len
= strlen(linkname
);
441 /* Check for a pre-existing symlink with identical content. */
442 if ((exists
&& (!S_ISLNK(tosb
.st_mode
) ||
443 readlink(toname
, buf
, sizeof buf
) != len
||
444 strncmp(buf
, name
, (unsigned int)len
) != 0)) ||
445 ((stat(name
, &fromsb
) == 0) &&
446 (fromsb
.st_mtime
> tosb
.st_mtime
))) {
447 (void) (S_ISDIR(tosb
.st_mode
) ? rmdir
: unlink
)(toname
);
450 if (!exists
&& symlink(name
, toname
) < 0)
451 fail("cannot make symbolic link %s", toname
);
453 if ((owner
|| group
) && lchown(toname
, uid
, gid
) < 0)
454 fail("cannot change owner of %s", toname
);
462 /* Copy from name to toname, which might be the same file. */
463 if( stat(name
, &sb
) == 0 && S_IFDIR
& sb
.st_mode
)
465 /* then is directory: must explicitly create destination dir */
466 /* and manually copy files over */
467 copydir( name
, todir
, mode
, group
, owner
, dotimes
, uid
, gid
);
471 copyfile(name
, toname
, mode
, group
, owner
, dotimes
, uid
, gid
);