[fpc-pascal] Popen

Carsten Bager carsten at beas.dk
Mon Jan 8 14:00:37 CET 2007


I am trying to use the "Popen" function in Read mode.
I first tried the example (---Write---) from the manual (see beneath).
The POpen gives an error 25 (Not a typewriter) but it looks like it works.
I try to do almost the same (---Read---)to read the output, but I cannot get this to read 
anything. As far as I can tell, the script is not executed.
Dos anybody have a hint.

Carsten

Program output is beneath ">>>>>>>>>"

------------------Write--------------------------
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.sh');
  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.sh',&755);
  popen(f,'./test21a.sh 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.sh');
  erase(f);
end.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Creating a shell script to which echoes its arguments
and input back to stdout
error from POpen: errno: 25
this is the child speaking . . . .
got arguments*arg1 arg2*
This is written to the pipe , and should appear on stdout.
This is written to the pipe , and should appear on stdout.
This is written to the pipe , and should appear on stdout.
This is written to the pipe , and should appear on stdout.
This is written to the pipe , and should appear on stdout.
This is written to the pipe , and should appear on stdout.
This is written to the pipe , and should appear on stdout.
This is written to the pipe , and should appear on stdout.
This is written to the pipe , and should appear on stdout.
This is written to the pipe , and should appear on stdout.
The script exited with status :2

Press < return > to remove shell script.
--------------------------

----- Read---------------------------------	
Program Example37 ;
{Program to demonstrate the Popen function.}
uses BaseUnix ,
     Unix,
     errors;      {System}
var
  f:text;
  i:longint;
  s:string[255];
begin
  writeln('Creating a shell script to which echoes its arguments ');
  writeln('and input back to stdout');
  assign (f,'test21a.sh');
  rewrite(f);
  writeln(f,'#!/bin/sh');
  writeln(f,'echo this is the child speaking . . . . ' ) ;
  writeln(f,'echo got arguments\*"$*"\*');
  writeln(f,'echo got arguments\*"$*"\* >m');
  writeln(f,'exit 3');
  writeln(f);
  close(f);
  fpchmod('test21a.sh',&755);
  popen(f,'./test21a.sh arg1 arg2','R') ;
  if fpgeterrno <>0 then
    writeln('error from POpen: errno: ',StrError(fpgeterrno));
  for i :=1 to 10 do
  begin
    Readln(f,s);
    writeln(s);
  end;
  Writeln('The script exited with status :' ,PClose(f));
  writeln;
  writeln('Press < return > to remove shell script. ');
  readln ;
  assign (f,'test21a.sh');
  erase(f);
end.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Creating a shell script to which echoes its arguments
and input back to stdout
error from POpen: errno: Not a typewriter










The script exited with status :127

Press < return > to remove shell script.
---------------------------------------------------





More information about the fpc-pascal mailing list