1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . O S _ L I B --
9 -- Copyright (C) 1995-2013, AdaCore --
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 ------------------------------------------------------------------------------
32 pragma Compiler_Unit_Warning
;
34 with Ada
.Unchecked_Conversion
;
35 with Ada
.Unchecked_Deallocation
;
36 with System
; use System
;
37 with System
.Case_Util
;
39 with System
.Soft_Links
;
41 package body System
.OS_Lib
is
43 subtype size_t
is CRTL
.size_t
;
45 procedure Strncpy
(dest
, src
: System
.Address
; n
: size_t
)
48 -- Imported procedures Dup and Dup2 are used in procedures Spawn and
49 -- Non_Blocking_Spawn.
51 function Dup
(Fd
: File_Descriptor
) return File_Descriptor
;
52 pragma Import
(C
, Dup
, "__gnat_dup");
54 procedure Dup2
(Old_Fd
, New_Fd
: File_Descriptor
);
55 pragma Import
(C
, Dup2
, "__gnat_dup2");
57 function Copy_Attributes
58 (From
, To
: System
.Address
;
59 Mode
: Integer) return Integer;
60 pragma Import
(C
, Copy_Attributes
, "__gnat_copy_attribs");
61 -- Mode = 0 - copy only time stamps.
62 -- Mode = 1 - copy time stamps and read/write/execute attributes
64 On_Windows
: constant Boolean := Directory_Separator
= '\';
65 -- An indication that we are on Windows. Used in Normalize_Pathname, to
66 -- deal with drive letters in the beginning of absolute paths.
68 package SSL
renames System
.Soft_Links
;
70 -- The following are used by Create_Temp_File
72 First_Temp_File_Name
: constant String := "GNAT-TEMP-000000.TMP";
73 -- Used to initialize Current_Temp_File_Name and Temp_File_Name_Last_Digit
75 Current_Temp_File_Name
: String := First_Temp_File_Name
;
76 -- Name of the temp file last created
78 Temp_File_Name_Last_Digit
: constant Positive :=
79 First_Temp_File_Name
'Last - 4;
80 -- Position of the last digit in Current_Temp_File_Name
82 Max_Attempts
: constant := 100;
83 -- The maximum number of attempts to create a new temp file
85 -----------------------
86 -- Local Subprograms --
87 -----------------------
89 function Args_Length
(Args
: Argument_List
) return Natural;
90 -- Returns total number of characters needed to create a string of all Args
91 -- terminated by ASCII.NUL characters.
93 procedure Create_Temp_File_Internal
94 (FD
: out File_Descriptor
;
95 Name
: out String_Access
;
97 -- Internal routine to implement two Create_Temp_File routines. If Stdout
98 -- is set to True the created descriptor is stdout-compatible, otherwise
99 -- it might not be depending on the OS (VMS is one example). The first two
100 -- parameters are as in Create_Temp_File.
102 function C_String_Length
(S
: Address
) return Integer;
103 -- Returns the length of C (null-terminated) string at S, or 0 for
106 procedure Spawn_Internal
107 (Program_Name
: String;
108 Args
: Argument_List
;
109 Result
: out Integer;
110 Pid
: out Process_Id
;
112 -- Internal routine to implement the two Spawn (blocking/non blocking)
113 -- routines. If Blocking is set to True then the spawn is blocking
114 -- otherwise it is non blocking. In this latter case the Pid contains the
115 -- process id number. The first three parameters are as in Spawn. Note that
116 -- Spawn_Internal normalizes the argument list before calling the low level
117 -- system spawn routines (see Normalize_Arguments).
119 -- Note: Normalize_Arguments is designed to do nothing if it is called more
120 -- than once, so calling Normalize_Arguments before calling one of the
121 -- spawn routines is fine.
123 function To_Path_String_Access
124 (Path_Addr
: Address
;
125 Path_Len
: Integer) return String_Access
;
126 -- Converts a C String to an Ada String. We could do this making use of
127 -- Interfaces.C.Strings but we prefer not to import that entire package
133 function "<" (X
, Y
: OS_Time
) return Boolean is
135 return Long_Integer (X
) < Long_Integer (Y
);
142 function "<=" (X
, Y
: OS_Time
) return Boolean is
144 return Long_Integer (X
) <= Long_Integer (Y
);
151 function ">" (X
, Y
: OS_Time
) return Boolean is
153 return Long_Integer (X
) > Long_Integer (Y
);
160 function ">=" (X
, Y
: OS_Time
) return Boolean is
162 return Long_Integer (X
) >= Long_Integer (Y
);
169 function Args_Length
(Args
: Argument_List
) return Natural is
173 for J
in Args
'Range loop
174 Len
:= Len
+ Args
(J
)'Length + 1; -- One extra for ASCII.NUL
180 -----------------------------
181 -- Argument_String_To_List --
182 -----------------------------
184 function Argument_String_To_List
185 (Arg_String
: String) return Argument_List_Access
187 Max_Args
: constant Integer := Arg_String
'Length;
188 New_Argv
: Argument_List
(1 .. Max_Args
);
189 New_Argc
: Natural := 0;
193 Idx
:= Arg_String
'First;
196 exit when Idx
> Arg_String
'Last;
199 Quoted
: Boolean := False;
200 Backqd
: Boolean := False;
207 -- An unquoted space is the end of an argument
209 if not (Backqd
or Quoted
)
210 and then Arg_String
(Idx
) = ' '
214 -- Start of a quoted string
216 elsif not (Backqd
or Quoted
)
217 and then Arg_String
(Idx
) = '"'
221 -- End of a quoted string and end of an argument
223 elsif (Quoted
and not Backqd
)
224 and then Arg_String
(Idx
) = '"'
229 -- Following character is backquoted
231 elsif Arg_String
(Idx
) = '\' then
234 -- Turn off backquoting after advancing one character
242 exit when Idx
> Arg_String
'Last;
247 New_Argc
:= New_Argc
+ 1;
248 New_Argv
(New_Argc
) :=
249 new String'(Arg_String (Old_Idx .. Idx - 1));
251 -- Skip extraneous spaces
253 while Idx <= Arg_String'Last and then Arg_String (Idx) = ' ' loop
259 return new Argument_List'(New_Argv
(1 .. New_Argc
));
260 end Argument_String_To_List
;
262 ---------------------
263 -- C_String_Length --
264 ---------------------
266 function C_String_Length
(S
: Address
) return Integer is
268 if S
= Null_Address
then
271 return Integer (CRTL
.strlen
(S
));
279 procedure Close
(FD
: File_Descriptor
) is
281 Discard
: constant int
:= close
(int
(FD
));
282 pragma Unreferenced
(Discard
);
287 procedure Close
(FD
: File_Descriptor
; Status
: out Boolean) is
290 Status
:= (close
(int
(FD
)) = 0);
300 Success
: out Boolean;
301 Mode
: Copy_Mode
:= Copy
;
302 Preserve
: Attribute
:= Time_Stamps
)
304 From
: File_Descriptor
;
305 To
: File_Descriptor
;
307 Copy_Error
: exception;
308 -- Internal exception raised to signal error in copy
310 function Build_Path
(Dir
: String; File
: String) return String;
311 -- Returns pathname Dir concatenated with File adding the directory
312 -- separator only if needed.
314 procedure Copy
(From
, To
: File_Descriptor
);
315 -- Read data from From and place them into To. In both cases the
316 -- operations uses the current file position. Raises Constraint_Error
317 -- if a problem occurs during the copy.
319 procedure Copy_To
(To_Name
: String);
320 -- Does a straight copy from source to designated destination file
326 function Build_Path
(Dir
: String; File
: String) return String is
327 Res
: String (1 .. Dir
'Length + File
'Length + 1);
329 Base_File_Ptr
: Integer;
330 -- The base file name is File (Base_File_Ptr + 1 .. File'Last)
332 function Is_Dirsep
(C
: Character) return Boolean;
333 pragma Inline
(Is_Dirsep
);
334 -- Returns True if C is a directory separator. On Windows we
335 -- handle both styles of directory separator.
341 function Is_Dirsep
(C
: Character) return Boolean is
343 return C
= Directory_Separator
or else C
= '/';
346 -- Start of processing for Build_Path
349 -- Find base file name
351 Base_File_Ptr
:= File
'Last;
352 while Base_File_Ptr
>= File
'First loop
353 exit when Is_Dirsep
(File
(Base_File_Ptr
));
354 Base_File_Ptr
:= Base_File_Ptr
- 1;
358 Base_File
: String renames
359 File
(Base_File_Ptr
+ 1 .. File
'Last);
362 Res
(1 .. Dir
'Length) := Dir
;
364 if Is_Dirsep
(Dir
(Dir
'Last)) then
365 Res
(Dir
'Length + 1 .. Dir
'Length + Base_File
'Length) :=
367 return Res
(1 .. Dir
'Length + Base_File
'Length);
370 Res
(Dir
'Length + 1) := Directory_Separator
;
371 Res
(Dir
'Length + 2 .. Dir
'Length + 1 + Base_File
'Length) :=
373 return Res
(1 .. Dir
'Length + 1 + Base_File
'Length);
382 procedure Copy
(From
, To
: File_Descriptor
) is
383 Buf_Size
: constant := 200_000
;
384 type Buf
is array (1 .. Buf_Size
) of Character;
385 type Buf_Ptr
is access Buf
;
391 Status_From
: Boolean;
393 -- Statuses for the calls to Close
395 procedure Free
is new Ada
.Unchecked_Deallocation
(Buf
, Buf_Ptr
);
398 -- Check for invalid descriptors, making sure that we do not
399 -- accidentally leave an open file descriptor around.
401 if From
= Invalid_FD
then
402 if To
/= Invalid_FD
then
403 Close
(To
, Status_To
);
408 elsif To
= Invalid_FD
then
409 Close
(From
, Status_From
);
413 -- Allocate the buffer on the heap
418 R
:= Read
(From
, Buffer
(1)'Address, Buf_Size
);
420 -- For VMS, the buffer may not be full. So, we need to try again
421 -- until there is nothing to read.
425 W
:= Write
(To
, Buffer
(1)'Address, R
);
429 -- Problem writing data, could be a disk full. Close files
430 -- without worrying about status, since we are raising a
431 -- Copy_Error exception in any case.
433 Close
(From
, Status_From
);
434 Close
(To
, Status_To
);
442 Close
(From
, Status_From
);
443 Close
(To
, Status_To
);
447 if not (Status_From
and Status_To
) then
456 procedure Copy_To
(To_Name
: String) is
457 C_From
: String (1 .. Name
'Length + 1);
458 C_To
: String (1 .. To_Name
'Length + 1);
461 From
:= Open_Read
(Name
, Binary
);
463 -- Do not clobber destination file if source file could not be opened
465 if From
/= Invalid_FD
then
466 To
:= Create_File
(To_Name
, Binary
);
473 C_From
(1 .. Name
'Length) := Name
;
474 C_From
(C_From
'Last) := ASCII
.NUL
;
476 C_To
(1 .. To_Name
'Length) := To_Name
;
477 C_To
(C_To
'Last) := ASCII
.NUL
;
482 if Copy_Attributes
(C_From
'Address, C_To
'Address, 0) = -1 then
487 if Copy_Attributes
(C_From
'Address, C_To
'Address, 1) = -1 then
497 -- Start of processing for Copy_File
502 -- The source file must exist
504 if not Is_Regular_File
(Name
) then
508 -- The source file exists
512 -- Copy case, target file must not exist
516 -- If the target file exists, we have an error
518 if Is_Regular_File
(Pathname
) then
521 -- Case of target is a directory
523 elsif Is_Directory
(Pathname
) then
525 Dest
: constant String := Build_Path
(Pathname
, Name
);
528 -- If target file exists, we have an error, else do copy
530 if Is_Regular_File
(Dest
) then
537 -- Case of normal copy to file (destination does not exist)
543 -- Overwrite case (destination file may or may not exist)
546 if Is_Directory
(Pathname
) then
547 Copy_To
(Build_Path
(Pathname
, Name
));
552 -- Append case (destination file may or may not exist)
556 -- Appending to existing file
558 if Is_Regular_File
(Pathname
) then
560 -- Append mode and destination file exists, append data at the
561 -- end of Pathname. But if we fail to open source file, do not
562 -- touch destination file at all.
564 From
:= Open_Read
(Name
, Binary
);
565 if From
/= Invalid_FD
then
566 To
:= Open_Read_Write
(Pathname
, Binary
);
569 Lseek
(To
, 0, Seek_End
);
573 -- Appending to directory, not allowed
575 elsif Is_Directory
(Pathname
) then
578 -- Appending when target file does not exist
585 -- All error cases are caught here
594 Pathname
: C_File_Name
;
595 Success
: out Boolean;
596 Mode
: Copy_Mode
:= Copy
;
597 Preserve
: Attribute
:= Time_Stamps
)
599 Ada_Name
: String_Access
:=
600 To_Path_String_Access
601 (Name
, C_String_Length
(Name
));
602 Ada_Pathname
: String_Access
:=
603 To_Path_String_Access
604 (Pathname
, C_String_Length
(Pathname
));
606 Copy_File
(Ada_Name
.all, Ada_Pathname
.all, Success
, Mode
, Preserve
);
611 ----------------------
612 -- Copy_Time_Stamps --
613 ----------------------
615 procedure Copy_Time_Stamps
(Source
, Dest
: String; Success
: out Boolean) is
617 if Is_Regular_File
(Source
) and then Is_Writable_File
(Dest
) then
619 C_Source
: String (1 .. Source
'Length + 1);
620 C_Dest
: String (1 .. Dest
'Length + 1);
623 C_Source
(1 .. Source
'Length) := Source
;
624 C_Source
(C_Source
'Last) := ASCII
.NUL
;
626 C_Dest
(1 .. Dest
'Length) := Dest
;
627 C_Dest
(C_Dest
'Last) := ASCII
.NUL
;
629 if Copy_Attributes
(C_Source
'Address, C_Dest
'Address, 0) = -1 then
639 end Copy_Time_Stamps
;
641 procedure Copy_Time_Stamps
642 (Source
, Dest
: C_File_Name
;
643 Success
: out Boolean)
645 Ada_Source
: String_Access
:=
646 To_Path_String_Access
647 (Source
, C_String_Length
(Source
));
648 Ada_Dest
: String_Access
:=
649 To_Path_String_Access
650 (Dest
, C_String_Length
(Dest
));
652 Copy_Time_Stamps
(Ada_Source
.all, Ada_Dest
.all, Success
);
655 end Copy_Time_Stamps
;
663 Fmode
: Mode
) return File_Descriptor
665 function C_Create_File
667 Fmode
: Mode
) return File_Descriptor
;
668 pragma Import
(C
, C_Create_File
, "__gnat_open_create");
670 return C_Create_File
(Name
, Fmode
);
675 Fmode
: Mode
) return File_Descriptor
677 C_Name
: String (1 .. Name
'Length + 1);
679 C_Name
(1 .. Name
'Length) := Name
;
680 C_Name
(C_Name
'Last) := ASCII
.NUL
;
681 return Create_File
(C_Name
(C_Name
'First)'Address, Fmode
);
684 ---------------------
685 -- Create_New_File --
686 ---------------------
688 function Create_New_File
690 Fmode
: Mode
) return File_Descriptor
692 function C_Create_New_File
694 Fmode
: Mode
) return File_Descriptor
;
695 pragma Import
(C
, C_Create_New_File
, "__gnat_open_new");
697 return C_Create_New_File
(Name
, Fmode
);
700 function Create_New_File
702 Fmode
: Mode
) return File_Descriptor
704 C_Name
: String (1 .. Name
'Length + 1);
706 C_Name
(1 .. Name
'Length) := Name
;
707 C_Name
(C_Name
'Last) := ASCII
.NUL
;
708 return Create_New_File
(C_Name
(C_Name
'First)'Address, Fmode
);
711 -----------------------------
712 -- Create_Output_Text_File --
713 -----------------------------
715 function Create_Output_Text_File
(Name
: String) return File_Descriptor
is
716 function C_Create_File
717 (Name
: C_File_Name
) return File_Descriptor
;
718 pragma Import
(C
, C_Create_File
, "__gnat_create_output_file");
719 C_Name
: String (1 .. Name
'Length + 1);
721 C_Name
(1 .. Name
'Length) := Name
;
722 C_Name
(C_Name
'Last) := ASCII
.NUL
;
723 return C_Create_File
(C_Name
(C_Name
'First)'Address);
724 end Create_Output_Text_File
;
726 ----------------------
727 -- Create_Temp_File --
728 ----------------------
730 procedure Create_Temp_File
731 (FD
: out File_Descriptor
;
732 Name
: out Temp_File_Name
)
734 function Open_New_Temp
735 (Name
: System
.Address
;
736 Fmode
: Mode
) return File_Descriptor
;
737 pragma Import
(C
, Open_New_Temp
, "__gnat_open_new_temp");
740 FD
:= Open_New_Temp
(Name
'Address, Binary
);
741 end Create_Temp_File
;
743 procedure Create_Temp_File
744 (FD
: out File_Descriptor
;
745 Name
: out String_Access
)
748 Create_Temp_File_Internal
(FD
, Name
, Stdout
=> False);
749 end Create_Temp_File
;
751 -----------------------------
752 -- Create_Temp_Output_File --
753 -----------------------------
755 procedure Create_Temp_Output_File
756 (FD
: out File_Descriptor
;
757 Name
: out String_Access
)
760 Create_Temp_File_Internal
(FD
, Name
, Stdout
=> True);
761 end Create_Temp_Output_File
;
763 -------------------------------
764 -- Create_Temp_File_Internal --
765 -------------------------------
767 procedure Create_Temp_File_Internal
768 (FD
: out File_Descriptor
;
769 Name
: out String_Access
;
773 Attempts
: Natural := 0;
774 Current
: String (Current_Temp_File_Name
'Range);
776 function Create_New_Output_Text_File
777 (Name
: String) return File_Descriptor
;
778 -- Similar to Create_Output_Text_File, except it fails if the file
779 -- already exists. We need this behavior to ensure we don't accidentally
780 -- open a temp file that has just been created by a concurrently running
781 -- process. There is no point exposing this function, as it's generally
782 -- not particularly useful.
784 ---------------------------------
785 -- Create_New_Output_Text_File --
786 ---------------------------------
788 function Create_New_Output_Text_File
789 (Name
: String) return File_Descriptor
791 function C_Create_File
792 (Name
: C_File_Name
) return File_Descriptor
;
793 pragma Import
(C
, C_Create_File
, "__gnat_create_output_file_new");
794 C_Name
: String (1 .. Name
'Length + 1);
796 C_Name
(1 .. Name
'Length) := Name
;
797 C_Name
(C_Name
'Last) := ASCII
.NUL
;
798 return C_Create_File
(C_Name
(C_Name
'First)'Address);
799 end Create_New_Output_Text_File
;
802 -- Loop until a new temp file can be created
807 -- We need to protect global variable Current_Temp_File_Name
808 -- against concurrent access by different tasks.
812 -- Start at the last digit
814 Pos
:= Temp_File_Name_Last_Digit
;
818 -- Increment the digit by one
820 case Current_Temp_File_Name
(Pos
) is
822 Current_Temp_File_Name
(Pos
) :=
823 Character'Succ (Current_Temp_File_Name
(Pos
));
828 -- For 9, set the digit to 0 and go to the previous digit
830 Current_Temp_File_Name
(Pos
) := '0';
835 -- If it is not a digit, then there are no available
836 -- temp file names. Return Invalid_FD. There is almost no
837 -- chance that this code will be ever be executed, since
838 -- it would mean that there are one million temp files in
839 -- the same directory.
848 Current
:= Current_Temp_File_Name
;
850 -- We can now release the lock, because we are no longer accessing
851 -- Current_Temp_File_Name.
861 -- Attempt to create the file
864 FD
:= Create_New_Output_Text_File
(Current
);
866 FD
:= Create_New_File
(Current
, Binary
);
869 if FD
/= Invalid_FD
then
870 Name
:= new String'(Current);
874 if not Is_Regular_File (Current) then
876 -- If the file does not already exist and we are unable to create
877 -- it, we give up after Max_Attempts. Otherwise, we try again with
878 -- the next available file name.
880 Attempts := Attempts + 1;
882 if Attempts >= Max_Attempts then
889 end Create_Temp_File_Internal;
895 procedure Delete_File (Name : Address; Success : out Boolean) is
898 R := System.CRTL.unlink (Name);
902 procedure Delete_File (Name : String; Success : out Boolean) is
903 C_Name : String (1 .. Name'Length + 1);
905 C_Name (1 .. Name'Length) := Name;
906 C_Name (C_Name'Last) := ASCII.NUL;
907 Delete_File (C_Name'Address, Success);
914 function Errno_Message
915 (Err : Integer := Errno;
916 Default : String := "") return String
918 function strerror (errnum : Integer) return System.Address;
919 pragma Import (C, strerror, "strerror");
921 C_Msg : constant System.Address := strerror (Err);
924 if C_Msg = Null_Address then
925 if Default /= "" then
929 -- Note: for bootstrap reasons, it is impractical
930 -- to use Integer'Image here.
936 Buf : String (1 .. 20);
937 -- Buffer large enough to hold image of largest Integer values
944 Character'Val (Character'Pos ('0') + Val mod 10);
955 return "errno = " & Buf (First .. Buf'Last);
961 Msg : String (1 .. Integer (CRTL.strlen (C_Msg)));
962 for Msg'Address use C_Msg;
963 pragma Import (Ada, Msg);
970 ---------------------
971 -- File_Time_Stamp --
972 ---------------------
974 function File_Time_Stamp (FD : File_Descriptor) return OS_Time is
975 function File_Time (FD : File_Descriptor) return OS_Time;
976 pragma Import (C, File_Time, "__gnat_file_time_fd");
978 return File_Time (FD);
981 function File_Time_Stamp (Name : C_File_Name) return OS_Time is
982 function File_Time (Name : Address) return OS_Time;
983 pragma Import (C, File_Time, "__gnat_file_time_name");
985 return File_Time (Name);
988 function File_Time_Stamp (Name : String) return OS_Time is
989 F_Name : String (1 .. Name'Length + 1);
991 F_Name (1 .. Name'Length) := Name;
992 F_Name (F_Name'Last) := ASCII.NUL;
993 return File_Time_Stamp (F_Name'Address);
996 ---------------------------
997 -- Get_Debuggable_Suffix --
998 ---------------------------
1000 function Get_Debuggable_Suffix return String_Access is
1001 procedure Get_Suffix_Ptr (Length, Ptr : Address);
1002 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_debuggable_suffix_ptr");
1004 Suffix_Ptr : Address;
1005 Suffix_Length : Integer;
1006 Result : String_Access;
1009 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
1010 Result := new String (1 .. Suffix_Length);
1012 if Suffix_Length > 0 then
1013 Strncpy (Result.all'Address, Suffix_Ptr, size_t (Suffix_Length));
1017 end Get_Debuggable_Suffix;
1019 ---------------------------
1020 -- Get_Executable_Suffix --
1021 ---------------------------
1023 function Get_Executable_Suffix return String_Access is
1024 procedure Get_Suffix_Ptr (Length, Ptr : Address);
1025 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_executable_suffix_ptr");
1027 Suffix_Ptr : Address;
1028 Suffix_Length : Integer;
1029 Result : String_Access;
1032 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
1033 Result := new String (1 .. Suffix_Length);
1035 if Suffix_Length > 0 then
1036 Strncpy (Result.all'Address, Suffix_Ptr, size_t (Suffix_Length));
1040 end Get_Executable_Suffix;
1042 -----------------------
1043 -- Get_Object_Suffix --
1044 -----------------------
1046 function Get_Object_Suffix return String_Access is
1047 procedure Get_Suffix_Ptr (Length, Ptr : Address);
1048 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_object_suffix_ptr");
1050 Suffix_Ptr : Address;
1051 Suffix_Length : Integer;
1052 Result : String_Access;
1055 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
1056 Result := new String (1 .. Suffix_Length);
1058 if Suffix_Length > 0 then
1059 Strncpy (Result.all'Address, Suffix_Ptr, size_t (Suffix_Length));
1063 end Get_Object_Suffix;
1065 ----------------------------------
1066 -- Get_Target_Debuggable_Suffix --
1067 ----------------------------------
1069 function Get_Target_Debuggable_Suffix return String_Access is
1070 Target_Exec_Ext_Ptr : Address;
1072 (C, Target_Exec_Ext_Ptr, "__gnat_target_debuggable_extension");
1074 Suffix_Length : Integer;
1075 Result : String_Access;
1078 Suffix_Length := Integer (CRTL.strlen (Target_Exec_Ext_Ptr));
1079 Result := new String (1 .. Suffix_Length);
1081 if Suffix_Length > 0 then
1083 (Result.all'Address, Target_Exec_Ext_Ptr, size_t (Suffix_Length));
1087 end Get_Target_Debuggable_Suffix;
1089 ----------------------------------
1090 -- Get_Target_Executable_Suffix --
1091 ----------------------------------
1093 function Get_Target_Executable_Suffix return String_Access is
1094 Target_Exec_Ext_Ptr : Address;
1096 (C, Target_Exec_Ext_Ptr, "__gnat_target_executable_extension");
1098 Suffix_Length : Integer;
1099 Result : String_Access;
1102 Suffix_Length := Integer (CRTL.strlen (Target_Exec_Ext_Ptr));
1103 Result := new String (1 .. Suffix_Length);
1105 if Suffix_Length > 0 then
1107 (Result.all'Address, Target_Exec_Ext_Ptr, size_t (Suffix_Length));
1111 end Get_Target_Executable_Suffix;
1113 ------------------------------
1114 -- Get_Target_Object_Suffix --
1115 ------------------------------
1117 function Get_Target_Object_Suffix return String_Access is
1118 Target_Object_Ext_Ptr : Address;
1120 (C, Target_Object_Ext_Ptr, "__gnat_target_object_extension");
1122 Suffix_Length : Integer;
1123 Result : String_Access;
1126 Suffix_Length := Integer (CRTL.strlen (Target_Object_Ext_Ptr));
1127 Result := new String (1 .. Suffix_Length);
1129 if Suffix_Length > 0 then
1131 (Result.all'Address, Target_Object_Ext_Ptr, size_t (Suffix_Length));
1135 end Get_Target_Object_Suffix;
1141 function Getenv (Name : String) return String_Access is
1142 procedure Get_Env_Value_Ptr (Name, Length, Ptr : Address);
1143 pragma Import (C, Get_Env_Value_Ptr, "__gnat_getenv");
1145 Env_Value_Ptr : aliased Address;
1146 Env_Value_Length : aliased Integer;
1147 F_Name : aliased String (1 .. Name'Length + 1);
1148 Result : String_Access;
1151 F_Name (1 .. Name'Length) := Name;
1152 F_Name (F_Name'Last) := ASCII.NUL;
1155 (F_Name'Address, Env_Value_Length'Address, Env_Value_Ptr'Address);
1157 Result := new String (1 .. Env_Value_Length);
1159 if Env_Value_Length > 0 then
1161 (Result.all'Address, Env_Value_Ptr, size_t (Env_Value_Length));
1171 function GM_Day (Date : OS_Time) return Day_Type is
1179 pragma Unreferenced (Y, Mo, H, Mn, S);
1182 GM_Split (Date, Y, Mo, D, H, Mn, S);
1190 function GM_Hour (Date : OS_Time) return Hour_Type is
1198 pragma Unreferenced (Y, Mo, D, Mn, S);
1201 GM_Split (Date, Y, Mo, D, H, Mn, S);
1209 function GM_Minute (Date : OS_Time) return Minute_Type is
1217 pragma Unreferenced (Y, Mo, D, H, S);
1220 GM_Split (Date, Y, Mo, D, H, Mn, S);
1228 function GM_Month (Date : OS_Time) return Month_Type is
1236 pragma Unreferenced (Y, D, H, Mn, S);
1239 GM_Split (Date, Y, Mo, D, H, Mn, S);
1247 function GM_Second (Date : OS_Time) return Second_Type is
1255 pragma Unreferenced (Y, Mo, D, H, Mn);
1258 GM_Split (Date, Y, Mo, D, H, Mn, S);
1268 Year : out Year_Type;
1269 Month : out Month_Type;
1271 Hour : out Hour_Type;
1272 Minute : out Minute_Type;
1273 Second : out Second_Type)
1275 procedure To_GM_Time
1276 (P_Time_T, P_Year, P_Month, P_Day, P_Hours, P_Mins, P_Secs : Address);
1277 pragma Import (C, To_GM_Time, "__gnat_to_gm_time");
1279 T : OS_Time := Date;
1288 -- Use the global lock because To_GM_Time is not thread safe
1290 Locked_Processing : begin
1293 (T'Address, Y'Address, Mo'Address, D'Address,
1294 H'Address, Mn'Address, S'Address);
1295 SSL.Unlock_Task.all;
1299 SSL.Unlock_Task.all;
1301 end Locked_Processing;
1315 function GM_Year (Date : OS_Time) return Year_Type is
1323 pragma Unreferenced (Mo, D, H, Mn, S);
1326 GM_Split (Date, Y, Mo, D, H, Mn, S);
1330 ----------------------
1331 -- Is_Absolute_Path --
1332 ----------------------
1334 function Is_Absolute_Path (Name : String) return Boolean is
1335 function Is_Absolute_Path
1337 Length : Integer) return Integer;
1338 pragma Import (C, Is_Absolute_Path, "__gnat_is_absolute_path");
1340 return Is_Absolute_Path (Name'Address, Name'Length) /= 0;
1341 end Is_Absolute_Path;
1347 function Is_Directory (Name : C_File_Name) return Boolean is
1348 function Is_Directory (Name : Address) return Integer;
1349 pragma Import (C, Is_Directory, "__gnat_is_directory");
1351 return Is_Directory (Name) /= 0;
1354 function Is_Directory (Name : String) return Boolean is
1355 F_Name : String (1 .. Name'Length + 1);
1357 F_Name (1 .. Name'Length) := Name;
1358 F_Name (F_Name'Last) := ASCII.NUL;
1359 return Is_Directory (F_Name'Address);
1362 ----------------------
1363 -- Is_Readable_File --
1364 ----------------------
1366 function Is_Readable_File (Name : C_File_Name) return Boolean is
1367 function Is_Readable_File (Name : Address) return Integer;
1368 pragma Import (C, Is_Readable_File, "__gnat_is_readable_file");
1370 return Is_Readable_File (Name) /= 0;
1371 end Is_Readable_File;
1373 function Is_Readable_File (Name : String) return Boolean is
1374 F_Name : String (1 .. Name'Length + 1);
1376 F_Name (1 .. Name'Length) := Name;
1377 F_Name (F_Name'Last) := ASCII.NUL;
1378 return Is_Readable_File (F_Name'Address);
1379 end Is_Readable_File;
1381 ------------------------
1382 -- Is_Executable_File --
1383 ------------------------
1385 function Is_Executable_File (Name : C_File_Name) return Boolean is
1386 function Is_Executable_File (Name : Address) return Integer;
1387 pragma Import (C, Is_Executable_File, "__gnat_is_executable_file");
1389 return Is_Executable_File (Name) /= 0;
1390 end Is_Executable_File;
1392 function Is_Executable_File (Name : String) return Boolean is
1393 F_Name : String (1 .. Name'Length + 1);
1395 F_Name (1 .. Name'Length) := Name;
1396 F_Name (F_Name'Last) := ASCII.NUL;
1397 return Is_Executable_File (F_Name'Address);
1398 end Is_Executable_File;
1400 ---------------------
1401 -- Is_Regular_File --
1402 ---------------------
1404 function Is_Regular_File (Name : C_File_Name) return Boolean is
1405 function Is_Regular_File (Name : Address) return Integer;
1406 pragma Import (C, Is_Regular_File, "__gnat_is_regular_file");
1408 return Is_Regular_File (Name) /= 0;
1409 end Is_Regular_File;
1411 function Is_Regular_File (Name : String) return Boolean is
1412 F_Name : String (1 .. Name'Length + 1);
1414 F_Name (1 .. Name'Length) := Name;
1415 F_Name (F_Name'Last) := ASCII.NUL;
1416 return Is_Regular_File (F_Name'Address);
1417 end Is_Regular_File;
1419 ----------------------
1420 -- Is_Symbolic_Link --
1421 ----------------------
1423 function Is_Symbolic_Link (Name : C_File_Name) return Boolean is
1424 function Is_Symbolic_Link (Name : Address) return Integer;
1425 pragma Import (C, Is_Symbolic_Link, "__gnat_is_symbolic_link");
1427 return Is_Symbolic_Link (Name) /= 0;
1428 end Is_Symbolic_Link;
1430 function Is_Symbolic_Link (Name : String) return Boolean is
1431 F_Name : String (1 .. Name'Length + 1);
1433 F_Name (1 .. Name'Length) := Name;
1434 F_Name (F_Name'Last) := ASCII.NUL;
1435 return Is_Symbolic_Link (F_Name'Address);
1436 end Is_Symbolic_Link;
1438 ----------------------
1439 -- Is_Writable_File --
1440 ----------------------
1442 function Is_Writable_File (Name : C_File_Name) return Boolean is
1443 function Is_Writable_File (Name : Address) return Integer;
1444 pragma Import (C, Is_Writable_File, "__gnat_is_writable_file");
1446 return Is_Writable_File (Name) /= 0;
1447 end Is_Writable_File;
1449 function Is_Writable_File (Name : String) return Boolean is
1450 F_Name : String (1 .. Name'Length + 1);
1452 F_Name (1 .. Name'Length) := Name;
1453 F_Name (F_Name'Last) := ASCII.NUL;
1454 return Is_Writable_File (F_Name'Address);
1455 end Is_Writable_File;
1457 -------------------------
1458 -- Locate_Exec_On_Path --
1459 -------------------------
1461 function Locate_Exec_On_Path
1462 (Exec_Name : String) return String_Access
1464 function Locate_Exec_On_Path (C_Exec_Name : Address) return Address;
1465 pragma Import (C, Locate_Exec_On_Path, "__gnat_locate_exec_on_path");
1467 C_Exec_Name : String (1 .. Exec_Name'Length + 1);
1468 Path_Addr : Address;
1470 Result : String_Access;
1473 C_Exec_Name (1 .. Exec_Name'Length) := Exec_Name;
1474 C_Exec_Name (C_Exec_Name'Last) := ASCII.NUL;
1476 Path_Addr := Locate_Exec_On_Path (C_Exec_Name'Address);
1477 Path_Len := C_String_Length (Path_Addr);
1479 if Path_Len = 0 then
1483 Result := To_Path_String_Access (Path_Addr, Path_Len);
1484 CRTL.free (Path_Addr);
1486 -- Always return an absolute path name
1488 if not Is_Absolute_Path (Result.all) then
1490 Absolute_Path : constant String :=
1491 Normalize_Pathname (Result.all, Resolve_Links => False);
1494 Result := new String'(Absolute_Path
);
1500 end Locate_Exec_On_Path
;
1502 -------------------------
1503 -- Locate_Regular_File --
1504 -------------------------
1506 function Locate_Regular_File
1507 (File_Name
: C_File_Name
;
1508 Path
: C_File_Name
) return String_Access
1510 function Locate_Regular_File
1511 (C_File_Name
, Path_Val
: Address
) return Address
;
1512 pragma Import
(C
, Locate_Regular_File
, "__gnat_locate_regular_file");
1514 Path_Addr
: Address
;
1516 Result
: String_Access
;
1519 Path_Addr
:= Locate_Regular_File
(File_Name
, Path
);
1520 Path_Len
:= C_String_Length
(Path_Addr
);
1522 if Path_Len
= 0 then
1526 Result
:= To_Path_String_Access
(Path_Addr
, Path_Len
);
1527 CRTL
.free
(Path_Addr
);
1530 end Locate_Regular_File
;
1532 function Locate_Regular_File
1533 (File_Name
: String;
1534 Path
: String) return String_Access
1536 C_File_Name
: String (1 .. File_Name
'Length + 1);
1537 C_Path
: String (1 .. Path
'Length + 1);
1538 Result
: String_Access
;
1541 C_File_Name
(1 .. File_Name
'Length) := File_Name
;
1542 C_File_Name
(C_File_Name
'Last) := ASCII
.NUL
;
1544 C_Path
(1 .. Path
'Length) := Path
;
1545 C_Path
(C_Path
'Last) := ASCII
.NUL
;
1547 Result
:= Locate_Regular_File
(C_File_Name
'Address, C_Path
'Address);
1549 -- Always return an absolute path name
1551 if Result
/= null and then not Is_Absolute_Path
(Result
.all) then
1553 Absolute_Path
: constant String := Normalize_Pathname
(Result
.all);
1556 Result
:= new String'(Absolute_Path);
1561 end Locate_Regular_File;
1563 ------------------------
1564 -- Non_Blocking_Spawn --
1565 ------------------------
1567 function Non_Blocking_Spawn
1568 (Program_Name : String;
1569 Args : Argument_List) return Process_Id
1573 pragma Warnings (Off, Junk);
1575 Spawn_Internal (Program_Name, Args, Junk, Pid, Blocking => False);
1577 end Non_Blocking_Spawn;
1579 function Non_Blocking_Spawn
1580 (Program_Name : String;
1581 Args : Argument_List;
1582 Output_File_Descriptor : File_Descriptor;
1583 Err_To_Out : Boolean := True) return Process_Id
1585 Saved_Output : File_Descriptor;
1586 Saved_Error : File_Descriptor := Invalid_FD; -- prevent warning
1590 if Output_File_Descriptor = Invalid_FD then
1594 -- Set standard output and, if specified, error to the temporary file
1596 Saved_Output := Dup (Standout);
1597 Dup2 (Output_File_Descriptor, Standout);
1600 Saved_Error := Dup (Standerr);
1601 Dup2 (Output_File_Descriptor, Standerr);
1604 -- Spawn the program
1606 Pid := Non_Blocking_Spawn (Program_Name, Args);
1608 -- Restore the standard output and error
1610 Dup2 (Saved_Output, Standout);
1613 Dup2 (Saved_Error, Standerr);
1616 -- And close the saved standard output and error file descriptors
1618 Close (Saved_Output);
1621 Close (Saved_Error);
1625 end Non_Blocking_Spawn;
1627 function Non_Blocking_Spawn
1628 (Program_Name : String;
1629 Args : Argument_List;
1630 Output_File : String;
1631 Err_To_Out : Boolean := True) return Process_Id
1633 Output_File_Descriptor : constant File_Descriptor :=
1634 Create_Output_Text_File (Output_File);
1635 Result : Process_Id;
1638 -- Do not attempt to spawn if the output file could not be created
1640 if Output_File_Descriptor = Invalid_FD then
1644 Result := Non_Blocking_Spawn
1645 (Program_Name, Args, Output_File_Descriptor, Err_To_Out);
1647 -- Close the file just created for the output, as the file descriptor
1648 -- cannot be used anywhere, being a local value. It is safe to do
1649 -- that, as the file descriptor has been duplicated to form
1650 -- standard output and error of the spawned process.
1652 Close (Output_File_Descriptor);
1656 end Non_Blocking_Spawn;
1658 -------------------------
1659 -- Normalize_Arguments --
1660 -------------------------
1662 procedure Normalize_Arguments (Args : in out Argument_List) is
1664 procedure Quote_Argument (Arg : in out String_Access);
1665 -- Add quote around argument if it contains spaces (or HT characters)
1667 C_Argument_Needs_Quote : Integer;
1668 pragma Import (C, C_Argument_Needs_Quote, "__gnat_argument_needs_quote");
1669 Argument_Needs_Quote : constant Boolean := C_Argument_Needs_Quote /= 0;
1671 --------------------
1672 -- Quote_Argument --
1673 --------------------
1675 procedure Quote_Argument (Arg : in out String_Access) is
1676 Res : String (1 .. Arg'Length * 2);
1678 Quote_Needed : Boolean := False;
1681 if Arg (Arg'First) /= '"' or else Arg (Arg'Last) /= '"' then
1687 for K in Arg'Range loop
1691 if Arg (K) = '"' then
1695 Quote_Needed := True;
1697 elsif Arg (K) = ' ' or else Arg (K) = ASCII.HT then
1699 Quote_Needed := True;
1706 if Quote_Needed then
1708 -- Case of null terminated string
1710 if Res (J) = ASCII.NUL then
1712 -- If the string ends with \, double it
1714 if Res (J - 1) = '\' then
1719 -- Put a quote just before the null at the end
1723 Res (J) := ASCII.NUL;
1725 -- If argument is terminated by '\
', then double it. Otherwise
1726 -- the ending quote will be taken as-is. This is quite strange
1727 -- spawn behavior from Windows, but this is what we see.
1730 if Res (J) = '\
' then
1742 Old : String_Access := Arg;
1745 Arg := new String'(Res (1 .. J));
1753 -- Start of processing for Normalize_Arguments
1756 if Argument_Needs_Quote then
1757 for K in Args'Range loop
1758 if Args (K) /= null and then Args (K)'Length /= 0 then
1759 Quote_Argument (Args (K));
1763 end Normalize_Arguments;
1765 ------------------------
1766 -- Normalize_Pathname --
1767 ------------------------
1769 function Normalize_Pathname
1771 Directory : String := "";
1772 Resolve_Links : Boolean := True;
1773 Case_Sensitive : Boolean := True) return String
1776 pragma Import (C, Max_Path, "__gnat_max_path_len
");
1777 -- Maximum length of a path name
1779 procedure Get_Current_Dir
1780 (Dir : System.Address;
1781 Length : System.Address);
1782 pragma Import (C, Get_Current_Dir, "__gnat_get_current_dir
");
1784 Path_Buffer : String (1 .. Max_Path + Max_Path + 2);
1785 End_Path : Natural := 0;
1786 Link_Buffer : String (1 .. Max_Path + 2);
1792 Max_Iterations : constant := 500;
1794 function Get_File_Names_Case_Sensitive return Integer;
1796 (C, Get_File_Names_Case_Sensitive,
1797 "__gnat_get_file_names_case_sensitive
");
1799 Fold_To_Lower_Case : constant Boolean :=
1801 and then Get_File_Names_Case_Sensitive = 0;
1804 (Path : System.Address;
1805 Buf : System.Address;
1806 Bufsiz : Integer) return Integer;
1807 pragma Import (C, Readlink, "__gnat_readlink
");
1809 function To_Canonical_File_Spec
1810 (Host_File : System.Address) return System.Address;
1812 (C, To_Canonical_File_Spec, "__gnat_to_canonical_file_spec
");
1814 The_Name : String (1 .. Name'Length + 1);
1815 Canonical_File_Addr : System.Address;
1816 Canonical_File_Len : Integer;
1818 function Final_Value (S : String) return String;
1819 -- Make final adjustment to the returned string. This function strips
1820 -- trailing directory separators, and folds returned string to lower
1821 -- case if required.
1823 function Get_Directory (Dir : String) return String;
1824 -- If Dir is not empty, return it, adding a directory separator
1825 -- if not already present, otherwise return current working directory
1826 -- with terminating directory separator.
1832 function Final_Value (S : String) return String is
1834 -- We may need to fold S to lower case, so we need a variable
1839 if Fold_To_Lower_Case then
1840 System.Case_Util.To_Lower (S1);
1843 -- Remove trailing directory separator, if any
1848 and then (S1 (Last) = '/'
1850 S1 (Last) = Directory_Separator)
1852 -- Special case for Windows: C:\
1855 and then S1 (1) /= Directory_Separator
1856 and then S1 (2) = ':'
1865 return S1 (1 .. Last);
1872 function Get_Directory (Dir : String) return String is
1873 Result : String (1 .. Dir'Length + 1);
1874 Length : constant Natural := Dir'Length;
1877 -- Directory given, add directory separator if needed
1880 Result (1 .. Length) := Dir;
1882 -- On Windows, change all '/' to '\'
1885 for J in 1 .. Length loop
1886 if Result (J) = '/' then
1887 Result (J) := Directory_Separator;
1892 -- Add directory separator, if needed
1894 if Result (Length) = Directory_Separator then
1895 return Result (1 .. Length);
1897 Result (Result'Length) := Directory_Separator;
1901 -- Directory name not given, get current directory
1905 Buffer : String (1 .. Max_Path + 2);
1906 Path_Len : Natural := Max_Path;
1909 Get_Current_Dir (Buffer'Address, Path_Len'Address);
1911 if Buffer (Path_Len) /= Directory_Separator then
1912 Path_Len := Path_Len + 1;
1913 Buffer (Path_Len) := Directory_Separator;
1916 -- By default, the drive letter on Windows is in upper case
1919 and then Path_Len >= 2
1920 and then Buffer (2) = ':'
1922 System.Case_Util.To_Upper (Buffer (1 .. 1));
1925 return Buffer (1 .. Path_Len);
1930 -- Start of processing for Normalize_Pathname
1933 -- Special case, return null if name is null, or if it is bigger than
1934 -- the biggest name allowed.
1936 if Name'Length = 0 or else Name'Length > Max_Path then
1940 -- First, convert VMS file spec to Unix file spec.
1941 -- If Name is not in VMS syntax, then this is equivalent
1942 -- to put Name at the beginning of Path_Buffer.
1944 VMS_Conversion : begin
1945 The_Name (1 .. Name'Length) := Name;
1946 The_Name (The_Name'Last) := ASCII.NUL;
1948 Canonical_File_Addr := To_Canonical_File_Spec (The_Name'Address);
1949 Canonical_File_Len := Integer (CRTL.strlen (Canonical_File_Addr));
1951 -- If VMS syntax conversion has failed, return an empty string
1952 -- to indicate the failure.
1954 if Canonical_File_Len = 0 then
1959 subtype Path_String is String (1 .. Canonical_File_Len);
1960 Canonical_File : Path_String;
1961 for Canonical_File'Address use Canonical_File_Addr;
1962 pragma Import (Ada, Canonical_File);
1965 Path_Buffer (1 .. Canonical_File_Len) := Canonical_File;
1966 End_Path := Canonical_File_Len;
1971 -- Replace all '/' by Directory Separators (this is for Windows)
1973 if Directory_Separator /= '/' then
1974 for Index in 1 .. End_Path loop
1975 if Path_Buffer (Index) = '/' then
1976 Path_Buffer (Index) := Directory_Separator;
1981 -- Resolve directory names for Windows (formerly also VMS)
1983 -- On VMS, if we have a Unix path such as /temp/..., and TEMP is a
1984 -- logical name, we must not try to resolve this logical name, because
1985 -- it may have multiple equivalences and if resolved we will only
1986 -- get the first one.
1990 -- On Windows, if we have an absolute path starting with a directory
1991 -- separator, we need to have the drive letter appended in front.
1993 -- On Windows, Get_Current_Dir will return a suitable directory name
1994 -- (path starting with a drive letter on Windows). So we take this
1995 -- drive letter and prepend it to the current path.
1997 if Path_Buffer (1) = Directory_Separator
1998 and then Path_Buffer (2) /= Directory_Separator
2001 Cur_Dir : constant String := Get_Directory ("");
2002 -- Get the current directory to get the drive letter
2005 if Cur_Dir'Length > 2
2006 and then Cur_Dir (Cur_Dir'First + 1) = ':'
2008 Path_Buffer (3 .. End_Path + 2) :=
2009 Path_Buffer (1 .. End_Path);
2010 Path_Buffer (1 .. 2) :=
2011 Cur_Dir (Cur_Dir'First .. Cur_Dir'First + 1);
2012 End_Path := End_Path + 2;
2016 -- We have a drive letter, ensure it is upper-case
2018 elsif Path_Buffer (1) in 'a' .. 'z'
2019 and then Path_Buffer (2) = ':'
2021 System.Case_Util.To_Upper (Path_Buffer (1 .. 1));
2025 -- On Windows, remove all double-quotes that are possibly part of the
2026 -- path but can cause problems with other methods.
2033 Index := Path_Buffer'First;
2034 for Current in Path_Buffer'First .. End_Path loop
2035 if Path_Buffer (Current) /= '"' then
2036 Path_Buffer (Index) := Path_Buffer (Current);
2041 End_Path := Index - 1;
2045 -- Start the conversions
2047 -- If this is not finished after Max_Iterations, give up and return an
2050 for J in 1 .. Max_Iterations loop
2052 -- If we don't have an absolute pathname, prepend the directory
2056 and then not Is_Absolute_Path (Path_Buffer (1 .. End_Path))
2059 Reference_Dir : constant String := Get_Directory (Directory);
2060 Ref_Dir_Len : constant Natural := Reference_Dir'Length;
2061 -- Current directory name specified and its length
2064 Path_Buffer (Ref_Dir_Len + 1 .. Ref_Dir_Len + End_Path) :=
2065 Path_Buffer (1 .. End_Path);
2066 End_Path := Ref_Dir_Len + End_Path;
2067 Path_Buffer (1 .. Ref_Dir_Len) := Reference_Dir;
2068 Last := Ref_Dir_Len;
2075 -- Ensure that Windows network drives are kept, e.g: \\server\drive-c
2078 and then Directory_Separator = '\
'
2079 and then Path_Buffer (1 .. 2) = "\\"
2084 -- If we have traversed the full pathname, return it
2086 if Start > End_Path then
2087 return Final_Value (Path_Buffer (1 .. End_Path));
2090 -- Remove duplicate directory separators
2092 while Path_Buffer (Start) = Directory_Separator loop
2093 if Start = End_Path then
2094 return Final_Value (Path_Buffer (1 .. End_Path - 1));
2097 Path_Buffer (Start .. End_Path - 1) :=
2098 Path_Buffer (Start + 1 .. End_Path);
2099 End_Path := End_Path - 1;
2103 -- Find the end of the current field: last character or the one
2104 -- preceding the next directory separator.
2106 while Finish < End_Path
2107 and then Path_Buffer (Finish + 1) /= Directory_Separator
2109 Finish := Finish + 1;
2114 if Start = Finish and then Path_Buffer (Start) = '.' then
2115 if Start = End_Path then
2117 return (1 => Directory_Separator);
2120 if Fold_To_Lower_Case then
2121 System.Case_Util.To_Lower (Path_Buffer (1 .. Last - 1));
2124 return Path_Buffer (1 .. Last - 1);
2129 Path_Buffer (Last + 1 .. End_Path - 2) :=
2130 Path_Buffer (Last + 3 .. End_Path);
2131 End_Path := End_Path - 2;
2134 -- Remove ".." fields
2136 elsif Finish = Start + 1
2137 and then Path_Buffer (Start .. Finish) = ".."
2143 or else Path_Buffer (Start) = Directory_Separator;
2147 if Finish = End_Path then
2148 return (1 => Directory_Separator);
2151 Path_Buffer (1 .. End_Path - Finish) :=
2152 Path_Buffer (Finish + 1 .. End_Path);
2153 End_Path := End_Path - Finish;
2158 if Finish = End_Path then
2159 return Final_Value (Path_Buffer (1 .. Start - 1));
2162 Path_Buffer (Start + 1 .. Start + End_Path - Finish - 1) :=
2163 Path_Buffer (Finish + 2 .. End_Path);
2164 End_Path := Start + End_Path - Finish - 1;
2169 -- Check if current field is a symbolic link
2171 elsif Resolve_Links then
2173 Saved : constant Character := Path_Buffer (Finish + 1);
2176 Path_Buffer (Finish + 1) := ASCII.NUL;
2177 Status := Readlink (Path_Buffer'Address,
2178 Link_Buffer'Address,
2179 Link_Buffer'Length);
2180 Path_Buffer (Finish + 1) := Saved;
2183 -- Not a symbolic link, move to the next field, if any
2188 -- Replace symbolic link with its value
2191 if Is_Absolute_Path (Link_Buffer (1 .. Status)) then
2192 Path_Buffer (Status + 1 .. End_Path - (Finish - Status)) :=
2193 Path_Buffer (Finish + 1 .. End_Path);
2194 End_Path := End_Path - (Finish - Status);
2195 Path_Buffer (1 .. Status) := Link_Buffer (1 .. Status);
2200 (Last + Status + 1 .. End_Path - Finish + Last + Status) :=
2201 Path_Buffer (Finish + 1 .. End_Path);
2202 End_Path := End_Path - Finish + Last + Status;
2203 Path_Buffer (Last + 1 .. Last + Status) :=
2204 Link_Buffer (1 .. Status);
2213 -- Too many iterations: give up
2215 -- This can happen when there is a circularity in the symbolic links: A
2216 -- is a symbolic link for B, which itself is a symbolic link, and the
2217 -- target of B or of another symbolic link target of B is A. In this
2218 -- case, we return an empty string to indicate failure to resolve.
2221 end Normalize_Pathname;
2228 (Name : C_File_Name;
2229 Fmode : Mode) return File_Descriptor
2231 function C_Open_Read
2232 (Name : C_File_Name;
2233 Fmode : Mode) return File_Descriptor;
2234 pragma Import (C, C_Open_Read, "__gnat_open_read");
2236 return C_Open_Read (Name, Fmode);
2241 Fmode : Mode) return File_Descriptor
2243 C_Name : String (1 .. Name'Length + 1);
2245 C_Name (1 .. Name'Length) := Name;
2246 C_Name (C_Name'Last) := ASCII.NUL;
2247 return Open_Read (C_Name (C_Name'First)'Address, Fmode);
2250 ---------------------
2251 -- Open_Read_Write --
2252 ---------------------
2254 function Open_Read_Write
2255 (Name : C_File_Name;
2256 Fmode : Mode) return File_Descriptor
2258 function C_Open_Read_Write
2259 (Name : C_File_Name;
2260 Fmode : Mode) return File_Descriptor;
2261 pragma Import (C, C_Open_Read_Write, "__gnat_open_rw");
2263 return C_Open_Read_Write (Name, Fmode);
2264 end Open_Read_Write;
2266 function Open_Read_Write
2268 Fmode : Mode) return File_Descriptor
2270 C_Name : String (1 .. Name'Length + 1);
2272 C_Name (1 .. Name'Length) := Name;
2273 C_Name (C_Name'Last) := ASCII.NUL;
2274 return Open_Read_Write (C_Name (C_Name'First)'Address, Fmode);
2275 end Open_Read_Write;
2281 procedure OS_Exit (Status : Integer) is
2283 OS_Exit_Ptr (Status);
2284 raise Program_Error;
2287 ---------------------
2288 -- OS_Exit_Default --
2289 ---------------------
2291 procedure OS_Exit_Default (Status : Integer) is
2292 procedure GNAT_OS_Exit (Status : Integer);
2293 pragma Import (C, GNAT_OS_Exit, "__gnat_os_exit");
2294 pragma No_Return (GNAT_OS_Exit);
2296 GNAT_OS_Exit (Status);
2297 end OS_Exit_Default;
2299 --------------------
2300 -- Pid_To_Integer --
2301 --------------------
2303 function Pid_To_Integer (Pid : Process_Id) return Integer is
2305 return Integer (Pid);
2313 (FD : File_Descriptor;
2315 N : Integer) return Integer
2319 Integer (System.CRTL.read
2320 (System.CRTL.int (FD),
2321 System.CRTL.chars (A),
2322 System.CRTL.size_t (N)));
2329 procedure Rename_File
2330 (Old_Name : C_File_Name;
2331 New_Name : C_File_Name;
2332 Success : out Boolean)
2334 function rename (From, To : Address) return Integer;
2335 pragma Import (C, rename, "__gnat_rename");
2338 R := rename (Old_Name, New_Name);
2342 procedure Rename_File
2345 Success : out Boolean)
2347 C_Old_Name : String (1 .. Old_Name'Length + 1);
2348 C_New_Name : String (1 .. New_Name'Length + 1);
2350 C_Old_Name (1 .. Old_Name'Length) := Old_Name;
2351 C_Old_Name (C_Old_Name'Last) := ASCII.NUL;
2352 C_New_Name (1 .. New_Name'Length) := New_Name;
2353 C_New_Name (C_New_Name'Last) := ASCII.NUL;
2354 Rename_File (C_Old_Name'Address, C_New_Name'Address, Success);
2357 -----------------------
2358 -- Set_Close_On_Exec --
2359 -----------------------
2361 procedure Set_Close_On_Exec
2362 (FD : File_Descriptor;
2363 Close_On_Exec : Boolean;
2364 Status : out Boolean)
2366 function C_Set_Close_On_Exec
2367 (FD : File_Descriptor; Close_On_Exec : System.CRTL.int)
2368 return System.CRTL.int;
2369 pragma Import (C, C_Set_Close_On_Exec, "__gnat_set_close_on_exec");
2371 Status := C_Set_Close_On_Exec (FD, Boolean'Pos (Close_On_Exec)) = 0;
2372 end Set_Close_On_Exec;
2374 --------------------
2375 -- Set_Executable --
2376 --------------------
2378 procedure Set_Executable (Name : String) is
2379 procedure C_Set_Executable (Name : C_File_Name);
2380 pragma Import (C, C_Set_Executable, "__gnat_set_executable");
2381 C_Name : aliased String (Name'First .. Name'Last + 1);
2383 C_Name (Name'Range) := Name;
2384 C_Name (C_Name'Last) := ASCII.NUL;
2385 C_Set_Executable (C_Name (C_Name'First)'Address);
2388 ----------------------
2389 -- Set_Non_Readable --
2390 ----------------------
2392 procedure Set_Non_Readable (Name : String) is
2393 procedure C_Set_Non_Readable (Name : C_File_Name);
2394 pragma Import (C, C_Set_Non_Readable, "__gnat_set_non_readable");
2395 C_Name : aliased String (Name'First .. Name'Last + 1);
2397 C_Name (Name'Range) := Name;
2398 C_Name (C_Name'Last) := ASCII.NUL;
2399 C_Set_Non_Readable (C_Name (C_Name'First)'Address);
2400 end Set_Non_Readable;
2402 ----------------------
2403 -- Set_Non_Writable --
2404 ----------------------
2406 procedure Set_Non_Writable (Name : String) is
2407 procedure C_Set_Non_Writable (Name : C_File_Name);
2408 pragma Import (C, C_Set_Non_Writable, "__gnat_set_non_writable");
2409 C_Name : aliased String (Name'First .. Name'Last + 1);
2411 C_Name (Name'Range) := Name;
2412 C_Name (C_Name'Last) := ASCII.NUL;
2413 C_Set_Non_Writable (C_Name (C_Name'First)'Address);
2414 end Set_Non_Writable;
2420 procedure Set_Readable (Name : String) is
2421 procedure C_Set_Readable (Name : C_File_Name);
2422 pragma Import (C, C_Set_Readable, "__gnat_set_readable");
2423 C_Name : aliased String (Name'First .. Name'Last + 1);
2425 C_Name (Name'Range) := Name;
2426 C_Name (C_Name'Last) := ASCII.NUL;
2427 C_Set_Readable (C_Name (C_Name'First)'Address);
2430 --------------------
2432 --------------------
2434 procedure Set_Writable (Name : String) is
2435 procedure C_Set_Writable (Name : C_File_Name);
2436 pragma Import (C, C_Set_Writable, "__gnat_set_writable");
2437 C_Name : aliased String (Name'First .. Name'Last + 1);
2439 C_Name (Name'Range) := Name;
2440 C_Name (C_Name'Last) := ASCII.NUL;
2441 C_Set_Writable (C_Name (C_Name'First)'Address);
2448 procedure Setenv (Name : String; Value : String) is
2449 F_Name : String (1 .. Name'Length + 1);
2450 F_Value : String (1 .. Value'Length + 1);
2452 procedure Set_Env_Value (Name, Value : System.Address);
2453 pragma Import (C, Set_Env_Value, "__gnat_setenv");
2456 F_Name (1 .. Name'Length) := Name;
2457 F_Name (F_Name'Last) := ASCII.NUL;
2459 F_Value (1 .. Value'Length) := Value;
2460 F_Value (F_Value'Last) := ASCII.NUL;
2462 Set_Env_Value (F_Name'Address, F_Value'Address);
2470 (Program_Name : String;
2471 Args : Argument_List) return Integer
2475 pragma Warnings (Off, Junk);
2477 Spawn_Internal (Program_Name, Args, Result, Junk, Blocking => True);
2482 (Program_Name : String;
2483 Args : Argument_List;
2484 Success : out Boolean)
2487 Success := (Spawn (Program_Name, Args) = 0);
2491 (Program_Name : String;
2492 Args : Argument_List;
2493 Output_File_Descriptor : File_Descriptor;
2494 Return_Code : out Integer;
2495 Err_To_Out : Boolean := True)
2497 Saved_Output : File_Descriptor;
2498 Saved_Error : File_Descriptor := Invalid_FD; -- prevent compiler warning
2501 -- Set standard output and error to the temporary file
2503 Saved_Output := Dup (Standout);
2504 Dup2 (Output_File_Descriptor, Standout);
2507 Saved_Error := Dup (Standerr);
2508 Dup2 (Output_File_Descriptor, Standerr);
2511 -- Spawn the program
2513 Return_Code := Spawn (Program_Name, Args);
2515 -- Restore the standard output and error
2517 Dup2 (Saved_Output, Standout);
2520 Dup2 (Saved_Error, Standerr);
2523 -- And close the saved standard output and error file descriptors
2525 Close (Saved_Output);
2528 Close (Saved_Error);
2533 (Program_Name : String;
2534 Args : Argument_List;
2535 Output_File : String;
2536 Success : out Boolean;
2537 Return_Code : out Integer;
2538 Err_To_Out : Boolean := True)
2540 FD : File_Descriptor;
2546 FD := Create_Output_Text_File (Output_File);
2548 if FD = Invalid_FD then
2553 Spawn (Program_Name, Args, FD, Return_Code, Err_To_Out);
2555 Close (FD, Success);
2558 --------------------
2559 -- Spawn_Internal --
2560 --------------------
2562 procedure Spawn_Internal
2563 (Program_Name : String;
2564 Args : Argument_List;
2565 Result : out Integer;
2566 Pid : out Process_Id;
2570 procedure Spawn (Args : Argument_List);
2571 -- Call Spawn with given argument list
2573 N_Args : Argument_List (Args'Range);
2574 -- Normalized arguments
2580 procedure Spawn (Args : Argument_List) is
2581 type Chars is array (Positive range <>) of aliased Character;
2582 type Char_Ptr is access constant Character;
2584 Command_Len : constant Positive := Program_Name'Length + 1
2585 + Args_Length (Args);
2586 Command_Last : Natural := 0;
2587 Command : aliased Chars (1 .. Command_Len);
2588 -- Command contains all characters of the Program_Name and Args, all
2589 -- terminated by ASCII.NUL characters.
2591 Arg_List_Len : constant Positive := Args'Length + 2;
2592 Arg_List_Last : Natural := 0;
2593 Arg_List : aliased array (1 .. Arg_List_Len) of Char_Ptr;
2594 -- List with pointers to NUL-terminated strings of the Program_Name
2595 -- and the Args and terminated with a null pointer. We rely on the
2596 -- default initialization for the last null pointer.
2598 procedure Add_To_Command (S : String);
2599 -- Add S and a NUL character to Command, updating Last
2601 function Portable_Spawn (Args : Address) return Integer;
2602 pragma Import (C, Portable_Spawn, "__gnat_portable_spawn");
2604 function Portable_No_Block_Spawn (Args : Address) return Process_Id;
2606 (C, Portable_No_Block_Spawn, "__gnat_portable_no_block_spawn");
2608 --------------------
2609 -- Add_To_Command --
2610 --------------------
2612 procedure Add_To_Command (S : String) is
2613 First : constant Natural := Command_Last + 1;
2616 Command_Last := Command_Last + S'Length;
2618 -- Move characters one at a time, because Command has aliased
2621 -- But not volatile, so why is this necessary ???
2623 for J in S'Range loop
2624 Command (First + J - S'First) := S (J);
2627 Command_Last := Command_Last + 1;
2628 Command (Command_Last) := ASCII.NUL;
2630 Arg_List_Last := Arg_List_Last + 1;
2631 Arg_List (Arg_List_Last) := Command (First)'Access;
2634 -- Start of processing for Spawn
2637 Add_To_Command (Program_Name);
2639 for J in Args'Range loop
2640 Add_To_Command (Args (J).all);
2645 Result := Portable_Spawn (Arg_List'Address);
2647 Pid := Portable_No_Block_Spawn (Arg_List'Address);
2648 Result := Boolean'Pos (Pid /= Invalid_Pid);
2652 -- Start of processing for Spawn_Internal
2655 -- Copy arguments into a local structure
2657 for K in N_Args'Range loop
2658 N_Args (K) := new String'(Args
(K
).all);
2661 -- Normalize those arguments
2663 Normalize_Arguments
(N_Args
);
2665 -- Call spawn using the normalized arguments
2669 -- Free arguments list
2671 for K
in N_Args
'Range loop
2676 ---------------------------
2677 -- To_Path_String_Access --
2678 ---------------------------
2680 function To_Path_String_Access
2681 (Path_Addr
: Address
;
2682 Path_Len
: Integer) return String_Access
2684 subtype Path_String
is String (1 .. Path_Len
);
2685 type Path_String_Access
is access Path_String
;
2687 function Address_To_Access
is new Ada
.Unchecked_Conversion
2688 (Source
=> Address
, Target
=> Path_String_Access
);
2690 Path_Access
: constant Path_String_Access
:=
2691 Address_To_Access
(Path_Addr
);
2693 Return_Val
: String_Access
;
2696 Return_Val
:= new String (1 .. Path_Len
);
2698 for J
in 1 .. Path_Len
loop
2699 Return_Val
(J
) := Path_Access
(J
);
2703 end To_Path_String_Access
;
2709 procedure Wait_Process
(Pid
: out Process_Id
; Success
: out Boolean) is
2712 function Portable_Wait
(S
: Address
) return Process_Id
;
2713 pragma Import
(C
, Portable_Wait
, "__gnat_portable_wait");
2716 Pid
:= Portable_Wait
(Status
'Address);
2717 Success
:= (Status
= 0);
2725 (FD
: File_Descriptor
;
2727 N
: Integer) return Integer
2731 Integer (System
.CRTL
.write
2732 (System
.CRTL
.int
(FD
),
2733 System
.CRTL
.chars
(A
),
2734 System
.CRTL
.size_t
(N
)));