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
)
219 char *base
, *destdir
, *direntry
, *destentry
;
221 base
= xbasename(from
);
223 /* create destination directory */
224 destdir
= xmalloc((unsigned int)(strlen(to
) + 1 + strlen(base
) + 1));
225 sprintf(destdir
, "%s%s%s", to
, _DIRECTORY_SEPARATOR
, base
);
226 if (mkdirs(destdir
, mode
) != 0) {
227 fail("cannot make directory %s\n", destdir
);
232 if (!(dir
= opendir(from
))) {
233 fail("cannot open directory %s\n", from
);
238 direntry
= xmalloc((unsigned int)PATH_MAX
);
239 destentry
= xmalloc((unsigned int)PATH_MAX
);
241 while ((ep
= readdir(dir
)))
243 if (strcmp(ep
->d_name
, ".") == 0 || strcmp(ep
->d_name
, "..") == 0)
246 sprintf(direntry
, "%s/%s", from
, ep
->d_name
);
247 sprintf(destentry
, "%s%s%s", destdir
, _DIRECTORY_SEPARATOR
, ep
->d_name
);
249 if (stat(direntry
, &sb
) == 0 && S_ISDIR(sb
.st_mode
))
250 copydir( direntry
, destdir
, mode
, group
, owner
, dotimes
, uid
, gid
);
252 copyfile( direntry
, destentry
, mode
, group
, owner
, dotimes
, uid
, gid
);
262 main(int argc
, char **argv
)
264 int onlydir
, dodir
, dolink
, dorelsymlink
, dotimes
, opt
, len
, lplen
, tdlen
, bnlen
, exists
;
266 char *linkprefix
, *owner
, *group
, *cp
, *cwd
, *todir
, *toname
, *name
, *base
, *linkname
, buf
[BUFSIZ
];
269 struct stat sb
, tosb
, fromsb
;
272 cwd
= linkname
= linkprefix
= owner
= group
= 0;
273 onlydir
= dodir
= dolink
= dorelsymlink
= dotimes
= lplen
= 0;
275 while ((opt
= getopt(argc
, argv
, "C:DdlL:Rm:o:g:t")) != EOF
) {
288 lplen
= strlen(linkprefix
);
292 dolink
= dorelsymlink
= 1;
295 mode
= strtoul(optarg
, &cp
, 8);
296 if (mode
== 0 && cp
== optarg
)
315 if (argc
< 2 - onlydir
)
318 todir
= argv
[argc
-1];
319 if ((stat(todir
, &sb
) < 0 || !S_ISDIR(sb
.st_mode
)) &&
320 mkdirs(todir
, 0777) < 0) {
321 fail("cannot make directory %s", todir
);
328 #ifndef GETCWD_CANT_MALLOC
329 cwd
= getcwd(0, PATH_MAX
);
331 cwd
= malloc(PATH_MAX
+ 1);
332 cwd
= getcwd(cwd
, PATH_MAX
);
335 cwd
= malloc(PATH_MAX
+ 1);
342 #ifndef GETCWD_CANT_MALLOC
343 todir
= getcwd(0, PATH_MAX
);
345 todir
= malloc(PATH_MAX
+ 1);
346 todir
= getcwd(todir
, PATH_MAX
);
349 todir
= malloc(PATH_MAX
+ 1);
350 todir
= getwd(todir
);
352 tdlen
= strlen(todir
);
354 tdlen
= strlen(todir
);
356 uid
= owner
? touid(owner
) : (uid_t
)(-1);
357 gid
= group
? togid(group
) : (gid_t
)(-1);
362 base
= xbasename(name
);
363 bnlen
= strlen(base
);
364 toname
= xmalloc((unsigned int)(tdlen
+ 1 + bnlen
+ 1));
365 sprintf(toname
, "%s%s%s", todir
, _DIRECTORY_SEPARATOR
, base
);
366 exists
= (lstat(toname
, &tosb
) == 0);
369 /* -d means create a directory, always */
370 if (exists
&& !S_ISDIR(tosb
.st_mode
)) {
371 (void) unlink(toname
);
374 if (!exists
&& mkdir(toname
, mode
) < 0)
375 fail("cannot make directory %s", toname
);
376 if ((owner
|| group
) && chown(toname
, uid
, gid
) < 0)
377 fail("cannot change owner of %s", toname
);
379 if (access(name
, R_OK
) != 0) {
380 fail("cannot access %s", name
);
383 /* source is absolute pathname, link to it directly */
387 /* -L prefixes names with a $cwd arg. */
389 linkname
= xmalloc((unsigned int)(len
+ 1));
390 sprintf(linkname
, "%s/%s", linkprefix
, name
);
391 } else if (dorelsymlink
) {
392 /* Symlink the relative path from todir to source name. */
393 linkname
= xmalloc(PATH_MAX
);
396 /* todir is absolute: skip over common prefix. */
397 lplen
= relatepaths(todir
, cwd
, linkname
);
398 strcpy(linkname
+ lplen
, name
);
400 /* todir is named by a relative path: reverse it. */
401 reversepath(todir
, name
, len
, linkname
);
405 len
= strlen(linkname
);
410 /* Check for a pre-existing symlink with identical content. */
411 if (exists
&& (!S_ISLNK(tosb
.st_mode
) ||
412 readlink(toname
, buf
, sizeof buf
) != len
||
413 strncmp(buf
, name
, (unsigned int)len
) != 0 ||
414 ((stat(name
, &fromsb
) == 0) &&
415 (fromsb
.st_mtime
> tosb
.st_mtime
)
417 (void) (S_ISDIR(tosb
.st_mode
) ? rmdir
: unlink
)(toname
);
420 if (!exists
&& symlink(name
, toname
) < 0)
421 fail("cannot make symbolic link %s", toname
);
423 if ((owner
|| group
) && lchown(toname
, uid
, gid
) < 0)
424 fail("cannot change owner of %s", toname
);
432 /* Copy from name to toname, which might be the same file. */
433 if( stat(name
, &sb
) == 0 && S_IFDIR
& sb
.st_mode
)
435 /* then is directory: must explicitly create destination dir */
436 /* and manually copy files over */
437 copydir( name
, todir
, mode
, group
, owner
, dotimes
, uid
, gid
);
441 copyfile(name
, toname
, mode
, group
, owner
, dotimes
, uid
, gid
);