process_readline(3fm) - [M_process] read a line of output from
a system command as a character variable
(LICENSE:PD)
subroutine process_readline(string,fp,ierr)
character(len=*) :: string
type(streampointer) :: fp
integer :: ierr
The M_process Fortran procedures use the ISO_C_BINDING interface to define Fortran-callable versions of the C procedures popen(3c)/pclose(3c) and fgets(3c)/fputs(3c). A set of record-oriented wrapper routines are then used to create a simple Fortran-callable interface.
A POSIX C interface is generally available but may require using a Linux subwindow or an application such as CygWin on MSWindows platforms.
See “M_process” for an extended description.
string data line to receive from process
fp C file pointer returned by process_open_*()
ierr error flag returned.
o process_writeline(3f) : negative indicates an error
o process_readline(3f) : Non-zero indicates an error
maximum character value length is currently 4096
This example shows a routine reading the output of a system command.
program demo_process_readline use M_process ,ONLY: process_open_read, process_readline use M_process ,ONLY: streampointer, process_close implicit none type(streampointer) :: fp ! line of data to read (assumed long enough to hold any output line) character(len=4096) :: line integer :: ierr ! open process to read from call process_open_read(‘ls -l’,fp,ierr) write(,)’READLINE: process is opened with status ‘,ierr ierr=0 do while(ierr .eq. 0) ! read a line from the process call process_readline(line,fp,ierr) if(ierr.ne.0)then write(,)’READLINE: ierr is ‘,ierr exit endif write(,)’READLINE: ‘,trim(line) enddo call process_close(fp,ierr) write(,)’READLINE: process closed with status ‘,ierr end program demo_process_readline
Sample output:
READLINE: process is opened with status 0 READLINE: total 108 READLINE: -rw-r–r–. 1 urbanjs urbanjs 3731 Oct 17 14:49 build.sh READLINE: -rw-rw-r–. 1 urbanjs urbanjs 56633 Oct 17 14:50 build.sh.log READLINE: drwxrwxr-x. 3 urbanjs urbanjs 4096 Oct 17 14:50 doc READLINE: -rw-rw-r–. 1 urbanjs urbanjs 39459 Oct 17 15:16 M_process.ff READLINE: -rw-rw-r–. 1 urbanjs urbanjs 826 Oct 17 15:17 xx.f90 READLINE: ierr is -1 READLINE: process closed with status 0
o PIPES: pipe(3c), popen(3c), pclose(3c), fflush(3c)
o NAMED PIPES: mkfifo(3c), mknod(3c)
o SUBPROCESSES: fork(3c)
o OTHER: fflush(3c)
John S. Urban
Public Domain
ios = system_pclose(fp%handle) if(process_debug)then write(,) ‘process_readline Closed pipe with status ‘,ios endif
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(out) | :: | readfrom | |||
type(streampointer), | intent(in) | :: | fp | |||
integer, | intent(out) | :: | ierr |
subroutine process_readline(readfrom,fp,ierr)
! ident_6="@(#)M_process::process_readline(3f): read line from process"
! readfrom length must be at least two
character(len=*),intent(out) :: readfrom
type(streampointer),intent(in) :: fp
integer,intent(out) :: ierr
integer (kind=c_int) :: clen
integer :: eos, i
integer :: ios
clen=len(readfrom)-1
readfrom=' '
do while (c_associated(system_fgets(readfrom, clen, fp%handle)))
eos=2
do i=1, clen
if (readfrom(i:i) == C_NULL_CHAR) then
eos=i-2 ! assuming line terminator character and
! line string terminator should not be printed
readfrom(eos+1:)=' '
exit
endif
enddo
if(process_debug)then
write(*,*) eos, ': "', trim(readfrom(1:eos)), '"'
endif
ierr=0
return
end do
! an error occurred
ios=0
!!ios = system_pclose(fp%handle)
!!if(process_debug)then
!! write(*,*) '*process_readline* Closed pipe with status ',ios
!!endif
ierr=min(-1,ios)
end subroutine process_readline