Portable file system access that standard Fortran lacks: listing a directory and telling
directories from files. Written in Fortran with interfaces to opendir, readdir and closedir,
which POSIX systems and MinGW-w64 (gfortran on Windows) provide.
The layout of struct dirent differs between systems, and gfortran's preprocessor does not say
which system it is compiling for, so the layout is found at run time: the offset of d_name is
where the . and .. entries, which every listing has, show their names. The known layouts
are Linux (offset 19), macOS (21), FreeBSD (24), and MinGW or macOS with 32-bit inodes (8).
Where the layout has a d_type field it is used; otherwise a directory is recognized by opening it.
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer, | private, | parameter | :: | DT_DIR | = | 4 | |
| integer, | private, | parameter | :: | DT_LNK | = | 10 | |
| integer, | private, | parameter | :: | DT_REG | = | 8 | |
| integer, | private, | parameter | :: | DT_UNKNOWN | = | 0 | |
| integer, | public, | parameter | :: | ENTRY_DIR | = | 2 |
a directory |
| integer, | public, | parameter | :: | ENTRY_FILE | = | 1 |
a regular file (or a link to one) |
| integer, | public, | parameter | :: | ENTRY_OTHER | = | 3 |
a link to a directory, or anything else |
| integer, | private, | parameter | :: | MAX_NAME | = | 1024 | |
| integer, | private, | parameter | :: | NAME_OFFSETS(*) | = | [19, 21, 24, 8] |
offsets of |
| integer, | private, | parameter | :: | TYPE_OFFSETS(*) | = | [18, 20, 20, -1] |
matching offsets of |
| integer, | private, | save | :: | layout | = | 0 |
index into NAME_OFFSETS once known |
C closedir: close dir; 0 on success.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(c_ptr), | value | :: | dir |
a directory opened by |
C getcwd: write the current directory into buf, NUL-terminated; a null pointer if
it does not fit or cannot be determined.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(kind=c_char, len=1), | intent(out) | :: | buf(*) |
receives the path |
||
| integer(kind=c_size_t), | value | :: | size |
size of |
C opendir: open directory name for reading; a null pointer if it cannot be opened.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(kind=c_char, len=1), | intent(in) | :: | name(*) |
the path, NUL-terminated |
C readdir: the next struct dirent of dir, or a null pointer at the end.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(c_ptr), | value | :: | dir |
a directory opened by |
One entry of a directory listing.
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer, | public | :: | kind | = | ENTRY_OTHER |
|
|
| character(len=:), | public, | allocatable | :: | name |
the entry's name, without the directory |
path made absolute by prefixing the current directory, unless it already is
(starts with / or \, or a drive letter such as C:).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | path |
a relative or absolute path |
The byte at offset in the memory at p, as a number from 0 to 255.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(c_ptr), | intent(in) | :: | p |
start of the memory |
||
| integer, | intent(in) | :: | offset |
in bytes, 0 for the first byte |
s as a NUL-terminated C string.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | s |
the Fortran string |
The current working directory, or . if it cannot be determined.
The NUL-terminated name in the directory entry at p.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(c_ptr), | intent(in) | :: | p |
a |
True if path starts with / or \, or has a drive letter such as C:.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | path |
the path to test |
True if path is a directory (or a link to one): if it can be opened as a directory.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | path |
the path to test |
dir/name, without doubling a separator. A trailing \ or : (Windows) also counts.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | dir |
the directory, and the name to append to it |
||
| character(len=*), | intent(in) | :: | name |
the directory, and the name to append to it |
The directory containing path (/a/b for /a/b/c), or '' if path has no parent.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | path |
a file or directory path |
Find the offset of d_name from the . and .. entries of directory path.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | path |
a directory to list |
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 |