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>
36 # include <bsd/libc.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
);
57 static void usage(void) {
59 "usage: %s [-C cwd] [-L linkprefix] [-m mode] [-o owner] [-g group]\n"
60 " %*s [-DdltR] file [file ...] directory\n",
61 program
, (int)strlen(program
), "");
65 static int mkdirs(char* path
, mode_t mode
) {
71 /* strip trailing "/." */
73 if (l
> 1 && path
[l
- 1] == '.' && path
[l
- 2] == '/') path
[l
- 2] = 0;
75 while (*path
== '/' && path
[1] == '/') path
++;
76 for (cp
= strrchr(path
, '/'); cp
&& cp
!= path
&& *(cp
- 1) == '/'; cp
--)
78 if (cp
&& cp
!= path
) {
80 if ((lstat(path
, &sb
) < 0 || !S_ISDIR(sb
.st_mode
)) &&
81 mkdirs(path
, mode
) < 0) {
87 res
= mkdir(path
, mode
);
88 if ((res
!= 0) && (errno
== EEXIST
))
94 static uid_t
touid(char* owner
) {
100 if (pw
) return pw
->pw_uid
;
101 uid
= strtol(owner
, &cp
, 0);
102 if (uid
== 0 && cp
== owner
) fail("cannot find uid for %s", owner
);
106 static gid_t
togid(char* group
) {
111 gr
= getgrnam(group
);
112 if (gr
) return gr
->gr_gid
;
113 gid
= strtol(group
, &cp
, 0);
114 if (gid
== 0 && cp
== group
) fail("cannot find gid for %s", group
);
118 static void copyfile(char* name
, char* toname
, mode_t mode
, char* group
,
119 char* owner
, int dotimes
, uid_t uid
, gid_t gid
) {
120 int fromfd
, tofd
= -1, cc
, wc
, exists
;
121 char buf
[BUFSIZ
], *bp
;
122 struct stat sb
, tosb
;
125 exists
= (lstat(toname
, &tosb
) == 0);
127 fromfd
= open(name
, O_RDONLY
);
128 if (fromfd
< 0 || fstat(fromfd
, &sb
) < 0) fail("cannot access %s", name
);
130 if (S_ISREG(tosb
.st_mode
)) {
131 /* See if we can open it. This is more reliable than 'access'. */
132 tofd
= open(toname
, O_CREAT
| O_WRONLY
, 0666);
135 (void)(S_ISDIR(tosb
.st_mode
) ? rmdir
: unlink
)(toname
);
139 tofd
= open(toname
, O_CREAT
| O_WRONLY
, 0666);
140 if (tofd
< 0) fail("cannot create %s", toname
);
144 while ((cc
= read(fromfd
, bp
, sizeof buf
)) > 0) {
145 while ((wc
= write(tofd
, bp
, (unsigned int)cc
)) > 0) {
146 if ((cc
-= wc
) == 0) break;
149 if (wc
< 0) fail("cannot write to %s", toname
);
151 if (cc
< 0) fail("cannot read from %s", name
);
153 if (ftruncate(tofd
, sb
.st_size
) < 0) fail("cannot truncate %s", toname
);
156 utb
.actime
= sb
.st_atime
;
157 utb
.modtime
= sb
.st_mtime
;
158 if (utime(toname
, &utb
) < 0) fail("cannot set times of %s", toname
);
161 if (fchmod(tofd
, mode
) < 0)
163 if (chmod(toname
, mode
) < 0)
165 fail("cannot change mode of %s", toname
);
167 if ((owner
|| group
) && fchown(tofd
, uid
, gid
) < 0)
168 fail("cannot change owner of %s", toname
);
170 /* Must check for delayed (NFS) write errors on close. */
171 if (close(tofd
) < 0) fail("cannot write to %s", toname
);
174 if (chmod(toname
, (mode
& (S_IREAD
| S_IWRITE
))) < 0)
175 fail("cannot change mode of %s", toname
);
177 utb
.actime
= sb
.st_atime
;
178 utb
.modtime
= sb
.st_mtime
;
179 if (utime(toname
, &utb
) < 0) fail("cannot set times of %s", toname
);
184 static void copydir(char* from
, char* to
, mode_t mode
, char* group
, char* owner
,
185 int dotimes
, uid_t uid
, gid_t gid
) {
189 char *base
, *destdir
, *direntry
, *destentry
;
191 base
= xbasename(from
);
193 /* create destination directory */
194 destdir
= xmalloc((unsigned int)(strlen(to
) + 1 + strlen(base
) + 1));
195 sprintf(destdir
, "%s%s%s", to
, _DIRECTORY_SEPARATOR
, base
);
196 if (mkdirs(destdir
, mode
) != 0) {
197 fail("cannot make directory %s\n", destdir
);
202 if (!(dir
= opendir(from
))) {
203 fail("cannot open directory %s\n", from
);
208 direntry
= xmalloc((unsigned int)PATH_MAX
);
209 destentry
= xmalloc((unsigned int)PATH_MAX
);
211 while ((ep
= readdir(dir
))) {
212 if (strcmp(ep
->d_name
, ".") == 0 || strcmp(ep
->d_name
, "..") == 0) continue;
214 sprintf(direntry
, "%s/%s", from
, ep
->d_name
);
215 sprintf(destentry
, "%s%s%s", destdir
, _DIRECTORY_SEPARATOR
, ep
->d_name
);
217 if (stat(direntry
, &sb
) == 0 && S_ISDIR(sb
.st_mode
))
218 copydir(direntry
, destdir
, mode
, group
, owner
, dotimes
, uid
, gid
);
220 copyfile(direntry
, destentry
, mode
, group
, owner
, dotimes
, uid
, gid
);
229 int main(int argc
, char** argv
) {
230 int onlydir
, dodir
, dolink
, dorelsymlink
, dotimes
, opt
, len
, lplen
, tdlen
,
233 char *linkprefix
, *owner
, *group
, *cp
, *cwd
, *todir
, *toname
, *name
, *base
,
234 *linkname
, buf
[BUFSIZ
];
237 struct stat sb
, tosb
, fromsb
;
240 cwd
= linkname
= linkprefix
= owner
= group
= 0;
241 onlydir
= dodir
= dolink
= dorelsymlink
= dotimes
= lplen
= 0;
243 while ((opt
= getopt(argc
, argv
, "C:DdlL:Rm:o:g:t")) != EOF
) {
256 lplen
= strlen(linkprefix
);
260 dolink
= dorelsymlink
= 1;
263 mode
= strtoul(optarg
, &cp
, 8);
264 if (mode
== 0 && cp
== optarg
) usage();
282 if (argc
< 2 - onlydir
) usage();
284 todir
= argv
[argc
- 1];
285 if ((stat(todir
, &sb
) < 0 || !S_ISDIR(sb
.st_mode
)) &&
286 mkdirs(todir
, 0777) < 0) {
287 fail("cannot make directory %s", todir
);
289 if (onlydir
) return 0;
293 # ifndef GETCWD_CANT_MALLOC
294 cwd
= getcwd(0, PATH_MAX
);
296 cwd
= malloc(PATH_MAX
+ 1);
297 cwd
= getcwd(cwd
, PATH_MAX
);
300 cwd
= malloc(PATH_MAX
+ 1);
307 # ifndef GETCWD_CANT_MALLOC
308 todir
= getcwd(0, PATH_MAX
);
310 todir
= malloc(PATH_MAX
+ 1);
311 todir
= getcwd(todir
, PATH_MAX
);
314 todir
= malloc(PATH_MAX
+ 1);
315 todir
= getwd(todir
);
317 tdlen
= strlen(todir
);
319 tdlen
= strlen(todir
);
321 uid
= owner
? touid(owner
) : (uid_t
)(-1);
322 gid
= group
? togid(group
) : (gid_t
)(-1);
327 base
= xbasename(name
);
328 bnlen
= strlen(base
);
329 toname
= xmalloc((unsigned int)(tdlen
+ 1 + bnlen
+ 1));
330 sprintf(toname
, "%s%s%s", todir
, _DIRECTORY_SEPARATOR
, base
);
331 exists
= (lstat(toname
, &tosb
) == 0);
334 /* -d means create a directory, always */
335 if (exists
&& !S_ISDIR(tosb
.st_mode
)) {
336 (void)unlink(toname
);
339 if (!exists
&& mkdir(toname
, mode
) < 0)
340 fail("cannot make directory %s", toname
);
341 if ((owner
|| group
) && chown(toname
, uid
, gid
) < 0)
342 fail("cannot change owner of %s", toname
);
344 if (access(name
, R_OK
) != 0) {
345 fail("cannot access %s", name
);
348 /* source is absolute pathname, link to it directly */
352 /* -L prefixes names with a $cwd arg. */
354 linkname
= xmalloc((unsigned int)(len
+ 1));
355 sprintf(linkname
, "%s/%s", linkprefix
, name
);
356 } else if (dorelsymlink
) {
357 /* Symlink the relative path from todir to source name. */
358 linkname
= xmalloc(PATH_MAX
);
361 /* todir is absolute: skip over common prefix. */
362 lplen
= relatepaths(todir
, cwd
, linkname
);
363 strcpy(linkname
+ lplen
, name
);
365 /* todir is named by a relative path: reverse it. */
366 reversepath(todir
, name
, len
, linkname
);
370 len
= strlen(linkname
);
375 /* Check for a pre-existing symlink with identical content. */
377 (!S_ISLNK(tosb
.st_mode
) || readlink(toname
, buf
, sizeof buf
) != len
||
378 strncmp(buf
, name
, (unsigned int)len
) != 0 ||
379 ((stat(name
, &fromsb
) == 0) && (fromsb
.st_mtime
> tosb
.st_mtime
)))) {
380 (void)(S_ISDIR(tosb
.st_mode
) ? rmdir
: unlink
)(toname
);
383 if (!exists
&& symlink(name
, toname
) < 0)
384 fail("cannot make symbolic link %s", toname
);
386 if ((owner
|| group
) && lchown(toname
, uid
, gid
) < 0)
387 fail("cannot change owner of %s", toname
);
395 /* Copy from name to toname, which might be the same file. */
396 if (stat(name
, &sb
) == 0 && S_IFDIR
& sb
.st_mode
) {
397 /* then is directory: must explicitly create destination dir */
398 /* and manually copy files over */
399 copydir(name
, todir
, mode
, group
, owner
, dotimes
, uid
, gid
);
401 copyfile(name
, toname
, mode
, group
, owner
, dotimes
, uid
, gid
);