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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,8dea6f46dfb95f66 X-Google-Attributes: gid103376,public From: wheeler@aphrodite (David Wheeler) Subject: Re: Environment variables Date: 1996/11/06 Message-ID: <55qhak$gsj@news.ida.org>#1/1 X-Deja-AN: 194909571 references: <55819q$mql@newslink.runet.edu> <3278B9B1.1773@watson.ibm.com> organization: IDA, Alexandria, Virginia newsgroups: comp.lang.ada Date: 1996-11-06T00:00:00+00:00 List-Id: 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");