1 /* chown/chgrp - Change file ownership Author: V. Archer */
3 /* Copyright 1991 by Vincent Archer
4 * You may freely redistribute this software, in source or binary
5 * form, provided that you do not alter this copyright mention in any
9 /* Changed 3 Feb 93 by Kees J. Bot: setuid execution nonsense removed.
12 #include <sys/types.h>
23 #include <minix/minlib.h>
27 #define S_ISLNK(mode) 0
31 #define S_IUGID (S_ISUID|S_ISGID)
33 /* Global variables, such as flags and path names */
34 int gflag
, oflag
, rflag
, error
, hflag
= 0;
35 char *pgmname
, path
[PATH_MAX
+ 1];
39 int main(int argc
, char **argv
);
40 void do_chown(char *file
);
43 /* Main module. If chown(1) is invoked as chgrp(1), the behaviour is nearly
44 * identical, except that the default when a single name is given as an
45 * argument is to take a group id rather than an user id. This allow the
46 * non-Posix "chgrp user:group file".
57 if (pgmname
= strrchr(*argv
, '/'))
61 gflag
= strcmp(pgmname
, "chgrp");
63 while((ch
= getopt(argc
, argv
, "Rh")) != -1) {
79 if (argc
< 2) usage();
83 if (id2
= strchr(id
, ':')) *id2
++ = '\0';
92 if (!(pwp
= getpwnam(id
))) {
94 std_err(": unknown user name\n");
107 if (!(grp
= getgrnam(id2
))) {
109 std_err(": unknown group name\n");
119 while (argc
--) do_chown(*argv
++);
123 /* Apply the user/group modification here.
133 if (lstat(file
, &st
)) {
139 if (S_ISLNK(st
.st_mode
) && rflag
) return; /* Note: violates POSIX. */
141 if (S_ISLNK(st
.st_mode
) && hflag
) {
142 fprintf(stderr
, "chown: cannot lchown %s\n", file
);
146 if (chown(file
, oflag
? nuid
: st
.st_uid
, gflag
? ngid
: st
.st_gid
)) {
151 if (S_ISDIR(st
.st_mode
) && rflag
) {
152 if (!(dirp
= opendir(file
))) {
157 if (path
!= file
) strcpy(path
, file
);
158 namp
= path
+ strlen(path
);
160 while (entp
= readdir(dirp
))
161 if (entp
->d_name
[0] != '.' ||
163 (entp
->d_name
[1] != '.' || entp
->d_name
[2]))) {
164 strcpy(namp
, entp
->d_name
);
172 /* Posix prototype of the chown/chgrp function */
177 std_err(gflag
? " owner[:group]" : " [owner:]group");
178 std_err(" file...\n");