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.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | path |
the directory |
||
| type(dir_entry_t), | intent(out), | allocatable | :: | entries(:) |
the entries; empty if |
|
| integer, | intent(out) | :: | stat |
0, or 1 if the listing failed |
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