system_isdir(3f) - [M_system:QUERY_FILE] checks if argument is a
directory path
(LICENSE:PD)
elemental impure logical function system_isdir(pathname)
character(len=*),intent(in) :: pathname
logical :: system_isdir
The system_isdir(3f) function checks if path is a directory.
path a character string representing a directory
pathname. Trailing spaces are ignored.
The system_isdir() function should always be successful and no
return value is reserved to indicate an error.
No errors are defined.
system_islnk(3f), system_stat(3f), isreg(3f), system_perm(3f)
Sample program
program demo_system_isdir
use M_system, only : system_isdir
use M_system, only : access=>system_access, R_OK
use M_system, only : system_dir
implicit none
character(len=:),allocatable :: fnames(:)
integer :: i
character(len=80),parameter :: names(*)=[ &
& '/tmp ', &
& '/tmp/NOTTHERE ', &
& '/usr/local ', &
& '. ', &
& 'PROBABLY_NOT ']
!
do i=1,size(names)
write(*,*)' is ',trim(names(i)),' a directory? ', &
& system_isdir(names(i))
enddo
!
! EXTENDED EXAMPLE: list readable non-hidden directories in
! current directory
fnames=system_dir(pattern='*') ! list all files in current directory
! select readable directories
fnames=pack(fnames,system_isdir(fnames).and.access(fnames,R_OK))
fnames=pack(fnames,fnames(:)(1:1) .ne.'.') ! skip hidden directories
do i=1,size(fnames)
write(*,*)' ',trim(fnames(i)),' is a directory'
enddo
!
end program demo_system_isdir
Results:
is /tmp a directory? T
is /tmp/NOTTHERE a directory? F
is /usr/local a directory? T
is . a directory? T
is PROBABLY_NOT a directory? F
TEST is a directory
EXAMPLE is a directory
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | dirname |
elemental impure function system_isdir(dirname)
implicit none
! ident_10="@(#) M_system system_isdir(3f) determine if DIRNAME is a directory name"
character(len=*),intent(in) :: dirname
logical :: system_isdir
character(kind=c_char,len=1),allocatable :: temp(:)
interface
function c_isdir(dirname) bind (C,name="my_isdir") result (c_ierr)
import c_char,c_int
character(kind=c_char,len=1),intent(in) :: dirname(*)
integer(kind=c_int) :: c_ierr
end function c_isdir
end interface
temp = str2_carr(trim(dirname)) ! kludge for bug in ifort (IFORT) 2021.3.0 20210609
if(c_isdir(temp).eq.1)then
system_isdir=.true.
else
system_isdir=.false.
endif
end function system_isdir