ptg10805159
106 Files and Directories Chapter 4
$umask first print the current file mode creation mask
002
$umask -S print the symbolic form
u=rwx,g=rwx,o=rx
$umask 027 change the file mode creation mask
$umask -S print the symbolic form
u=rwx,g=rx,o=
4.9 chmod,fchmod,and fchmodatFunctions
Thechmod, fchmod,andfchmodat functions allow us to change the file access
permissions for an existing file.
#include <sys/stat.h>
int chmod(const char *pathname,mode_tmode);
int fchmod(intfd,mode_tmode);
int fchmodat(intfd,const char *pathname,mode_tmode,intflag);
All three return: 0 if OK,−1 on error
Thechmod function operates on the specified file, whereas the fchmodfunction
operates on a file that has already been opened. Thefchmodatfunction behaves like
chmodwhen thepathnameargument is absolute or when thefdargument has the value
AT_FDCWDand thepathnameargument is relative. Otherwise,fchmodatevaluates the
pathname relative to the open directory referenced by the fd argument. The flag
argument can be used to change the behavior of fchmodat—when the
AT_SYMLINK_NOFOLLOWflag is set,fchmodatdoesn’t follow symbolic links.
To change the permission bits of a file, the effective user ID of the process must be
equal to the owner ID of the file, or the process must have superuser permissions.
Themodeis specified as the bitwise OR of the constants shown in Figure4.11.
mode Description
S_ISUID set-user-ID on execution
S_ISGID set-group-ID on execution
S_ISVTX saved-text (sticky bit)
S_IRWXU read, write, and execute by user (owner)
S_IRUSR read by user (owner)
S_IWUSR write by user (owner)
S_IXUSR execute by user (owner)
S_IRWXG read, write, and execute by group
S_IRGRP read by group
S_IWGRP write by group
S_IXGRP execute by group
S_IRWXO read, write, and execute by other (world)
S_IROTH read by other (world)
S_IWOTH write by other (world)
S_IXOTH execute by other (world)
Figure 4.11 Themodeconstants forchmodfunctions, from<sys/stat.h>