system_isreg(3f) - [M_system:QUERY_FILE] checks if argument is a
regular file
(LICENSE:PD)
elemental impure logical function system_isreg(pathname)
character(len=*),intent(in) :: pathname
logical :: system_isreg
The isreg(3f) function checks if path is a regular file
path a character string representing a pathname. Trailing spaces
are ignored.
The system_isreg() 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), system_isdir(3f), system_perm(3f)
check if filename is a regular file
program simple
use M_system, only : system_isreg
implicit none
integer :: i
character(len=80),parameter :: names(*)=[ &
'/tmp ', &
'test.txt ', &
'~/.bashrc ', &
'.bashrc ', &
'. ']
do i=1,size(names)
write(*,*)' is ',trim(names(i)),' a regular file? ', &
& system_isreg(names(i))
enddo
end program simple
EXTENDED EXAMPLE list readable non-hidden regular files and links in current directory
program demo_system_isreg
use M_system, only : isreg=>system_isreg, islnk=>system_islnk
use M_system, only : access=>system_access, R_OK
use M_system, only : system_dir
implicit none
character(len=:),allocatable :: filenames(:)
logical,allocatable :: mymask(:)
integer :: i
! list readable non-hidden regular files and links in current directory
! make list of all files in current directory
filenames=system_dir(pattern='*')
! select regular files and links
mymask= isreg(filenames).or.islnk(filenames)
! skip hidden directories in those
where(mymask) mymask=filenames(:)(1:1).ne.'.'
! select readable files in those
where(mymask) mymask=access(filenames,R_OK)
filenames=pack(filenames,mask=mymask)
write(*,'(a)')(trim(filenames(i)),i=1,size(filenames))
end program demo_system_isreg
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | pathname |
elemental impure function system_isreg(pathname)
implicit none
! ident_7="@(#) M_system system_isreg(3f) determine if pathname is a regular file"
character(len=*),intent(in) :: pathname
logical :: system_isreg
character(kind=c_char,len=1),allocatable :: temp(:)
interface
function c_isreg(pathname) bind (C,name="my_isreg") result (c_ierr)
import c_char,c_int
character(kind=c_char,len=1),intent(in) :: pathname(*)
integer(kind=c_int) :: c_ierr
end function c_isreg
end interface
! kludge for bug in ifort (IFORT) 2021.3.0 20210609
temp = str2_carr(trim(pathname))
if(c_isreg(temp).eq.1)then
system_isreg=.true.
else
system_isreg=.false.
endif
end function system_isreg