1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/. */
7 ** Netscape portable install command.
9 ** Brendan Eich, 7/20/95
11 #include <stdio.h> /* OSF/1 requires this before grp.h, so put it first */
20 #include <sys/types.h>
25 #ifdef USE_REENTRANT_LIBC
27 #endif /* USE_REENTRANT_LIBC */
35 * Does getcwd() take NULL as the first argument and malloc
39 #define GETCWD_CAN_MALLOC
42 #if defined(LINUX) || defined(__GNU__) || defined(__GLIBC__)
46 #if defined(SCO) || defined(UNIXWARE)
47 #if !defined(S_ISLNK) && defined(S_IFLNK)
48 #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK)
53 #define d_ino d_stat.st_ino
60 "usage: %s [-C cwd] [-L linkprefix] [-m mode] [-o owner] [-g group]\n"
61 " %*s [-DdltR] file [file ...] directory\n",
62 program
, (int)strlen(program
), "");
67 mkdirs(char *path
, mode_t mode
)
73 while (*path
== '/' && path
[1] == '/') {
76 for (cp
= strrchr(path
, '/'); cp
&& cp
!= path
&& cp
[-1] == '/'; cp
--)
78 if (cp
&& cp
!= path
) {
80 if ((stat(path
, &sb
) < 0 || !S_ISDIR(sb
.st_mode
)) &&
81 mkdirs(path
, mode
) < 0) {
86 res
= mkdir(path
, mode
);
87 if ((res
!= 0) && (errno
== EEXIST
)) {
102 pw
= getpwnam(owner
);
106 uid
= strtol(owner
, &cp
, 0);
107 if (uid
== 0 && cp
== owner
) {
108 fail("cannot find uid for %s", owner
);
120 gr
= getgrnam(group
);
124 gid
= strtol(group
, &cp
, 0);
125 if (gid
== 0 && cp
== group
) {
126 fail("cannot find gid for %s", group
);
132 main(int argc
, char **argv
)
134 int onlydir
, dodir
, dolink
, dorelsymlink
, dotimes
, opt
, len
, lplen
, tdlen
, bnlen
, exists
, fromfd
, tofd
, cc
, wc
;
136 char *linkprefix
, *owner
, *group
, *cp
, *cwd
, *todir
, *toname
, *name
, *base
, *linkname
, *bp
, buf
[BUFSIZ
];
139 struct stat sb
, tosb
;
143 cwd
= linkname
= linkprefix
= owner
= group
= 0;
144 onlydir
= dodir
= dolink
= dorelsymlink
= dotimes
= lplen
= 0;
146 while ((opt
= getopt(argc
, argv
, "C:DdlL:Rm:o:g:t")) != EOF
) {
162 lplen
= strlen(linkprefix
);
166 dolink
= dorelsymlink
= 1;
169 mode
= strtoul(optarg
, &cp
, 8);
170 if (mode
== 0 && cp
== optarg
) {
190 if (argc
< 2 - onlydir
) {
194 todir
= argv
[argc
-1];
195 if ((stat(todir
, &sb
) < 0 || !S_ISDIR(sb
.st_mode
)) &&
196 mkdirs(todir
, 0777) < 0) {
197 fail("cannot make directory %s", todir
);
204 #ifdef GETCWD_CAN_MALLOC
205 cwd
= getcwd(0, PATH_MAX
);
207 cwd
= malloc(PATH_MAX
+ 1);
208 cwd
= getcwd(cwd
, PATH_MAX
);
212 #ifdef GETCWD_CAN_MALLOC
213 todir
= getcwd(0, PATH_MAX
);
215 todir
= malloc(PATH_MAX
+ 1);
216 todir
= getcwd(todir
, PATH_MAX
);
219 tdlen
= strlen(todir
);
221 uid
= owner
? touid(owner
) : -1;
222 gid
= group
? togid(group
) : -1;
227 base
= xbasename(name
);
228 bnlen
= strlen(base
);
229 toname
= (char*)xmalloc(tdlen
+ 1 + bnlen
+ 1);
230 sprintf(toname
, "%s/%s", todir
, base
);
231 exists
= (lstat(toname
, &tosb
) == 0);
234 /* -d means create a directory, always */
235 if (exists
&& !S_ISDIR(tosb
.st_mode
)) {
236 (void) unlink(toname
);
239 if (!exists
&& mkdir(toname
, mode
) < 0) {
240 fail("cannot make directory %s", toname
);
242 if ((owner
|| group
) && chown(toname
, uid
, gid
) < 0) {
243 fail("cannot change owner of %s", toname
);
247 /* source is absolute pathname, link to it directly */
251 /* -L implies -l and prefixes names with a $cwd arg. */
253 linkname
= (char*)xmalloc(len
+ 1);
254 sprintf(linkname
, "%s/%s", linkprefix
, name
);
255 } else if (dorelsymlink
) {
256 /* Symlink the relative path from todir to source name. */
257 linkname
= (char*)xmalloc(PATH_MAX
);
260 /* todir is absolute: skip over common prefix. */
261 lplen
= relatepaths(todir
, cwd
, linkname
);
262 strcpy(linkname
+ lplen
, name
);
264 /* todir is named by a relative path: reverse it. */
265 reversepath(todir
, name
, len
, linkname
);
269 len
= strlen(linkname
);
274 /* Check for a pre-existing symlink with identical content. */
276 (!S_ISLNK(tosb
.st_mode
) ||
277 readlink(toname
, buf
, sizeof buf
) != len
||
278 strncmp(buf
, name
, len
) != 0)) {
279 (void) (S_ISDIR(tosb
.st_mode
) ? rmdir
: unlink
)(toname
);
282 if (!exists
&& symlink(name
, toname
) < 0) {
283 fail("cannot make symbolic link %s", toname
);
286 if ((owner
|| group
) && lchown(toname
, uid
, gid
) < 0) {
287 fail("cannot change owner of %s", toname
);
296 /* Copy from name to toname, which might be the same file. */
297 fromfd
= open(name
, O_RDONLY
);
298 if (fromfd
< 0 || fstat(fromfd
, &sb
) < 0) {
299 fail("cannot access %s", name
);
301 if (exists
&& (!S_ISREG(tosb
.st_mode
) || access(toname
, W_OK
) < 0)) {
302 (void) (S_ISDIR(tosb
.st_mode
) ? rmdir
: unlink
)(toname
);
304 tofd
= open(toname
, O_CREAT
| O_WRONLY
, 0666);
306 fail("cannot create %s", toname
);
310 while ((cc
= read(fromfd
, bp
, sizeof buf
)) > 0) {
311 while ((wc
= write(tofd
, bp
, cc
)) > 0) {
312 if ((cc
-= wc
) == 0) {
318 fail("cannot write to %s", toname
);
322 fail("cannot read from %s", name
);
325 if (ftruncate(tofd
, sb
.st_size
) < 0) {
326 fail("cannot truncate %s", toname
);
329 utb
.actime
= sb
.st_atime
;
330 utb
.modtime
= sb
.st_mtime
;
331 if (utime(toname
, &utb
) < 0) {
332 fail("cannot set times of %s", toname
);
336 if (fchmod(tofd
, mode
) < 0)
338 if (chmod(toname
, mode
) < 0)
340 fail("cannot change mode of %s", toname
);
341 if ((owner
|| group
) && fchown(tofd
, uid
, gid
) < 0) {
342 fail("cannot change owner of %s", toname
);
345 /* Must check for delayed (NFS) write errors on close. */
346 if (close(tofd
) < 0) {
347 fail("cannot write to %s", toname
);
361 ** Pathname subroutines.
363 ** Brendan Eich, 8/29/95
369 fail(char *format
, ...)
374 #ifdef USE_REENTRANT_LIBC
379 fprintf(stderr
, "%s: ", program
);
380 va_start(ap
, format
);
381 vfprintf(stderr
, format
, ap
);
385 #ifdef USE_REENTRANT_LIBC
387 fprintf(stderr
, ": %s", r_strerror_r
);
389 fprintf(stderr
, ": %s", strerror(errno
));
397 getcomponent(char *path
, char *name
)
407 } while (*path
!= '/' && *path
!= '\0');
410 while (*path
== '/') {
416 #ifdef UNIXWARE_READDIR_BUFFER_TOO_SMALL
417 /* Sigh. The static buffer in Unixware's readdir is too small. */
418 struct dirent
* readdir(DIR *d
)
420 static struct dirent
*buf
= NULL
;
421 #define MAX_PATH_LEN 1024
425 buf
= (struct dirent
*) malloc(sizeof(struct dirent
) + MAX_PATH_LEN
)
427 return(readdir_r(d
, buf
));
432 ino2name(ino_t ino
, char *dir
)
440 fail("cannot read parent directory");
443 if (!(ep
= readdir(dp
))) {
444 fail("cannot find current directory");
446 if (ep
->d_ino
== ino
) {
450 name
= xstrdup(ep
->d_name
);
458 void *p
= malloc(size
);
460 fail("cannot allocate %u bytes", size
);
468 return strcpy((char*)xmalloc(strlen(s
) + 1), s
);
472 xbasename(char *path
)
476 while ((cp
= strrchr(path
, '/')) && cp
[1] == '\0') {
488 if (chdir(dir
) < 0) {
489 fail("cannot change directory to %s", dir
);
494 relatepaths(char *from
, char *to
, char *outpath
)
500 assert(*from
== '/' && *to
== '/');
501 for (cp
= to
, cp2
= from
; *cp
== *cp2
; cp
++, cp2
++)
505 while (cp
[-1] != '/') {
509 /* closest common ancestor is /, so use full pathname */
510 len
= strlen(strcpy(outpath
, to
));
511 if (outpath
[len
] != '/') {
512 outpath
[len
++] = '/';
517 while ((cp2
= getcomponent(cp2
, buf
)) != 0) {
518 strcpy(outpath
+ len
, "../");
521 while ((cp
= getcomponent(cp
, buf
)) != 0) {
522 sprintf(outpath
+ len
, "%s/", buf
);
523 len
+= strlen(outpath
+ len
);
530 reversepath(char *inpath
, char *name
, int len
, char *outpath
)
536 cp
= strcpy(outpath
+ PATH_MAX
- (len
+ 1), name
);
538 while ((cp2
= getcomponent(cp2
, buf
)) != 0) {
539 if (strcmp(buf
, ".") == 0) {
542 if (strcmp(buf
, "..") == 0) {
543 if (stat(".", &sb
) < 0) {
544 fail("cannot stat current directory");
546 name
= ino2name(sb
.st_ino
, "..");
555 memcpy(cp
, "../", 3);