3 * chmod.c - chmod() 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
13 /****** net.lib/chmod *********************************************************
16 chmod, fchmod - change mode of file
21 int chmod(const char *path, mode_t mode);
23 int fchmod(int fd, mode_t mode);
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
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.
66 Chmod() will fail and the file mode will be unchanged if:
68 [ENOTDIR] A component of the path prefix is not a directory.
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
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
87 [EIO] An I/O error occurred while reading from or writing to
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
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.
107 open(), chown(), stat()
109 *****************************************************************************
112 #include <proto/dos.h>
114 #include <sys/types.h>
115 #include <sys/stat.h>
123 const static BYTE pbits
[8] =
125 0, 0X2, 0X5, 0X7, 0X8, 0XA, 0XD, 0XF,
128 int chmod(const char *path
, mode_t mode
)
131 (pbits
[mode
& 7] << FIBB_OTR_DELETE
) |
132 (pbits
[(mode
>> 3) & 7] << FIBB_GRP_DELETE
) |
133 (pbits
[(mode
>> 6) & 7] ^ 0xf);
142 if (!SetProtection((STRPTR
)path
, prot
)) {
151 #include <libraries/usergroup.h>
152 #include <proto/exec.h>
153 #include <proto/usergroup.h>
156 #include <exec/execbase.h>
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]) {
183 FPrintf(Stderr
, usage
);
190 FPrintf(Stderr
, usage
);
194 modenum
= argv
[1]; argv
++, argc
--;
197 while (isdigit(*modenum
)) {
199 mode
+= *modenum
++ - '0';
203 FPrintf(Stderr
, usage
);
208 if (chmod(argv
++[1], mode
) == -1) {
210 PrintUserFault(errno
, "chmod");