sys_get_current_dir Subroutine

public subroutine sys_get_current_dir(cwd, success)

Get current working directory

Arguments

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

Source Code

    subroutine sys_get_current_dir(cwd, success)
        character(len=*), intent(out) :: cwd
        logical, intent(out), optional :: success
        character(len=512) :: command, temp_file
        integer :: unit, iostat

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

        if (get_os_type() == OS_WINDOWS) then
            command = 'cd > "'//trim(escape_shell_arg(temp_file))//'"'
        else
            command = 'pwd > "'//trim(escape_shell_arg(temp_file))//'"'
        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)') cwd
                close (unit)
                if (present(success)) success = .true.
            else
                cwd = '.'
                if (present(success)) success = .false.
            end if
        else
            cwd = '.'
            if (present(success)) success = .false.
        end if

        call sys_remove_file(temp_file)
    end subroutine sys_get_current_dir