gtpc2m24C/C++ Language Support User's Guide

fchown-Change the Owner and Group by File Descriptor

This function changes the owner and group by a file descriptor.

Format

#include <unistd.h>
int fchown(int fildes, uid_t owner, gid_t group);

fildes
An open file descriptor for the file or directory whose owner is to be changed.

owner
The new user ID (UID) for the file or directory.

group
The new group ID (GID) for the file or directory.

A process can change the group of a file only if the effective user ID of the process is equal to the user ID of the file owner or if the process has appropriate privileges (superuser authority). If the file is a regular file and the process does not have superuser authority, the chown function clears the S_ISUID and S_ISGID bits in the mode of the file.

TPF deviation from POSIX

The TPF system does not support the ctime time stamp.

Normal Return

If successful, the fchown function updates the owner and group for the file and returns 0.

Error Return

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

EBADF
fildes is not a valid open file descriptor.

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

Programming Considerations

None.

Examples

The following example changes the owner ID and group ID.

#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.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 owner was %d and group was %d\n", info.st_uid,
           info.st_gid);
    if (fchown(fd, 25, 0) != 0)
      perror("fchown() error");
    else {
      stat(fn, &info);
      printf("after fchown(), owner is %d and group is %d\n",
             info.st_uid, info.st_gid);
    }
    close(fd);
    unlink(fn);
  }
}

Output

original owner was 256 and group was 257
after fchown(), owner is 25 and group is 0

Related Information

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