1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . O S _ L I B --
9 -- Copyright (C) 1995-2009, 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
36 with System
.Case_Util
;
38 with System
.Soft_Links
;
39 with Ada
.Unchecked_Conversion
;
40 with Ada
.Unchecked_Deallocation
;
41 with System
; use System
;
43 package body System
.OS_Lib
is
45 -- Imported procedures Dup and Dup2 are used in procedures Spawn and
46 -- Non_Blocking_Spawn.
48 function Dup
(Fd
: File_Descriptor
) return File_Descriptor
;
49 pragma Import
(C
, Dup
, "__gnat_dup");
51 procedure Dup2
(Old_Fd
, New_Fd
: File_Descriptor
);
52 pragma Import
(C
, Dup2
, "__gnat_dup2");
54 On_Windows
: constant Boolean := Directory_Separator
= '\';
55 -- An indication that we are on Windows. Used in Normalize_Pathname, to
56 -- deal with drive letters in the beginning of absolute paths.
58 package SSL
renames System
.Soft_Links
;
60 -- The following are used by Create_Temp_File
62 First_Temp_File_Name
: constant String := "GNAT-TEMP-000000.TMP";
63 -- Used to initialize Current_Temp_File_Name and Temp_File_Name_Last_Digit
65 Current_Temp_File_Name
: String := First_Temp_File_Name
;
66 -- Name of the temp file last created
68 Temp_File_Name_Last_Digit
: constant Positive :=
69 First_Temp_File_Name
'Last - 4;
70 -- Position of the last digit in Current_Temp_File_Name
72 Max_Attempts
: constant := 100;
73 -- The maximum number of attempts to create a new temp file
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 function Args_Length
(Args
: Argument_List
) return Natural;
80 -- Returns total number of characters needed to create a string
81 -- of all Args terminated by ASCII.NUL characters
83 function C_String_Length
(S
: Address
) return Integer;
84 -- Returns the length of a C string. Does check for null address
87 procedure Spawn_Internal
88 (Program_Name
: String;
93 -- Internal routine to implement the two Spawn (blocking/non blocking)
94 -- routines. If Blocking is set to True then the spawn is blocking
95 -- otherwise it is non blocking. In this latter case the Pid contains the
96 -- process id number. The first three parameters are as in Spawn. Note that
97 -- Spawn_Internal normalizes the argument list before calling the low level
98 -- system spawn routines (see Normalize_Arguments).
100 -- Note: Normalize_Arguments is designed to do nothing if it is called more
101 -- than once, so calling Normalize_Arguments before calling one of the
102 -- spawn routines is fine.
104 function To_Path_String_Access
105 (Path_Addr
: Address
;
106 Path_Len
: Integer) return String_Access
;
107 -- Converts a C String to an Ada String. We could do this making use of
108 -- Interfaces.C.Strings but we prefer not to import that entire package
114 function "<" (X
, Y
: OS_Time
) return Boolean is
116 return Long_Integer (X
) < Long_Integer (Y
);
123 function "<=" (X
, Y
: OS_Time
) return Boolean is
125 return Long_Integer (X
) <= Long_Integer (Y
);
132 function ">" (X
, Y
: OS_Time
) return Boolean is
134 return Long_Integer (X
) > Long_Integer (Y
);
141 function ">=" (X
, Y
: OS_Time
) return Boolean is
143 return Long_Integer (X
) >= Long_Integer (Y
);
150 function Args_Length
(Args
: Argument_List
) return Natural is
154 for J
in Args
'Range loop
155 Len
:= Len
+ Args
(J
)'Length + 1; -- One extra for ASCII.NUL
161 -----------------------------
162 -- Argument_String_To_List --
163 -----------------------------
165 function Argument_String_To_List
166 (Arg_String
: String) return Argument_List_Access
168 Max_Args
: constant Integer := Arg_String
'Length;
169 New_Argv
: Argument_List
(1 .. Max_Args
);
170 New_Argc
: Natural := 0;
174 Idx
:= Arg_String
'First;
177 exit when Idx
> Arg_String
'Last;
180 Quoted
: Boolean := False;
181 Backqd
: Boolean := False;
188 -- An unquoted space is the end of an argument
190 if not (Backqd
or Quoted
)
191 and then Arg_String
(Idx
) = ' '
195 -- Start of a quoted string
197 elsif not (Backqd
or Quoted
)
198 and then Arg_String
(Idx
) = '"'
202 -- End of a quoted string and end of an argument
204 elsif (Quoted
and not Backqd
)
205 and then Arg_String
(Idx
) = '"'
210 -- Following character is backquoted
212 elsif Arg_String
(Idx
) = '\' then
215 -- Turn off backquoting after advancing one character
223 exit when Idx
> Arg_String
'Last;
228 New_Argc
:= New_Argc
+ 1;
229 New_Argv
(New_Argc
) :=
230 new String'(Arg_String (Old_Idx .. Idx - 1));
232 -- Skip extraneous spaces
234 while Idx <= Arg_String'Last and then Arg_String (Idx) = ' ' loop
240 return new Argument_List'(New_Argv
(1 .. New_Argc
));
241 end Argument_String_To_List
;
243 ---------------------
244 -- C_String_Length --
245 ---------------------
247 function C_String_Length
(S
: Address
) return Integer is
248 function Strlen
(S
: Address
) return Integer;
249 pragma Import
(C
, Strlen
, "strlen");
251 if S
= Null_Address
then
262 procedure Close
(FD
: File_Descriptor
) is
263 procedure C_Close
(FD
: File_Descriptor
);
264 pragma Import
(C
, C_Close
, "close");
269 procedure Close
(FD
: File_Descriptor
; Status
: out Boolean) is
270 function C_Close
(FD
: File_Descriptor
) return Integer;
271 pragma Import
(C
, C_Close
, "close");
273 Status
:= (C_Close
(FD
) = 0);
283 Success
: out Boolean;
284 Mode
: Copy_Mode
:= Copy
;
285 Preserve
: Attribute
:= Time_Stamps
)
287 From
: File_Descriptor
;
288 To
: File_Descriptor
;
290 Copy_Error
: exception;
291 -- Internal exception raised to signal error in copy
293 function Build_Path
(Dir
: String; File
: String) return String;
294 -- Returns pathname Dir concatenated with File adding the directory
295 -- separator only if needed.
297 procedure Copy
(From
, To
: File_Descriptor
);
298 -- Read data from From and place them into To. In both cases the
299 -- operations uses the current file position. Raises Constraint_Error
300 -- if a problem occurs during the copy.
302 procedure Copy_To
(To_Name
: String);
303 -- Does a straight copy from source to designated destination file
309 function Build_Path
(Dir
: String; File
: String) return String is
310 Res
: String (1 .. Dir
'Length + File
'Length + 1);
312 Base_File_Ptr
: Integer;
313 -- The base file name is File (Base_File_Ptr + 1 .. File'Last)
315 function Is_Dirsep
(C
: Character) return Boolean;
316 pragma Inline
(Is_Dirsep
);
317 -- Returns True if C is a directory separator. On Windows we
318 -- handle both styles of directory separator.
324 function Is_Dirsep
(C
: Character) return Boolean is
326 return C
= Directory_Separator
or else C
= '/';
329 -- Start of processing for Build_Path
332 -- Find base file name
334 Base_File_Ptr
:= File
'Last;
335 while Base_File_Ptr
>= File
'First loop
336 exit when Is_Dirsep
(File
(Base_File_Ptr
));
337 Base_File_Ptr
:= Base_File_Ptr
- 1;
341 Base_File
: String renames
342 File
(Base_File_Ptr
+ 1 .. File
'Last);
345 Res
(1 .. Dir
'Length) := Dir
;
347 if Is_Dirsep
(Dir
(Dir
'Last)) then
348 Res
(Dir
'Length + 1 .. Dir
'Length + Base_File
'Length) :=
350 return Res
(1 .. Dir
'Length + Base_File
'Length);
353 Res
(Dir
'Length + 1) := Directory_Separator
;
354 Res
(Dir
'Length + 2 .. Dir
'Length + 1 + Base_File
'Length) :=
356 return Res
(1 .. Dir
'Length + 1 + Base_File
'Length);
365 procedure Copy
(From
, To
: File_Descriptor
) is
366 Buf_Size
: constant := 200_000
;
367 type Buf
is array (1 .. Buf_Size
) of Character;
368 type Buf_Ptr
is access Buf
;
374 Status_From
: Boolean;
376 -- Statuses for the calls to Close
378 procedure Free
is new Ada
.Unchecked_Deallocation
(Buf
, Buf_Ptr
);
381 -- Check for invalid descriptors, making sure that we do not
382 -- accidentally leave an open file descriptor around.
384 if From
= Invalid_FD
then
385 if To
/= Invalid_FD
then
386 Close
(To
, Status_To
);
391 elsif To
= Invalid_FD
then
392 Close
(From
, Status_From
);
396 -- Allocate the buffer on the heap
401 R
:= Read
(From
, Buffer
(1)'Address, Buf_Size
);
403 -- For VMS, the buffer may not be full. So, we need to try again
404 -- until there is nothing to read.
408 W
:= Write
(To
, Buffer
(1)'Address, R
);
412 -- Problem writing data, could be a disk full. Close files
413 -- without worrying about status, since we are raising a
414 -- Copy_Error exception in any case.
416 Close
(From
, Status_From
);
417 Close
(To
, Status_To
);
425 Close
(From
, Status_From
);
426 Close
(To
, Status_To
);
430 if not (Status_From
and Status_To
) then
439 procedure Copy_To
(To_Name
: String) is
441 function Copy_Attributes
442 (From
, To
: System
.Address
;
443 Mode
: Integer) return Integer;
444 pragma Import
(C
, Copy_Attributes
, "__gnat_copy_attribs");
445 -- Mode = 0 - copy only time stamps.
446 -- Mode = 1 - copy time stamps and read/write/execute attributes
448 C_From
: String (1 .. Name
'Length + 1);
449 C_To
: String (1 .. To_Name
'Length + 1);
452 From
:= Open_Read
(Name
, Binary
);
454 -- Do not clobber destination file if source file could not be opened
456 if From
/= Invalid_FD
then
457 To
:= Create_File
(To_Name
, Binary
);
464 C_From
(1 .. Name
'Length) := Name
;
465 C_From
(C_From
'Last) := ASCII
.NUL
;
467 C_To
(1 .. To_Name
'Length) := To_Name
;
468 C_To
(C_To
'Last) := ASCII
.NUL
;
473 if Copy_Attributes
(C_From
'Address, C_To
'Address, 0) = -1 then
478 if Copy_Attributes
(C_From
'Address, C_To
'Address, 1) = -1 then
488 -- Start of processing for Copy_File
493 -- The source file must exist
495 if not Is_Regular_File
(Name
) then
499 -- The source file exists
503 -- Copy case, target file must not exist
507 -- If the target file exists, we have an error
509 if Is_Regular_File
(Pathname
) then
512 -- Case of target is a directory
514 elsif Is_Directory
(Pathname
) then
516 Dest
: constant String := Build_Path
(Pathname
, Name
);
519 -- If target file exists, we have an error, else do copy
521 if Is_Regular_File
(Dest
) then
528 -- Case of normal copy to file (destination does not exist)
534 -- Overwrite case (destination file may or may not exist)
537 if Is_Directory
(Pathname
) then
538 Copy_To
(Build_Path
(Pathname
, Name
));
543 -- Append case (destination file may or may not exist)
547 -- Appending to existing file
549 if Is_Regular_File
(Pathname
) then
551 -- Append mode and destination file exists, append data at the
552 -- end of Pathname. But if we fail to open source file, do not
553 -- touch destination file at all.
555 From
:= Open_Read
(Name
, Binary
);
556 if From
/= Invalid_FD
then
557 To
:= Open_Read_Write
(Pathname
, Binary
);
560 Lseek
(To
, 0, Seek_End
);
564 -- Appending to directory, not allowed
566 elsif Is_Directory
(Pathname
) then
569 -- Appending when target file does not exist
576 -- All error cases are caught here
585 Pathname
: C_File_Name
;
586 Success
: out Boolean;
587 Mode
: Copy_Mode
:= Copy
;
588 Preserve
: Attribute
:= Time_Stamps
)
590 Ada_Name
: String_Access
:=
591 To_Path_String_Access
592 (Name
, C_String_Length
(Name
));
593 Ada_Pathname
: String_Access
:=
594 To_Path_String_Access
595 (Pathname
, C_String_Length
(Pathname
));
597 Copy_File
(Ada_Name
.all, Ada_Pathname
.all, Success
, Mode
, Preserve
);
602 ----------------------
603 -- Copy_Time_Stamps --
604 ----------------------
606 procedure Copy_Time_Stamps
(Source
, Dest
: String; Success
: out Boolean) is
608 function Copy_Attributes
609 (From
, To
: System
.Address
;
610 Mode
: Integer) return Integer;
611 pragma Import
(C
, Copy_Attributes
, "__gnat_copy_attribs");
612 -- Mode = 0 - copy only time stamps.
613 -- Mode = 1 - copy time stamps and read/write/execute attributes
616 if Is_Regular_File
(Source
) and then Is_Writable_File
(Dest
) then
618 C_Source
: String (1 .. Source
'Length + 1);
619 C_Dest
: String (1 .. Dest
'Length + 1);
622 C_Source
(1 .. Source
'Length) := Source
;
623 C_Source
(C_Source
'Last) := ASCII
.NUL
;
625 C_Dest
(1 .. Dest
'Length) := Dest
;
626 C_Dest
(C_Dest
'Last) := ASCII
.NUL
;
628 if Copy_Attributes
(C_Source
'Address, C_Dest
'Address, 0) = -1 then
638 end Copy_Time_Stamps
;
640 procedure Copy_Time_Stamps
641 (Source
, Dest
: C_File_Name
;
642 Success
: out Boolean)
644 Ada_Source
: String_Access
:=
645 To_Path_String_Access
646 (Source
, C_String_Length
(Source
));
647 Ada_Dest
: String_Access
:=
648 To_Path_String_Access
649 (Dest
, C_String_Length
(Dest
));
651 Copy_Time_Stamps
(Ada_Source
.all, Ada_Dest
.all, Success
);
654 end Copy_Time_Stamps
;
662 Fmode
: Mode
) return File_Descriptor
664 function C_Create_File
666 Fmode
: Mode
) return File_Descriptor
;
667 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);
680 C_Name
(1 .. Name
'Length) := Name
;
681 C_Name
(C_Name
'Last) := ASCII
.NUL
;
682 return Create_File
(C_Name
(C_Name
'First)'Address, Fmode
);
685 ---------------------
686 -- Create_New_File --
687 ---------------------
689 function Create_New_File
691 Fmode
: Mode
) return File_Descriptor
693 function C_Create_New_File
695 Fmode
: Mode
) return File_Descriptor
;
696 pragma Import
(C
, C_Create_New_File
, "__gnat_open_new");
699 return C_Create_New_File
(Name
, Fmode
);
702 function Create_New_File
704 Fmode
: Mode
) return File_Descriptor
706 C_Name
: String (1 .. Name
'Length + 1);
709 C_Name
(1 .. Name
'Length) := Name
;
710 C_Name
(C_Name
'Last) := ASCII
.NUL
;
711 return Create_New_File
(C_Name
(C_Name
'First)'Address, Fmode
);
714 -----------------------------
715 -- Create_Output_Text_File --
716 -----------------------------
718 function Create_Output_Text_File
(Name
: String) return File_Descriptor
is
719 function C_Create_File
720 (Name
: C_File_Name
) return File_Descriptor
;
721 pragma Import
(C
, C_Create_File
, "__gnat_create_output_file");
723 C_Name
: String (1 .. Name
'Length + 1);
726 C_Name
(1 .. Name
'Length) := Name
;
727 C_Name
(C_Name
'Last) := ASCII
.NUL
;
728 return C_Create_File
(C_Name
(C_Name
'First)'Address);
729 end Create_Output_Text_File
;
731 ----------------------
732 -- Create_Temp_File --
733 ----------------------
735 procedure Create_Temp_File
736 (FD
: out File_Descriptor
;
737 Name
: out Temp_File_Name
)
739 function Open_New_Temp
740 (Name
: System
.Address
;
741 Fmode
: Mode
) return File_Descriptor
;
742 pragma Import
(C
, Open_New_Temp
, "__gnat_open_new_temp");
745 FD
:= Open_New_Temp
(Name
'Address, Binary
);
746 end Create_Temp_File
;
748 procedure Create_Temp_File
749 (FD
: out File_Descriptor
;
750 Name
: out String_Access
)
753 Attempts
: Natural := 0;
754 Current
: String (Current_Temp_File_Name
'Range);
757 -- Loop until a new temp file can be created
761 -- We need to protect global variable Current_Temp_File_Name
762 -- against concurrent access by different tasks.
766 -- Start at the last digit
768 Pos
:= Temp_File_Name_Last_Digit
;
772 -- Increment the digit by one
774 case Current_Temp_File_Name
(Pos
) is
776 Current_Temp_File_Name
(Pos
) :=
777 Character'Succ (Current_Temp_File_Name
(Pos
));
782 -- For 9, set the digit to 0 and go to the previous digit
784 Current_Temp_File_Name
(Pos
) := '0';
789 -- If it is not a digit, then there are no available
790 -- temp file names. Return Invalid_FD. There is almost
791 -- no chance that this code will be ever be executed,
792 -- since it would mean that there are one million temp
793 -- files in the same directory!
802 Current
:= Current_Temp_File_Name
;
804 -- We can now release the lock, because we are no longer
805 -- accessing Current_Temp_File_Name.
815 -- Attempt to create the file
817 FD
:= Create_New_File
(Current
, Binary
);
819 if FD
/= Invalid_FD
then
820 Name
:= new String'(Current);
824 if not Is_Regular_File (Current) then
826 -- If the file does not already exist and we are unable to create
827 -- it, we give up after Max_Attempts. Otherwise, we try again with
828 -- the next available file name.
830 Attempts := Attempts + 1;
832 if Attempts >= Max_Attempts then
839 end Create_Temp_File;
845 procedure Delete_File (Name : Address; Success : out Boolean) is
848 R := System.CRTL.unlink (Name);
852 procedure Delete_File (Name : String; Success : out Boolean) is
853 C_Name : String (1 .. Name'Length + 1);
856 C_Name (1 .. Name'Length) := Name;
857 C_Name (C_Name'Last) := ASCII.NUL;
859 Delete_File (C_Name'Address, Success);
862 ---------------------
863 -- File_Time_Stamp --
864 ---------------------
866 function File_Time_Stamp (FD : File_Descriptor) return OS_Time is
867 function File_Time (FD : File_Descriptor) return OS_Time;
868 pragma Import (C, File_Time, "__gnat_file_time_fd");
870 return File_Time (FD);
873 function File_Time_Stamp (Name : C_File_Name) return OS_Time is
874 function File_Time (Name : Address) return OS_Time;
875 pragma Import (C, File_Time, "__gnat_file_time_name");
877 return File_Time (Name);
880 function File_Time_Stamp (Name : String) return OS_Time is
881 F_Name : String (1 .. Name'Length + 1);
883 F_Name (1 .. Name'Length) := Name;
884 F_Name (F_Name'Last) := ASCII.NUL;
885 return File_Time_Stamp (F_Name'Address);
888 ---------------------------
889 -- Get_Debuggable_Suffix --
890 ---------------------------
892 function Get_Debuggable_Suffix return String_Access is
893 procedure Get_Suffix_Ptr (Length, Ptr : Address);
894 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_debuggable_suffix_ptr");
896 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
897 pragma Import (C, Strncpy, "strncpy");
899 Suffix_Ptr : Address;
900 Suffix_Length : Integer;
901 Result : String_Access;
904 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
906 Result := new String (1 .. Suffix_Length);
908 if Suffix_Length > 0 then
909 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
913 end Get_Debuggable_Suffix;
915 ---------------------------
916 -- Get_Executable_Suffix --
917 ---------------------------
919 function Get_Executable_Suffix return String_Access is
920 procedure Get_Suffix_Ptr (Length, Ptr : Address);
921 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_executable_suffix_ptr");
923 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
924 pragma Import (C, Strncpy, "strncpy");
926 Suffix_Ptr : Address;
927 Suffix_Length : Integer;
928 Result : String_Access;
931 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
933 Result := new String (1 .. Suffix_Length);
935 if Suffix_Length > 0 then
936 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
940 end Get_Executable_Suffix;
942 -----------------------
943 -- Get_Object_Suffix --
944 -----------------------
946 function Get_Object_Suffix return String_Access is
947 procedure Get_Suffix_Ptr (Length, Ptr : Address);
948 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_object_suffix_ptr");
950 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
951 pragma Import (C, Strncpy, "strncpy");
953 Suffix_Ptr : Address;
954 Suffix_Length : Integer;
955 Result : String_Access;
958 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
960 Result := new String (1 .. Suffix_Length);
962 if Suffix_Length > 0 then
963 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
967 end Get_Object_Suffix;
969 ----------------------------------
970 -- Get_Target_Debuggable_Suffix --
971 ----------------------------------
973 function Get_Target_Debuggable_Suffix return String_Access is
974 Target_Exec_Ext_Ptr : Address;
976 (C, Target_Exec_Ext_Ptr, "__gnat_target_debuggable_extension");
978 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
979 pragma Import (C, Strncpy, "strncpy");
981 function Strlen (Cstring : Address) return Integer;
982 pragma Import (C, Strlen, "strlen");
984 Suffix_Length : Integer;
985 Result : String_Access;
988 Suffix_Length := Strlen (Target_Exec_Ext_Ptr);
990 Result := new String (1 .. Suffix_Length);
992 if Suffix_Length > 0 then
993 Strncpy (Result.all'Address, Target_Exec_Ext_Ptr, Suffix_Length);
997 end Get_Target_Debuggable_Suffix;
999 ----------------------------------
1000 -- Get_Target_Executable_Suffix --
1001 ----------------------------------
1003 function Get_Target_Executable_Suffix return String_Access is
1004 Target_Exec_Ext_Ptr : Address;
1006 (C, Target_Exec_Ext_Ptr, "__gnat_target_executable_extension");
1008 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1009 pragma Import (C, Strncpy, "strncpy");
1011 function Strlen (Cstring : Address) return Integer;
1012 pragma Import (C, Strlen, "strlen");
1014 Suffix_Length : Integer;
1015 Result : String_Access;
1018 Suffix_Length := Strlen (Target_Exec_Ext_Ptr);
1020 Result := new String (1 .. Suffix_Length);
1022 if Suffix_Length > 0 then
1023 Strncpy (Result.all'Address, Target_Exec_Ext_Ptr, Suffix_Length);
1027 end Get_Target_Executable_Suffix;
1029 ------------------------------
1030 -- Get_Target_Object_Suffix --
1031 ------------------------------
1033 function Get_Target_Object_Suffix return String_Access is
1034 Target_Object_Ext_Ptr : Address;
1036 (C, Target_Object_Ext_Ptr, "__gnat_target_object_extension");
1038 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1039 pragma Import (C, Strncpy, "strncpy");
1041 function Strlen (Cstring : Address) return Integer;
1042 pragma Import (C, Strlen, "strlen");
1044 Suffix_Length : Integer;
1045 Result : String_Access;
1048 Suffix_Length := Strlen (Target_Object_Ext_Ptr);
1050 Result := new String (1 .. Suffix_Length);
1052 if Suffix_Length > 0 then
1053 Strncpy (Result.all'Address, Target_Object_Ext_Ptr, Suffix_Length);
1057 end Get_Target_Object_Suffix;
1063 function Getenv (Name : String) return String_Access is
1064 procedure Get_Env_Value_Ptr (Name, Length, Ptr : Address);
1065 pragma Import (C, Get_Env_Value_Ptr, "__gnat_getenv");
1067 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1068 pragma Import (C, Strncpy, "strncpy");
1070 Env_Value_Ptr : aliased Address;
1071 Env_Value_Length : aliased Integer;
1072 F_Name : aliased String (1 .. Name'Length + 1);
1073 Result : String_Access;
1076 F_Name (1 .. Name'Length) := Name;
1077 F_Name (F_Name'Last) := ASCII.NUL;
1080 (F_Name'Address, Env_Value_Length'Address, Env_Value_Ptr'Address);
1082 Result := new String (1 .. Env_Value_Length);
1084 if Env_Value_Length > 0 then
1085 Strncpy (Result.all'Address, Env_Value_Ptr, Env_Value_Length);
1095 function GM_Day (Date : OS_Time) return Day_Type is
1098 pragma Warnings (Off);
1104 pragma Warnings (On);
1107 GM_Split (Date, Y, Mo, D, H, Mn, S);
1115 function GM_Hour (Date : OS_Time) return Hour_Type is
1118 pragma Warnings (Off);
1124 pragma Warnings (On);
1127 GM_Split (Date, Y, Mo, D, H, Mn, S);
1135 function GM_Minute (Date : OS_Time) return Minute_Type is
1138 pragma Warnings (Off);
1144 pragma Warnings (On);
1147 GM_Split (Date, Y, Mo, D, H, Mn, S);
1155 function GM_Month (Date : OS_Time) return Month_Type is
1158 pragma Warnings (Off);
1164 pragma Warnings (On);
1167 GM_Split (Date, Y, Mo, D, H, Mn, S);
1175 function GM_Second (Date : OS_Time) return Second_Type is
1178 pragma Warnings (Off);
1184 pragma Warnings (On);
1187 GM_Split (Date, Y, Mo, D, H, Mn, S);
1197 Year : out Year_Type;
1198 Month : out Month_Type;
1200 Hour : out Hour_Type;
1201 Minute : out Minute_Type;
1202 Second : out Second_Type)
1204 procedure To_GM_Time
1205 (P_Time_T, P_Year, P_Month, P_Day, P_Hours, P_Mins, P_Secs : Address);
1206 pragma Import (C, To_GM_Time, "__gnat_to_gm_time");
1208 T : OS_Time := Date;
1217 -- Use the global lock because To_GM_Time is not thread safe
1219 Locked_Processing : begin
1222 (T'Address, Y'Address, Mo'Address, D'Address,
1223 H'Address, Mn'Address, S'Address);
1224 SSL.Unlock_Task.all;
1228 SSL.Unlock_Task.all;
1230 end Locked_Processing;
1244 function GM_Year (Date : OS_Time) return Year_Type is
1247 pragma Warnings (Off);
1253 pragma Warnings (On);
1256 GM_Split (Date, Y, Mo, D, H, Mn, S);
1260 ----------------------
1261 -- Is_Absolute_Path --
1262 ----------------------
1264 function Is_Absolute_Path (Name : String) return Boolean is
1265 function Is_Absolute_Path
1267 Length : Integer) return Integer;
1268 pragma Import (C, Is_Absolute_Path, "__gnat_is_absolute_path");
1270 return Is_Absolute_Path (Name'Address, Name'Length) /= 0;
1271 end Is_Absolute_Path;
1277 function Is_Directory (Name : C_File_Name) return Boolean is
1278 function Is_Directory (Name : Address) return Integer;
1279 pragma Import (C, Is_Directory, "__gnat_is_directory");
1281 return Is_Directory (Name) /= 0;
1284 function Is_Directory (Name : String) return Boolean is
1285 F_Name : String (1 .. Name'Length + 1);
1287 F_Name (1 .. Name'Length) := Name;
1288 F_Name (F_Name'Last) := ASCII.NUL;
1289 return Is_Directory (F_Name'Address);
1292 ----------------------
1293 -- Is_Readable_File --
1294 ----------------------
1296 function Is_Readable_File (Name : C_File_Name) return Boolean is
1297 function Is_Readable_File (Name : Address) return Integer;
1298 pragma Import (C, Is_Readable_File, "__gnat_is_readable_file");
1300 return Is_Readable_File (Name) /= 0;
1301 end Is_Readable_File;
1303 function Is_Readable_File (Name : String) return Boolean is
1304 F_Name : String (1 .. Name'Length + 1);
1306 F_Name (1 .. Name'Length) := Name;
1307 F_Name (F_Name'Last) := ASCII.NUL;
1308 return Is_Readable_File (F_Name'Address);
1309 end Is_Readable_File;
1311 ------------------------
1312 -- Is_Executable_File --
1313 ------------------------
1315 function Is_Executable_File (Name : C_File_Name) return Boolean is
1316 function Is_Executable_File (Name : Address) return Integer;
1317 pragma Import (C, Is_Executable_File, "__gnat_is_executable_file");
1319 return Is_Executable_File (Name) /= 0;
1320 end Is_Executable_File;
1322 function Is_Executable_File (Name : String) return Boolean is
1323 F_Name : String (1 .. Name'Length + 1);
1325 F_Name (1 .. Name'Length) := Name;
1326 F_Name (F_Name'Last) := ASCII.NUL;
1327 return Is_Executable_File (F_Name'Address);
1328 end Is_Executable_File;
1330 ---------------------
1331 -- Is_Regular_File --
1332 ---------------------
1334 function Is_Regular_File (Name : C_File_Name) return Boolean is
1335 function Is_Regular_File (Name : Address) return Integer;
1336 pragma Import (C, Is_Regular_File, "__gnat_is_regular_file");
1338 return Is_Regular_File (Name) /= 0;
1339 end Is_Regular_File;
1341 function Is_Regular_File (Name : String) return Boolean is
1342 F_Name : String (1 .. Name'Length + 1);
1344 F_Name (1 .. Name'Length) := Name;
1345 F_Name (F_Name'Last) := ASCII.NUL;
1346 return Is_Regular_File (F_Name'Address);
1347 end Is_Regular_File;
1349 ----------------------
1350 -- Is_Symbolic_Link --
1351 ----------------------
1353 function Is_Symbolic_Link (Name : C_File_Name) return Boolean is
1354 function Is_Symbolic_Link (Name : Address) return Integer;
1355 pragma Import (C, Is_Symbolic_Link, "__gnat_is_symbolic_link");
1357 return Is_Symbolic_Link (Name) /= 0;
1358 end Is_Symbolic_Link;
1360 function Is_Symbolic_Link (Name : String) return Boolean is
1361 F_Name : String (1 .. Name'Length + 1);
1363 F_Name (1 .. Name'Length) := Name;
1364 F_Name (F_Name'Last) := ASCII.NUL;
1365 return Is_Symbolic_Link (F_Name'Address);
1366 end Is_Symbolic_Link;
1368 ----------------------
1369 -- Is_Writable_File --
1370 ----------------------
1372 function Is_Writable_File (Name : C_File_Name) return Boolean is
1373 function Is_Writable_File (Name : Address) return Integer;
1374 pragma Import (C, Is_Writable_File, "__gnat_is_writable_file");
1376 return Is_Writable_File (Name) /= 0;
1377 end Is_Writable_File;
1379 function Is_Writable_File (Name : String) return Boolean is
1380 F_Name : String (1 .. Name'Length + 1);
1382 F_Name (1 .. Name'Length) := Name;
1383 F_Name (F_Name'Last) := ASCII.NUL;
1384 return Is_Writable_File (F_Name'Address);
1385 end Is_Writable_File;
1387 -------------------------
1388 -- Locate_Exec_On_Path --
1389 -------------------------
1391 function Locate_Exec_On_Path
1392 (Exec_Name : String) return String_Access
1394 function Locate_Exec_On_Path (C_Exec_Name : Address) return Address;
1395 pragma Import (C, Locate_Exec_On_Path, "__gnat_locate_exec_on_path");
1397 procedure Free (Ptr : System.Address);
1398 pragma Import (C, Free, "free");
1400 C_Exec_Name : String (1 .. Exec_Name'Length + 1);
1401 Path_Addr : Address;
1403 Result : String_Access;
1406 C_Exec_Name (1 .. Exec_Name'Length) := Exec_Name;
1407 C_Exec_Name (C_Exec_Name'Last) := ASCII.NUL;
1409 Path_Addr := Locate_Exec_On_Path (C_Exec_Name'Address);
1410 Path_Len := C_String_Length (Path_Addr);
1412 if Path_Len = 0 then
1416 Result := To_Path_String_Access (Path_Addr, Path_Len);
1419 -- Always return an absolute path name
1421 if not Is_Absolute_Path (Result.all) then
1423 Absolute_Path : constant String :=
1424 Normalize_Pathname (Result.all);
1427 Result := new String'(Absolute_Path
);
1433 end Locate_Exec_On_Path
;
1435 -------------------------
1436 -- Locate_Regular_File --
1437 -------------------------
1439 function Locate_Regular_File
1440 (File_Name
: C_File_Name
;
1441 Path
: C_File_Name
) return String_Access
1443 function Locate_Regular_File
1444 (C_File_Name
, Path_Val
: Address
) return Address
;
1445 pragma Import
(C
, Locate_Regular_File
, "__gnat_locate_regular_file");
1447 procedure Free
(Ptr
: System
.Address
);
1448 pragma Import
(C
, Free
, "free");
1450 Path_Addr
: Address
;
1452 Result
: String_Access
;
1455 Path_Addr
:= Locate_Regular_File
(File_Name
, Path
);
1456 Path_Len
:= C_String_Length
(Path_Addr
);
1458 if Path_Len
= 0 then
1462 Result
:= To_Path_String_Access
(Path_Addr
, Path_Len
);
1466 end Locate_Regular_File
;
1468 function Locate_Regular_File
1469 (File_Name
: String;
1470 Path
: String) return String_Access
1472 C_File_Name
: String (1 .. File_Name
'Length + 1);
1473 C_Path
: String (1 .. Path
'Length + 1);
1474 Result
: String_Access
;
1477 C_File_Name
(1 .. File_Name
'Length) := File_Name
;
1478 C_File_Name
(C_File_Name
'Last) := ASCII
.NUL
;
1480 C_Path
(1 .. Path
'Length) := Path
;
1481 C_Path
(C_Path
'Last) := ASCII
.NUL
;
1483 Result
:= Locate_Regular_File
(C_File_Name
'Address, C_Path
'Address);
1485 -- Always return an absolute path name
1487 if Result
/= null and then not Is_Absolute_Path
(Result
.all) then
1489 Absolute_Path
: constant String := Normalize_Pathname
(Result
.all);
1492 Result
:= new String'(Absolute_Path);
1497 end Locate_Regular_File;
1499 ------------------------
1500 -- Non_Blocking_Spawn --
1501 ------------------------
1503 function Non_Blocking_Spawn
1504 (Program_Name : String;
1505 Args : Argument_List) return Process_Id
1509 pragma Warnings (Off, Junk);
1511 Spawn_Internal (Program_Name, Args, Junk, Pid, Blocking => False);
1513 end Non_Blocking_Spawn;
1515 function Non_Blocking_Spawn
1516 (Program_Name : String;
1517 Args : Argument_List;
1518 Output_File_Descriptor : File_Descriptor;
1519 Err_To_Out : Boolean := True) return Process_Id
1521 Saved_Output : File_Descriptor;
1522 Saved_Error : File_Descriptor := Invalid_FD; -- prevent warning
1526 if Output_File_Descriptor = Invalid_FD then
1530 -- Set standard output and, if specified, error to the temporary file
1532 Saved_Output := Dup (Standout);
1533 Dup2 (Output_File_Descriptor, Standout);
1536 Saved_Error := Dup (Standerr);
1537 Dup2 (Output_File_Descriptor, Standerr);
1540 -- Spawn the program
1542 Pid := Non_Blocking_Spawn (Program_Name, Args);
1544 -- Restore the standard output and error
1546 Dup2 (Saved_Output, Standout);
1549 Dup2 (Saved_Error, Standerr);
1552 -- And close the saved standard output and error file descriptors
1554 Close (Saved_Output);
1557 Close (Saved_Error);
1561 end Non_Blocking_Spawn;
1563 function Non_Blocking_Spawn
1564 (Program_Name : String;
1565 Args : Argument_List;
1566 Output_File : String;
1567 Err_To_Out : Boolean := True) return Process_Id
1569 Output_File_Descriptor : constant File_Descriptor :=
1570 Create_Output_Text_File (Output_File);
1571 Result : Process_Id;
1574 -- Do not attempt to spawn if the output file could not be created
1576 if Output_File_Descriptor = Invalid_FD then
1580 Result := Non_Blocking_Spawn
1581 (Program_Name, Args, Output_File_Descriptor, Err_To_Out);
1583 -- Close the file just created for the output, as the file descriptor
1584 -- cannot be used anywhere, being a local value. It is safe to do
1585 -- that, as the file descriptor has been duplicated to form
1586 -- standard output and error of the spawned process.
1588 Close (Output_File_Descriptor);
1592 end Non_Blocking_Spawn;
1594 -------------------------
1595 -- Normalize_Arguments --
1596 -------------------------
1598 procedure Normalize_Arguments (Args : in out Argument_List) is
1600 procedure Quote_Argument (Arg : in out String_Access);
1601 -- Add quote around argument if it contains spaces
1603 C_Argument_Needs_Quote : Integer;
1604 pragma Import (C, C_Argument_Needs_Quote, "__gnat_argument_needs_quote");
1605 Argument_Needs_Quote : constant Boolean := C_Argument_Needs_Quote /= 0;
1607 --------------------
1608 -- Quote_Argument --
1609 --------------------
1611 procedure Quote_Argument (Arg : in out String_Access) is
1612 Res : String (1 .. Arg'Length * 2);
1614 Quote_Needed : Boolean := False;
1617 if Arg (Arg'First) /= '"' or else Arg (Arg'Last) /= '"' then
1623 for K in Arg'Range loop
1627 if Arg (K) = '"' then
1631 Quote_Needed := True;
1633 elsif Arg (K) = ' ' then
1635 Quote_Needed := True;
1643 if Quote_Needed then
1645 -- If null terminated string, put the quote before
1647 if Res (J) = ASCII.NUL then
1650 Res (J) := ASCII.NUL;
1652 -- If argument is terminated by '\
', then double it. Otherwise
1653 -- the ending quote will be taken as-is. This is quite strange
1654 -- spawn behavior from Windows, but this is what we see!
1657 if Res (J) = '\
' then
1669 Old : String_Access := Arg;
1672 Arg := new String'(Res (1 .. J));
1680 -- Start of processing for Normalize_Arguments
1683 if Argument_Needs_Quote then
1684 for K in Args'Range loop
1685 if Args (K) /= null and then Args (K)'Length /= 0 then
1686 Quote_Argument (Args (K));
1690 end Normalize_Arguments;
1692 ------------------------
1693 -- Normalize_Pathname --
1694 ------------------------
1696 function Normalize_Pathname
1698 Directory : String := "";
1699 Resolve_Links : Boolean := True;
1700 Case_Sensitive : Boolean := True) return String
1703 pragma Import (C, Max_Path, "__gnat_max_path_len
");
1704 -- Maximum length of a path name
1706 procedure Get_Current_Dir
1707 (Dir : System.Address;
1708 Length : System.Address);
1709 pragma Import (C, Get_Current_Dir, "__gnat_get_current_dir
");
1711 Path_Buffer : String (1 .. Max_Path + Max_Path + 2);
1712 End_Path : Natural := 0;
1713 Link_Buffer : String (1 .. Max_Path + 2);
1719 Max_Iterations : constant := 500;
1721 function Get_File_Names_Case_Sensitive return Integer;
1723 (C, Get_File_Names_Case_Sensitive,
1724 "__gnat_get_file_names_case_sensitive
");
1726 Fold_To_Lower_Case : constant Boolean :=
1728 and then Get_File_Names_Case_Sensitive = 0;
1731 (Path : System.Address;
1732 Buf : System.Address;
1733 Bufsiz : Integer) return Integer;
1734 pragma Import (C, Readlink, "__gnat_readlink
");
1736 function To_Canonical_File_Spec
1737 (Host_File : System.Address) return System.Address;
1739 (C, To_Canonical_File_Spec, "__gnat_to_canonical_file_spec
");
1741 The_Name : String (1 .. Name'Length + 1);
1742 Canonical_File_Addr : System.Address;
1743 Canonical_File_Len : Integer;
1745 function Strlen (S : System.Address) return Integer;
1746 pragma Import (C, Strlen, "strlen
");
1748 function Final_Value (S : String) return String;
1749 -- Make final adjustment to the returned string. This function strips
1750 -- trailing directory separators, and folds returned string to lower
1751 -- case if required.
1753 function Get_Directory (Dir : String) return String;
1754 -- If Dir is not empty, return it, adding a directory separator
1755 -- if not already present, otherwise return current working directory
1756 -- with terminating directory separator.
1762 function Final_Value (S : String) return String is
1764 -- We may need to fold S to lower case, so we need a variable
1769 if Fold_To_Lower_Case then
1770 System.Case_Util.To_Lower (S1);
1773 -- Remove trailing directory separator, if any
1778 and then (S1 (Last) = '/'
1780 S1 (Last) = Directory_Separator)
1782 -- Special case for Windows: C:\
1785 and then S1 (1) /= Directory_Separator
1786 and then S1 (2) = ':'
1795 return S1 (1 .. Last);
1802 function Get_Directory (Dir : String) return String is
1803 Result : String (1 .. Dir'Length + 1);
1804 Length : constant Natural := Dir'Length;
1807 -- Directory given, add directory separator if needed
1810 Result (1 .. Length) := Dir;
1812 -- On Windows, change all '/' to '\'
1815 for J in 1 .. Length loop
1816 if Result (J) = '/' then
1817 Result (J) := Directory_Separator;
1822 -- Add directory separator, if needed
1824 if Result (Length) = Directory_Separator then
1825 return Result (1 .. Length);
1827 Result (Result'Length) := Directory_Separator;
1831 -- Directory name not given, get current directory
1835 Buffer : String (1 .. Max_Path + 2);
1836 Path_Len : Natural := Max_Path;
1839 Get_Current_Dir (Buffer'Address, Path_Len'Address);
1841 if Buffer (Path_Len) /= Directory_Separator then
1842 Path_Len := Path_Len + 1;
1843 Buffer (Path_Len) := Directory_Separator;
1846 -- By default, the drive letter on Windows is in upper case
1849 and then Path_Len >= 2
1850 and then Buffer (2) = ':'
1852 System.Case_Util.To_Upper (Buffer (1 .. 1));
1855 return Buffer (1 .. Path_Len);
1860 -- Start of processing for Normalize_Pathname
1863 -- Special case, if name is null, then return null
1865 if Name'Length = 0 then
1869 -- First, convert VMS file spec to Unix file spec.
1870 -- If Name is not in VMS syntax, then this is equivalent
1871 -- to put Name at the beginning of Path_Buffer.
1873 VMS_Conversion : begin
1874 The_Name (1 .. Name'Length) := Name;
1875 The_Name (The_Name'Last) := ASCII.NUL;
1877 Canonical_File_Addr := To_Canonical_File_Spec (The_Name'Address);
1878 Canonical_File_Len := Strlen (Canonical_File_Addr);
1880 -- If VMS syntax conversion has failed, return an empty string
1881 -- to indicate the failure.
1883 if Canonical_File_Len = 0 then
1888 subtype Path_String is String (1 .. Canonical_File_Len);
1889 type Path_String_Access is access Path_String;
1891 function Address_To_Access is new
1892 Ada.Unchecked_Conversion (Source => Address,
1893 Target => Path_String_Access);
1895 Path_Access : constant Path_String_Access :=
1896 Address_To_Access (Canonical_File_Addr);
1899 Path_Buffer (1 .. Canonical_File_Len) := Path_Access.all;
1900 End_Path := Canonical_File_Len;
1905 -- Replace all '/' by Directory Separators (this is for Windows)
1907 if Directory_Separator /= '/' then
1908 for Index in 1 .. End_Path loop
1909 if Path_Buffer (Index) = '/' then
1910 Path_Buffer (Index) := Directory_Separator;
1915 -- Resolve directory names for Windows (formerly also VMS)
1917 -- On VMS, if we have a Unix path such as /temp/..., and TEMP is a
1918 -- logical name, we must not try to resolve this logical name, because
1919 -- it may have multiple equivalences and if resolved we will only
1920 -- get the first one.
1924 -- On Windows, if we have an absolute path starting with a directory
1925 -- separator, we need to have the drive letter appended in front.
1927 -- On Windows, Get_Current_Dir will return a suitable directory name
1928 -- (path starting with a drive letter on Windows). So we take this
1929 -- drive letter and prepend it to the current path.
1931 if Path_Buffer (1) = Directory_Separator
1932 and then Path_Buffer (2) /= Directory_Separator
1935 Cur_Dir : constant String := Get_Directory ("");
1936 -- Get the current directory to get the drive letter
1939 if Cur_Dir'Length > 2
1940 and then Cur_Dir (Cur_Dir'First + 1) = ':'
1942 Path_Buffer (3 .. End_Path + 2) :=
1943 Path_Buffer (1 .. End_Path);
1944 Path_Buffer (1 .. 2) :=
1945 Cur_Dir (Cur_Dir'First .. Cur_Dir'First + 1);
1946 End_Path := End_Path + 2;
1950 -- We have a drive letter, ensure it is upper-case
1952 elsif Path_Buffer (1) in 'a' .. 'z'
1953 and then Path_Buffer (2) = ':'
1955 System.Case_Util.To_Upper (Path_Buffer (1 .. 1));
1959 -- On Windows, remove all double-quotes that are possibly part of the
1960 -- path but can cause problems with other methods.
1967 Index := Path_Buffer'First;
1968 for Current in Path_Buffer'First .. End_Path loop
1969 if Path_Buffer (Current) /= '"' then
1970 Path_Buffer (Index) := Path_Buffer (Current);
1975 End_Path := Index - 1;
1979 -- Start the conversions
1981 -- If this is not finished after Max_Iterations, give up and return an
1984 for J in 1 .. Max_Iterations loop
1986 -- If we don't have an absolute pathname, prepend the directory
1990 and then not Is_Absolute_Path (Path_Buffer (1 .. End_Path))
1993 Reference_Dir : constant String := Get_Directory (Directory);
1994 Ref_Dir_Len : constant Natural := Reference_Dir'Length;
1995 -- Current directory name specified and its length
1998 Path_Buffer (Ref_Dir_Len + 1 .. Ref_Dir_Len + End_Path) :=
1999 Path_Buffer (1 .. End_Path);
2000 End_Path := Ref_Dir_Len + End_Path;
2001 Path_Buffer (1 .. Ref_Dir_Len) := Reference_Dir;
2002 Last := Ref_Dir_Len;
2009 -- Ensure that Windows network drives are kept, e.g: \\server\drive-c
2012 and then Directory_Separator = '\
'
2013 and then Path_Buffer (1 .. 2) = "\\"
2018 -- If we have traversed the full pathname, return it
2020 if Start > End_Path then
2021 return Final_Value (Path_Buffer (1 .. End_Path));
2024 -- Remove duplicate directory separators
2026 while Path_Buffer (Start) = Directory_Separator loop
2027 if Start = End_Path then
2028 return Final_Value (Path_Buffer (1 .. End_Path - 1));
2031 Path_Buffer (Start .. End_Path - 1) :=
2032 Path_Buffer (Start + 1 .. End_Path);
2033 End_Path := End_Path - 1;
2037 -- Find the end of the current field: last character or the one
2038 -- preceding the next directory separator.
2040 while Finish < End_Path
2041 and then Path_Buffer (Finish + 1) /= Directory_Separator
2043 Finish := Finish + 1;
2048 if Start = Finish and then Path_Buffer (Start) = '.' then
2049 if Start = End_Path then
2051 return (1 => Directory_Separator);
2054 if Fold_To_Lower_Case then
2055 System.Case_Util.To_Lower (Path_Buffer (1 .. Last - 1));
2058 return Path_Buffer (1 .. Last - 1);
2063 Path_Buffer (Last + 1 .. End_Path - 2) :=
2064 Path_Buffer (Last + 3 .. End_Path);
2065 End_Path := End_Path - 2;
2068 -- Remove ".." fields
2070 elsif Finish = Start + 1
2071 and then Path_Buffer (Start .. Finish) = ".."
2076 exit when Start < 1 or else
2077 Path_Buffer (Start) = Directory_Separator;
2081 if Finish = End_Path then
2082 return (1 => Directory_Separator);
2085 Path_Buffer (1 .. End_Path - Finish) :=
2086 Path_Buffer (Finish + 1 .. End_Path);
2087 End_Path := End_Path - Finish;
2092 if Finish = End_Path then
2093 return Final_Value (Path_Buffer (1 .. Start - 1));
2096 Path_Buffer (Start + 1 .. Start + End_Path - Finish - 1) :=
2097 Path_Buffer (Finish + 2 .. End_Path);
2098 End_Path := Start + End_Path - Finish - 1;
2103 -- Check if current field is a symbolic link
2105 elsif Resolve_Links then
2107 Saved : constant Character := Path_Buffer (Finish + 1);
2110 Path_Buffer (Finish + 1) := ASCII.NUL;
2111 Status := Readlink (Path_Buffer'Address,
2112 Link_Buffer'Address,
2113 Link_Buffer'Length);
2114 Path_Buffer (Finish + 1) := Saved;
2117 -- Not a symbolic link, move to the next field, if any
2122 -- Replace symbolic link with its value
2125 if Is_Absolute_Path (Link_Buffer (1 .. Status)) then
2126 Path_Buffer (Status + 1 .. End_Path - (Finish - Status)) :=
2127 Path_Buffer (Finish + 1 .. End_Path);
2128 End_Path := End_Path - (Finish - Status);
2129 Path_Buffer (1 .. Status) := Link_Buffer (1 .. Status);
2134 (Last + Status + 1 .. End_Path - Finish + Last + Status) :=
2135 Path_Buffer (Finish + 1 .. End_Path);
2136 End_Path := End_Path - Finish + Last + Status;
2137 Path_Buffer (Last + 1 .. Last + Status) :=
2138 Link_Buffer (1 .. Status);
2147 -- Too many iterations: give up
2149 -- This can happen when there is a circularity in the symbolic links: A
2150 -- is a symbolic link for B, which itself is a symbolic link, and the
2151 -- target of B or of another symbolic link target of B is A. In this
2152 -- case, we return an empty string to indicate failure to resolve.
2155 end Normalize_Pathname;
2162 (Name : C_File_Name;
2163 Fmode : Mode) return File_Descriptor
2165 function C_Open_Read
2166 (Name : C_File_Name;
2167 Fmode : Mode) return File_Descriptor;
2168 pragma Import (C, C_Open_Read, "__gnat_open_read");
2170 return C_Open_Read (Name, Fmode);
2175 Fmode : Mode) return File_Descriptor
2177 C_Name : String (1 .. Name'Length + 1);
2179 C_Name (1 .. Name'Length) := Name;
2180 C_Name (C_Name'Last) := ASCII.NUL;
2181 return Open_Read (C_Name (C_Name'First)'Address, Fmode);
2184 ---------------------
2185 -- Open_Read_Write --
2186 ---------------------
2188 function Open_Read_Write
2189 (Name : C_File_Name;
2190 Fmode : Mode) return File_Descriptor
2192 function C_Open_Read_Write
2193 (Name : C_File_Name;
2194 Fmode : Mode) return File_Descriptor;
2195 pragma Import (C, C_Open_Read_Write, "__gnat_open_rw");
2197 return C_Open_Read_Write (Name, Fmode);
2198 end Open_Read_Write;
2200 function Open_Read_Write
2202 Fmode : Mode) return File_Descriptor
2204 C_Name : String (1 .. Name'Length + 1);
2206 C_Name (1 .. Name'Length) := Name;
2207 C_Name (C_Name'Last) := ASCII.NUL;
2208 return Open_Read_Write (C_Name (C_Name'First)'Address, Fmode);
2209 end Open_Read_Write;
2215 procedure OS_Exit (Status : Integer) is
2217 OS_Exit_Ptr (Status);
2218 raise Program_Error;
2221 ---------------------
2222 -- OS_Exit_Default --
2223 ---------------------
2225 procedure OS_Exit_Default (Status : Integer) is
2226 procedure GNAT_OS_Exit (Status : Integer);
2227 pragma Import (C, GNAT_OS_Exit, "__gnat_os_exit");
2228 pragma No_Return (GNAT_OS_Exit);
2230 GNAT_OS_Exit (Status);
2231 end OS_Exit_Default;
2233 --------------------
2234 -- Pid_To_Integer --
2235 --------------------
2237 function Pid_To_Integer (Pid : Process_Id) return Integer is
2239 return Integer (Pid);
2247 (FD : File_Descriptor;
2249 N : Integer) return Integer
2252 return Integer (System.CRTL.read
2253 (System.CRTL.int (FD), System.CRTL.chars (A), System.CRTL.int (N)));
2260 procedure Rename_File
2261 (Old_Name : C_File_Name;
2262 New_Name : C_File_Name;
2263 Success : out Boolean)
2265 function rename (From, To : Address) return Integer;
2266 pragma Import (C, rename, "__gnat_rename");
2269 R := rename (Old_Name, New_Name);
2273 procedure Rename_File
2276 Success : out Boolean)
2278 C_Old_Name : String (1 .. Old_Name'Length + 1);
2279 C_New_Name : String (1 .. New_Name'Length + 1);
2281 C_Old_Name (1 .. Old_Name'Length) := Old_Name;
2282 C_Old_Name (C_Old_Name'Last) := ASCII.NUL;
2283 C_New_Name (1 .. New_Name'Length) := New_Name;
2284 C_New_Name (C_New_Name'Last) := ASCII.NUL;
2285 Rename_File (C_Old_Name'Address, C_New_Name'Address, Success);
2288 -----------------------
2289 -- Set_Close_On_Exec --
2290 -----------------------
2292 procedure Set_Close_On_Exec
2293 (FD : File_Descriptor;
2294 Close_On_Exec : Boolean;
2295 Status : out Boolean)
2297 function C_Set_Close_On_Exec
2298 (FD : File_Descriptor; Close_On_Exec : System.CRTL.int)
2299 return System.CRTL.int;
2300 pragma Import (C, C_Set_Close_On_Exec, "__gnat_set_close_on_exec");
2302 Status := C_Set_Close_On_Exec (FD, Boolean'Pos (Close_On_Exec)) = 0;
2303 end Set_Close_On_Exec;
2305 --------------------
2306 -- Set_Executable --
2307 --------------------
2309 procedure Set_Executable (Name : String) is
2310 procedure C_Set_Executable (Name : C_File_Name);
2311 pragma Import (C, C_Set_Executable, "__gnat_set_executable");
2312 C_Name : aliased String (Name'First .. Name'Last + 1);
2314 C_Name (Name'Range) := Name;
2315 C_Name (C_Name'Last) := ASCII.NUL;
2316 C_Set_Executable (C_Name (C_Name'First)'Address);
2319 ----------------------
2320 -- Set_Non_Readable --
2321 ----------------------
2323 procedure Set_Non_Readable (Name : String) is
2324 procedure C_Set_Non_Readable (Name : C_File_Name);
2325 pragma Import (C, C_Set_Non_Readable, "__gnat_set_non_readable");
2326 C_Name : aliased String (Name'First .. Name'Last + 1);
2328 C_Name (Name'Range) := Name;
2329 C_Name (C_Name'Last) := ASCII.NUL;
2330 C_Set_Non_Readable (C_Name (C_Name'First)'Address);
2331 end Set_Non_Readable;
2333 ----------------------
2334 -- Set_Non_Writable --
2335 ----------------------
2337 procedure Set_Non_Writable (Name : String) is
2338 procedure C_Set_Non_Writable (Name : C_File_Name);
2339 pragma Import (C, C_Set_Non_Writable, "__gnat_set_non_writable");
2340 C_Name : aliased String (Name'First .. Name'Last + 1);
2342 C_Name (Name'Range) := Name;
2343 C_Name (C_Name'Last) := ASCII.NUL;
2344 C_Set_Non_Writable (C_Name (C_Name'First)'Address);
2345 end Set_Non_Writable;
2351 procedure Set_Readable (Name : String) is
2352 procedure C_Set_Readable (Name : C_File_Name);
2353 pragma Import (C, C_Set_Readable, "__gnat_set_readable");
2354 C_Name : aliased String (Name'First .. Name'Last + 1);
2356 C_Name (Name'Range) := Name;
2357 C_Name (C_Name'Last) := ASCII.NUL;
2358 C_Set_Readable (C_Name (C_Name'First)'Address);
2361 --------------------
2363 --------------------
2365 procedure Set_Writable (Name : String) is
2366 procedure C_Set_Writable (Name : C_File_Name);
2367 pragma Import (C, C_Set_Writable, "__gnat_set_writable");
2368 C_Name : aliased String (Name'First .. Name'Last + 1);
2370 C_Name (Name'Range) := Name;
2371 C_Name (C_Name'Last) := ASCII.NUL;
2372 C_Set_Writable (C_Name (C_Name'First)'Address);
2379 procedure Setenv (Name : String; Value : String) is
2380 F_Name : String (1 .. Name'Length + 1);
2381 F_Value : String (1 .. Value'Length + 1);
2383 procedure Set_Env_Value (Name, Value : System.Address);
2384 pragma Import (C, Set_Env_Value, "__gnat_setenv");
2387 F_Name (1 .. Name'Length) := Name;
2388 F_Name (F_Name'Last) := ASCII.NUL;
2390 F_Value (1 .. Value'Length) := Value;
2391 F_Value (F_Value'Last) := ASCII.NUL;
2393 Set_Env_Value (F_Name'Address, F_Value'Address);
2401 (Program_Name : String;
2402 Args : Argument_List) return Integer
2406 pragma Warnings (Off, Junk);
2408 Spawn_Internal (Program_Name, Args, Result, Junk, Blocking => True);
2413 (Program_Name : String;
2414 Args : Argument_List;
2415 Success : out Boolean)
2418 Success := (Spawn (Program_Name, Args) = 0);
2422 (Program_Name : String;
2423 Args : Argument_List;
2424 Output_File_Descriptor : File_Descriptor;
2425 Return_Code : out Integer;
2426 Err_To_Out : Boolean := True)
2428 Saved_Output : File_Descriptor;
2429 Saved_Error : File_Descriptor := Invalid_FD; -- prevent compiler warning
2432 -- Set standard output and error to the temporary file
2434 Saved_Output := Dup (Standout);
2435 Dup2 (Output_File_Descriptor, Standout);
2438 Saved_Error := Dup (Standerr);
2439 Dup2 (Output_File_Descriptor, Standerr);
2442 -- Spawn the program
2444 Return_Code := Spawn (Program_Name, Args);
2446 -- Restore the standard output and error
2448 Dup2 (Saved_Output, Standout);
2451 Dup2 (Saved_Error, Standerr);
2454 -- And close the saved standard output and error file descriptors
2456 Close (Saved_Output);
2459 Close (Saved_Error);
2464 (Program_Name : String;
2465 Args : Argument_List;
2466 Output_File : String;
2467 Success : out Boolean;
2468 Return_Code : out Integer;
2469 Err_To_Out : Boolean := True)
2471 FD : File_Descriptor;
2477 FD := Create_Output_Text_File (Output_File);
2479 if FD = Invalid_FD then
2484 Spawn (Program_Name, Args, FD, Return_Code, Err_To_Out);
2486 Close (FD, Success);
2489 --------------------
2490 -- Spawn_Internal --
2491 --------------------
2493 procedure Spawn_Internal
2494 (Program_Name : String;
2495 Args : Argument_List;
2496 Result : out Integer;
2497 Pid : out Process_Id;
2501 procedure Spawn (Args : Argument_List);
2502 -- Call Spawn with given argument list
2504 N_Args : Argument_List (Args'Range);
2505 -- Normalized arguments
2511 procedure Spawn (Args : Argument_List) is
2512 type Chars is array (Positive range <>) of aliased Character;
2513 type Char_Ptr is access constant Character;
2515 Command_Len : constant Positive := Program_Name'Length + 1
2516 + Args_Length (Args);
2517 Command_Last : Natural := 0;
2518 Command : aliased Chars (1 .. Command_Len);
2519 -- Command contains all characters of the Program_Name and Args, all
2520 -- terminated by ASCII.NUL characters.
2522 Arg_List_Len : constant Positive := Args'Length + 2;
2523 Arg_List_Last : Natural := 0;
2524 Arg_List : aliased array (1 .. Arg_List_Len) of Char_Ptr;
2525 -- List with pointers to NUL-terminated strings of the Program_Name
2526 -- and the Args and terminated with a null pointer. We rely on the
2527 -- default initialization for the last null pointer.
2529 procedure Add_To_Command (S : String);
2530 -- Add S and a NUL character to Command, updating Last
2532 function Portable_Spawn (Args : Address) return Integer;
2533 pragma Import (C, Portable_Spawn, "__gnat_portable_spawn");
2535 function Portable_No_Block_Spawn (Args : Address) return Process_Id;
2537 (C, Portable_No_Block_Spawn, "__gnat_portable_no_block_spawn");
2539 --------------------
2540 -- Add_To_Command --
2541 --------------------
2543 procedure Add_To_Command (S : String) is
2544 First : constant Natural := Command_Last + 1;
2547 Command_Last := Command_Last + S'Length;
2549 -- Move characters one at a time, because Command has aliased
2552 -- But not volatile, so why is this necessary ???
2554 for J in S'Range loop
2555 Command (First + J - S'First) := S (J);
2558 Command_Last := Command_Last + 1;
2559 Command (Command_Last) := ASCII.NUL;
2561 Arg_List_Last := Arg_List_Last + 1;
2562 Arg_List (Arg_List_Last) := Command (First)'Access;
2565 -- Start of processing for Spawn
2568 Add_To_Command (Program_Name);
2570 for J in Args'Range loop
2571 Add_To_Command (Args (J).all);
2576 Result := Portable_Spawn (Arg_List'Address);
2578 Pid := Portable_No_Block_Spawn (Arg_List'Address);
2579 Result := Boolean'Pos (Pid /= Invalid_Pid);
2583 -- Start of processing for Spawn_Internal
2586 -- Copy arguments into a local structure
2588 for K in N_Args'Range loop
2589 N_Args (K) := new String'(Args
(K
).all);
2592 -- Normalize those arguments
2594 Normalize_Arguments
(N_Args
);
2596 -- Call spawn using the normalized arguments
2600 -- Free arguments list
2602 for K
in N_Args
'Range loop
2607 ---------------------------
2608 -- To_Path_String_Access --
2609 ---------------------------
2611 function To_Path_String_Access
2612 (Path_Addr
: Address
;
2613 Path_Len
: Integer) return String_Access
2615 subtype Path_String
is String (1 .. Path_Len
);
2616 type Path_String_Access
is access Path_String
;
2618 function Address_To_Access
is new Ada
.Unchecked_Conversion
2619 (Source
=> Address
, Target
=> Path_String_Access
);
2621 Path_Access
: constant Path_String_Access
:=
2622 Address_To_Access
(Path_Addr
);
2624 Return_Val
: String_Access
;
2627 Return_Val
:= new String (1 .. Path_Len
);
2629 for J
in 1 .. Path_Len
loop
2630 Return_Val
(J
) := Path_Access
(J
);
2634 end To_Path_String_Access
;
2640 procedure Wait_Process
(Pid
: out Process_Id
; Success
: out Boolean) is
2643 function Portable_Wait
(S
: Address
) return Process_Id
;
2644 pragma Import
(C
, Portable_Wait
, "__gnat_portable_wait");
2647 Pid
:= Portable_Wait
(Status
'Address);
2648 Success
:= (Status
= 0);
2656 (FD
: File_Descriptor
;
2658 N
: Integer) return Integer
2661 return Integer (System
.CRTL
.write
2662 (System
.CRTL
.int
(FD
), System
.CRTL
.chars
(A
), System
.CRTL
.int
(N
)));