vss_24.0.0_b4d0be7c/source/os/implementation/vss-application-platform-application_file__linux.adb

 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
--
--  Copyright (C) 2022, AdaCore
--
--  SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--

with Ada.Strings.UTF_Encoding;
with Interfaces.C;

with VSS.Strings.Conversions;

separate (VSS.Application.Platform)
function Application_File return VSS.Strings.Virtual_String is

   use type Interfaces.C.char_array;
   use type Interfaces.C.size_t;

   proc_self_exe_Path : constant Interfaces.C.char_array :=
     "/proc/self/exe" & Interfaces.C.nul;

   function readlink
     (pathname : not null access constant Interfaces.C.char;
      buf      : not null access Interfaces.C.char;
      bufsiz   : Interfaces.C.size_t) return Interfaces.C.size_t
     with Import, Convention => C, External_Name => "readlink";

   Size : Interfaces.C.size_t := 512;

begin
   loop
      declare
         Buffer : Interfaces.C.char_array (1 .. Size);
         Result : Interfaces.C.size_t;

      begin
         Result :=
           readlink
             (proc_self_exe_Path (proc_self_exe_Path'First)'Access,
              Buffer (Buffer'First)'Access,
              Size);

         if Result = Interfaces.C.size_t'Last then
            --  'readlink' returns -1, it means failure of read of the
            --  symbolic link, so fallback to the first element of the
            --  'argv' vector.

            declare
               Args : constant chars_ptr_Array :=
                 chars_ptr_Pointers.Value
                   (GNAT_Argv, Interfaces.C.ptrdiff_t (GNAT_Argc));

            begin
               return
                 VSS.Strings.Conversions.To_Virtual_String
                   (Interfaces.C.Strings.Value (Args (0)));
            end;

         elsif Result < Size then
            declare
               Path : Ada.Strings.UTF_Encoding.UTF_8_String
                 (1 .. Integer (Result))
                   with Import, Address => Buffer (Buffer'First)'Address;

            begin
               return
                 VSS.Strings.Conversions.To_Virtual_String (Path);
               --  ??? Locale specific encoding must be used here.
            end;
         end if;

         Size := Size * 2;
      end;
   end loop;
end Application_File;