gtpc2m23 | C/C++ Language Support User's Guide |
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);
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:
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.