fchmod()--Change File Authorizations by Descriptor


  Syntax
 #include <sys/stat.h>

 int fchmod(int fildes, mode_t mode);  
  Service Program Name: QP0LLIB1

  Default Public Authority: *USE

  Threadsafe: Conditional; see Usage Notes for chmod().

The fchmod() function changes S_ISUID, S_ISGID, S_ISVTX, and the permission bits of the open file or directory, identified by fildes, to the corresponding bits specified in mode. fchmod() has no effect on file descriptions for files that are open at the time fchmod() is called.

fchmod() marks for update the change time of the file.

If the file is checked out by another user (someone other than the user profile of the current job), fchmod() fails with the [EBUSY] error.


Parameters

fildes
(Input) The file descriptor of the file.
mode
(Input) Bits that define S_ISUID, S_ISGID, S_ISVTX, and the access permissions of the file.

The mode argument is created with one of the symbols defined in the <sys/stat.h> header file. For more information about the symbols, see chmod()--Change File Authorizations.

If bits other than the bits listed above are set in mode, fchmod() returns the [EINVAL] error.


Authorities

Note: Adopted authority is not used.

Authorization Required for fchmod() (excluding QDLS)


Authorization Required for fchmod() in the QDLS File System


Return Value

0
fchmod() was successful.
-1
fchmod() was not successful. The errno global variable is set to indicate the error.

Error Conditions

If fchmod() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.

If interaction with a file server is required to access the object, errno could indicate one of the following errors:



Error Messages

The following messages may be sent from this API:


Related Information


Example

The following example changes a file permission.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

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

main() {
  char fn[]="temp.file";
  int  file_descriptor;
  struct stat info;

  if ((file_descriptor = creat(fn, S_IWUSR)) < 0)
    perror("creat() error");
  else {
    if (stat(fn, &info)!= 0)
       perror("stat() error");
    else {
       printf("original permissions were: %08o\n", info.st_mode);
    }
    if (fchmod(file_descriptor, S_IRWXU|S_IRWXG) != 0)
      perror("fchmod() error");
    else {
      if (stat(fn, &info)!= 0)
         perror("stat() error");
      else { 
         printf("after fchmod(), permissions are: %08o\n", info.st_mode);
      }
    }
    if (close(file_descriptor)!= 0)
       perror("close() error");
    if (unlink(fn)!= 0)
       perror("unlink() error");
  }
}

Output:

original permissions were: 00100200
after fchmod(), permissions are: 00100770


API introduced: V3R1

[ Back to top | UNIX-Type APIs | APIs by category ]