chmod -- Change File Permission Setting

Format

#include <io.h>
#include <sys\stat.h>
int chmod(char *pathname, int pmode);

Language Level: XPG4, Extension
chmod changes the permission setting of the file specified by pathname. The permission setting controls access to the file for reading or writing. You can use chmod only if the file is closed.

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 Meaning
S_IREAD Reading permitted
S_IWRITE Writing permitted
S_IREAD | S_IWRITE Reading and writing permitted.

If you do not give permission to write to the file, chmod makes the file read-only. 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.

Note: In earlier releases of the C/C++ run-time library, chmod began with an underscore (_chmod). Because it is defined by the X/Open standard, the underscore has been removed. For compatibility, IBM C and C++ Compilers will map _chmod to chmod for you.

Return Value
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 Meaning
ENOENT The system cannot find the file or the path that you specified, or the file name was incorrect.
EOS2ERR The call to the operating system was not successful.
EINVAL The mode specified was not valid.

Example
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.
   *******************************************************************/
}


access -- Determine Access Mode
_sopen -- Open Shared File
umask -- Set File Mask of Current Process
<sys\stat.h>
<io.h>