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 */
38 * Does getcwd() take NULL as the first argument and malloc
42 #define GETCWD_CAN_MALLOC
45 #if defined(LINUX) || defined(__GNU__) || defined(__GLIBC__)
49 #if defined(SCO) || defined(UNIXWARE)
50 #if !defined(S_ISLNK) && defined(S_IFLNK)
51 #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK)
56 #define d_ino d_stat.st_ino
63 "usage: %s [-C cwd] [-L linkprefix] [-m mode] [-o owner] [-g group]\n"
64 " %*s [-DdltR] file [file ...] directory\n",
65 program
, (int)strlen(program
), "");
70 mkdirs(char *path
, mode_t mode
)
76 while (*path
== '/' && path
[1] == '/')
78 for (cp
= strrchr(path
, '/'); cp
&& cp
!= path
&& cp
[-1] == '/'; cp
--)
80 if (cp
&& cp
!= path
) {
82 if ((stat(path
, &sb
) < 0 || !S_ISDIR(sb
.st_mode
)) &&
83 mkdirs(path
, mode
) < 0) {
88 res
= mkdir(path
, mode
);
89 if ((res
!= 0) && (errno
== EEXIST
))
102 pw
= getpwnam(owner
);
105 uid
= strtol(owner
, &cp
, 0);
106 if (uid
== 0 && cp
== owner
)
107 fail("cannot find uid for %s", owner
);
118 gr
= getgrnam(group
);
121 gid
= strtol(group
, &cp
, 0);
122 if (gid
== 0 && cp
== group
)
123 fail("cannot find gid for %s", group
);
128 main(int argc
, char **argv
)
130 int onlydir
, dodir
, dolink
, dorelsymlink
, dotimes
, opt
, len
, lplen
, tdlen
, bnlen
, exists
, fromfd
, tofd
, cc
, wc
;
132 char *linkprefix
, *owner
, *group
, *cp
, *cwd
, *todir
, *toname
, *name
, *base
, *linkname
, *bp
, buf
[BUFSIZ
];
135 struct stat sb
, tosb
;
139 cwd
= linkname
= linkprefix
= owner
= group
= 0;
140 onlydir
= dodir
= dolink
= dorelsymlink
= dotimes
= lplen
= 0;
142 while ((opt
= getopt(argc
, argv
, "C:DdlL:Rm:o:g:t")) != EOF
) {
158 lplen
= strlen(linkprefix
);
162 dolink
= dorelsymlink
= 1;
165 mode
= strtoul(optarg
, &cp
, 8);
166 if (mode
== 0 && cp
== optarg
)
185 if (argc
< 2 - onlydir
)
188 todir
= argv
[argc
-1];
189 if ((stat(todir
, &sb
) < 0 || !S_ISDIR(sb
.st_mode
)) &&
190 mkdirs(todir
, 0777) < 0) {
191 fail("cannot make directory %s", todir
);
197 #ifdef GETCWD_CAN_MALLOC
198 cwd
= getcwd(0, PATH_MAX
);
200 cwd
= malloc(PATH_MAX
+ 1);
201 cwd
= getcwd(cwd
, PATH_MAX
);
205 #ifdef GETCWD_CAN_MALLOC
206 todir
= getcwd(0, PATH_MAX
);
208 todir
= malloc(PATH_MAX
+ 1);
209 todir
= getcwd(todir
, PATH_MAX
);
212 tdlen
= strlen(todir
);
214 uid
= owner
? touid(owner
) : -1;
215 gid
= group
? togid(group
) : -1;
220 base
= xbasename(name
);
221 bnlen
= strlen(base
);
222 toname
= (char*)xmalloc(tdlen
+ 1 + bnlen
+ 1);
223 sprintf(toname
, "%s/%s", todir
, base
);
224 exists
= (lstat(toname
, &tosb
) == 0);
227 /* -d means create a directory, always */
228 if (exists
&& !S_ISDIR(tosb
.st_mode
)) {
229 (void) unlink(toname
);
232 if (!exists
&& mkdir(toname
, mode
) < 0)
233 fail("cannot make directory %s", toname
);
234 if ((owner
|| group
) && chown(toname
, uid
, gid
) < 0)
235 fail("cannot change owner of %s", toname
);
238 /* source is absolute pathname, link to it directly */
242 /* -L implies -l and prefixes names with a $cwd arg. */
244 linkname
= (char*)xmalloc(len
+ 1);
245 sprintf(linkname
, "%s/%s", linkprefix
, name
);
246 } else if (dorelsymlink
) {
247 /* Symlink the relative path from todir to source name. */
248 linkname
= (char*)xmalloc(PATH_MAX
);
251 /* todir is absolute: skip over common prefix. */
252 lplen
= relatepaths(todir
, cwd
, linkname
);
253 strcpy(linkname
+ lplen
, name
);
255 /* todir is named by a relative path: reverse it. */
256 reversepath(todir
, name
, len
, linkname
);
260 len
= strlen(linkname
);
265 /* Check for a pre-existing symlink with identical content. */
267 (!S_ISLNK(tosb
.st_mode
) ||
268 readlink(toname
, buf
, sizeof buf
) != len
||
269 strncmp(buf
, name
, len
) != 0)) {
270 (void) (S_ISDIR(tosb
.st_mode
) ? rmdir
: unlink
)(toname
);
273 if (!exists
&& symlink(name
, toname
) < 0)
274 fail("cannot make symbolic link %s", toname
);
276 if ((owner
|| group
) && lchown(toname
, uid
, gid
) < 0)
277 fail("cannot change owner of %s", toname
);
285 /* Copy from name to toname, which might be the same file. */
286 fromfd
= open(name
, O_RDONLY
);
287 if (fromfd
< 0 || fstat(fromfd
, &sb
) < 0)
288 fail("cannot access %s", name
);
289 if (exists
&& (!S_ISREG(tosb
.st_mode
) || access(toname
, W_OK
) < 0))
290 (void) (S_ISDIR(tosb
.st_mode
) ? rmdir
: unlink
)(toname
);
291 tofd
= open(toname
, O_CREAT
| O_WRONLY
, 0666);
293 fail("cannot create %s", toname
);
296 while ((cc
= read(fromfd
, bp
, sizeof buf
)) > 0) {
297 while ((wc
= write(tofd
, bp
, cc
)) > 0) {
303 fail("cannot write to %s", toname
);
306 fail("cannot read from %s", name
);
308 if (ftruncate(tofd
, sb
.st_size
) < 0)
309 fail("cannot truncate %s", toname
);
311 utb
.actime
= sb
.st_atime
;
312 utb
.modtime
= sb
.st_mtime
;
313 if (utime(toname
, &utb
) < 0)
314 fail("cannot set times of %s", toname
);
317 if (fchmod(tofd
, mode
) < 0)
319 if (chmod(toname
, mode
) < 0)
321 fail("cannot change mode of %s", toname
);
322 if ((owner
|| group
) && fchown(tofd
, uid
, gid
) < 0)
323 fail("cannot change owner of %s", toname
);
325 /* Must check for delayed (NFS) write errors on close. */
327 fail("cannot write to %s", toname
);
340 ** Pathname subroutines.
342 ** Brendan Eich, 8/29/95
348 fail(char *format
, ...)
353 #ifdef USE_REENTRANT_LIBC
358 fprintf(stderr
, "%s: ", program
);
359 va_start(ap
, format
);
360 vfprintf(stderr
, format
, ap
);
364 #ifdef USE_REENTRANT_LIBC
366 fprintf(stderr
, ": %s", r_strerror_r
);
368 fprintf(stderr
, ": %s", strerror(errno
));
376 getcomponent(char *path
, char *name
)
385 } while (*path
!= '/' && *path
!= '\0');
393 #ifdef UNIXWARE_READDIR_BUFFER_TOO_SMALL
394 /* Sigh. The static buffer in Unixware's readdir is too small. */
395 struct dirent
* readdir(DIR *d
)
397 static struct dirent
*buf
= NULL
;
398 #define MAX_PATH_LEN 1024
402 buf
= (struct dirent
*) malloc(sizeof(struct dirent
) + MAX_PATH_LEN
)
404 return(readdir_r(d
, buf
));
409 ino2name(ino_t ino
, char *dir
)
417 fail("cannot read parent directory");
419 if (!(ep
= readdir(dp
)))
420 fail("cannot find current directory");
421 if (ep
->d_ino
== ino
)
424 name
= xstrdup(ep
->d_name
);
432 void *p
= malloc(size
);
434 fail("cannot allocate %u bytes", size
);
441 return strcpy((char*)xmalloc(strlen(s
) + 1), s
);
445 xbasename(char *path
)
449 while ((cp
= strrchr(path
, '/')) && cp
[1] == '\0')
451 if (!cp
) return path
;
459 fail("cannot change directory to %s", dir
);
463 relatepaths(char *from
, char *to
, char *outpath
)
469 assert(*from
== '/' && *to
== '/');
470 for (cp
= to
, cp2
= from
; *cp
== *cp2
; cp
++, cp2
++)
473 while (cp
[-1] != '/')
476 /* closest common ancestor is /, so use full pathname */
477 len
= strlen(strcpy(outpath
, to
));
478 if (outpath
[len
] != '/') {
479 outpath
[len
++] = '/';
484 while ((cp2
= getcomponent(cp2
, buf
)) != 0) {
485 strcpy(outpath
+ len
, "../");
488 while ((cp
= getcomponent(cp
, buf
)) != 0) {
489 sprintf(outpath
+ len
, "%s/", buf
);
490 len
+= strlen(outpath
+ len
);
497 reversepath(char *inpath
, char *name
, int len
, char *outpath
)
503 cp
= strcpy(outpath
+ PATH_MAX
- (len
+ 1), name
);
505 while ((cp2
= getcomponent(cp2
, buf
)) != 0) {
506 if (strcmp(buf
, ".") == 0)
508 if (strcmp(buf
, "..") == 0) {
509 if (stat(".", &sb
) < 0)
510 fail("cannot stat current directory");
511 name
= ino2name(sb
.st_ino
, "..");
520 strncpy(cp
, "../", 3);