From: jerry@jvdsys.stuyts.nl (Jerry van Dijk)
Subject: Re: "system" Call in GNAT?
Date: 1999/04/05
Date: 1999-04-05T00:00:00+00:00 [thread overview]
Message-ID: <F9qqJM.37@jvdsys.stuyts.nl> (raw)
In-Reply-To: 3708E4C0.F13C2023@alphasoft-inc.com
M. Smith (work@alphasoft-inc.com) wrote:
: I was looking for a call in GNAT that would make invoke a command to the OS
: for me and I found the function "system" and "execl".
Calling the OS is, of course, OS specific, so not addressed by Ada or an
Ada compiler (although a compiler may provided a vendor specific package
for it).
The system() call is the easiest to use, since you simply give it the command
to execute in a string, and of it goes.
However, the system() call is part of the C runtime library, not of the Win32
API, so you are making things more difficult than they need to be.
Here is example:
-- using the system call on Win32 to list a directory
with System; -- to use the Address type
procedure List is
--------------------------------------------------------------------
procedure System_Call (S : in String) is
-- The system() call is not part of the Win32 API, but of the C
-- runtime library, which GNAT links in by default.
-- Note that the system() call will go away and not return until
-- its job is done.
-- The return value of the system() call in the Win32 environment
-- indicates the result of the system call, not the result of the
-- command it tried to execute. Since system() is unlikely to
-- fail I discard the return value here.
procedure System_Call (Command : in System.Address);
pragma Import (C, System_Call, "system");
-- The system() call expects a C string pointer, which is the
-- address of the first character in a character array terminated
-- by a zero.
-- Note the distingishing between the Ada name (System_Call) and
-- the C name (system) which are 'linked' by the import pragma.
Command : constant String := S & ASCII.NUL;
-- Add a terminating zero to the Ada string to make it look like
-- a C string.
begin
System_Call (Command'Address);
-- Command'Address returns the address of the first character
end System_Call;
-----------------------------------------------------------------------
begin
System_Call ("dir");
end List;
--
-- Jerry van Dijk | Leiden, Holland
-- Team Ada | jdijk@acm.org
-- see http://stad.dsl.nl/~jvandyk
next prev parent reply other threads:[~1999-04-05 0:00 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-04-05 0:00 "system" Call in GNAT? M. Smith
1999-04-05 0:00 ` Jerry van Dijk [this message]
1999-04-07 0:00 ` Robert Dewar
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox