system_access Function

public impure elemental function system_access(pathname, amode)

NAME

system_access(3f) - [M_system:QUERY_FILE] checks accessibility or
                    existence of a pathname
(LICENSE:PD)

SYNOPSIS

elemental impure logical function system_access(pathname,amode)

character(len=*),intent(in) :: pathname
integer,intent(in)          :: amode

DESCRIPTION

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).

OPTIONS

    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.

RETURN VALUE

    If not true an error occurred or the requested access is not
    granted

EXAMPLE

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

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: pathname
integer, intent(in) :: amode

Return Value logical


Contents

Source Code


Source Code

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