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: eachus@spectre.mitre.org (Robert I. Eachus) Subject: Re: Environment variables Date: 1996/10/31 Message-ID: #1/1 X-Deja-AN: 193686100 references: <55819q$mql@newslink.runet.edu> organization: The Mitre Corp., Bedford, MA. newsgroups: comp.lang.ada Date: 1996-10-31T00:00:00+00:00 List-Id: 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...