aboutsummaryrefslogtreecommitdiff
path: root/gcc/ada/libgnat/g-binenv.adb
blob: e7245412ca16143ec51859777e0c32db694d094c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
------------------------------------------------------------------------------
--                                                                          --
--                  GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS                --
--                                                                          --
--                  G N A T . B I N D _ E N V I R O N M E N T               --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--             Copyright (C) 2015-2023, Free Software Foundation, Inc.      --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
-- GNARL was developed by the GNARL team at Florida State University.       --
-- Extensive contributions were provided by AdaCore.                        --
--                                                                          --
------------------------------------------------------------------------------

with System;

package body GNAT.Bind_Environment is

   ---------
   -- Get --
   ---------

   function Get (Key : String) return String is
      use type System.Address;

      Bind_Env_Addr : constant System.Address;
      pragma Import (C, Bind_Env_Addr, "__gl_bind_env_addr");
      --  Variable provided by init.c/s-init.ads, and initialized by
      --  the binder generated file.

      Bind_Env : String (Positive);
      for Bind_Env'Address use Bind_Env_Addr;
      pragma Import (Ada, Bind_Env);
      --  Import Bind_Env string from binder file. Note that we import
      --  it here as a string with maximum boundaries. The "real" end
      --  of the string is indicated by a NUL byte.

      Index, KLen, VLen : Integer;

   begin
      if Bind_Env_Addr = System.Null_Address then
         return "";
      end if;

      Index := Bind_Env'First;
      loop
         --  Index points to key length

         VLen := 0;
         KLen := Character'Pos (Bind_Env (Index));
         exit when KLen = 0;

         Index := Index + KLen + 1;

         --  Index points to value length

         VLen := Character'Pos (Bind_Env (Index));
         exit when Bind_Env (Index - KLen .. Index - 1) = Key;

         Index := Index + VLen + 1;
      end loop;

      return Bind_Env (Index + 1 .. Index + VLen);
   end Get;

end GNAT.Bind_Environment;