get_command_as_string Function

public function get_command_as_string(command) result(str)

Return the result of the command as a string.

Arguments

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

the command to run

Return Value character(len=:), allocatable

the result of that command


Calls

proc~~get_command_as_string~~CallsGraph proc~get_command_as_string popen_module::get_command_as_string interface~fgets popen_module::fgets proc~get_command_as_string->interface~fgets interface~pclose popen_module::pclose proc~get_command_as_string->interface~pclose interface~popen popen_module::popen proc~get_command_as_string->interface~popen proc~c2f_string popen_module::c2f_string proc~get_command_as_string->proc~c2f_string

Source Code

    function get_command_as_string(command) result(str)

        character(len=*),intent(in) :: command !! the command to run
        character(len=:),allocatable :: str !! the result of that command

        integer,parameter :: buffer_length = 1000 !! read stream in chunks of this size

        type(c_ptr) :: h !! for `popen`
        integer(c_int) :: istat !! `pclose` status
        character(kind=c_char,len=buffer_length) :: line !! buffer to read from `fgets`

        str = ''
        h = c_null_ptr
        h = popen(command//c_null_char,'r'//c_null_char)

        if (c_associated(h)) then
            do while (c_associated(fgets(line,buffer_length,h)))
                str = str//c2f_string(line)
            end do
            istat = pclose(h)
        end if

    end function get_command_as_string