Page 1 of 1

Redirect output into memory?

Posted: Mon Oct 10, 2005 7:58 pm
by Beach
Rings posted an example of how to redirect output from a DOS app into memory here: viewtopic.php?t=3704 and it works fine.. on Windows..

Is there a way to do this with Linux? I know I can pipe the output to a file and read it but I was hoping for a way similar to Rings solution. I thought I read that the newer versions of PB would be able to do this... hopefully I was not dreaming...

Posted: Tue Oct 11, 2005 1:48 am
by freak
It simpler than on Windows actually...

Code: Select all

Command$ = "df -h" 

Pipe = popen_(Command$, "r")
If Pipe

  *Buffer = AllocateMemory(2000)
  If *Buffer

    Repeat
      result = fgets_(*Buffer, 2000, Pipe)
      If result
        
        Debug PeekS(*Buffer)
        
      EndIf
    Until result = 0
  
  EndIf

  pclose_(Pipe)
Else
  Debug "Error"
EndIf

You can replace the fgets_() with any other libc command that works on streams
if you prefer another reading method.

Posted: Tue Oct 11, 2005 1:55 am
by Beach
Thank you very much! :o

Posted: Sat Nov 19, 2005 4:09 pm
by Nik
How to write to this pipe?

Posted: Sat Nov 19, 2005 8:29 pm
by olejr
Shange the "r" to "w":

Code: Select all

Pipe = popen_(Command$, "w") ; "w" = Write, "r" = Read
Then do something like:

Code: Select all

*Buf = AllocateMemory(2000)
If *Buf
  PokeS(*Buf, "test string")
  res = fputs_(*Buf, Pipe)  ; fputs_ = PutString (without newlinechar)
                                      ; puts_ = PutString.. (newlinechar)
EndIf
Have no idead if it will work, but in theory it should.. :wink:

Here's a link with some docs to the Linux api functions.. http://www.opengroup.org/onlinepubs/007908799/

Posted: Sat Apr 01, 2006 4:30 pm
by WishMaster
freak wrote:It simpler than on Windows actually...

Code: Select all

...

You can replace the fgets_() with any other libc command that works on streams
if you prefer another reading method.
May I use your example in a GPL/LGPL program?

Posted: Sat Apr 01, 2006 4:50 pm
by freak
why not ?

Posted: Sat Apr 01, 2006 5:10 pm
by WishMaster
Thanks.