[Overview][Constants][Types][Procedures and functions][Variables][Index] Reference for unit 'Unix' (#rtl)

POpen

Pipe file to standard input/output of program

Declaration

Source position: unix.pp line 117

function POpen(

  var F: text;

  const Prog: RawByteString;

  rw: Char

):cint;

function POpen(

  var F: file;

  const Prog: RawByteString;

  rw: Char

):cint;

function POpen(

  var F: text;

  const Prog: UnicodeString;

  rw: Char

):cint;

function POpen(

  var F: file;

  const Prog: UnicodeString;

  rw: Char

):cint;

Description

POpen runs the command specified in Prog, and redirects the standard in or output of the command to the other end of the pipe F. The parameter rw indicates the direction of the pipe. If it is set to 'W', then F can be used to write data, which will then be read by the command from stdinput. If it is set to 'R', then the standard output of the command can be read from F. F should be reset or rewritten prior to using it. F can be of type Text or File. A file opened with POpen can be closed with Close, but also with PClose. The result is the same, but PClose returns the exit status of the command Prog.

Errors

Extended error information is returned by the FpGetErrno function. Errors are essentially those of the Execve, Dup and AssignPipe commands.

See also

AssignPipe

  

Create a set of pipe file handlers

PClose

  

Close file opened with POpen

Example

Program Example37;

{ Program to demonstrate the Popen function. }

uses BaseUnix,Unix;

var f : text;
    i : longint;

begin
  writeln ('Creating a shell script to which echoes its arguments');
  writeln ('and input back to stdout');
  assign (f,'test21a');
  rewrite (f);
  writeln (f,'#!/bin/sh');
  writeln (f,'echo this is the child speaking.... ');
  writeln (f,'echo got arguments \*"$*"\*');
  writeln (f,'cat');
  writeln (f,'exit 2');
  writeln (f);
  close (f);
  fpchmod ('test21a',&755);
  popen (f,'./test21a arg1 arg2','W');
  if fpgeterrno<>0 then
     writeln ('error from POpen : errno : ', fpgeterrno);
  for i:=1 to 10 do
    writeln (f,'This is written to the pipe, and should appear on stdout.');
  Flush(f);
  Writeln ('The script exited with status : ',PClose (f));
  writeln;
  writeln ('Press <return> to remove shell script.');
  readln;
  assign (f,'test21a');
  erase (f)
end.

Documentation generated on: Sep 28 2017