These two functions allow us to change the access permissions of existing files:
# Include <sys/STAT. h> int chmod (const char * pathname, mode_t mode); int fchmod (INT filedes, mode_t mode); two function return values: 0 if successful, and-1 if an error occurs
The chmod function operates on the specified file, while the fchmod function operates on the opened file.
To change the permission bit of a file, the valid user ID of the process must be equal to the owner ID of the file, or the process must have the superuser permission.
A bitwise OR operation of constants in the mode parameter.
| Mode |
Description |
S_isuid S_isgid S_isvtx |
Set User ID during execution Set Group ID during execution Save the body (sticky position) |
S_irwxu S_irusr S_iwusr S_ixusr |
User (owner) read, write, and execute User (owner) read User (owner) Write User (owner) Execution |
S_irwxg S_irgrp S_iwgrp S_ixgrp |
Group read, write, and execution Group read Group Write Group execution |
S_irwxo S_iroth S_iwoth S_ixoth |
Other reads, writes, and executions Other reads Others Other executions
|
The chown function can be used to change the user ID and group ID of a file:
# Include <unistd. h> int chown (const char * pathname, uid_t owner, gid_t group); int fchown (INT filedes, uid_t owner, gid_t group); int lchown (const char * pathname, uid_t owner, gid_t group); return values of the three functions: 0 if successful, and-1 if an error occurs.
CHMOD and fchmod functions/chown, fchown, and lchown Functions