Chapter Contents

Previous

Next
chmod

chmod



Change Directory or File Mode

Portability: POSIX.1 conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <sys/types.h>
#include <sys/stat.h>

int chmod(const char *pathname, mode_t mode);


DESCRIPTION

chmod changes the mode bits for the directory or file specified by pathname .

The following symbols are defined in the <sys/stat.h> include file:
S_ISUID sets the execution user ID. When the specified file is processed through an exec function, the user id of the file owner becomes the effective user ID of the process.
S_ISGID sets execution group ID. When the specified file is processed through an exec function, the group ID that owns the file becomes the effective group ID of the process.
S_ISVTX specifies shared text.
S_IRUSR sets file owner permission to read.
S_IWUSR sets file owner permission to write.
S_IXUSR sets file owner permission to execute.
S_IRWXU sets file owner permission to read, write, and execute.
S_IRGRP sets group permission to read.
S_IWGRP sets group permission to write.
S_IXGRP sets group permission to execute.
S_IRWXG sets group permission to read, write, and execute.
S_IROTH sets general permission to read.
S_IWOTH sets general permission to write.
S_IXOTH sets general permission to execute.
S_IRWXO sets general permission to read, write, and execute.

A process can set mode bits if it has superuser authority, or if the user ID is the same as that of the file's owner. The S_ISGID bit in the file's mode bits is cleared if


RETURN VALUE

chmod returns 0 if it is successful and -1 if it is not successful.


EXAMPLE

This example changes a file (named by the function argument) so that it can be executed by any user who can read it.

#include <sys/types.h>
#include <sys/stat.h>

int chexec(const char *name) {
   struct stat stat_data;
   mode_t newmode;
   int rc;

   rc = stat(name, &stat_data);
   if (rc != 0) {
      perror("stat failure");
      return -1;
   }
   newmode = stat_data.st_mode;
   if (newmode & S_IRUSR) newmode |= S_IXUSR;
   if (newmode & S_IRGRP) newmode |= S_IXGRP;
   if (newmode & S_IROTH) newmode |= S_IXOTH;

      /* If the mode bits changed, make them effective. */
   if (newmode != stat_data.st_mode) {  
      rc = chmod(name, newmode);        
      if (rc != 0) perror("chmod failure");
      return rc;
   }
   return(0);               /* No change was necessary. */
}


RELATED FUNCTIONS

chown , fchmod


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.