Execing Processes in Fortran

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

Our example demonstrates how to replace the current process with another one using Fortran. This is similar to the classic exec function in Unix-like operating systems.

program exec_process
    use, intrinsic :: iso_c_binding, only: c_char, c_null_char
    use, intrinsic :: iso_fortran_env, only: error_unit
    implicit none
    
    character(len=:), allocatable :: binary
    character(len=:), allocatable :: args(:)
    integer :: status

    ! Find the full path of the 'ls' command
    call get_command_argument(0, binary)
    binary = trim(binary)

    ! Set up the arguments for the 'ls' command
    args = [character(len=2) :: 'ls', '-a', '-l', '-h']

    ! Execute the 'ls' command
    call execute_command_line(binary // ' ' // join(args), wait=.false., exitstat=status)

    if (status /= 0) then
        write(error_unit,*) 'Error executing command'
        stop 1
    end if

contains

    function join(strings) result(joined)
        character(len=*), intent(in) :: strings(:)
        character(len=:), allocatable :: joined
        integer :: i

        joined = ''
        do i = 1, size(strings)
            if (i > 1) joined = joined // ' '
            joined = joined // strings(i)
        end do
    end function join

end program exec_process

In this Fortran program:

  1. We use the iso_c_binding and iso_fortran_env modules for some system-level operations and error handling.

  2. Instead of exec.LookPath, we use get_command_argument(0) to get the name of the current executable. In a real-world scenario, you might want to implement a more sophisticated method to find the full path of the ’ls’ command.

  3. We set up the arguments for the ’ls’ command in an array of strings.

  4. The execute_command_line subroutine is used to run the ’ls’ command. This is the closest equivalent to syscall.Exec in Fortran. However, it doesn’t replace the current process; instead, it creates a new process.

  5. We use wait=.false. to make the call non-blocking, which is somewhat similar to the behavior of syscall.Exec.

  6. We check the exit status of the command and print an error message if it failed.

  7. A helper function join is implemented to concatenate the command and its arguments.

When we run our program, it will execute the ’ls’ command with the specified arguments:

$ gfortran exec_process.f90 -o exec_process
$ ./exec_process
total 16
drwxr-xr-x  4 user 136B Oct 3 16:29 .
drwxr-xr-x 91 user 3.0K Oct 3 12:50 ..
-rw-r--r--  1 user 1.3K Oct 3 16:28 exec_process.f90
-rwxr-xr-x  1 user 31K  Oct 3 16:30 exec_process

Note that Fortran doesn’t offer a direct equivalent to Unix’s fork or exec functions. The execute_command_line subroutine is the closest we can get to executing external commands, but it doesn’t replace the current process. For more complex process management, you might need to use external libraries or system-specific extensions.