Redirect output into memory?

Linux specific forum
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Redirect output into memory?

Post 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...
-Beach
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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.
quidquid Latine dictum sit altum videtur
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Post by Beach »

Thank you very much! :o
-Beach
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post by Nik »

How to write to this pipe?
olejr
Enthusiast
Enthusiast
Posts: 152
Joined: Sun Jul 11, 2004 7:48 pm
Location: Lillehammer, No(r)way
Contact:

Post 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/
WishMaster
Enthusiast
Enthusiast
Posts: 277
Joined: Fri Jun 17, 2005 7:13 pm
Location: Franconia
Contact:

Post 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?
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

why not ?
quidquid Latine dictum sit altum videtur
WishMaster
Enthusiast
Enthusiast
Posts: 277
Joined: Fri Jun 17, 2005 7:13 pm
Location: Franconia
Contact:

Post by WishMaster »

Thanks.
Post Reply