1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . E N V I R O N M E N T _ V A R I A B L E S --
9 -- Copyright (C) 2009-2013, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
33 with Interfaces
.C
.Strings
;
34 with Ada
.Unchecked_Deallocation
;
36 package body Ada
.Environment_Variables
is
42 procedure Clear
(Name
: String) is
43 procedure Clear_Env_Var
(Name
: System
.Address
);
44 pragma Import
(C
, Clear_Env_Var
, "__gnat_unsetenv");
46 F_Name
: String (1 .. Name
'Length + 1);
49 F_Name
(1 .. Name
'Length) := Name
;
50 F_Name
(F_Name
'Last) := ASCII
.NUL
;
52 Clear_Env_Var
(F_Name
'Address);
61 pragma Import
(C
, Clear_Env
, "__gnat_clearenv");
70 function Exists
(Name
: String) return Boolean is
73 procedure Get_Env_Value_Ptr
(Name
, Length
, Ptr
: Address
);
74 pragma Import
(C
, Get_Env_Value_Ptr
, "__gnat_getenv");
76 Env_Value_Ptr
: aliased Address
;
77 Env_Value_Length
: aliased Integer;
78 F_Name
: aliased String (1 .. Name
'Length + 1);
81 F_Name
(1 .. Name
'Length) := Name
;
82 F_Name
(F_Name
'Last) := ASCII
.NUL
;
85 (F_Name
'Address, Env_Value_Length
'Address, Env_Value_Ptr
'Address);
87 if Env_Value_Ptr
= System
.Null_Address
then
99 (Process
: not null access procedure (Name
, Value
: String))
101 use Interfaces
.C
.Strings
;
102 type C_String_Array
is array (Natural) of aliased chars_ptr
;
103 type C_String_Array_Access
is access C_String_Array
;
105 function Get_Env
return C_String_Array_Access
;
106 pragma Import
(C
, Get_Env
, "__gnat_environ");
108 type String_Access
is access all String;
109 procedure Free
is new Ada
.Unchecked_Deallocation
(String, String_Access
);
111 Env_Length
: Natural := 0;
112 Env
: constant C_String_Array_Access
:= Get_Env
;
115 -- If the environment is null return directly
121 -- First get the number of environment variables
124 exit when Env
(Env_Length
) = Null_Ptr
;
125 Env_Length
:= Env_Length
+ 1;
129 Env_Copy
: array (1 .. Env_Length
) of String_Access
;
132 -- Copy the environment
134 for Iterator
in 1 .. Env_Length
loop
135 Env_Copy
(Iterator
) := new String'(Value (Env (Iterator - 1)));
138 -- Iterate on the environment copy
140 for Iterator in 1 .. Env_Length loop
142 Current_Var : constant String := Env_Copy (Iterator).all;
143 Value_Index : Natural := Env_Copy (Iterator)'First;
147 exit when Current_Var (Value_Index) = '=';
148 Value_Index := Value_Index + 1;
152 (Current_Var (Current_Var'First .. Value_Index - 1),
153 Current_Var (Value_Index + 1 .. Current_Var'Last));
157 -- Free the copy of the environment
159 for Iterator in 1 .. Env_Length loop
160 Free (Env_Copy (Iterator));
169 procedure Set (Name : String; Value : String) is
170 F_Name : String (1 .. Name'Length + 1);
171 F_Value : String (1 .. Value'Length + 1);
173 procedure Set_Env_Value (Name, Value : System.Address);
174 pragma Import (C, Set_Env_Value, "__gnat_setenv");
177 F_Name (1 .. Name'Length) := Name;
178 F_Name (F_Name'Last) := ASCII.NUL;
180 F_Value (1 .. Value'Length) := Value;
181 F_Value (F_Value'Last) := ASCII.NUL;
183 Set_Env_Value (F_Name'Address, F_Value'Address);
190 function Value (Name : String) return String is
191 use System, System.CRTL;
193 procedure Get_Env_Value_Ptr (Name, Length, Ptr : Address);
194 pragma Import (C, Get_Env_Value_Ptr, "__gnat_getenv");
196 Env_Value_Ptr : aliased Address;
197 Env_Value_Length : aliased Integer;
198 F_Name : aliased String (1 .. Name'Length + 1);
201 F_Name (1 .. Name'Length) := Name;
202 F_Name (F_Name'Last) := ASCII.NUL;
205 (F_Name'Address, Env_Value_Length'Address, Env_Value_Ptr'Address);
207 if Env_Value_Ptr = System.Null_Address then
208 raise Constraint_Error;
211 if Env_Value_Length > 0 then
213 Result : aliased String (1 .. Env_Value_Length);
215 strncpy (Result'Address, Env_Value_Ptr, size_t (Env_Value_Length));
223 function Value (Name : String; Default : String) return String is
225 return (if Exists (Name) then Value (Name) else Default);
228 end Ada.Environment_Variables;