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: mheaney@ni.net (Matthew Heaney) Subject: Re: Environment variables Date: 1996/11/03 Message-ID: #1/1 X-Deja-AN: 194231915 references: <55819q$mql@newslink.runet.edu> content-type: text/plain; charset=ISO-8859-1 organization: Estormza Software mime-version: 1.0 newsgroups: comp.lang.ada Date: 1996-11-03T00:00:00+00:00 List-Id: In article , 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