Directories in Fortran

Here’s the translation of the Go code example to Fortran, along with explanations in Markdown format suitable for Hugo:

Our program demonstrates working with directories in the file system. Here’s the full source code:

program directories
    use, intrinsic :: iso_fortran_env
    use, intrinsic :: iso_c_binding
    implicit none

    integer :: stat
    character(len=256) :: cwd

    ! Create a new sub-directory in the current working directory
    call execute_command_line("mkdir -p subdir", exitstat=stat)
    if (stat /= 0) call error_handler("Failed to create directory")

    ! Helper subroutine to create a new empty file
    call create_empty_file("subdir/file1")

    ! Create a hierarchy of directories, including parents
    call execute_command_line("mkdir -p subdir/parent/child", exitstat=stat)
    if (stat /= 0) call error_handler("Failed to create directory hierarchy")

    call create_empty_file("subdir/parent/file2")
    call create_empty_file("subdir/parent/file3")
    call create_empty_file("subdir/parent/child/file4")

    ! List directory contents
    print *, "Listing subdir/parent"
    call execute_command_line("ls -l subdir/parent", exitstat=stat)
    if (stat /= 0) call error_handler("Failed to list directory")

    ! Change the current working directory
    call chdir("subdir/parent/child", stat)
    if (stat /= 0) call error_handler("Failed to change directory")

    ! List contents of the current directory
    print *, "Listing current directory (subdir/parent/child)"
    call execute_command_line("ls -l", exitstat=stat)
    if (stat /= 0) call error_handler("Failed to list directory")

    ! Change back to the original directory
    call getcwd(cwd)
    call chdir("../../..", stat)
    if (stat /= 0) call error_handler("Failed to change directory")

    ! Recursively visit the directory
    print *, "Visiting subdir recursively"
    call execute_command_line("find subdir -type d -o -type f", exitstat=stat)
    if (stat /= 0) call error_handler("Failed to recursively visit directory")

    ! Clean up (remove the created directory and its contents)
    call execute_command_line("rm -rf subdir", exitstat=stat)
    if (stat /= 0) call error_handler("Failed to remove directory")

contains

    subroutine create_empty_file(filename)
        character(len=*), intent(in) :: filename
        integer :: unit, stat
        
        open(newunit=unit, file=filename, status='replace', action='write', iostat=stat)
        if (stat /= 0) call error_handler("Failed to create file: " // filename)
        close(unit)
    end subroutine create_empty_file

    subroutine error_handler(message)
        character(len=*), intent(in) :: message
        print *, "Error: ", message
        stop 1
    end subroutine error_handler

end program directories

This Fortran program demonstrates various operations with directories:

  1. We create a new sub-directory in the current working directory using the mkdir command.

  2. We use a helper subroutine create_empty_file to create empty files.

  3. We create a hierarchy of directories using mkdir -p.

  4. We list directory contents using the ls -l command.

  5. We change the current working directory using the chdir subroutine.

  6. We recursively visit the directory structure using the find command.

  7. Finally, we clean up by removing the created directory and its contents.

Note that Fortran doesn’t have built-in functions for all directory operations, so we use system commands via execute_command_line for some tasks. This approach might not be portable across all systems.

To compile and run the program:

$ gfortran -o directories directories.f90
$ ./directories
Listing subdir/parent
total 12
drwxr-xr-x 2 user user 4096 Jul 10 10:00 child
-rw-r--r-- 1 user user    0 Jul 10 10:00 file2
-rw-r--r-- 1 user user    0 Jul 10 10:00 file3
Listing current directory (subdir/parent/child)
total 4
-rw-r--r-- 1 user user 0 Jul 10 10:00 file4
Visiting subdir recursively
subdir
subdir/file1
subdir/parent
subdir/parent/child
subdir/parent/child/file4
subdir/parent/file2
subdir/parent/file3

This program demonstrates basic directory operations in Fortran, including creating directories, changing the current directory, and listing directory contents.