ffmt_fs Module

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.


Uses

  • module~~ffmt_fs~~UsesGraph module~ffmt_fs ffmt_fs iso_c_binding iso_c_binding module~ffmt_fs->iso_c_binding

Used by

  • module~~ffmt_fs~~UsedByGraph module~ffmt_fs ffmt_fs module~ffmt_cli ffmt_cli module~ffmt_cli->module~ffmt_fs module~ffmt_config ffmt_config module~ffmt_cli->module~ffmt_config module~ffmt_walk ffmt_walk module~ffmt_cli->module~ffmt_walk module~ffmt_config->module~ffmt_fs module~ffmt_walk->module~ffmt_fs

Variables

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 d_name to try

integer, private, parameter :: TYPE_OFFSETS(*) = [18, 20, 20, -1]

matching offsets of d_type (-1: none)

integer, private, save :: layout = 0

index into NAME_OFFSETS once known


Interfaces

interface

  • private function c_closedir(dir) result(status) bind(c, name='closedir'))

    C closedir: close dir; 0 on success.

    Arguments

    Type IntentOptional Attributes Name
    type(c_ptr), value :: dir

    a directory opened by c_opendir

    Return Value integer(kind=c_int)

interface

  • private function c_getcwd(buf, size) result(p) bind(c, name='getcwd'))

    C getcwd: write the current directory into buf, NUL-terminated; a null pointer if it does not fit or cannot be determined.

    Arguments

    Type IntentOptional Attributes Name
    character(kind=c_char, len=1), intent(out) :: buf(*)

    receives the path

    integer(kind=c_size_t), value :: size

    size of buf in bytes

    Return Value type(c_ptr)

interface

  • private function c_opendir(name) result(dir) bind(c, name='opendir'))

    C opendir: open directory name for reading; a null pointer if it cannot be opened.

    Arguments

    Type IntentOptional Attributes Name
    character(kind=c_char, len=1), intent(in) :: name(*)

    the path, NUL-terminated

    Return Value type(c_ptr)

interface

  • private function c_readdir(dir) result(entry) bind(c, name='readdir'))

    C readdir: the next struct dirent of dir, or a null pointer at the end.

    Arguments

    Type IntentOptional Attributes Name
    type(c_ptr), value :: dir

    a directory opened by c_opendir

    Return Value type(c_ptr)


Derived Types

type, public ::  dir_entry_t

One entry of a directory listing.

Components

Type Visibility Attributes Name Initial
integer, public :: kind = ENTRY_OTHER

ENTRY_FILE, ENTRY_DIR or ENTRY_OTHER

character(len=:), public, allocatable :: name

the entry's name, without the directory


Functions

public function absolute_path(path) result(abs)

path made absolute by prefixing the current directory, unless it already is (starts with / or \, or a drive letter such as C:).

Arguments

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

a relative or absolute path

Return Value character(len=:), allocatable

private function byte_at(p, offset) result(b)

The byte at offset in the memory at p, as a number from 0 to 255.

Arguments

Type IntentOptional Attributes Name
type(c_ptr), intent(in) :: p

start of the memory

integer, intent(in) :: offset

in bytes, 0 for the first byte

Return Value integer

private pure function c_string(s) result(c)

s as a NUL-terminated C string.

Arguments

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

the Fortran string

Return Value character(kind=c_char, len=1), (len(s)+1)

public function current_directory() result(path)

The current working directory, or . if it cannot be determined.

Arguments

None

Return Value character(len=:), allocatable

private function entry_name(p) result(name)

The NUL-terminated name in the directory entry at p.

Arguments

Type IntentOptional Attributes Name
type(c_ptr), intent(in) :: p

a struct dirent returned by c_readdir, with the layout known

Return Value character(len=:), allocatable

private pure function is_absolute(path)

True if path starts with / or \, or has a drive letter such as C:.

Arguments

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

the path to test

Return Value logical

public function is_directory(path)

True if path is a directory (or a link to one): if it can be opened as a directory.

Arguments

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

the path to test

Return Value logical

public pure function join_path(dir, name) result(path)

dir/name, without doubling a separator. A trailing \ or : (Windows) also counts.

Arguments

Type IntentOptional 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

Return Value character(len=:), allocatable

public pure function parent_directory(path) result(parent)

The directory containing path (/a/b for /a/b/c), or '' if path has no parent.

Arguments

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

a file or directory path

Return Value character(len=:), allocatable


Subroutines

private subroutine find_layout(path)

Find the offset of d_name from the . and .. entries of directory path.

Arguments

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

a directory to list

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