system_access(3f) - [M_system:QUERY_FILE] checks accessibility or
existence of a pathname
(LICENSE:PD)
elemental impure logical function system_access(pathname,amode)
character(len=*),intent(in) :: pathname
integer,intent(in) :: amode
The system_access(3f) function checks pathname existence and access
permissions. The function checks the pathname for accessibility
according to the bit pattern contained in amode, using the real user
ID in place of the effective user ID and the real group ID in place
of the effective group ID.
The value of amode is either the bitwise-inclusive OR of the access
permissions to be checked (R_OK, W_OK, X_OK) or the existence test
(F_OK).
pathname a character string representing a directory
pathname. Trailing spaces are ignored.
amode bitwise-inclusive OR of the values R_OK, W_OK, X_OK,
or F_OK.
If not true an error occurred or the requested access is not
granted
check if filename is accessible
Sample program:
program demo_system_access
use M_system, only : system_access, F_OK, R_OK, W_OK, X_OK
implicit none
integer :: i
character(len=80),parameter :: names(*)=[ &
'/usr/bin/bash ', &
'/tmp/NOTTHERE ', &
'/usr/local ', &
'. ', &
'PROBABLY_NOT ']
do i=1,size(names)
write(*,*)' does ',trim(names(i)),' exist? ', &
& system_access(names(i),F_OK)
write(*,*)' is ',trim(names(i)),' readable? ', &
& system_access(names(i),R_OK)
write(*,*)' is ',trim(names(i)),' writable? ', &
& system_access(names(i),W_OK)
write(*,*)' is ',trim(names(i)),' executable? ', &
& system_access(names(i),X_OK)
enddo
end program demo_system_access
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | pathname | |||
integer, | intent(in) | :: | amode |
elemental impure function system_access(pathname,amode)
implicit none
! ident_1="@(#) M_system system_access(3f) checks accessibility or existence of a pathname"
character(len=*),intent(in) :: pathname
integer,intent(in) :: amode
logical :: system_access
character(kind=c_char,len=1),allocatable :: temp(:)
interface
function c_access(c_pathname,c_amode) bind (C,name="my_access") result (c_ierr)
import c_char,c_int
character(kind=c_char,len=1),intent(in) :: c_pathname(*)
integer(kind=c_int),value :: c_amode
integer(kind=c_int) :: c_ierr
end function c_access
end interface
temp = str2_carr(trim(pathname)) ! kludge for bug in ifort (IFORT) 2021.3.0 20210609
if(c_access(temp,int(amode,kind=c_int)).eq.0)then
system_access=.true.
else
system_access=.false.
!x!if(system_errno().ne.0)then
!x! call perror('*system_access*')
!x!endif
endif
end function system_access