comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Environment variables
Date: 1996/11/03
Date: 1996-11-03T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023180000311961747550001@news.ni.net> (raw)
In-Reply-To: EACHUS.96Oct30201754@spectre.mitre.org


In article <EACHUS.96Oct30201754@spectre.mitre.org>,
eachus@spectre.mitre.org (Robert I. Eachus) wrote:


>with System; use System;
>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.
>  type String_Pointer is access String(1..Integer'LAST);
>  function getenv(Variable : System.Address) return String_Pointer;
>  pragma Interface(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 : constant String := Variable&Ascii.Nul;
>  Result_Ptr : String_Pointer := getenv(Variable_In_C_Format(1)'ADDRESS);
>  function strlen(Pointer: in String_Pointer) return Integer;
>  pragma Interface(C,strlen);
>begin
>  if Result_Ptr = null
>  then return "";
>  else return Result_Ptr(1..strlen(Result_Ptr));
>  end if;
>end Get_Environment;

This isn't a very good way to interface Ada string pointers to C, because
this example assumes the string access object designates the first
character of the string.  This is a very dangerous assumption, because a
string access object could be pointing to a string descriptor, not the
string itself.  In fact, this is how access types are implemented in DEC
Ada.

The issue is with pointers to unconstrained arrays.  A more portable way is
to make the string access type designate a constrained array; thay way, no
dope vector is required.

function Get_Environment (Variable : String) return String is

   type Character_Array is array (Positive) of Character;
   type String_Access is access Character_Array;    -- more portable
   pragma String_Access'Storage_Size = 0;

   Variable_In_C_Format : constant String := Variable & ASCII.NUL;

   function Getenv (S : System.Address) return String_Access;
   pragma Interface (C, Getenv);

   The_Environment_Value : constant String_Access :=
      Getenv (Variable_In_C_Format (1)'Address);
...

Generally, if the accessed type is constrained, no dope vector is required,
and access objects designate the array (in this case), not a descriptor, so
that the Ada string pointer matches the C string pointer.

And remember to set the storage_size of the pool to 0, to explicitly
indicate that no memory is being allocated by the Ada runtime environment.

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
mheaney@ni.net
(818) 985-1271




  parent reply	other threads:[~1996-11-03  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 Taylor
1996-10-30  0:00 ` David Emery
1996-10-30  0:00   ` Laurent Guerby
1996-11-02  0:00     ` Keith Thompson
1996-10-30  0:00   ` Robert Dewar
1996-11-01  0:00   ` Norman H. Cohen
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   ` 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   ` 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-05  0:00   ` Environment variables David Emery
1996-10-31  0:00 ` James Rogers
1996-10-31  0:00 ` Robert I. Eachus
1996-10-31  0:00   ` Doug Smith
1996-11-03  0:00   ` Matthew Heaney [this message]
1996-10-31  0:00 ` Norman H. Cohen
1996-11-06  0:00   ` David Wheeler
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