Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / workbench / network / stacks / AROSTCP / netlib / chmod.c
blob08523ba292b5ca44566e3c0b5e4a19459e6f3028
1 /* $Id$
3 * chmod.c - chmod() for usergroup link library
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
7 * All rights reserved.
9 * This function is based on SetOwner37 code
10 * Copyright © 1993 by Geert Uytterhoeven
13 /****** net.lib/chmod *********************************************************
15 NAME
16 chmod, fchmod - change mode of file
18 SYNOPSIS
19 #include <sys/stat.h>
21 int chmod(const char *path, mode_t mode);
23 int fchmod(int fd, mode_t mode);
25 DESCRIPTION
26 The function chmod() sets the file permission bits of the file
27 specified by the pathname path to mode. Fchmod() sets the permission
28 bits of the specified file descriptor fd. Chmod() verifies that the
29 process owner (user) either owns the file specified by path (or fd),
30 or is the super-user. A mode is created from or'd permission bit
31 masks defined in <sys/stat.h>:
33 #define S_IRWXU 0000700 \* RWX mask for owner *\
34 #define S_IRUSR 0000400 \* R for owner *\
35 #define S_IWUSR 0000200 \* W for owner *\
36 #define S_IXUSR 0000100 \* X for owner *\
38 #define S_IRWXG 0000070 \* RWX mask for group *\
39 #define S_IRGRP 0000040 \* R for group *\
40 #define S_IWGRP 0000020 \* W for group *\
41 #define S_IXGRP 0000010 \* X for group *\
43 #define S_IRWXO 0000007 \* RWX mask for other *\
44 #define S_IROTH 0000004 \* R for other *\
45 #define S_IWOTH 0000002 \* W for other *\
46 #define S_IXOTH 0000001 \* X for other *\
48 #define S_ISUID 0004000 \* set user id on execution *\
49 #define S_ISGID 0002000 \* set group id on execution *\
50 #define S_ISVTX 0001000 \* save swapped text even after use *\
52 The ISVTX (the sticky bit) indicates to the system which executable
53 files are shareable (pure).
55 Writing or changing the owner of a file turns off the set-user-id
56 and set-group-id bits unless the user is the super-user. This makes
57 the system somewhat more secure by protecting set-user-id
58 (set-group-id) files from remaining set-user-id (set-group-id) if
59 they are modified.
61 RETURN VALUES
62 Upon successful completion, a value of 0 is returned. Otherwise, a
63 value of -1 is returned and errno is set to indicate the error.
65 ERRORS
66 Chmod() will fail and the file mode will be unchanged if:
68 [ENOTDIR] A component of the path prefix is not a directory.
70 [ENAMETOOLONG]
71 A component of a pathname exceeded 255 characters, or
72 an entire path name exceeded 1023 characters.
74 [ENOENT] The named file does not exist.
76 [EACCES] Search permission is denied for a component of the
77 path prefix.
79 [EPERM] The effective user ID does not match the owner of the
80 file and the effective user ID is not the super-user.
82 [EROFS] The named file resides on a read-only file system.
84 [EFAULT] Path points outside the process's allocated address
85 space.
87 [EIO] An I/O error occurred while reading from or writing to
88 the file system.
90 Fchmod() will fail if:
92 [EBADF] The descriptor is not valid.
94 [EINVAL] Fd refers to a socket, not to a file.
96 [EROFS] The file resides on a read-only file system.
98 [EIO] An I/O error occurred while reading from or writing to
99 the file system.
101 NOTES
102 This call is provided for Unix compatibility. It does not know all
103 Amiga protection bits (Delete, Archive, Script). The archive and
104 script bits are cleared, Delete set according the Write bit.
106 SEE ALSO
107 open(), chown(), stat()
109 *****************************************************************************
112 #include <libraries/usergroup.h>
113 #include <proto/dos.h>
115 #include <sys/types.h>
116 #include <sys/stat.h>
118 #include "fibex.h"
119 #include "netlib.h"
122 * rwx -> rwed
124 const static BYTE pbits[8] =
126 0, 0X2, 0X5, 0X7, 0X8, 0XA, 0XD, 0XF,
129 int chmod(const char *path, mode_t mode)
131 LONG prot =
132 (pbits[mode & 7] << FIBB_OTR_DELETE) |
133 (pbits[(mode >> 3) & 7] << FIBB_GRP_DELETE) |
134 (pbits[(mode >> 6) & 7] ^ 0xf);
136 if (mode & S_ISVTX)
137 prot |= FIBF_PURE;
138 if (mode & S_ISUID)
139 prot |= FIBF_SUID;
140 if (mode & S_ISGID)
141 prot |= FIBF_SGID;
143 if (!SetProtection((STRPTR)path, prot)) {
144 set_errno(IoErr());
145 return -1;
146 } else {
147 return 0;
151 #ifdef DEBUGGING
152 #include <proto/usergroup.h>
153 #include <stdlib.h>
154 #include <string.h>
155 #include <exec/execbase.h>
156 #include <ctype.h>
158 int errno;
159 extern struct ExecBase *SysBase;
161 void PrintUserFault(LONG code, const UBYTE *banner);
163 const static char usage[] = "usage: chmod [-fR] mode file ...";
165 void main(int argc, char *argv[])
167 struct Process *p = (struct Process *)SysBase->ThisTask;
168 BPTR Stderr = p->pr_CES ? p->pr_CES : p->pr_COS;
170 short perrors = 1, recursive = 0;
171 char *modenum; mode_t mode;
173 while (argc > 1 && argv[1][0] == '-') {
174 switch (argv[1][1]) {
175 case 'f':
176 perrors = 0;
177 break;
178 case 'R':
179 recursive = 1;
180 break;
181 default:
182 FPrintf(Stderr, usage);
183 exit(10);
185 argv++, argc--;
188 if (argc <= 2) {
189 FPrintf(Stderr, usage);
190 exit(10);
193 modenum = argv[1]; argv++, argc--;
195 mode = 0;
196 while (isdigit(*modenum)) {
197 mode <<= 3;
198 mode += *modenum++ - '0';
201 if (*modenum) {
202 FPrintf(Stderr, usage);
203 exit(10);
206 while (argc-- > 1) {
207 if (chmod(argv++[1], mode) == -1) {
208 if (perrors)
209 PrintUserFault(errno, "chmod");
210 exit(RETURN_ERROR);
214 exit(0);
216 #endif