From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,cc1e5a80c87c0755 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-06-22 06:12:48 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!kibo.news.demon.net!demon!newshub1.home.nl!home.nl!skynet.be!skynet.be!louie!tlk!not-for-mail Sender: lbrenta@lbrenta Newsgroups: comp.lang.ada Subject: Re: Running external program, getting output References: From: Ludovic Brenta Date: 22 Jun 2003 15:07:24 +0200 Message-ID: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: -= Belgacom Usenet Service =- NNTP-Posting-Host: 80.201.75.23 X-Trace: 1056287566 reader0.news.skynet.be 313 80.201.75.23:48878 X-Complaints-To: abuse@skynet.be Xref: archiver1.google.com comp.lang.ada:39561 Date: 2003-06-22T15:07:24+02:00 List-Id: Jarle Thorsen writes: > As far as I have gathered the best way of (non OS-specific) running > an external program from Ada is using the Spawn procedure in > g-os_lib, correct ? > > But how do I get the output from the command that I run? Lets say > that I want to run "ls" for example..... Spawn does not allow you to read from the child process' standard output. For this, I'd use popen(2) but unfortunately, GNAT doesn't provide a binding to it. Here is a quick and dirty example: with Interfaces.C; use Interfaces.C; with Interfaces.C_Streams; with System; with Ada.Text_IO; procedure Spawn_And_Read is type Popen_Mode is (Read, Write, Read_Write); Popen_Error : exception; procedure Popen (Command : in String; Mode : in Popen_Mode; Stream : out Interfaces.C_Streams.FILEs) -- Executes COMMAND in a subshell, and attaches STREAM to the -- subprocess' standard input and/or output. If MODE = Read, -- you can read the process' output from STREAM. If MODE = Write, -- you can write into the process' standard input with STREAM. -- if MODE = Read_Write, both operations are allowed. is function Popen_Native (Command, Mode : Interfaces.C.char_array) return Interfaces.C_Streams.FILEs; pragma Import (C, Popen_Native, "popen"); use type Interfaces.C_Streams.FILEs; begin case Mode is when Read => Stream := Popen_Native (To_C (Command), To_C ("r")); when Write => Stream := Popen_Native (To_C (Command), To_C ("w")); when Read_Write => Stream := Popen_Native (To_C (Command), To_C ("rw")); end case; if Stream = Interfaces.C_Streams.NULL_Stream then raise Popen_Error; end if; end Popen; procedure Pclose (Stream : Interfaces.C_Streams.FILEs); pragma Import (C, Pclose, "pclose"); Stream : Interfaces.C_Streams.FILEs; Buffer : String (1 .. 100); Bytes_Read : Interfaces.C_Streams.size_t; begin Popen (Command => "ls -lF /", Mode => Read, Stream => Stream); while Interfaces.C_Streams.Feof (Stream) = 0 loop Bytes_Read := Interfaces.C_Streams.fread (Buffer => Buffer'Address, Size => 1, Count => Buffer'Length, Stream => Stream); Ada.Text_IO.Put (Buffer (1 .. Integer (Bytes_Read))); end loop; Pclose (Stream); end Spawn_And_Read;