tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / workbench / network / stacks / AROSTCP / netlib / chmod.c
blob2ae27063f811ac84fccbd04cb2eeb9fab9de25ac
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 <proto/dos.h>
114 #include <sys/types.h>
115 #include <sys/stat.h>
117 #include "fibex.h"
118 #include "netlib.h"
121 * rwx -> rwed
123 const static BYTE pbits[8] =
125 0, 0X2, 0X5, 0X7, 0X8, 0XA, 0XD, 0XF,
128 int chmod(const char *path, mode_t mode)
130 LONG prot =
131 (pbits[mode & 7] << FIBB_OTR_DELETE) |
132 (pbits[(mode >> 3) & 7] << FIBB_GRP_DELETE) |
133 (pbits[(mode >> 6) & 7] ^ 0xf);
135 if (mode & S_ISVTX)
136 prot |= FIBF_PURE;
137 if (mode & S_ISUID)
138 prot |= FIBF_SUID;
139 if (mode & S_ISGID)
140 prot |= FIBF_SGID;
142 if (!SetProtection((STRPTR)path, prot)) {
143 set_errno(IoErr());
144 return -1;
145 } else {
146 return 0;
150 #ifdef DEBUGGING
151 #include <libraries/usergroup.h>
152 #include <proto/exec.h>
153 #include <proto/usergroup.h>
154 #include <stdlib.h>
155 #include <string.h>
156 #include <exec/execbase.h>
157 #include <ctype.h>
159 int errno;
160 extern struct ExecBase *SysBase;
162 void PrintUserFault(LONG code, const UBYTE *banner);
164 const static char usage[] = "usage: chmod [-fR] mode file ...";
166 void main(int argc, char *argv[])
168 struct Process *p = (struct Process *)FindTask(NULL);
169 BPTR Stderr = p->pr_CES ? p->pr_CES : p->pr_COS;
171 short perrors = 1, recursive = 0;
172 char *modenum; mode_t mode;
174 while (argc > 1 && argv[1][0] == '-') {
175 switch (argv[1][1]) {
176 case 'f':
177 perrors = 0;
178 break;
179 case 'R':
180 recursive = 1;
181 break;
182 default:
183 FPrintf(Stderr, usage);
184 exit(10);
186 argv++, argc--;
189 if (argc <= 2) {
190 FPrintf(Stderr, usage);
191 exit(10);
194 modenum = argv[1]; argv++, argc--;
196 mode = 0;
197 while (isdigit(*modenum)) {
198 mode <<= 3;
199 mode += *modenum++ - '0';
202 if (*modenum) {
203 FPrintf(Stderr, usage);
204 exit(10);
207 while (argc-- > 1) {
208 if (chmod(argv++[1], mode) == -1) {
209 if (perrors)
210 PrintUserFault(errno, "chmod");
211 exit(RETURN_ERROR);
215 exit(0);
217 #endif