tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / workbench / network / stacks / AROSTCP / netlib / chown.c
blob8f23ec19c53e356d0a7d283b6fa624fd5259aca9
1 /* $Id$
3 * chown.c - chown() for usergroup link library
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
7 * All rights reserved.
8 *
9 * This function is based on SetOwner37 code
10 * Copyright © 1993 by Geert Uytterhoeven
14 /****** net.lib/chown *********************************************************
16 NAME
17 chown - change owner and group of a file
19 SYNOPSIS
20 #include <unistd.h>
22 success = chown(path, owner, group)
24 int chown(const char *, uid_t, gid_t);
26 DESCRIPTION
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
31 super-user.
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
38 as -1.
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.
44 RETURN VALUES
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.
49 ERRORS
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
55 bit set.
57 [ENAMETOOLONG]
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
64 prefix.
66 [ELOOP] Too many symbolic links were encountered in translating
67 the pathname.
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
74 space.
76 [EIO] An I/O error occurred while reading from or writing to
77 the file system.
79 SEE ALSO
80 chmod(2)
82 *****************************************************************************
85 #include <libraries/usergroup.h>
86 #include <proto/dos.h>
87 #include <dos/dosextens.h>
89 #include "netlib.h"
91 #ifndef ACTION_SET_OWNER
92 #define ACTION_SET_OWNER 1036
93 #endif
95 int chown(const char *name, uid_t uid, gid_t gid)
97 BPTR lock;
98 short rc = -1;
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,
106 ACTION_SET_OWNER,
107 NULL, lock, MKBADDR("\0"),
108 (UG2MU(uid) << 16) | UG2MU(gid), NULL);
109 UnLock(lock);
112 if (!rc) {
113 set_errno(IoErr());
114 return -1;
115 } else {
116 return 0;
120 #ifdef DEBUGGING
121 #include <proto/usergroup.h>
122 #include <proto/exec.h>
123 #include <stdlib.h>
124 #include <string.h>
125 #include <exec/execbase.h>
126 int errno;
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;
140 char *group, *user;
142 while (argc > 1 && argv[1][0] == '-') {
143 switch (argv[1][1]) {
144 case 'f':
145 perrors = 0;
146 break;
147 case 'R':
148 recursive = 1;
149 break;
150 default:
151 FPrintf(Stderr, usage);
152 exit(10);
154 argv++, argc--;
157 if (argc <= 2) {
158 FPrintf(Stderr, usage);
159 exit(10);
162 user = argv[1]; argv++, argc--;
163 group = rindex(user, ':');
165 if (group) {
166 *group++ = '\0';
167 if (StrToLong(group, &gid) < 1) {
168 struct group *gr = getgrnam(group);
169 if (gr) {
170 gid = gr->gr_gid;
171 } else {
172 if (perrors)
173 PrintUserFault(errno, "chown: getgrnam");
174 exit(RETURN_ERROR);
179 if (StrToLong(user, &uid) < 1) {
180 struct passwd *pw = getpwnam(user);
181 if (pw) {
182 uid = pw->pw_uid;
183 } else {
184 if (perrors)
185 PrintUserFault(errno, "chown: getpwnam");
186 exit(RETURN_ERROR);
190 while (argc-- > 1) {
191 if (chown(argv++[1], uid, gid) == -1) {
192 if (perrors)
193 PrintUserFault(errno, "chmod");
194 exit(RETURN_ERROR);
198 exit(0);
200 #endif