gtpc2m23C/C++ Language Support User's Guide

fchmod-Change the Mode of a File or Directory by Descriptor

This function changes the mode of a file or directory by a file descriptor.

Format

#include <sys/stat.h>
int fchmod(int fildes, mode_t);

fildes
An open file descriptor for the file or directory whose permissions are to be changed.

mode
The new permission. The mode argument is created with one of the symbols defined in the sys/stat.h header file. See chmod-Change the Mode of a File or Directory for information about these symbols and the mode parameter.

This function sets the file permission bits of the open file identified by fildes, its file descriptor.

A process can set mode bits only if the effective user ID of the process is the same as the file's owner or if the process has appropriate privileges (superuser authority). The chmod function automatically clears the S_ISGID bit in the file's mode bits if all the following conditions are true:

TPF deviation from POSIX

The TPF system does not support the ctime time stamp.

Normal Return

If successful, the fchmod function returns 0.

Error Return

If unsuccessful, the fchmod function returns -1 and sets errno to one of the following:

EBADF
fildes is not a valid open file descriptor.

EPERM
The user ID (UID) does not match the owner of the file.

Programming Considerations

None.

Examples

The following example changes a file permission.

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
 
main() {
  char fn[]="temp.file";
  int  fd;
  struct stat info;
 
  if ((fd = creat(fn, S_IWUSR)) < 0)
    perror("creat() error");
  else {
    stat(fn, &info);
    printf("original permissions were: %08x\n", info.st_mode);
    if (fchmod(fd, S_IRWXU|S_IRWXG) != 0)
      perror("fchmod() error");
    else {
      stat(fn, &info);
      printf("after fchmod(), permissions are: %08x\n", info.st_mode);
    }
    close(fd);
    unlink(fn);
  }
}

Output

original permissions were: 03000080
after fchmod(), permissions are: 030001f8

Related Information

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.