comp.lang.ada
 help / color / mirror / Atom feed
From: eachus@spectre.mitre.org (Robert I. Eachus)
Subject: Re: Environment variables
Date: 1996/10/31
Date: 1996-10-31T00:00:00+00:00	[thread overview]
Message-ID: <EACHUS.96Oct30201754@spectre.mitre.org> (raw)
In-Reply-To: 55819q$mql@newslink.runet.edu


In article <55819q$mql@newslink.runet.edu> carnold@runet.edu (Christopher J Arnold) writes:

  > Ok, some one please help me.

  > 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.

  > Thanks in advance.

   I won't claim credit for all this code.  I think part of the
Get_Enviroment code goes back to Dave Emery's POSIX work. (I will take
all of any blame though, since I recently modified Get_Enviroment to
be less dependant on any one compiler, and rewrote Set_Environment
from memory.)  This code has only been tested on a few compilers, but
should be very portable on Unix machines.

    Incidently notice that garbage collectors, conservative or
otherwise, can be dangerous to your health here.  The only retained
pointer to the string allocated in Set_Environment is in the OS, not
in your program.  (Yes, this is the behavior that Unix expects.  Since
the only processes which share the environment variable are children
of your process, you are safe unless you do an exec.  An exec after a
setenv in the same process is usually a BAD THING, since that
environment variable is likely to get overwritten.)

-----------------------------------------------------------------------------

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;

with System; use System;
procedure Set_Environment(Str: String)is
-- Set the value of the given environment variable.
-- Str should be in the form Name=Value
  function putenv(String_Ptr : System.Address) return Integer;
  pragma Interface(C, putenv);
  type String_Pointer is access String;
  Str_In_C_Format : String_Pointer := new String'(Str&Ascii.Nul);
  Temp: Integer;
begin
  Temp := putenv(Str_In_C_Format(1)'Address);
  if Temp /= 0 then raise Program_Error; end if;
end Set_Environment;

-----------------------------------------------------------------------------
--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




  parent reply	other threads:[~1996-10-31  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   ` 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             ` Larry Kilgallen
1996-11-04  0:00             ` Robert Dewar
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-01  0:00   ` Environment variables 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-05  0:00   ` David Emery
1996-10-30  0:00 ` David Taylor
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
1996-10-31  0:00 ` Robert I. Eachus [this message]
1996-10-31  0:00   ` Doug Smith
1996-11-03  0:00   ` Matthew Heaney
1996-10-31  0:00 ` James Rogers
  -- strict thread matches above, loose matches on Subject: below --
1997-01-21  0:00 Environment Variables John M. Greer
1997-01-24  0:00 ` Mike Bishop
1997-01-24  0:00 ` Matthew Heaney
1997-01-25  0:00   ` Robert Dewar
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