'\"macro stdmacro
.if n .pH g2.chmod @(#)chmod	40.35 of 10/10/89
.\" Copyright 1989 AT&T
.nr X
.if \nX=0 .ds x} chmod 2 "" "\&"
.if \nX=1 .ds x} chmod 2 ""
.if \nX=2 .ds x} chmod 2 "" "\&"
.if \nX=3 .ds x} chmod "" "" "\&"
.TH \*(x}
.SH NAME
\f4chmod\f1, \f4fchmod\f1 \- change mode of file
.SH SYNOPSIS
.PP
\f4#include <sys/types.h>\f1
.br
\f4#include <sys/stat.h>\f1
.PP
\f4int chmod(const char \(**path, mode_t mode);\f1
.PP
\f4int fchmod(int fildes, mode_t mode);\f1
.SH DESCRIPTION
\f4chmod\fP and \f4fchmod\fP set the
access permission portion of the mode of
the file whose name is given by 
.I path 
or referenced by the descriptor
.I fildes
to the bit pattern contained in 
.IR mode .
Access permission bits are interpreted as follows:
.PP
.TS
center, tab(:);
lf4 l l.
S_ISUID:04000:Set user \s-1ID\s0 on execution.
S_ISGID:020#0:Set group \s-1ID\s0 on execution if # is \f47\f1, \f45\f1, \f43\f1, or \f41\f1
::Enable mandatory file/record locking if # is \f46\f1, \f44\f1, \f42\f1, or \f40\f1
S_ISVTX:01000:Save text image  after execution.
S_IRWXU:00700:Read, write, execute by owner.
S_IRUSR:00400:Read by owner.
S_IWUSR:00200:Write by owner.
S_IXUSR:00100:Execute (search if a directory) by owner.
S_IRWXG:00070:Read, write, execute by group.
S_IRGRP:00040:Read by group.
S_IWGRP:00020:Write by group.
S_IXGRP:00010:Execute by group.
S_IRWXO:00007:Read, write, execute (search) by others.
S_IROTH:00004:Read by others.
S_IWOTH:00002:Write by others
S_IXOTH:00001:Execute by others.
.TE
.PP
Modes are constructed by
\f4OR'ing\fP the access permission bits.
.PP
The effective user 
.SM ID
of the process must match the owner of the file or the
process must have the appropriate privilege to change the mode of a file.
.PP
If the process is not a privileged process
and the file is not a directory,
mode bit 01000 (save text image on execution) is cleared.
.PP
If neither the process
nor a member of the supplementary group list is privileged,
and the effective group
.SM ID
of the process does not match the
group
.SM ID
of the file,
mode bit 02000 (set group
.SM ID
on execution)
is cleared.
.PP
If a 0410 executable file 
has the sticky bit (mode bit 01000) set,
the operating system
will not delete the program text from the swap area
when the last user process terminates.
If a 0413 or \f4ELF\f1 executable file
has the sticky bit set,
the operating system
will not delete the program text from memory
when the last user process terminates.
In either case, 
if the sticky bit is set
the text will already be available 
(either in a swap area or in memory)
when the next user of the file executes it,
thus making execution faster.
.PP
If a directory is writable and has the sticky
bit set, files within that directory can be removed or renamed only
if one or more of the following is true [see \f4unlink\fP(2) and \f4rename\fP(2)]:
.nf

	the user owns the file
	the user owns the directory
	the file is writable by the user
	the user is a privileged user

.fi
.PP
If the mode bit 02000 (set group \s-1ID\s0 on execution) is set and the
mode bit 00010 (execute or search by group) is not set, mandatory
file/record locking will exist on a regular file. This may affect
future calls to \f4open\fP(2), \f4creat\fP(2), \f4read\fP(2),
and \f4write\fP(2) on this file.
.PP
Upon successful completion, \f4chmod\fP and \f4fchmod\fP mark for update
the \f4st_ctime\fP field of the file.
.PP
\f4chmod\fP
will fail and the file mode will be unchanged
if one or more of the following are true:
.TP 20
\f4EACCES\fP
Search permission is denied on a component of the path prefix of
.IR path .
.TP 20
\f4EFAULT\fP
.I path
points outside the allocated address space of the process.
.TP 20
\f4EINTR\fP
A signal was caught during execution of the system call.
.TP 20
\f4EIO\fP
An I/O error occurred while reading from or writing to the file system.
.TP 20
\f4ELOOP\fP
Too many symbolic links were encountered in translating
.IR path .
.TP 20
\f4EMULTIHOP\fP 
Components of
.I path
require hopping to multiple remote machines
and file system type does not allow it.
.TP 20
\f4ENAMETOOLONG\fP
The length of the \f2path\f1 argument exceeds {\f4PATH_MAX\fP}, or the
length of a \f2path\f1 component exceeds {\f4NAME_MAX\fP} while
\f4_POSIX_NO_TRUNC\fP is in effect.
.TP 20
\f4ENOTDIR\fP
A component of the prefix of
.I path
is not a directory.
.TP 20
\f4ENOENT\fP
Either a component of the path prefix, or the file referred to by
.I path
does not exist or is a null pathname.
.TP 20
\f4ENOLINK\fP 
.I fildes
points to a remote machine and the link to that machine is no longer
active.
.TP 20
\f4EPERM\fP
The effective user \s-1ID\s0 does not match the owner of the file and the
process does not have appropriate privilege.
.TP 20
\f4EROFS\fP
The file referred to by
.I path
resides on a read-only file system.
.LP
\f4fchmod\fP
will fail and the file mode will be unchanged if:
.TP 20
\f4EBADF\fP
.I fildes
is not an open file descriptor
.TP 20
\f4EIO\fP
An I/O error occurred while reading from or writing to the file system.
.TP 20
\f4EINTR\fP
A signal was caught during execution of the \f4fchmod\fP system call.
.TP 20
\f4ENOLINK\fP 
.I path 
points to a remote machine and the link to that machine is no longer
active.
.TP 20
\f4EPERM\fP
The effective user \s-1ID\s0 does not match the owner of the file and the
process does not have appropriate privilege.
.TP 20
\f4EROFS\fP
The file referred to by
.I fildes
resides on a read-only file system.
.SH "SEE ALSO"
\f4chown\fP(2), \f4creat\fP(2), \f4fcntl\fP(2), \f4mknod\fP(2), \f4open\fP(2), \f4read\fP(2), \f4stat\fP(2), \f4write\fP(2),
\f4mkfifo\fP(3C), \f4stat\fP(5).
.br
\f4chmod\fP(1) in the \f2User's Reference Manual\f1.
.br
The "File and Record Locking" chapter in the \f2Application Programmer's Guide\f1.
.SH DIAGNOSTICS
Upon successful completion, a value of 0 is returned.
Otherwise, a value of \-1 is returned and
\f4errno\fP
is set to indicate the error.
