process_open_read Subroutine

public subroutine process_open_read(cmd, fp, ierr)

NAME

process_open_read(3fm) - [M_process] open a process for reading using
POSIX interface
(LICENSE:PD)

SYNOPSIS

 subroutine process_open_read(cmd,fp,ierr)

   character(len=*)    :: cmd
   type(streampointer) :: fp
   integer             :: ierr

DESCRIPTION

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.

OPTIONS

cmd      command passed to system to start 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

EXAMPLES

This example shows a routine to read the output of a system command.

program demo_process_open_read 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(,)’READTEST: 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(,)’READTEST: ierr is ‘,ierr exit endif write(,)’READTEST: ‘,trim(line) enddo call process_close(fp,ierr) write(,)’READTEST: process closed with status ‘,ierr end program demo_process_open_read

Sample output:

READTEST: process is opened with status 0 READTEST: total 108 READTEST: -rw-r–r–. 1 urbanjs urbanjs 3731 Oct 17 14:49 build.sh READTEST: -rw-rw-r–. 1 urbanjs urbanjs 56633 Oct 17 14:50 build.sh.log READTEST: drwxrwxr-x. 3 urbanjs urbanjs 4096 Oct 17 14:50 doc READTEST: -rw-rw-r–. 1 urbanjs urbanjs 39459 Oct 17 15:16 M_process.ff READTEST: -rw-rw-r–. 1 urbanjs urbanjs 826 Oct 17 15:17 xx.f90 READTEST: ierr is -1 READTEST: process closed with status 0

SEE ALSO

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)

AUTHOR

John S. Urban

LICENSE

Public Domain

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: cmd
type(streampointer), intent(out) :: fp
integer, intent(out) :: ierr

Contents

Source Code


Source Code

subroutine process_open_read(cmd,fp,ierr)

! ident_2="@(#)M_process::process_open_read(3f): open process to read from"

! shell command to start process with
character(len=*),intent(in)     :: cmd
! file pointer returned for process
type(streampointer),intent(out) :: fp
! status for attempt to open process (0= no error)
integer,intent(out)             :: ierr
! read/write mode parameter to pass to popen(3c)
character(len=3),parameter      :: mode='r'
!-------------------------------------------------------------------------------
   ierr=0
   call process_open(cmd,mode,fp,ierr)

end subroutine process_open_read