Syntax
#include <io.h> #include <sys\stat.h> int chmod(char *pathname, int pmode);Description
The pmode expression contains one or both of the constants S_IWRITE and S_IREAD, defined in <sys\stat.h>. The meanings of the values of pmode are:
Value
If you do not give permission to write to the file, chmod makes the file read-only. With the OS/2 operating system, all files are readable; you cannot give write-only permission. Thus, the modes S_IWRITE and S_IREAD | S_IWRITE set the same permission.
Specifying a pmode of S_IREAD is similar to making a file read-only with the ATTRIB system command.
chmod returns the value 0 if it successfully changes the permission setting. A return value of -1 shows an error; chmod sets errno to one of the following values:
Value
This example opens the file chmod.dat for writing after checking the file to see if writing is permissible. It then writes from the buffer to the opened file. This program takes file names passed as arguments and sets each to read-only.
#include <sys\stat.h>#include <io.h> #include <stdio.h> #include <stdlib.h> int main(void) { if (-1 == access("chmod.dat", 00)) /* Check if file exists. */ { printf("\nCreating chmod.dat.\n"); system("echo Sample Program > chmod.dat"); printf("chmod chmod.dat to be readonly.\n"); if (-1 == chmod("chmod.dat", S_IREAD)) perror("Chmod failed"); if (-1 == access("chmod.dat", 02)) printf("File chmod.dat is now readonly.\n\n"); printf("Run this program again to erase chmod.dat.\n\n"); } else { printf("\nFile chmod.dat exist.\n"); printf("chmod chmod.dat to become writable.\n"); if (-1 == chmod("chmod.dat", S_IWRITE)) perror("Chmod failed"); system("erase chmod.dat"); printf("File chmod.dat removed.\n\n"); } return 0; /**************************************************************************** If chmod.dat does not exist, the output should be : Creating chmod.dat. chmod chmod.dat to be readonly. File chmod.dat is now readonly. Run this program again to erase chmod.dat. ****************************************************************************/ }
Related Information