Chapter Contents

Previous

Next
fchmod

fchmod



Change Directory or File Mode

Portability: POSIX.1 conforming


SYNOPSIS
DESCRIPTION
RETURN VALUE
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

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

int fchmod(int fileDescriptor, mode_t mode);


DESCRIPTION

fchmod changes the file permission flags for the directory or file specified by fileDescriptor . The mode argument can be any combination of the following symbols, which are defined in <stat.h> :
S_ISUID sets the user ID for execution. When the specified file is processed through an exec function, the user ID of the process is also set for execution.
S_ISGID sets group ID for execution. When the specified file is processed through an exec function, the group ID of the process is also set for execution.
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.


RETURN VALUE

fchmod returns 0 if it is successful. If unsuccessful, a -1 is returned.


EXAMPLE

This example changes a file whose file number is passed so that it can be executed by any user who can read it:

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

int fchexec(int fd) {
   struct stat stat_data;
   mode_t newmode;
   int rc;

   rc = fstat(fd, &stat_data);
   if (rc != 0) {
      perror("fstat 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 = fchmod(fd, newmode);         
      if (rc != 0) perror("fchmod failure");
      return rc;
   }
   return(0);    /* No change was necessary.            */
}


RELATED FUNCTIONS

chmod , chown


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

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