Updated PCI IDs to latest snapshot.
[tangerine.git] / workbench / network / stacks / AROSTCP / netlib / chown.c
blob839058ea4a817d7da639b18ef5447d20070d5fd1
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 <stdlib.h>
123 #include <string.h>
124 #include <exec/execbase.h>
125 int errno;
126 extern struct ExecBase *SysBase;
128 void PrintUserFault(LONG code, const UBYTE *banner);
130 const static char usage[] = "usage: chown [-fR] owner[:group] file ...";
132 void main(int argc, char *argv[])
134 struct Process *p = (struct Process *)SysBase->ThisTask;
135 BPTR Stderr = p->pr_CES ? p->pr_CES : p->pr_COS;
137 short perrors = 1, recursive = 0;
138 uid_t uid; gid_t gid = -1;
139 char *group, *user;
141 while (argc > 1 && argv[1][0] == '-') {
142 switch (argv[1][1]) {
143 case 'f':
144 perrors = 0;
145 break;
146 case 'R':
147 recursive = 1;
148 break;
149 default:
150 FPrintf(Stderr, usage);
151 exit(10);
153 argv++, argc--;
156 if (argc <= 2) {
157 FPrintf(Stderr, usage);
158 exit(10);
161 user = argv[1]; argv++, argc--;
162 group = rindex(user, ':');
164 if (group) {
165 *group++ = '\0';
166 if (StrToLong(group, &gid) < 1) {
167 struct group *gr = getgrnam(group);
168 if (gr) {
169 gid = gr->gr_gid;
170 } else {
171 if (perrors)
172 PrintUserFault(errno, "chown: getgrnam");
173 exit(RETURN_ERROR);
178 if (StrToLong(user, &uid) < 1) {
179 struct passwd *pw = getpwnam(user);
180 if (pw) {
181 uid = pw->pw_uid;
182 } else {
183 if (perrors)
184 PrintUserFault(errno, "chown: getpwnam");
185 exit(RETURN_ERROR);
189 while (argc-- > 1) {
190 if (chown(argv++[1], uid, gid) == -1) {
191 if (perrors)
192 PrintUserFault(errno, "chmod");
193 exit(RETURN_ERROR);
197 exit(0);
199 #endif