comp.lang.ada
 help / color / mirror / Atom feed
From: wheeler@aphrodite (David Wheeler)
Subject: Re: Environment variables
Date: 1996/11/06
Date: 1996-11-06T00:00:00+00:00	[thread overview]
Message-ID: <55qhak$gsj@news.ida.org> (raw)
In-Reply-To: 3278B9B1.1773@watson.ibm.com


Christopher J Arnold wrote:
> All I want to do is to be able to read the value of environment
> variables, such as PATH for example.  I've looked around at FAQ's and the
> adahome reference pages, but I haven't found anything.

Norman H. Cohen said:
: This happens to be the example used to illustrate the use of
: Interfaces.C.Strings on page 989 of Ada as a Second Language.  It's only
: a couple of dozen lines long, including comments.

This is also an example in "Lovelace", lesson 16 (which discusses
interfacing to C).  The most portable approach to getting environment
variables I've found is assuming that there's a C library handy
(usually true), and since the C library includes a "getenv" function
just take advantage of that.

Yes, it'd be nice if there was a standard Ada function that did this for you,
but there are standard mechanisms for interfacing to C, so you don't
have to do a lot work.

--- David A. Wheeler
    dwheeler@ida.org

P.S. Here's the body of the version given in Lovelace.
Slap it into a package and you're ready to go:

with Interfaces.C.Strings; use Interfaces.C.Strings;
-- ...

 function Value_Without_Exception(S : chars_ptr) return String is
 -- Translate S from a C-style char* into an Ada String.
 -- If S is Null_Ptr, return "", don't raise an exception.
 begin
   if S = Null_Ptr then return "";
    else return Value(S);
   end if;
 end Value_Without_Exception;
 pragma Inline(Value_Without_Exception);
 
 function Get_Environment(Variable : String) return String is
 -- Return the value of the given environment variable.
 -- If there's no such environment variable, return an empty string.
 
   function getenv(Variable : chars_ptr) return chars_ptr;
   pragma Import(C, getenv);
   -- getenv is a standard C library function; see K&R 2, 1988, page 253.
   -- it returns a pointer to the first character; do NOT free its results.
 
   Variable_In_C_Format : chars_ptr := New_String(Variable);
   Result_Ptr : chars_ptr := getenv(Variable_In_C_Format);
   Result : String := Value_Without_Exception(Result_Ptr);
 
 begin
  Free(Variable_In_C_Format);
  return Result;
 end Get_Environment;

-- .. end of the body.


-- ... and here's a sample use:

  Request_Method_Text : String := Get_Environment("REQUEST_METHOD");






  reply	other threads:[~1996-11-06  0:00 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-10-30  0:00 Environment variables Christopher J Arnold
1996-10-30  0:00 ` David Emery
1996-10-30  0:00   ` Robert Dewar
1996-10-30  0:00   ` Laurent Guerby
1996-11-02  0:00     ` Keith Thompson
1996-11-01  0:00   ` David Shochat
1996-11-02  0:00     ` Larry Kilgallen
1996-11-04  0:00       ` Michael F Brenner
1996-11-04  0:00         ` Larry Kilgallen
1996-11-12  0:00         ` Robert Dewar
1996-11-13  0:00           ` Norman H. Cohen
1996-11-14  0:00           ` Standard libraries (Was: Environment variables) Geert Bosch
1996-11-14  0:00             ` Robert Dewar
1996-11-16  0:00               ` Geert Bosch
1996-11-16  0:00                 ` Robert Dewar
1996-11-17  0:00                   ` Geert Bosch
1996-11-18  0:00                     ` Larry Kilgallen
1996-11-01  0:00   ` Environment variables Stephen Leake
1996-11-02  0:00     ` Robert Dewar
1996-11-03  0:00       ` Robert A Duff
1996-11-03  0:00         ` Robert Dewar
1996-11-04  0:00           ` Stephen Leake
1996-11-04  0:00             ` Robert Dewar
1996-11-04  0:00             ` Larry Kilgallen
1996-11-04  0:00         ` Tucker Taft
1996-11-01  0:00   ` Laurent Guerby
1996-11-02  0:00     ` Robert Dewar
1996-11-02  0:00     ` Robert A Duff
1996-11-01  0:00   ` Norman H. Cohen
1996-11-05  0:00   ` David Emery
1996-10-30  0:00 ` David Taylor
1996-10-31  0:00 ` Robert I. Eachus
1996-10-31  0:00   ` Doug Smith
1996-11-03  0:00   ` Matthew Heaney
1996-10-31  0:00 ` James Rogers
1996-10-31  0:00 ` Norman H. Cohen
1996-11-06  0:00   ` David Wheeler [this message]
1996-11-08  0:00     ` Christopher J Arnold
1996-11-09  0:00     ` Robert Dewar
  -- strict thread matches above, loose matches on Subject: below --
1997-01-21  0:00 Environment Variables John M. Greer
1997-01-24  0:00 ` Matthew Heaney
1997-01-25  0:00   ` Robert Dewar
1997-01-24  0:00 ` Mike Bishop
1997-01-25  0:00 ` Doug Smith
1997-01-27  0:00 ` David Emery
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox