Name
chmod - Changes file permissions.
Syntax
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
int chmod(pathname, pmode)
char *pathname;
int pmode;
Description
The chmod function changes the permission setting of the
file specified by pathname. The permission setting controls
read and write access to the file. The constant expression
pmode contains one or both of the manifest constants
S_IWRITE and S_IREAD, defined in sys/stat.h. Any other
values for pmode are ignored. When both constants are given,
they are joined with the bitwise-OR operator ( | ). The
meaning of the pmode argument is as follows:
Value Meaning
S_IWRITE Writing permitted
S_IREAD Reading permitted
S_IREAD | S_IWRITE Reading and writing permitted
If write permission is not given, the file is made read-
only. Under DOS and OS/2, all files are readable; it is not
possible to give write-only permission. Thus, the modes
S_IWRITE and S_IREAD | S_IWRITE are equivalent.
Return Value
The chmod function returns the value 0 if the permission
setting is successfully changed. A return value of -1
indicates an error; in this case, errno is set to ENOENT,
indicating that the specified file could not be found.
See Also
access(DOS), creat(DOS), fstat(DOS), open(DOS), stat(DOS)
Example
#include <sys/types.h> #include <sys/stat.h> #include <io.h>
#include <stdio.h>
int result; int savestderr;
main()
{
/* make file read-only:*/
result = chmod("data", S_IREAD);
if (result == -1)
perror("Can't change mode; file not found");
else
printf("Mode changed successfully\n");
}
This program uses chmod to change the mode of the file data
to read-only. It then displays a message indicating whether
the mode was changed successfully.
(printed 6/18/89)