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
;
35 char *pgmname
, path
[PATH_MAX
+ 1];
39 _PROTOTYPE(int main
, (int argc
, char **argv
));
40 _PROTOTYPE(void do_chown
, (char *file
));
41 _PROTOTYPE(void usage
, (void));
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".
47 * The single option switch used by chown/chgrp (-R) does not warrant a
48 * call to the getopt stuff. The two others flags (-g, -u) are set from
49 * the program name and arguments.
59 if (pgmname
= strrchr(*argv
, '/'))
65 gflag
= strcmp(pgmname
, "chgrp");
67 if (argc
&& **argv
== '-' && argv
[0][1] == 'R') {
72 if (argc
< 2) usage();
76 if (id2
= strchr(id
, ':')) *id2
++ = '\0';
85 if (!(pwp
= getpwnam(id
))) {
87 std_err(": unknown user name\n");
100 if (!(grp
= getgrnam(id2
))) {
102 std_err(": unknown group name\n");
112 while (argc
--) do_chown(*argv
++);
116 /* Apply the user/group modification here.
126 if (lstat(file
, &st
)) {
132 if (S_ISLNK(st
.st_mode
) && rflag
) return; /* Note: violates POSIX. */
134 if (chown(file
, oflag
? nuid
: st
.st_uid
, gflag
? ngid
: st
.st_gid
)) {
139 if (S_ISDIR(st
.st_mode
) && rflag
) {
140 if (!(dirp
= opendir(file
))) {
145 if (path
!= file
) strcpy(path
, file
);
146 namp
= path
+ strlen(path
);
148 while (entp
= readdir(dirp
))
149 if (entp
->d_name
[0] != '.' ||
151 (entp
->d_name
[1] != '.' || entp
->d_name
[2]))) {
152 strcpy(namp
, entp
->d_name
);
160 /* Posix prototype of the chown/chgrp function */
165 std_err(gflag
? " owner[:group]" : " [owner:]group");
166 std_err(" file...\n");