system_chmod(3f) - [M_system_FILE_SYSTEM] call chmod(3c) to change
permission mode of a file relative to directory file descriptor
(LICENSE:PD)
function system_chmod(filename,mode) result(ierr)
character(len=*),intent(in) :: filename
integer,value,intent(in) :: mode
integer :: ierr
The system_chmod(3f) function shall change UID, _ISGID, S_ISVTX,
and the file permission bits of the file named by the pathname
pointed to by the path argument to the corresponding bits in the
mode argument. The application shall ensure that the effective
user ID of the process matches the owner of the file or the
process has appropriate privileges in order to do this.
S_ISUID, S_ISGID, S_ISVTX, and the file permission bits are
described in <sys/stat.h>.
If the calling process does not have appropriate privileges,
and if the group ID of the file does not match the effective
group ID or one of the supplementary group IDs and if the file
is a regular file, bit S_ISGID (set-group-ID on execution) in the
file's mode shall be cleared upon successful return from chmod().
Additional implementation-defined restrictions may cause the
S_ISUID and S_ISGID bits in mode to be ignored.
Upon successful completion, system_chmod() marks for update the
last file status change timestamp of the file.
Values for flag are constructed by a bitwise-inclusive OR of
flags from the following list, defined in <fcntl.h>:
AT_SYMLINK_NOFOLLOW
If path names a symbolic link, then the mode of the symbolic
link is changed.
Upon successful completion, system_chmod(3f) returns 0.
Otherwise, it returns -1 and sets errno to indicate the error. If
-1 is returned, no change to the file mode occurs.
Sample program:
program demo_system_chmod
use M_system, only : system_chmod
use M_system, only : system_stat
use M_system, only : R_GRP,R_OTH,R_USR, RWX_G, RWX_U, W_OTH, X_GRP
!use M_system, only : RWX_O, W_GRP,W_USR,X_OTH,X_USR
!use M_system, only : DEFFILEMODE, ACCESSPERMS
use,intrinsic :: iso_fortran_env, only : int64
implicit none
integer :: ierr
integer :: status
integer(kind=int64) :: buffer(13)
!Setting Read Permissions for User, Group, and Others
! The following example sets read permissions for the owner, group,
! and others.
open(file='_test1',unit=10)
write(10,*)'TEST FILE 1'
close(unit=10)
ierr=system_chmod('_test1', IANY([R_USR,R_GRP,R_OTH]))
!Setting Read, Write, and Execute Permissions for the Owner Only
! The following example sets read, write, and execute permissions
! for the owner, and no permissions for group and others.
open(file='_test2',unit=10)
write(10,*)'TEST FILE 2'
close(unit=10)
ierr=system_chmod('_test2', RWX_U)
!Setting Different Permissions for Owner, Group, and Other
! The following example sets owner permissions for CHANGEFILE to
! read, write, and execute, group permissions to read and
! execute, and other permissions to read.
open(file='_test3',unit=10)
write(10,*)'TEST FILE 3'
close(unit=10)
ierr=system_chmod('_test3', IANY([RWX_U,R_GRP,X_GRP,R_OTH]));
!Setting and Checking File Permissions
! The following example sets the file permission bits for a file
! named /home/cnd/mod1, then calls the stat() function to
! verify the permissions.
ierr=system_chmod("home/cnd/mod1", IANY([RWX_U,RWX_G,R_OTH,W_OTH]))
call system_stat("home/cnd/mod1", buffer,status)
! In order to ensure that the S_ISUID and S_ISGID bits are set,
! an application requiring this should use stat() after a
! successful chmod() to verify this.
! Any files currently open could possibly become invalid if the
! mode of the file is changed to a value which would deny access
! to that process.
end program demo_system_chmod
John S. Urban
Public Domain
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | filename | |||
integer, | intent(in), | value | :: | mode |
function system_chmod(filename,mode) result(ierr)
character(len=*),intent(in) :: filename
integer,value,intent(in) :: mode
integer :: ierr
character(kind=c_char,len=1),allocatable :: temp(:)
interface
function c_chmod(c_filename,c_mode) bind(c,name="chmod") result(c_err)
import c_char,c_int
character(kind=c_char),intent(in) :: c_filename(*)
integer(c_int),value,intent(in) :: c_mode
integer(c_int) :: c_err
end function
end interface
!-----------------------------------------------------------------------------------------------------------------------------------
temp = str2_carr(trim(filename)) ! kludge for bug in ifort (IFORT) 2021.3.0 20210609
ierr=c_chmod(temp,int(mode,kind(0_c_int)))
end function system_chmod