3 * chown.c - chown() for usergroup link library
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
9 * This function is based on SetOwner37 code
10 * Copyright © 1993 by Geert Uytterhoeven
14 /****** net.lib/chown *********************************************************
17 chown - change owner and group of a file
22 success = chown(path, owner, group)
24 int chown(const char *, uid_t, gid_t);
27 The owner ID and group ID of the file named by path or referenced by
28 fd is changed as specified by the arguments owner and group. The
29 owner of a file may change the group to a group of which he or she is
30 a member, but the change owner capability is restricted to the
33 Chown() clears the set-user-id and set-group-id bits on the file to
34 prevent accidental or mischievious creation of set-user-id and
35 set-group-id programs.
37 One of the owner or group id's may be left unchanged by specifying it
40 If the final component of path is a symbolic link, the ownership and
41 group of the symbolic link is changed, not the ownership and group of
42 the file or directory to which it points.
45 Zero is returned if the operation was successful; -1 is returned if an
46 error occurs, with a more specific error code being placed in the
47 global variable errno.
50 Chown() will fail and the file will be unchanged if:
52 [ENOTDIR] A component of the path prefix is not a directory.
54 [EINVAL] The pathname contains a character with the high-order
58 A component of a pathname exceeded 80 characters, or an
59 entire path name exceeded 1023 characters.
61 [ENOENT] The named file does not exist.
63 [EACCES] Search permission is denied for a component of the path
66 [ELOOP] Too many symbolic links were encountered in translating
69 [EPERM] The effective user ID is not the super-user.
71 [EROFS] The named file resides on a read-only file system.
73 [EFAULT] Path points outside the process's allocated address
76 [EIO] An I/O error occurred while reading from or writing to
82 *****************************************************************************
85 #include <libraries/usergroup.h>
86 #include <proto/dos.h>
87 #include <dos/dosextens.h>
91 #ifndef ACTION_SET_OWNER
92 #define ACTION_SET_OWNER 1036
95 int chown(const char *name
, uid_t uid
, gid_t gid
)
100 if (lock
= Lock((STRPTR
)name
, ACCESS_READ
)) {
101 if (uid
== -1 || gid
== -1) {
102 /* XXX We are supposed to do stat() and find out the suitable value */
105 rc
= DoPkt(((struct FileLock
*)BADDR(lock
))->fl_Task
,
107 NULL
, lock
, MKBADDR("\0"),
108 (UG2MU(uid
) << 16) | UG2MU(gid
), NULL
);
121 #include <proto/usergroup.h>
122 #include <proto/exec.h>
125 #include <exec/execbase.h>
127 extern struct ExecBase
*SysBase
;
129 void PrintUserFault(LONG code
, const UBYTE
*banner
);
131 const static char usage
[] = "usage: chown [-fR] owner[:group] file ...";
133 void main(int argc
, char *argv
[])
135 struct Process
*p
= (struct Process
*)FindTask(NULL
);
136 BPTR Stderr
= p
->pr_CES
? p
->pr_CES
: p
->pr_COS
;
138 short perrors
= 1, recursive
= 0;
139 uid_t uid
; gid_t gid
= -1;
142 while (argc
> 1 && argv
[1][0] == '-') {
143 switch (argv
[1][1]) {
151 FPrintf(Stderr
, usage
);
158 FPrintf(Stderr
, usage
);
162 user
= argv
[1]; argv
++, argc
--;
163 group
= rindex(user
, ':');
167 if (StrToLong(group
, &gid
) < 1) {
168 struct group
*gr
= getgrnam(group
);
173 PrintUserFault(errno
, "chown: getgrnam");
179 if (StrToLong(user
, &uid
) < 1) {
180 struct passwd
*pw
= getpwnam(user
);
185 PrintUserFault(errno
, "chown: getpwnam");
191 if (chown(argv
++[1], uid
, gid
) == -1) {
193 PrintUserFault(errno
, "chmod");