sys_get_absolute_path Subroutine

public subroutine sys_get_absolute_path(filepath, abs_path, success)

Get absolute path of a file

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: filepath
character(len=*), intent(out) :: abs_path
logical, intent(out), optional :: success

Source Code

    subroutine sys_get_absolute_path(filepath, abs_path, success)
        character(len=*), intent(in) :: filepath
        character(len=*), intent(out) :: abs_path
        logical, intent(out), optional :: success
        character(len=512) :: command, temp_file
        integer :: unit, iostat

        temp_file = create_temp_file('sys_abspath_path', '.tmp')

        if (get_os_type() == OS_WINDOWS) then
            command = 'powershell -Command "(Resolve-Path -Path '''//trim(escape_shell_arg(filepath))// &
                      ''').Path" > "'//trim(escape_shell_arg(temp_file))//'"'
        else
       command = 'realpath "'//trim(escape_shell_arg(filepath))//'" > "'//trim(escape_shell_arg(temp_file))//'" 2>/dev/null'
        end if

        call execute_command_line(command, exitstat=iostat)

        if (iostat == 0) then
            open (newunit=unit, file=temp_file, status='old', iostat=iostat)
            if (iostat == 0) then
                read (unit, '(A)') abs_path
                close (unit)
                if (present(success)) success = .true.
            else
                abs_path = filepath
                if (present(success)) success = .false.
            end if
        else
            abs_path = filepath
            if (present(success)) success = .false.
        end if

        call sys_remove_file(temp_file)
    end subroutine sys_get_absolute_path