list_directory Subroutine

public subroutine list_directory(path, entries, stat)

The entries of directory path, without . and .., in the order the system returns them. Links to directories are reported as ENTRY_OTHER, so a walk does not follow them. stat is nonzero if the directory cannot be read, or its entries cannot be decoded.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: path

the directory

type(dir_entry_t), intent(out), allocatable :: entries(:)

the entries; empty if stat is nonzero

integer, intent(out) :: stat

0, or 1 if the listing failed


Calls

proc~~list_directory~~CallsGraph proc~list_directory list_directory interface~c_closedir c_closedir proc~list_directory->interface~c_closedir interface~c_opendir c_opendir proc~list_directory->interface~c_opendir interface~c_readdir c_readdir proc~list_directory->interface~c_readdir proc~byte_at byte_at proc~list_directory->proc~byte_at proc~c_string c_string proc~list_directory->proc~c_string proc~entry_name entry_name proc~list_directory->proc~entry_name proc~find_layout find_layout proc~list_directory->proc~find_layout proc~is_directory is_directory proc~list_directory->proc~is_directory proc~join_path join_path proc~list_directory->proc~join_path proc~find_layout->interface~c_closedir proc~find_layout->interface~c_opendir proc~find_layout->interface~c_readdir proc~find_layout->proc~byte_at proc~find_layout->proc~c_string proc~is_directory->interface~c_closedir proc~is_directory->interface~c_opendir proc~is_directory->proc~c_string

Called by

proc~~list_directory~~CalledByGraph proc~list_directory list_directory proc~collect_files collect_files proc~collect_files->proc~list_directory proc~directory_files directory_files proc~directory_files->proc~collect_files proc~run run proc~run->proc~directory_files

Source Code

subroutine list_directory(path, entries, stat)
    !! The entries of directory `path`, without `.` and `..`, in the order the system returns them.
    !! Links to directories are reported as `ENTRY_OTHER`, so a walk does not follow them. `stat` is
    !! nonzero if the directory cannot be read, or its entries cannot be decoded.
    character(len=*), intent(in) :: path   !! the directory
    type(dir_entry_t), allocatable, intent(out) :: entries(:)   !! the entries; empty if `stat` is nonzero
    integer, intent(out) :: stat        !! 0, or 1 if the listing failed
    type(c_ptr) :: dir, p
    type(dir_entry_t) :: e
    integer :: dtype, status

    allocate (entries(0))
    stat = 1
    if (layout == 0) call find_layout(path)
    if (layout == 0) return
    dir = c_opendir(c_string(path))
    if (.not. c_associated(dir)) return
    stat = 0
    do
        p = c_readdir(dir)
        if (.not. c_associated(p)) exit
        e%name = entry_name(p)
        if (e%name == '.' .or. e%name == '..') cycle
        dtype = DT_UNKNOWN
        if (TYPE_OFFSETS(layout) >= 0) dtype = byte_at(p, TYPE_OFFSETS(layout))
        select case (dtype)
        case (DT_DIR)
            e%kind = ENTRY_DIR
        case (DT_REG)
            e%kind = ENTRY_FILE
        case (DT_LNK)
            e%kind = merge(ENTRY_OTHER, ENTRY_FILE, is_directory(join_path(path, e%name)))
        case (DT_UNKNOWN)
            e%kind = merge(ENTRY_DIR, ENTRY_FILE, is_directory(join_path(path, e%name)))
        case default
            e%kind = ENTRY_OTHER
        end select
        entries = [entries, e]
    end do
    status = c_closedir(dir)
end subroutine list_directory