Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-08

popen problems...

6 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Martyn Ranyard

Hi all,

I am writing a menu program in FPC, gpl'd so I'm asking here for comments, as I don't quite understand why my calls are no longer working.

I used this call in a completely different menu program, and it worked nicely. Can't remember if it was a different version of FPC, but I'm now running on "Free Pascal Compiler version 1.0.6 [2002/04/30] for i386". I just don't quite get it. I did write this heap of ****, but it was quite a while ago, and it was by trial and error - I even ended up with zombies in an early revision because I used close() instead of pclose(). :(

The call :

ExecAndDisplay('/home/mranyard/newmenuserv/usr/sbin/smeserver/share2iso.sh
/usr/share/ToBurn /tmp/duplicate.iso ASK');

or the call :

ExecAndDisplay('/bin/sh -c
''/home/mranyard/newmenuserv/usr/sbin/smeserver/share2iso.sh /usr/share/ToBurn /tmp/duplicate.iso ASK''');

gives :

An unhandled exception occurred at 0x08069D5F :
Unknown Run-Time error : 029
0x08069D5F
0x08074E0F
0x080480B0

and yes, the file share2iso.sh exists and has execute rights - It executes if I copy the command out into the command prompt.

and now for the heap (remember - early code, please don't be too nasty, also, I know it does no more than shell() would do, but there is a similar function yet to be made that will do more with the output.) :

Procedure ExecAndDisplay(Command: String);
Var
   TheFile : File;
   Ch      : Char;
   TheBuff : String;
   EndOfFile : Boolean;
Begin
   EndOfFile := False;
   popen(TheFile, Command, 'r');
   Repeat
     If not Eof(TheFile) then
     Begin
       If not Eof(TheFile) then
       Begin
         TheBuff := '';
         Repeat
           If Eof(TheFile) then
             Ch := #10
           else
             Try
               BlockRead(Thefile, Ch, 1)
             Except
               EndOfFile := True;
               Ch := #10
             End;
           If Ch = #13 then Ch := #10;
           if Ch  #10 then TheBuff := TheBuff + Ch
         Until Ch = #10;
         Writeln(TheBuff);
       End
     End
   Until Eof(TheFile) or EndOfFile;
   pClose(TheFile)
End;

Martyn Ranyard
Free Software Advocate

jabber -
icq - 122500800
irc - Joran on oftc
msn -
e -

Reply by email

Martyn Ranyard
to Martyn Ranyard
I am writing a menu program in FPC, gpl'd so I'm asking here for comments, as I don't quite understand why my calls are no longer working.

I am attaching the tail end of a strace command, because I don't understand it very well. Is there some reason why read is creating an invalid seek?

pipe([3, 4]) = 0
fork() = 7669
close(4) = 0
fstat(3, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0 lseek(3, 0, SEEK_CUR) = -1 ESPIPE (Illegal seek) write(1, "An unhandled exception occurred "..., 48An unhandled exception occurred at 0x08069D5F :
) = 48
write(1, "Unknown Run-Time error : 029\n", 29Unknown Run-Time error : 029 ) = 29
write(1, " 0x08069D5F\n", 13 0x08069D5F
) = 13
ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0 write(1, " 0x08074E0F\n", 13 0x08074E0F
) = 13
write(1, " 0x080480B0\n", 13 0x080480B0
) = 13
write(1, "\n", 1
) = 1
_exit(217)

I hope someone can kindly enlighten me.

Martyn Ranyard
Free Software Advocate

jabber -
icq - 122500800
irc - Joran on oftc
msn -
e -

Reply by email

Michael Van Canneyt
to Martyn Ranyard: I am writing a menu program in FPC, gpl'd so I'm asking here for comments, as I don't…
Martyn Ranyard wrote 17 lines
Hi all, I am writing a menu program in FPC, gpl'd so I'm asking here for comments, as I don't quite understand why my calls are no longer working. Procedure ExecAndDisplay(Command: String); Var TheFile : File; Ch : Char; TheBuff : String; EndOfFile : Boolean; Begin EndOfFile := False; popen(TheFile, Command, 'r'); Repeat If not Eof(TheFile) then

You cannot do eof(thefile) on a binary file, because that one will attempt to determine the position in the file. This is done using a seek, and a seek on a pip is not legal.

The correct way to code this would be

Procedure ExecAndDisplay(Command: String);
Var
   TheFile : File;
   Ch      : Char;
   TheBuff : String;
   EndOfFile : Boolean;
   BytesRead : INteger;
Begin
   Repeat
     BlockRead(Thefile, Ch, 1,BytesRead)
     If BytesRead1 then
       begin
       // ...
       end
   Until (bytesread=0);

Michael.

Reply by email

Martyn Ranyard
to Michael Van Canneyt
Michael Van Canneyt wrote
You cannot do eof(thefile) on a binary file, because that one will attempt to determine the position in the file. This is done using a seek, and a seek on a pip is not legal.

Hmm.. thanks for giving me a code sample, but I wonder why it worked before, like I say, the procedure works in a program, I just cannot remember which ver. I assume a quick grep for changes to eof() would tell me when, and by whom, but I'm not really that interested in when, but why it was changed.

Thanks again.

Martyn Ranyard
Free Software Advocate

jabber -
icq - 122500800
irc - Joran on oftc
msn -
e -

Reply by email

Michael Van Canneyt
to Martyn Ranyard
Martyn Ranyard wrote 10 lines
At 04:34 PM 8/14/02 +0200, Michael Van Canneyt wrote: >You cannot do eof(thefile) on a binary file, because that one will >attempt to determine the position in the file. This is done using a >seek, and a seek on a pip is not legal. Hmm.. thanks for giving me a code sample, but I wonder why it worked before, like I say, the procedure works in a program, I just cannot remember which ver. I assume a quick grep for changes to eof() would tell me when, and by whom, but I'm not really that interested in when, but why it was changed.

I think that for untyped files, it has always been like that. For text files the code you wrote would work, i.e. EOF works as it is supposed to do.

Michael.

Reply by email

Martyn Ranyard
to Michael Van Canneyt
Michael Van Canneyt wrote
I think that for untyped files, it has always been like that. For text files the code you wrote would work, i.e. EOF works as it is supposed to do.

Well, the code is in a unit that is common to both programs and now doesn't compile, so that's unlikely to be the case in this instance. Thanks for the insight though.

Michael.

Martyn Ranyard
Free Software Advocate

jabber -
icq - 122500800
irc - Joran on oftc
msn -
e -

Reply by email