This was part of the AT&T Unix standard, and incorporated into Turbo C. There was a more extensive alternative of this, that was for DOS only (named _chmod), and it may still work in Windows, but far safer to use the Windows API unless Microsoft has stated that they will continue to keep the old standard for this, in their OS.
Code:
chmod Changes access mode.
Syntax:
int chmod(const char *path, int amode);
Prototype in:
io.h
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 S_IREAD
(defined in sys\stat.h).
Value of amode ³ Access permission
S_IWRITE ³ Permission to write
S_IREAD ³ Permission to read
S_IREAD|S_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 and the global variable errno is set
to one of the following:
Setting ³ Description
ENOENT ³ Path or file name not found
EACCES ³ Permission denied
Portability:
chmod is available on UNIX systems.
See Also:
_chmod
Example:
#include <sys\stat.h>
#include <stdio.h>
#include <io.h>
void make_read_only(char *filename);
int main(void)
{
make_read_only("NOTEXIST.FIL");
make_read_only("MYFILE.FIL");
return 0;
}
void make_read_only(char *filename)
{
int stat;
stat = chmod(filename, S_IREAD);
if (stat)
printf("Couldn't make %s read-only\n", filename);
else
printf("Made %s read-only\n", filename);
}