2 * Copyright (c) 1998, by Sun Microsystems, Inc.
6 #pragma ident "%Z%%M% %I% %E% SMI"
12 #include <sys/types.h>
17 * safechown changes the owner ship of src to uid. If the mode parameter
18 * does not equal -1 changes the mode of src as well.
20 * return -1 on failure and 0 on success.
24 safechown(const char *src
, uid_t uid
, gid_t gid
, int mode
)
30 if ((fd
= open(src
, O_RDONLY
, 0)) == -1)
33 if (fstat(fd
, &fdbuf
)) {
38 /* Make sure non directories are not hard links */
39 if (!S_ISDIR(fdbuf
.st_mode
) && fdbuf
.st_nlink
!= 1) {
44 if (lstat(src
, &lbuf
)) {
49 /* Make sure file is not a symlink */
50 if (fdbuf
.st_ino
!= lbuf
.st_ino
|| fdbuf
.st_dev
!= lbuf
.st_dev
||
51 fdbuf
.st_mode
!= lbuf
.st_mode
) {
57 /* we should probably get the primary group id for uid here */
58 if (fchown(fd
, uid
, gid
)) {
64 if (fchmod(fd
, (mode_t
)mode
)) {
79 fprintf(stderr
, "Usage %s [-u uid] [-m mode] source\n", prg
);
83 main(int argc
, char *argv
[])
89 while ((opt
= getopt(argc
, argv
, "m:u:")) != EOF
) {
92 mode
= strtol(optarg
, 0, 8);
102 if (argc
- optind
!= 1)
105 if (safechown(argv
[optind
], uid
, getgid(), mode
)) {