system_opendir(3f) - [M_system:QUERY_FILE] open directory stream by
calling opendir(3c)
(LICENSE:PD)
subroutine system_opendir(dirname,dir,ierr)
character(len=*), intent(in) :: dirname
type(c_ptr) :: dir
integer,intent(out) :: ierr
The system_opendir(3f) procedure opens a directory stream
corresponding to the directory named by the dirname argument.
The directory stream is positioned at the first entry.
Upon successful completion, a pointer to a C dir type is returned.
Otherwise, these functions shall return a null pointer and set
IERR to indicate the error.
An error corresponds to a condition described in opendir(3c):
EACCES Search permission is denied for the component of the
path prefix of dirname or read permission is denied
for dirname.
ELOOP A loop exists in symbolic links encountered during
resolution of the dirname argument.
ENAMETOOLONG The length of a component of a pathname is longer
than {NAME_MAX}.
ENOENT A component of dirname does not name an existing
directory or dirname is an empty string.
ENOTDIR A component of dirname names an existing file that
is neither a directory nor a symbolic link to
a directory.
ELOOP More than {SYMLOOP_MAX} symbolic links were
encountered during resolution of the dirname argument.
EMFILE All file descriptors available to the process are
currently open.
ENAMETOOLONG The length of a pathname exceeds {PATH_MAX},
or pathname resolution of a symbolic link produced
an intermediate result with a length that exceeds
{PATH_MAX}.
ENFILE Too many files are currently open in the system.
The opendir() function should be used in conjunction with
readdir(), closedir(), and rewinddir() to examine the contents
of the directory (see the EXAMPLES section in readdir()). This
method is recommended for portability.
dirname name of directory to open a directory stream for
dir pointer to directory stream. If an
error occurred, it will not be associated.
ierr 0 indicates no error occurred
Sample program:
program demo_system_opendir
use M_system, only : system_opendir,system_readdir
use M_system, only : system_closedir
use iso_c_binding
implicit none
type(c_ptr) :: dir
character(len=:),allocatable :: filename
integer :: ierr
!--- open directory stream to read from
call system_opendir('.',dir,ierr)
if(ierr.eq.0)then
!--- read directory stream
do
call system_readdir(dir,filename,ierr)
if(filename.eq.' ')exit
write(*,*)filename
enddo
endif
!--- close directory stream
call system_closedir(dir,ierr)
end program demo_system_opendir
John S. Urban
Public Domain
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | dirname | |||
type(c_ptr) | :: | dir | ||||
integer, | intent(out), | optional | :: | ierr |
subroutine system_opendir(dirname,dir,ierr)
character(len=*), intent(in) :: dirname
type(c_ptr) :: dir
integer,intent(out),optional :: ierr
integer :: ierr_local
character(kind=c_char,len=1),allocatable :: temp(:)
interface
function c_opendir(c_dirname) bind(c,name="opendir") result(c_dir)
import c_char, c_int, c_ptr
character(kind=c_char),intent(in) :: c_dirname(*)
type(c_ptr) :: c_dir
end function c_opendir
end interface
temp = str2_carr(trim(dirname)) ! kludge for bug in ifort (IFORT) 2021.3.0 20210609
dir = c_opendir(temp)
if(.not.c_associated(dir)) then
ierr_local=-1
else
ierr_local=0
endif
if(present(ierr))then
ierr=ierr_local
else
write(*,'(a)')'*system_opendir* Error opening '//trim(dirname)
!x!stop 2
endif
end subroutine system_opendir