chmod
raw OCR
Changes file access mode.
Defined in header <sys/stat.h>
Syntax
#include <sys\stat.h>
int chmod(const char *path, int amode);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ | ■ | ■ |
Remarks
chmod sets the file-access permissions of the file given by path according to the mask given by amode. path points to a string; *path is the first character of that string.
amode can contain one or both of the symbolic constants S_IWRITE and
| SJREAD | (defined in sysXstat.h). |
| Value of amode | Access permission |
| SJWRITE | Permission to write |
| SJREAD | Permission to read |
| S IREADIS IWRITE | Permission to read and write |
Return value
Upon successfully changing the file access mode, chmod returns 0. Otherwise, chmod returns a value of -1.
In the event of an error, the global variable errno is set to one of the following:
ENOENT Path or file name not found EACCES Permission denied
See also
Example
#include <errno.h>
#include <stdio.h>
#include <io.h>
#include <process.h>
#include <sys\stat.h>
int main(void)
{
char filename [64];
struct Stat stbuf;
int amode;
printf("Enter name of file: ");
scanf("%s", filename);
if (stat (filename, &stbuf) != 0) {
perror("Unable to get file information");
return (1);
}
if (stbuf.st_mode & S_IWRITE) {
printf("Changing to read-only\n") ;
amode = S_IREAD;
else {
printf("Changing to read-write\n") ;
amode = S_IREADiS_IWRITE;
}
if (chmod (filename, amode) != 0) {
perror ("Unable to change file mode");
return(1);
}
return(0);
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Stable link: /3.1/sys/stat.h/chmod/
· short form /3.1/chmod/