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/. */
6 ** Netscape portable install command.
8 ** Brendan Eich, 7/20/95
10 #include <stdio.h> /* OSF/1 requires this before grp.h, so put it first */
23 #include <sys/types.h>
44 #if !defined(S_ISLNK) && defined(S_IFLNK)
45 #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK)
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
);
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
), "");
68 mkdirs(char *path
, mode_t mode
)
75 /* strip trailing "/." */
77 if(l
> 1 && path
[l
- 1] == '.' && path
[l
- 2] == '/')
80 while (*path
== '/' && path
[1] == '/')
82 for (cp
= strrchr(path
, '/'); cp
&& cp
!= path
&& *(cp
- 1) == '/'; cp
--);
83 if (cp
&& cp
!= path
) {
85 if ((lstat(path
, &sb
) < 0 || !S_ISDIR(sb
.st_mode
)) &&
86 mkdirs(path
, mode
) < 0) {
92 res
= mkdir(path
, mode
);
93 if ((res
!= 0) && (errno
== EEXIST
))
106 pw
= getpwnam(owner
);
109 uid
= strtol(owner
, &cp
, 0);
110 if (uid
== 0 && cp
== owner
)
111 fail("cannot find uid for %s", owner
);
122 gr
= getgrnam(group
);
125 gid
= strtol(group
, &cp
, 0);
126 if (gid
== 0 && cp
== group
)
127 fail("cannot find gid for %s", group
);
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
;
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
);
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);
151 (void) (S_ISDIR(tosb
.st_mode
) ? rmdir
: unlink
)(toname
);
155 tofd
= open(toname
, O_CREAT
| O_WRONLY
, 0666);
157 fail("cannot create %s", toname
);
161 while ((cc
= read(fromfd
, bp
, sizeof buf
)) > 0)
163 while ((wc
= write(tofd
, bp
, (unsigned int)cc
)) > 0)
170 fail("cannot write to %s", toname
);
173 fail("cannot read from %s", name
);
175 if (ftruncate(tofd
, sb
.st_size
) < 0)
176 fail("cannot truncate %s", toname
);
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
);
186 if (fchmod(tofd
, mode
) < 0)
188 if (chmod(toname
, mode
) < 0)
190 fail("cannot change mode of %s", toname
);
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. */
197 fail("cannot write to %s", toname
);
200 if (chmod(toname
, (mode
& (S_IREAD
| S_IWRITE
))) < 0)
201 fail("cannot change mode of %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
);
213 copydir( char *from
, char *to
, mode_t mode
, char *group
, char *owner
,
214 int dotimes
, uid_t uid
, gid_t gid
)
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
);
233 if (!(dir
= opendir(from
))) {
234 fail("cannot open directory %s\n", from
);
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)
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
);
253 copyfile( direntry
, destentry
, mode
, group
, owner
, dotimes
, uid
, gid
);
263 main(int argc
, char **argv
)
265 int onlydir
, dodir
, dolink
, dorelsymlink
, dotimes
, opt
, len
, lplen
, tdlen
, bnlen
, exists
, fromfd
, tofd
, cc
, wc
;
267 char *linkprefix
, *owner
, *group
, *cp
, *cwd
, *todir
, *toname
, *name
, *base
, *linkname
, *bp
, buf
[BUFSIZ
];
270 struct stat sb
, tosb
, fromsb
;
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
) {
290 lplen
= strlen(linkprefix
);
294 dolink
= dorelsymlink
= 1;
297 mode
= strtoul(optarg
, &cp
, 8);
298 if (mode
== 0 && cp
== optarg
)
317 if (argc
< 2 - onlydir
)
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
);
330 #ifndef GETCWD_CANT_MALLOC
331 cwd
= getcwd(0, PATH_MAX
);
333 cwd
= malloc(PATH_MAX
+ 1);
334 cwd
= getcwd(cwd
, PATH_MAX
);
337 cwd
= malloc(PATH_MAX
+ 1);
344 #ifndef GETCWD_CANT_MALLOC
345 todir
= getcwd(0, PATH_MAX
);
347 todir
= malloc(PATH_MAX
+ 1);
348 todir
= getcwd(todir
, PATH_MAX
);
351 todir
= malloc(PATH_MAX
+ 1);
352 todir
= getwd(todir
);
354 tdlen
= strlen(todir
);
356 tdlen
= strlen(todir
);
358 uid
= owner
? touid(owner
) : (uid_t
)(-1);
359 gid
= group
? togid(group
) : (gid_t
)(-1);
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);
371 /* -d means create a directory, always */
372 if (exists
&& !S_ISDIR(tosb
.st_mode
)) {
373 (void) unlink(toname
);
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
);
381 if (access(name
, R_OK
) != 0) {
382 fail("cannot access %s", name
);
385 /* source is absolute pathname, link to it directly */
389 /* -L prefixes names with a $cwd arg. */
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
);
398 /* todir is absolute: skip over common prefix. */
399 lplen
= relatepaths(todir
, cwd
, linkname
);
400 strcpy(linkname
+ lplen
, name
);
402 /* todir is named by a relative path: reverse it. */
403 reversepath(todir
, name
, len
, linkname
);
407 len
= strlen(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
)
419 (void) (S_ISDIR(tosb
.st_mode
) ? rmdir
: unlink
)(toname
);
422 if (!exists
&& symlink(name
, toname
) < 0)
423 fail("cannot make symbolic link %s", toname
);
425 if ((owner
|| group
) && lchown(toname
, uid
, gid
) < 0)
426 fail("cannot change owner of %s", toname
);
434 /* Copy from name to toname, which might be the same file. */
435 if( stat(name
, &sb
) == 0 && S_IFDIR
& sb
.st_mode
)
437 /* then is directory: must explicitly create destination dir */
438 /* and manually copy files over */
439 copydir( name
, todir
, mode
, group
, owner
, dotimes
, uid
, gid
);
443 copyfile(name
, toname
, mode
, group
, owner
, dotimes
, uid
, gid
);