1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . O S _ L I B --
9 -- Copyright (C) 1995-2007, 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 ------------------------------------------------------------------------------
34 pragma Warnings
(Off
);
38 with System
.Case_Util
;
40 with System
.Soft_Links
;
41 with Ada
.Unchecked_Conversion
;
42 with Ada
.Unchecked_Deallocation
;
43 with System
; use System
;
45 package body System
.OS_Lib
is
47 -- Imported procedures Dup and Dup2 are used in procedures Spawn and
48 -- Non_Blocking_Spawn.
50 function Dup
(Fd
: File_Descriptor
) return File_Descriptor
;
51 pragma Import
(C
, Dup
, "__gnat_dup");
53 procedure Dup2
(Old_Fd
, New_Fd
: File_Descriptor
);
54 pragma Import
(C
, Dup2
, "__gnat_dup2");
56 On_Windows
: constant Boolean := Directory_Separator
= '\';
57 -- An indication that we are on Windows. Used in Normalize_Pathname, to
58 -- deal with drive letters in the beginning of absolute paths.
60 package SSL
renames System
.Soft_Links
;
62 -- The following are used by Create_Temp_File
64 First_Temp_File_Name
: constant String := "GNAT-TEMP-000000.TMP";
65 -- Used to initialize Current_Temp_File_Name and Temp_File_Name_Last_Digit
67 Current_Temp_File_Name
: String := First_Temp_File_Name
;
68 -- Name of the temp file last created
70 Temp_File_Name_Last_Digit
: constant Positive :=
71 First_Temp_File_Name
'Last - 4;
72 -- Position of the last digit in Current_Temp_File_Name
74 Max_Attempts
: constant := 100;
75 -- The maximum number of attempts to create a new temp file
77 -----------------------
78 -- Local Subprograms --
79 -----------------------
81 function Args_Length
(Args
: Argument_List
) return Natural;
82 -- Returns total number of characters needed to create a string
83 -- of all Args terminated by ASCII.NUL characters
85 function C_String_Length
(S
: Address
) return Integer;
86 -- Returns the length of a C string. Does check for null address
89 procedure Spawn_Internal
90 (Program_Name
: String;
95 -- Internal routine to implement the two Spawn (blocking/non blocking)
96 -- routines. If Blocking is set to True then the spawn is blocking
97 -- otherwise it is non blocking. In this latter case the Pid contains the
98 -- process id number. The first three parameters are as in Spawn. Note that
99 -- Spawn_Internal normalizes the argument list before calling the low level
100 -- system spawn routines (see Normalize_Arguments).
102 -- Note: Normalize_Arguments is designed to do nothing if it is called more
103 -- than once, so calling Normalize_Arguments before calling one of the
104 -- spawn routines is fine.
106 function To_Path_String_Access
107 (Path_Addr
: Address
;
108 Path_Len
: Integer) return String_Access
;
109 -- Converts a C String to an Ada String. We could do this making use of
110 -- Interfaces.C.Strings but we prefer not to import that entire package
116 function "<" (X
, Y
: OS_Time
) return Boolean is
118 return Long_Integer (X
) < Long_Integer (Y
);
125 function "<=" (X
, Y
: OS_Time
) return Boolean is
127 return Long_Integer (X
) <= Long_Integer (Y
);
134 function ">" (X
, Y
: OS_Time
) return Boolean is
136 return Long_Integer (X
) > Long_Integer (Y
);
143 function ">=" (X
, Y
: OS_Time
) return Boolean is
145 return Long_Integer (X
) >= Long_Integer (Y
);
152 function Args_Length
(Args
: Argument_List
) return Natural is
156 for J
in Args
'Range loop
157 Len
:= Len
+ Args
(J
)'Length + 1; -- One extra for ASCII.NUL
163 -----------------------------
164 -- Argument_String_To_List --
165 -----------------------------
167 function Argument_String_To_List
168 (Arg_String
: String) return Argument_List_Access
170 Max_Args
: constant Integer := Arg_String
'Length;
171 New_Argv
: Argument_List
(1 .. Max_Args
);
172 New_Argc
: Natural := 0;
176 Idx
:= Arg_String
'First;
179 exit when Idx
> Arg_String
'Last;
182 Quoted
: Boolean := False;
183 Backqd
: Boolean := False;
190 -- An unquoted space is the end of an argument
192 if not (Backqd
or Quoted
)
193 and then Arg_String
(Idx
) = ' '
197 -- Start of a quoted string
199 elsif not (Backqd
or Quoted
)
200 and then Arg_String
(Idx
) = '"'
204 -- End of a quoted string and end of an argument
206 elsif (Quoted
and not Backqd
)
207 and then Arg_String
(Idx
) = '"'
212 -- Following character is backquoted
214 elsif Arg_String
(Idx
) = '\' then
217 -- Turn off backquoting after advancing one character
225 exit when Idx
> Arg_String
'Last;
230 New_Argc
:= New_Argc
+ 1;
231 New_Argv
(New_Argc
) :=
232 new String'(Arg_String (Old_Idx .. Idx - 1));
234 -- Skip extraneous spaces
236 while Idx <= Arg_String'Last and then Arg_String (Idx) = ' ' loop
242 return new Argument_List'(New_Argv
(1 .. New_Argc
));
243 end Argument_String_To_List
;
245 ---------------------
246 -- C_String_Length --
247 ---------------------
249 function C_String_Length
(S
: Address
) return Integer is
250 function Strlen
(S
: Address
) return Integer;
251 pragma Import
(C
, Strlen
, "strlen");
253 if S
= Null_Address
then
264 procedure Close
(FD
: File_Descriptor
) is
265 procedure C_Close
(FD
: File_Descriptor
);
266 pragma Import
(C
, C_Close
, "close");
271 procedure Close
(FD
: File_Descriptor
; Status
: out Boolean) is
272 function C_Close
(FD
: File_Descriptor
) return Integer;
273 pragma Import
(C
, C_Close
, "close");
275 Status
:= (C_Close
(FD
) = 0);
285 Success
: out Boolean;
286 Mode
: Copy_Mode
:= Copy
;
287 Preserve
: Attribute
:= Time_Stamps
)
289 From
: File_Descriptor
;
290 To
: File_Descriptor
;
292 Copy_Error
: exception;
293 -- Internal exception raised to signal error in copy
295 function Build_Path
(Dir
: String; File
: String) return String;
296 -- Returns pathname Dir catenated with File adding the directory
297 -- separator only if needed.
299 procedure Copy
(From
, To
: File_Descriptor
);
300 -- Read data from From and place them into To. In both cases the
301 -- operations uses the current file position. Raises Constraint_Error
302 -- if a problem occurs during the copy.
304 procedure Copy_To
(To_Name
: String);
305 -- Does a straight copy from source to designated destination file
311 function Build_Path
(Dir
: String; File
: String) return String is
312 Res
: String (1 .. Dir
'Length + File
'Length + 1);
314 Base_File_Ptr
: Integer;
315 -- The base file name is File (Base_File_Ptr + 1 .. File'Last)
317 function Is_Dirsep
(C
: Character) return Boolean;
318 pragma Inline
(Is_Dirsep
);
319 -- Returns True if C is a directory separator. On Windows we
320 -- handle both styles of directory separator.
326 function Is_Dirsep
(C
: Character) return Boolean is
328 return C
= Directory_Separator
or else C
= '/';
331 -- Start of processing for Build_Path
334 -- Find base file name
336 Base_File_Ptr
:= File
'Last;
337 while Base_File_Ptr
>= File
'First loop
338 exit when Is_Dirsep
(File
(Base_File_Ptr
));
339 Base_File_Ptr
:= Base_File_Ptr
- 1;
343 Base_File
: String renames
344 File
(Base_File_Ptr
+ 1 .. File
'Last);
347 Res
(1 .. Dir
'Length) := Dir
;
349 if Is_Dirsep
(Dir
(Dir
'Last)) then
350 Res
(Dir
'Length + 1 .. Dir
'Length + Base_File
'Length) :=
352 return Res
(1 .. Dir
'Length + Base_File
'Length);
355 Res
(Dir
'Length + 1) := Directory_Separator
;
356 Res
(Dir
'Length + 2 .. Dir
'Length + 1 + Base_File
'Length) :=
358 return Res
(1 .. Dir
'Length + 1 + Base_File
'Length);
367 procedure Copy
(From
, To
: File_Descriptor
) is
368 Buf_Size
: constant := 200_000
;
369 type Buf
is array (1 .. Buf_Size
) of Character;
370 type Buf_Ptr
is access Buf
;
376 Status_From
: Boolean;
378 -- Statuses for the calls to Close
380 procedure Free
is new Ada
.Unchecked_Deallocation
(Buf
, Buf_Ptr
);
383 -- Check for invalid descriptors, making sure that we do not
384 -- accidentally leave an open file descriptor around.
386 if From
= Invalid_FD
then
387 if To
/= Invalid_FD
then
388 Close
(To
, Status_To
);
393 elsif To
= Invalid_FD
then
394 Close
(From
, Status_From
);
398 -- Allocate the buffer on the heap
403 R
:= Read
(From
, Buffer
(1)'Address, Buf_Size
);
405 -- For VMS, the buffer may not be full. So, we need to try again
406 -- until there is nothing to read.
410 W
:= Write
(To
, Buffer
(1)'Address, R
);
414 -- Problem writing data, could be a disk full. Close files
415 -- without worrying about status, since we are raising a
416 -- Copy_Error exception in any case.
418 Close
(From
, Status_From
);
419 Close
(To
, Status_To
);
427 Close
(From
, Status_From
);
428 Close
(To
, Status_To
);
432 if not (Status_From
and Status_To
) then
441 procedure Copy_To
(To_Name
: String) is
443 function Copy_Attributes
444 (From
, To
: System
.Address
;
445 Mode
: Integer) return Integer;
446 pragma Import
(C
, Copy_Attributes
, "__gnat_copy_attribs");
447 -- Mode = 0 - copy only time stamps.
448 -- Mode = 1 - copy time stamps and read/write/execute attributes
450 C_From
: String (1 .. Name
'Length + 1);
451 C_To
: String (1 .. To_Name
'Length + 1);
454 From
:= Open_Read
(Name
, Binary
);
455 To
:= Create_File
(To_Name
, Binary
);
460 C_From
(1 .. Name
'Length) := Name
;
461 C_From
(C_From
'Last) := ASCII
.Nul
;
463 C_To
(1 .. To_Name
'Length) := To_Name
;
464 C_To
(C_To
'Last) := ASCII
.Nul
;
469 if Copy_Attributes
(C_From
'Address, C_To
'Address, 0) = -1 then
474 if Copy_Attributes
(C_From
'Address, C_To
'Address, 1) = -1 then
484 -- Start of processing for Copy_File
489 -- The source file must exist
491 if not Is_Regular_File
(Name
) then
495 -- The source file exists
499 -- Copy case, target file must not exist
503 -- If the target file exists, we have an error
505 if Is_Regular_File
(Pathname
) then
508 -- Case of target is a directory
510 elsif Is_Directory
(Pathname
) then
512 Dest
: constant String := Build_Path
(Pathname
, Name
);
515 -- If target file exists, we have an error, else do copy
517 if Is_Regular_File
(Dest
) then
524 -- Case of normal copy to file (destination does not exist)
530 -- Overwrite case (destination file may or may not exist)
533 if Is_Directory
(Pathname
) then
534 Copy_To
(Build_Path
(Pathname
, Name
));
539 -- Append case (destination file may or may not exist)
543 -- Appending to existing file
545 if Is_Regular_File
(Pathname
) then
547 -- Append mode and destination file exists, append data at the
550 From
:= Open_Read
(Name
, Binary
);
551 To
:= Open_Read_Write
(Pathname
, Binary
);
552 Lseek
(To
, 0, Seek_End
);
556 -- Appending to directory, not allowed
558 elsif Is_Directory
(Pathname
) then
561 -- Appending when target file does not exist
568 -- All error cases are caught here
577 Pathname
: C_File_Name
;
578 Success
: out Boolean;
579 Mode
: Copy_Mode
:= Copy
;
580 Preserve
: Attribute
:= Time_Stamps
)
582 Ada_Name
: String_Access
:=
583 To_Path_String_Access
584 (Name
, C_String_Length
(Name
));
586 Ada_Pathname
: String_Access
:=
587 To_Path_String_Access
588 (Pathname
, C_String_Length
(Pathname
));
591 Copy_File
(Ada_Name
.all, Ada_Pathname
.all, Success
, Mode
, Preserve
);
596 ----------------------
597 -- Copy_Time_Stamps --
598 ----------------------
600 procedure Copy_Time_Stamps
(Source
, Dest
: String; Success
: out Boolean) is
602 function Copy_Attributes
603 (From
, To
: System
.Address
;
604 Mode
: Integer) return Integer;
605 pragma Import
(C
, Copy_Attributes
, "__gnat_copy_attribs");
606 -- Mode = 0 - copy only time stamps.
607 -- Mode = 1 - copy time stamps and read/write/execute attributes
610 if Is_Regular_File
(Source
) and then Is_Writable_File
(Dest
) then
612 C_Source
: String (1 .. Source
'Length + 1);
613 C_Dest
: String (1 .. Dest
'Length + 1);
615 C_Source
(1 .. Source
'Length) := Source
;
616 C_Source
(C_Source
'Last) := ASCII
.NUL
;
618 C_Dest
(1 .. Dest
'Length) := Dest
;
619 C_Dest
(C_Dest
'Last) := ASCII
.NUL
;
621 if Copy_Attributes
(C_Source
'Address, C_Dest
'Address, 0) = -1 then
631 end Copy_Time_Stamps
;
633 procedure Copy_Time_Stamps
634 (Source
, Dest
: C_File_Name
;
635 Success
: out Boolean)
637 Ada_Source
: String_Access
:=
638 To_Path_String_Access
639 (Source
, C_String_Length
(Source
));
641 Ada_Dest
: String_Access
:=
642 To_Path_String_Access
643 (Dest
, C_String_Length
(Dest
));
645 Copy_Time_Stamps
(Ada_Source
.all, Ada_Dest
.all, Success
);
648 end Copy_Time_Stamps
;
656 Fmode
: Mode
) return File_Descriptor
658 function C_Create_File
660 Fmode
: Mode
) return File_Descriptor
;
661 pragma Import
(C
, C_Create_File
, "__gnat_open_create");
664 return C_Create_File
(Name
, Fmode
);
669 Fmode
: Mode
) return File_Descriptor
671 C_Name
: String (1 .. Name
'Length + 1);
674 C_Name
(1 .. Name
'Length) := Name
;
675 C_Name
(C_Name
'Last) := ASCII
.NUL
;
676 return Create_File
(C_Name
(C_Name
'First)'Address, Fmode
);
679 ---------------------
680 -- Create_New_File --
681 ---------------------
683 function Create_New_File
685 Fmode
: Mode
) return File_Descriptor
687 function C_Create_New_File
689 Fmode
: Mode
) return File_Descriptor
;
690 pragma Import
(C
, C_Create_New_File
, "__gnat_open_new");
693 return C_Create_New_File
(Name
, Fmode
);
696 function Create_New_File
698 Fmode
: Mode
) return File_Descriptor
700 C_Name
: String (1 .. Name
'Length + 1);
703 C_Name
(1 .. Name
'Length) := Name
;
704 C_Name
(C_Name
'Last) := ASCII
.NUL
;
705 return Create_New_File
(C_Name
(C_Name
'First)'Address, Fmode
);
708 -----------------------------
709 -- Create_Output_Text_File --
710 -----------------------------
712 function Create_Output_Text_File
(Name
: String) return File_Descriptor
is
713 function C_Create_File
714 (Name
: C_File_Name
) return File_Descriptor
;
715 pragma Import
(C
, C_Create_File
, "__gnat_create_output_file");
717 C_Name
: String (1 .. Name
'Length + 1);
720 C_Name
(1 .. Name
'Length) := Name
;
721 C_Name
(C_Name
'Last) := ASCII
.NUL
;
722 return C_Create_File
(C_Name
(C_Name
'First)'Address);
723 end Create_Output_Text_File
;
725 ----------------------
726 -- Create_Temp_File --
727 ----------------------
729 procedure Create_Temp_File
730 (FD
: out File_Descriptor
;
731 Name
: out Temp_File_Name
)
733 function Open_New_Temp
734 (Name
: System
.Address
;
735 Fmode
: Mode
) return File_Descriptor
;
736 pragma Import
(C
, Open_New_Temp
, "__gnat_open_new_temp");
739 FD
:= Open_New_Temp
(Name
'Address, Binary
);
740 end Create_Temp_File
;
742 procedure Create_Temp_File
743 (FD
: out File_Descriptor
;
744 Name
: out String_Access
)
747 Attempts
: Natural := 0;
748 Current
: String (Current_Temp_File_Name
'Range);
751 -- Loop until a new temp file can be created
755 -- We need to protect global variable Current_Temp_File_Name
756 -- against concurrent access by different tasks.
760 -- Start at the last digit
762 Pos
:= Temp_File_Name_Last_Digit
;
766 -- Increment the digit by one
768 case Current_Temp_File_Name
(Pos
) is
770 Current_Temp_File_Name
(Pos
) :=
771 Character'Succ (Current_Temp_File_Name
(Pos
));
776 -- For 9, set the digit to 0 and go to the previous digit
778 Current_Temp_File_Name
(Pos
) := '0';
783 -- If it is not a digit, then there are no available
784 -- temp file names. Return Invalid_FD. There is almost
785 -- no that this code will be ever be executed, since
786 -- it would mean that there are one million temp files
787 -- in the same directory!
796 Current
:= Current_Temp_File_Name
;
798 -- We can now release the lock, because we are no longer
799 -- accessing Current_Temp_File_Name.
809 -- Attempt to create the file
811 FD
:= Create_New_File
(Current
, Binary
);
813 if FD
/= Invalid_FD
then
814 Name
:= new String'(Current);
818 if not Is_Regular_File (Current) then
820 -- If the file does not already exist and we are unable to create
821 -- it, we give up after Max_Attempts. Otherwise, we try again with
822 -- the next available file name.
824 Attempts := Attempts + 1;
826 if Attempts >= Max_Attempts then
833 end Create_Temp_File;
839 procedure Delete_File (Name : Address; Success : out Boolean) is
842 function unlink (A : Address) return Integer;
843 pragma Import (C, unlink, "unlink");
850 procedure Delete_File (Name : String; Success : out Boolean) is
851 C_Name : String (1 .. Name'Length + 1);
854 C_Name (1 .. Name'Length) := Name;
855 C_Name (C_Name'Last) := ASCII.NUL;
857 Delete_File (C_Name'Address, Success);
860 ---------------------
861 -- File_Time_Stamp --
862 ---------------------
864 function File_Time_Stamp (FD : File_Descriptor) return OS_Time is
865 function File_Time (FD : File_Descriptor) return OS_Time;
866 pragma Import (C, File_Time, "__gnat_file_time_fd");
868 return File_Time (FD);
871 function File_Time_Stamp (Name : C_File_Name) return OS_Time is
872 function File_Time (Name : Address) return OS_Time;
873 pragma Import (C, File_Time, "__gnat_file_time_name");
875 return File_Time (Name);
878 function File_Time_Stamp (Name : String) return OS_Time is
879 F_Name : String (1 .. Name'Length + 1);
881 F_Name (1 .. Name'Length) := Name;
882 F_Name (F_Name'Last) := ASCII.NUL;
883 return File_Time_Stamp (F_Name'Address);
886 ---------------------------
887 -- Get_Debuggable_Suffix --
888 ---------------------------
890 function Get_Debuggable_Suffix return String_Access is
891 procedure Get_Suffix_Ptr (Length, Ptr : Address);
892 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_debuggable_suffix_ptr");
894 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
895 pragma Import (C, Strncpy, "strncpy");
897 Suffix_Ptr : Address;
898 Suffix_Length : Integer;
899 Result : String_Access;
902 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
904 Result := new String (1 .. Suffix_Length);
906 if Suffix_Length > 0 then
907 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
911 end Get_Debuggable_Suffix;
913 ---------------------------
914 -- Get_Executable_Suffix --
915 ---------------------------
917 function Get_Executable_Suffix return String_Access is
918 procedure Get_Suffix_Ptr (Length, Ptr : Address);
919 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_executable_suffix_ptr");
921 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
922 pragma Import (C, Strncpy, "strncpy");
924 Suffix_Ptr : Address;
925 Suffix_Length : Integer;
926 Result : String_Access;
929 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
931 Result := new String (1 .. Suffix_Length);
933 if Suffix_Length > 0 then
934 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
938 end Get_Executable_Suffix;
940 -----------------------
941 -- Get_Object_Suffix --
942 -----------------------
944 function Get_Object_Suffix return String_Access is
945 procedure Get_Suffix_Ptr (Length, Ptr : Address);
946 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_object_suffix_ptr");
948 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
949 pragma Import (C, Strncpy, "strncpy");
951 Suffix_Ptr : Address;
952 Suffix_Length : Integer;
953 Result : String_Access;
956 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
958 Result := new String (1 .. Suffix_Length);
960 if Suffix_Length > 0 then
961 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
965 end Get_Object_Suffix;
967 ----------------------------------
968 -- Get_Target_Debuggable_Suffix --
969 ----------------------------------
971 function Get_Target_Debuggable_Suffix return String_Access is
972 Target_Exec_Ext_Ptr : Address;
974 (C, Target_Exec_Ext_Ptr, "__gnat_target_debuggable_extension");
976 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
977 pragma Import (C, Strncpy, "strncpy");
979 function Strlen (Cstring : Address) return Integer;
980 pragma Import (C, Strlen, "strlen");
982 Suffix_Length : Integer;
983 Result : String_Access;
986 Suffix_Length := Strlen (Target_Exec_Ext_Ptr);
988 Result := new String (1 .. Suffix_Length);
990 if Suffix_Length > 0 then
991 Strncpy (Result.all'Address, Target_Exec_Ext_Ptr, Suffix_Length);
995 end Get_Target_Debuggable_Suffix;
997 ----------------------------------
998 -- Get_Target_Executable_Suffix --
999 ----------------------------------
1001 function Get_Target_Executable_Suffix return String_Access is
1002 Target_Exec_Ext_Ptr : Address;
1004 (C, Target_Exec_Ext_Ptr, "__gnat_target_executable_extension");
1006 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1007 pragma Import (C, Strncpy, "strncpy");
1009 function Strlen (Cstring : Address) return Integer;
1010 pragma Import (C, Strlen, "strlen");
1012 Suffix_Length : Integer;
1013 Result : String_Access;
1016 Suffix_Length := Strlen (Target_Exec_Ext_Ptr);
1018 Result := new String (1 .. Suffix_Length);
1020 if Suffix_Length > 0 then
1021 Strncpy (Result.all'Address, Target_Exec_Ext_Ptr, Suffix_Length);
1025 end Get_Target_Executable_Suffix;
1027 ------------------------------
1028 -- Get_Target_Object_Suffix --
1029 ------------------------------
1031 function Get_Target_Object_Suffix return String_Access is
1032 Target_Object_Ext_Ptr : Address;
1034 (C, Target_Object_Ext_Ptr, "__gnat_target_object_extension");
1036 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1037 pragma Import (C, Strncpy, "strncpy");
1039 function Strlen (Cstring : Address) return Integer;
1040 pragma Import (C, Strlen, "strlen");
1042 Suffix_Length : Integer;
1043 Result : String_Access;
1046 Suffix_Length := Strlen (Target_Object_Ext_Ptr);
1048 Result := new String (1 .. Suffix_Length);
1050 if Suffix_Length > 0 then
1051 Strncpy (Result.all'Address, Target_Object_Ext_Ptr, Suffix_Length);
1055 end Get_Target_Object_Suffix;
1061 function Getenv (Name : String) return String_Access is
1062 procedure Get_Env_Value_Ptr (Name, Length, Ptr : Address);
1063 pragma Import (C, Get_Env_Value_Ptr, "__gnat_getenv");
1065 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1066 pragma Import (C, Strncpy, "strncpy");
1068 Env_Value_Ptr : aliased Address;
1069 Env_Value_Length : aliased Integer;
1070 F_Name : aliased String (1 .. Name'Length + 1);
1071 Result : String_Access;
1074 F_Name (1 .. Name'Length) := Name;
1075 F_Name (F_Name'Last) := ASCII.NUL;
1078 (F_Name'Address, Env_Value_Length'Address, Env_Value_Ptr'Address);
1080 Result := new String (1 .. Env_Value_Length);
1082 if Env_Value_Length > 0 then
1083 Strncpy (Result.all'Address, Env_Value_Ptr, Env_Value_Length);
1093 function GM_Day (Date : OS_Time) return Day_Type is
1096 pragma Warnings (Off);
1102 pragma Warnings (On);
1105 GM_Split (Date, Y, Mo, D, H, Mn, S);
1113 function GM_Hour (Date : OS_Time) return Hour_Type is
1116 pragma Warnings (Off);
1122 pragma Warnings (On);
1125 GM_Split (Date, Y, Mo, D, H, Mn, S);
1133 function GM_Minute (Date : OS_Time) return Minute_Type is
1136 pragma Warnings (Off);
1142 pragma Warnings (On);
1145 GM_Split (Date, Y, Mo, D, H, Mn, S);
1153 function GM_Month (Date : OS_Time) return Month_Type is
1156 pragma Warnings (Off);
1162 pragma Warnings (On);
1165 GM_Split (Date, Y, Mo, D, H, Mn, S);
1173 function GM_Second (Date : OS_Time) return Second_Type is
1176 pragma Warnings (Off);
1182 pragma Warnings (On);
1185 GM_Split (Date, Y, Mo, D, H, Mn, S);
1195 Year : out Year_Type;
1196 Month : out Month_Type;
1198 Hour : out Hour_Type;
1199 Minute : out Minute_Type;
1200 Second : out Second_Type)
1202 procedure To_GM_Time
1203 (P_Time_T, P_Year, P_Month, P_Day, P_Hours, P_Mins, P_Secs : Address);
1204 pragma Import (C, To_GM_Time, "__gnat_to_gm_time");
1206 T : OS_Time := Date;
1215 -- Use the global lock because To_GM_Time is not thread safe
1217 Locked_Processing : begin
1220 (T'Address, Y'Address, Mo'Address, D'Address,
1221 H'Address, Mn'Address, S'Address);
1222 SSL.Unlock_Task.all;
1226 SSL.Unlock_Task.all;
1228 end Locked_Processing;
1242 function GM_Year (Date : OS_Time) return Year_Type is
1245 pragma Warnings (Off);
1251 pragma Warnings (On);
1254 GM_Split (Date, Y, Mo, D, H, Mn, S);
1258 ----------------------
1259 -- Is_Absolute_Path --
1260 ----------------------
1262 function Is_Absolute_Path (Name : String) return Boolean is
1263 function Is_Absolute_Path
1265 Length : Integer) return Integer;
1266 pragma Import (C, Is_Absolute_Path, "__gnat_is_absolute_path");
1268 return Is_Absolute_Path (Name'Address, Name'Length) /= 0;
1269 end Is_Absolute_Path;
1275 function Is_Directory (Name : C_File_Name) return Boolean is
1276 function Is_Directory (Name : Address) return Integer;
1277 pragma Import (C, Is_Directory, "__gnat_is_directory");
1279 return Is_Directory (Name) /= 0;
1282 function Is_Directory (Name : String) return Boolean is
1283 F_Name : String (1 .. Name'Length + 1);
1285 F_Name (1 .. Name'Length) := Name;
1286 F_Name (F_Name'Last) := ASCII.NUL;
1287 return Is_Directory (F_Name'Address);
1290 ----------------------
1291 -- Is_Readable_File --
1292 ----------------------
1294 function Is_Readable_File (Name : C_File_Name) return Boolean is
1295 function Is_Readable_File (Name : Address) return Integer;
1296 pragma Import (C, Is_Readable_File, "__gnat_is_readable_file");
1298 return Is_Readable_File (Name) /= 0;
1299 end Is_Readable_File;
1301 function Is_Readable_File (Name : String) return Boolean is
1302 F_Name : String (1 .. Name'Length + 1);
1304 F_Name (1 .. Name'Length) := Name;
1305 F_Name (F_Name'Last) := ASCII.NUL;
1306 return Is_Readable_File (F_Name'Address);
1307 end Is_Readable_File;
1309 ---------------------
1310 -- Is_Regular_File --
1311 ---------------------
1313 function Is_Regular_File (Name : C_File_Name) return Boolean is
1314 function Is_Regular_File (Name : Address) return Integer;
1315 pragma Import (C, Is_Regular_File, "__gnat_is_regular_file");
1317 return Is_Regular_File (Name) /= 0;
1318 end Is_Regular_File;
1320 function Is_Regular_File (Name : String) return Boolean is
1321 F_Name : String (1 .. Name'Length + 1);
1323 F_Name (1 .. Name'Length) := Name;
1324 F_Name (F_Name'Last) := ASCII.NUL;
1325 return Is_Regular_File (F_Name'Address);
1326 end Is_Regular_File;
1328 ----------------------
1329 -- Is_Symbolic_Link --
1330 ----------------------
1332 function Is_Symbolic_Link (Name : C_File_Name) return Boolean is
1333 function Is_Symbolic_Link (Name : Address) return Integer;
1334 pragma Import (C, Is_Symbolic_Link, "__gnat_is_symbolic_link");
1336 return Is_Symbolic_Link (Name) /= 0;
1337 end Is_Symbolic_Link;
1339 function Is_Symbolic_Link (Name : String) return Boolean is
1340 F_Name : String (1 .. Name'Length + 1);
1342 F_Name (1 .. Name'Length) := Name;
1343 F_Name (F_Name'Last) := ASCII.NUL;
1344 return Is_Symbolic_Link (F_Name'Address);
1345 end Is_Symbolic_Link;
1347 ----------------------
1348 -- Is_Writable_File --
1349 ----------------------
1351 function Is_Writable_File (Name : C_File_Name) return Boolean is
1352 function Is_Writable_File (Name : Address) return Integer;
1353 pragma Import (C, Is_Writable_File, "__gnat_is_writable_file");
1355 return Is_Writable_File (Name) /= 0;
1356 end Is_Writable_File;
1358 function Is_Writable_File (Name : String) return Boolean is
1359 F_Name : String (1 .. Name'Length + 1);
1361 F_Name (1 .. Name'Length) := Name;
1362 F_Name (F_Name'Last) := ASCII.NUL;
1363 return Is_Writable_File (F_Name'Address);
1364 end Is_Writable_File;
1366 -------------------------
1367 -- Locate_Exec_On_Path --
1368 -------------------------
1370 function Locate_Exec_On_Path
1371 (Exec_Name : String) return String_Access
1373 function Locate_Exec_On_Path (C_Exec_Name : Address) return Address;
1374 pragma Import (C, Locate_Exec_On_Path, "__gnat_locate_exec_on_path");
1376 procedure Free (Ptr : System.Address);
1377 pragma Import (C, Free, "free");
1379 C_Exec_Name : String (1 .. Exec_Name'Length + 1);
1380 Path_Addr : Address;
1382 Result : String_Access;
1385 C_Exec_Name (1 .. Exec_Name'Length) := Exec_Name;
1386 C_Exec_Name (C_Exec_Name'Last) := ASCII.NUL;
1388 Path_Addr := Locate_Exec_On_Path (C_Exec_Name'Address);
1389 Path_Len := C_String_Length (Path_Addr);
1391 if Path_Len = 0 then
1395 Result := To_Path_String_Access (Path_Addr, Path_Len);
1398 -- Always return an absolute path name
1400 if not Is_Absolute_Path (Result.all) then
1402 Absolute_Path : constant String :=
1403 Normalize_Pathname (Result.all);
1406 Result := new String'(Absolute_Path
);
1412 end Locate_Exec_On_Path
;
1414 -------------------------
1415 -- Locate_Regular_File --
1416 -------------------------
1418 function Locate_Regular_File
1419 (File_Name
: C_File_Name
;
1420 Path
: C_File_Name
) return String_Access
1422 function Locate_Regular_File
1423 (C_File_Name
, Path_Val
: Address
) return Address
;
1424 pragma Import
(C
, Locate_Regular_File
, "__gnat_locate_regular_file");
1426 procedure Free
(Ptr
: System
.Address
);
1427 pragma Import
(C
, Free
, "free");
1429 Path_Addr
: Address
;
1431 Result
: String_Access
;
1434 Path_Addr
:= Locate_Regular_File
(File_Name
, Path
);
1435 Path_Len
:= C_String_Length
(Path_Addr
);
1437 if Path_Len
= 0 then
1440 Result
:= To_Path_String_Access
(Path_Addr
, Path_Len
);
1444 end Locate_Regular_File
;
1446 function Locate_Regular_File
1447 (File_Name
: String;
1448 Path
: String) return String_Access
1450 C_File_Name
: String (1 .. File_Name
'Length + 1);
1451 C_Path
: String (1 .. Path
'Length + 1);
1452 Result
: String_Access
;
1455 C_File_Name
(1 .. File_Name
'Length) := File_Name
;
1456 C_File_Name
(C_File_Name
'Last) := ASCII
.NUL
;
1458 C_Path
(1 .. Path
'Length) := Path
;
1459 C_Path
(C_Path
'Last) := ASCII
.NUL
;
1461 Result
:= Locate_Regular_File
(C_File_Name
'Address, C_Path
'Address);
1463 -- Always return an absolute path name
1465 if Result
/= null and then not Is_Absolute_Path
(Result
.all) then
1467 Absolute_Path
: constant String := Normalize_Pathname
(Result
.all);
1470 Result
:= new String'(Absolute_Path);
1475 end Locate_Regular_File;
1477 ------------------------
1478 -- Non_Blocking_Spawn --
1479 ------------------------
1481 function Non_Blocking_Spawn
1482 (Program_Name : String;
1483 Args : Argument_List) return Process_Id
1487 pragma Warnings (Off, Junk);
1489 Spawn_Internal (Program_Name, Args, Junk, Pid, Blocking => False);
1491 end Non_Blocking_Spawn;
1493 function Non_Blocking_Spawn
1494 (Program_Name : String;
1495 Args : Argument_List;
1496 Output_File_Descriptor : File_Descriptor;
1497 Err_To_Out : Boolean := True) return Process_Id
1499 Saved_Output : File_Descriptor;
1500 Saved_Error : File_Descriptor := Invalid_FD; -- prevent warning
1504 if Output_File_Descriptor = Invalid_FD then
1508 -- Set standard output and, if specified, error to the temporary file
1510 Saved_Output := Dup (Standout);
1511 Dup2 (Output_File_Descriptor, Standout);
1514 Saved_Error := Dup (Standerr);
1515 Dup2 (Output_File_Descriptor, Standerr);
1518 -- Spawn the program
1520 Pid := Non_Blocking_Spawn (Program_Name, Args);
1522 -- Restore the standard output and error
1524 Dup2 (Saved_Output, Standout);
1527 Dup2 (Saved_Error, Standerr);
1530 -- And close the saved standard output and error file descriptors
1532 Close (Saved_Output);
1535 Close (Saved_Error);
1539 end Non_Blocking_Spawn;
1541 function Non_Blocking_Spawn
1542 (Program_Name : String;
1543 Args : Argument_List;
1544 Output_File : String;
1545 Err_To_Out : Boolean := True) return Process_Id
1547 Output_File_Descriptor : constant File_Descriptor :=
1548 Create_Output_Text_File (Output_File);
1549 Result : Process_Id;
1552 -- Do not attempt to spawn if the output file could not be created
1554 if Output_File_Descriptor = Invalid_FD then
1558 Result := Non_Blocking_Spawn
1559 (Program_Name, Args, Output_File_Descriptor, Err_To_Out);
1561 -- Close the file just created for the output, as the file descriptor
1562 -- cannot be used anywhere, being a local value. It is safe to do
1563 -- that, as the file descriptor has been duplicated to form
1564 -- standard output and error of the spawned process.
1566 Close (Output_File_Descriptor);
1570 end Non_Blocking_Spawn;
1572 -------------------------
1573 -- Normalize_Arguments --
1574 -------------------------
1576 procedure Normalize_Arguments (Args : in out Argument_List) is
1578 procedure Quote_Argument (Arg : in out String_Access);
1579 -- Add quote around argument if it contains spaces
1581 C_Argument_Needs_Quote : Integer;
1582 pragma Import (C, C_Argument_Needs_Quote, "__gnat_argument_needs_quote");
1583 Argument_Needs_Quote : constant Boolean := C_Argument_Needs_Quote /= 0;
1585 --------------------
1586 -- Quote_Argument --
1587 --------------------
1589 procedure Quote_Argument (Arg : in out String_Access) is
1590 Res : String (1 .. Arg'Length * 2);
1592 Quote_Needed : Boolean := False;
1595 if Arg (Arg'First) /= '"' or else Arg (Arg'Last) /= '"' then
1601 for K in Arg'Range loop
1605 if Arg (K) = '"' then
1609 Quote_Needed := True;
1611 elsif Arg (K) = ' ' then
1613 Quote_Needed := True;
1621 if Quote_Needed then
1623 -- If null terminated string, put the quote before
1625 if Res (J) = ASCII.Nul then
1628 Res (J) := ASCII.Nul;
1630 -- If argument is terminated by '\
', then double it. Otherwise
1631 -- the ending quote will be taken as-is. This is quite strange
1632 -- spawn behavior from Windows, but this is what we see!
1635 if Res (J) = '\
' then
1647 Old : String_Access := Arg;
1650 Arg := new String'(Res (1 .. J));
1658 -- Start of processing for Normalize_Arguments
1661 if Argument_Needs_Quote then
1662 for K in Args'Range loop
1663 if Args (K) /= null and then Args (K)'Length /= 0 then
1664 Quote_Argument (Args (K));
1668 end Normalize_Arguments;
1670 ------------------------
1671 -- Normalize_Pathname --
1672 ------------------------
1674 function Normalize_Pathname
1676 Directory : String := "";
1677 Resolve_Links : Boolean := True;
1678 Case_Sensitive : Boolean := True) return String
1681 pragma Import (C, Max_Path, "__gnat_max_path_len
");
1682 -- Maximum length of a path name
1684 procedure Get_Current_Dir
1685 (Dir : System.Address;
1686 Length : System.Address);
1687 pragma Import (C, Get_Current_Dir, "__gnat_get_current_dir
");
1689 Path_Buffer : String (1 .. Max_Path + Max_Path + 2);
1690 End_Path : Natural := 0;
1691 Link_Buffer : String (1 .. Max_Path + 2);
1697 Max_Iterations : constant := 500;
1699 function Get_File_Names_Case_Sensitive return Integer;
1701 (C, Get_File_Names_Case_Sensitive,
1702 "__gnat_get_file_names_case_sensitive
");
1704 Fold_To_Lower_Case : constant Boolean :=
1706 and then Get_File_Names_Case_Sensitive = 0;
1709 (Path : System.Address;
1710 Buf : System.Address;
1711 Bufsiz : Integer) return Integer;
1712 pragma Import (C, Readlink, "__gnat_readlink
");
1714 function To_Canonical_File_Spec
1715 (Host_File : System.Address) return System.Address;
1717 (C, To_Canonical_File_Spec, "__gnat_to_canonical_file_spec
");
1719 The_Name : String (1 .. Name'Length + 1);
1720 Canonical_File_Addr : System.Address;
1721 Canonical_File_Len : Integer;
1723 function Strlen (S : System.Address) return Integer;
1724 pragma Import (C, Strlen, "strlen
");
1726 function Final_Value (S : String) return String;
1727 -- Make final adjustment to the returned string. This function strips
1728 -- trailing directory separators, and folds returned string to lower
1729 -- case if required.
1731 function Get_Directory (Dir : String) return String;
1732 -- If Dir is not empty, return it, adding a directory separator
1733 -- if not already present, otherwise return current working directory
1734 -- with terminating directory separator.
1740 function Final_Value (S : String) return String is
1742 -- We may need to fold S to lower case, so we need a variable
1747 if Fold_To_Lower_Case then
1748 System.Case_Util.To_Lower (S1);
1751 -- Remove trailing directory separator, if any
1756 and then (S1 (Last) = '/'
1758 S1 (Last) = Directory_Separator)
1760 -- Special case for Windows: C:\
1763 and then S1 (1) /= Directory_Separator
1764 and then S1 (2) = ':'
1773 return S1 (1 .. Last);
1780 function Get_Directory (Dir : String) return String is
1782 -- Directory given, add directory separator if needed
1784 if Dir'Length > 0 then
1785 if Dir (Dir'Last) = Directory_Separator then
1789 Result : String (1 .. Dir'Length + 1);
1791 Result (1 .. Dir'Length) := Dir;
1792 Result (Result'Length) := Directory_Separator;
1797 -- Directory name not given, get current directory
1801 Buffer : String (1 .. Max_Path + 2);
1802 Path_Len : Natural := Max_Path;
1805 Get_Current_Dir (Buffer'Address, Path_Len'Address);
1807 if Buffer (Path_Len) /= Directory_Separator then
1808 Path_Len := Path_Len + 1;
1809 Buffer (Path_Len) := Directory_Separator;
1812 -- By default, the drive letter on Windows is in upper case
1814 if On_Windows and then Path_Len >= 2 and then
1817 System.Case_Util.To_Upper (Buffer (1 .. 1));
1820 return Buffer (1 .. Path_Len);
1825 -- Start of processing for Normalize_Pathname
1828 -- Special case, if name is null, then return null
1830 if Name'Length = 0 then
1834 -- First, convert VMS file spec to Unix file spec.
1835 -- If Name is not in VMS syntax, then this is equivalent
1836 -- to put Name at the begining of Path_Buffer.
1838 VMS_Conversion : begin
1839 The_Name (1 .. Name'Length) := Name;
1840 The_Name (The_Name'Last) := ASCII.NUL;
1842 Canonical_File_Addr := To_Canonical_File_Spec (The_Name'Address);
1843 Canonical_File_Len := Strlen (Canonical_File_Addr);
1845 -- If VMS syntax conversion has failed, return an empty string
1846 -- to indicate the failure.
1848 if Canonical_File_Len = 0 then
1853 subtype Path_String is String (1 .. Canonical_File_Len);
1854 type Path_String_Access is access Path_String;
1856 function Address_To_Access is new
1857 Ada.Unchecked_Conversion (Source => Address,
1858 Target => Path_String_Access);
1860 Path_Access : constant Path_String_Access :=
1861 Address_To_Access (Canonical_File_Addr);
1864 Path_Buffer (1 .. Canonical_File_Len) := Path_Access.all;
1865 End_Path := Canonical_File_Len;
1870 -- Replace all '/' by Directory Separators (this is for Windows)
1872 if Directory_Separator /= '/' then
1873 for Index in 1 .. End_Path loop
1874 if Path_Buffer (Index) = '/' then
1875 Path_Buffer (Index) := Directory_Separator;
1880 -- Resolve directory names for Windows (formerly also VMS)
1882 -- On VMS, if we have a Unix path such as /temp/..., and TEMP is a
1883 -- logical name, we must not try to resolve this logical name, because
1884 -- it may have multiple equivalences and if resolved we will only
1885 -- get the first one.
1887 -- On Windows, if we have an absolute path starting with a directory
1888 -- separator, we need to have the drive letter appended in front.
1890 -- On Windows, Get_Current_Dir will return a suitable directory
1891 -- name (path starting with a drive letter on Windows). So we take this
1892 -- drive letter and prepend it to the current path.
1895 and then Path_Buffer (1) = Directory_Separator
1896 and then Path_Buffer (2) /= Directory_Separator
1899 Cur_Dir : String := Get_Directory ("");
1900 -- Get the current directory to get the drive letter
1903 if Cur_Dir'Length > 2
1904 and then Cur_Dir (Cur_Dir'First + 1) = ':'
1906 Path_Buffer (3 .. End_Path + 2) := Path_Buffer (1 .. End_Path);
1907 Path_Buffer (1 .. 2) :=
1908 Cur_Dir (Cur_Dir'First .. Cur_Dir'First + 1);
1909 End_Path := End_Path + 2;
1914 -- Start the conversions
1916 -- If this is not finished after Max_Iterations, give up and return an
1919 for J in 1 .. Max_Iterations loop
1921 -- If we don't have an absolute pathname, prepend the directory
1925 and then not Is_Absolute_Path (Path_Buffer (1 .. End_Path))
1928 Reference_Dir : constant String := Get_Directory (Directory);
1929 Ref_Dir_Len : constant Natural := Reference_Dir'Length;
1930 -- Current directory name specified and its length
1933 Path_Buffer (Ref_Dir_Len + 1 .. Ref_Dir_Len + End_Path) :=
1934 Path_Buffer (1 .. End_Path);
1935 End_Path := Ref_Dir_Len + End_Path;
1936 Path_Buffer (1 .. Ref_Dir_Len) := Reference_Dir;
1937 Last := Ref_Dir_Len;
1944 -- Ensure that Windows network drives are kept, e.g: \\server\drive-c
1947 and then Directory_Separator = '\'
1948 and then Path_Buffer (1 .. 2) = "\\"
1953 -- If we have traversed the full pathname, return it
1955 if Start > End_Path then
1956 return Final_Value (Path_Buffer (1 .. End_Path));
1959 -- Remove duplicate directory separators
1961 while Path_Buffer (Start) = Directory_Separator loop
1962 if Start = End_Path then
1963 return Final_Value (Path_Buffer (1 .. End_Path - 1));
1966 Path_Buffer (Start .. End_Path - 1) :=
1967 Path_Buffer (Start + 1 .. End_Path);
1968 End_Path := End_Path - 1;
1972 -- Find the end of the current field: last character or the one
1973 -- preceding the next directory separator.
1975 while Finish < End_Path
1976 and then Path_Buffer (Finish + 1) /= Directory_Separator
1978 Finish := Finish + 1;
1983 if Start = Finish and then Path_Buffer (Start) = '.' then
1984 if Start = End_Path then
1986 return (1 => Directory_Separator);
1989 if Fold_To_Lower_Case then
1990 System.Case_Util.To_Lower (Path_Buffer (1 .. Last - 1));
1993 return Path_Buffer (1 .. Last - 1);
1998 Path_Buffer (Last + 1 .. End_Path - 2) :=
1999 Path_Buffer (Last + 3 .. End_Path);
2000 End_Path := End_Path - 2;
2003 -- Remove ".." fields
2005 elsif Finish = Start + 1
2006 and then Path_Buffer (Start .. Finish) = ".."
2011 exit when Start < 1 or else
2012 Path_Buffer (Start) = Directory_Separator;
2016 if Finish = End_Path then
2017 return (1 => Directory_Separator);
2020 Path_Buffer (1 .. End_Path - Finish) :=
2021 Path_Buffer (Finish + 1 .. End_Path);
2022 End_Path := End_Path - Finish;
2027 if Finish = End_Path then
2028 return Final_Value (Path_Buffer (1 .. Start - 1));
2031 Path_Buffer (Start + 1 .. Start + End_Path - Finish - 1) :=
2032 Path_Buffer (Finish + 2 .. End_Path);
2033 End_Path := Start + End_Path - Finish - 1;
2038 -- Check if current field is a symbolic link
2040 elsif Resolve_Links then
2042 Saved : constant Character := Path_Buffer (Finish + 1);
2045 Path_Buffer (Finish + 1) := ASCII.NUL;
2046 Status := Readlink (Path_Buffer'Address,
2047 Link_Buffer'Address,
2048 Link_Buffer'Length);
2049 Path_Buffer (Finish + 1) := Saved;
2052 -- Not a symbolic link, move to the next field, if any
2057 -- Replace symbolic link with its value
2060 if Is_Absolute_Path (Link_Buffer (1 .. Status)) then
2061 Path_Buffer (Status + 1 .. End_Path - (Finish - Status)) :=
2062 Path_Buffer (Finish + 1 .. End_Path);
2063 End_Path := End_Path - (Finish - Status);
2064 Path_Buffer (1 .. Status) := Link_Buffer (1 .. Status);
2069 (Last + Status + 1 .. End_Path - Finish + Last + Status) :=
2070 Path_Buffer (Finish + 1 .. End_Path);
2071 End_Path := End_Path - Finish + Last + Status;
2072 Path_Buffer (Last + 1 .. Last + Status) :=
2073 Link_Buffer (1 .. Status);
2082 -- Too many iterations: give up
2084 -- This can happen when there is a circularity in the symbolic links: A
2085 -- is a symbolic link for B, which itself is a symbolic link, and the
2086 -- target of B or of another symbolic link target of B is A. In this
2087 -- case, we return an empty string to indicate failure to resolve.
2090 end Normalize_Pathname;
2097 (Name : C_File_Name;
2098 Fmode : Mode) return File_Descriptor
2100 function C_Open_Read
2101 (Name : C_File_Name;
2102 Fmode : Mode) return File_Descriptor;
2103 pragma Import (C, C_Open_Read, "__gnat_open_read
");
2105 return C_Open_Read (Name, Fmode);
2110 Fmode : Mode) return File_Descriptor
2112 C_Name : String (1 .. Name'Length + 1);
2114 C_Name (1 .. Name'Length) := Name;
2115 C_Name (C_Name'Last) := ASCII.NUL;
2116 return Open_Read (C_Name (C_Name'First)'Address, Fmode);
2119 ---------------------
2120 -- Open_Read_Write --
2121 ---------------------
2123 function Open_Read_Write
2124 (Name : C_File_Name;
2125 Fmode : Mode) return File_Descriptor
2127 function C_Open_Read_Write
2128 (Name : C_File_Name;
2129 Fmode : Mode) return File_Descriptor;
2130 pragma Import (C, C_Open_Read_Write, "__gnat_open_rw
");
2132 return C_Open_Read_Write (Name, Fmode);
2133 end Open_Read_Write;
2135 function Open_Read_Write
2137 Fmode : Mode) return File_Descriptor
2139 C_Name : String (1 .. Name'Length + 1);
2141 C_Name (1 .. Name'Length) := Name;
2142 C_Name (C_Name'Last) := ASCII.NUL;
2143 return Open_Read_Write (C_Name (C_Name'First)'Address, Fmode);
2144 end Open_Read_Write;
2150 procedure OS_Exit (Status : Integer) is
2152 OS_Exit_Ptr (Status);
2153 raise Program_Error;
2156 ---------------------
2157 -- OS_Exit_Default --
2158 ---------------------
2160 procedure OS_Exit_Default (Status : Integer) is
2161 procedure GNAT_OS_Exit (Status : Integer);
2162 pragma Import (C, GNAT_OS_Exit, "__gnat_os_exit
");
2163 pragma No_Return (GNAT_OS_Exit);
2165 GNAT_OS_Exit (Status);
2166 end OS_Exit_Default;
2168 --------------------
2169 -- Pid_To_Integer --
2170 --------------------
2172 function Pid_To_Integer (Pid : Process_Id) return Integer is
2174 return Integer (Pid);
2182 (FD : File_Descriptor;
2184 N : Integer) return Integer
2187 return Integer (System.CRTL.read
2188 (System.CRTL.int (FD), System.CRTL.chars (A), System.CRTL.int (N)));
2195 procedure Rename_File
2196 (Old_Name : C_File_Name;
2197 New_Name : C_File_Name;
2198 Success : out Boolean)
2200 function rename (From, To : Address) return Integer;
2201 pragma Import (C, rename, "rename
");
2204 R := rename (Old_Name, New_Name);
2208 procedure Rename_File
2211 Success : out Boolean)
2213 C_Old_Name : String (1 .. Old_Name'Length + 1);
2214 C_New_Name : String (1 .. New_Name'Length + 1);
2216 C_Old_Name (1 .. Old_Name'Length) := Old_Name;
2217 C_Old_Name (C_Old_Name'Last) := ASCII.NUL;
2218 C_New_Name (1 .. New_Name'Length) := New_Name;
2219 C_New_Name (C_New_Name'Last) := ASCII.NUL;
2220 Rename_File (C_Old_Name'Address, C_New_Name'Address, Success);
2223 -----------------------
2224 -- Set_Close_On_Exec --
2225 -----------------------
2227 procedure Set_Close_On_Exec
2228 (FD : File_Descriptor;
2229 Close_On_Exec : Boolean;
2230 Status : out Boolean)
2232 function C_Set_Close_On_Exec
2233 (FD : File_Descriptor; Close_On_Exec : System.CRTL.int)
2234 return System.CRTL.int;
2235 pragma Import (C, C_Set_Close_On_Exec, "__gnat_set_close_on_exec
");
2237 Status := C_Set_Close_On_Exec (FD, Boolean'Pos (Close_On_Exec)) = 0;
2238 end Set_Close_On_Exec;
2240 --------------------
2241 -- Set_Executable --
2242 --------------------
2244 procedure Set_Executable (Name : String) is
2245 procedure C_Set_Executable (Name : C_File_Name);
2246 pragma Import (C, C_Set_Executable, "__gnat_set_executable
");
2247 C_Name : aliased String (Name'First .. Name'Last + 1);
2249 C_Name (Name'Range) := Name;
2250 C_Name (C_Name'Last) := ASCII.NUL;
2251 C_Set_Executable (C_Name (C_Name'First)'Address);
2254 --------------------
2256 --------------------
2258 procedure Set_Read_Only (Name : String) is
2259 procedure C_Set_Read_Only (Name : C_File_Name);
2260 pragma Import (C, C_Set_Read_Only, "__gnat_set_readonly
");
2261 C_Name : aliased String (Name'First .. Name'Last + 1);
2263 C_Name (Name'Range) := Name;
2264 C_Name (C_Name'Last) := ASCII.NUL;
2265 C_Set_Read_Only (C_Name (C_Name'First)'Address);
2268 --------------------
2270 --------------------
2272 procedure Set_Writable (Name : String) is
2273 procedure C_Set_Writable (Name : C_File_Name);
2274 pragma Import (C, C_Set_Writable, "__gnat_set_writable
");
2275 C_Name : aliased String (Name'First .. Name'Last + 1);
2277 C_Name (Name'Range) := Name;
2278 C_Name (C_Name'Last) := ASCII.NUL;
2279 C_Set_Writable (C_Name (C_Name'First)'Address);
2286 procedure Setenv (Name : String; Value : String) is
2287 F_Name : String (1 .. Name'Length + 1);
2288 F_Value : String (1 .. Value'Length + 1);
2290 procedure Set_Env_Value (Name, Value : System.Address);
2291 pragma Import (C, Set_Env_Value, "__gnat_setenv
");
2294 F_Name (1 .. Name'Length) := Name;
2295 F_Name (F_Name'Last) := ASCII.NUL;
2297 F_Value (1 .. Value'Length) := Value;
2298 F_Value (F_Value'Last) := ASCII.NUL;
2300 Set_Env_Value (F_Name'Address, F_Value'Address);
2308 (Program_Name : String;
2309 Args : Argument_List) return Integer
2313 pragma Warnings (Off, Junk);
2315 Spawn_Internal (Program_Name, Args, Result, Junk, Blocking => True);
2320 (Program_Name : String;
2321 Args : Argument_List;
2322 Success : out Boolean)
2325 Success := (Spawn (Program_Name, Args) = 0);
2329 (Program_Name : String;
2330 Args : Argument_List;
2331 Output_File_Descriptor : File_Descriptor;
2332 Return_Code : out Integer;
2333 Err_To_Out : Boolean := True)
2335 Saved_Output : File_Descriptor;
2336 Saved_Error : File_Descriptor := Invalid_FD; -- prevent compiler warning
2339 -- Set standard output and error to the temporary file
2341 Saved_Output := Dup (Standout);
2342 Dup2 (Output_File_Descriptor, Standout);
2345 Saved_Error := Dup (Standerr);
2346 Dup2 (Output_File_Descriptor, Standerr);
2349 -- Spawn the program
2351 Return_Code := Spawn (Program_Name, Args);
2353 -- Restore the standard output and error
2355 Dup2 (Saved_Output, Standout);
2358 Dup2 (Saved_Error, Standerr);
2361 -- And close the saved standard output and error file descriptors
2363 Close (Saved_Output);
2366 Close (Saved_Error);
2371 (Program_Name : String;
2372 Args : Argument_List;
2373 Output_File : String;
2374 Success : out Boolean;
2375 Return_Code : out Integer;
2376 Err_To_Out : Boolean := True)
2378 FD : File_Descriptor;
2384 FD := Create_Output_Text_File (Output_File);
2386 if FD = Invalid_FD then
2391 Spawn (Program_Name, Args, FD, Return_Code, Err_To_Out);
2393 Close (FD, Success);
2396 --------------------
2397 -- Spawn_Internal --
2398 --------------------
2400 procedure Spawn_Internal
2401 (Program_Name : String;
2402 Args : Argument_List;
2403 Result : out Integer;
2404 Pid : out Process_Id;
2408 procedure Spawn (Args : Argument_List);
2409 -- Call Spawn with given argument list
2411 N_Args : Argument_List (Args'Range);
2412 -- Normalized arguments
2418 procedure Spawn (Args : Argument_List) is
2419 type Chars is array (Positive range <>) of aliased Character;
2420 type Char_Ptr is access constant Character;
2422 Command_Len : constant Positive := Program_Name'Length + 1
2423 + Args_Length (Args);
2424 Command_Last : Natural := 0;
2425 Command : aliased Chars (1 .. Command_Len);
2426 -- Command contains all characters of the Program_Name and Args, all
2427 -- terminated by ASCII.NUL characters
2429 Arg_List_Len : constant Positive := Args'Length + 2;
2430 Arg_List_Last : Natural := 0;
2431 Arg_List : aliased array (1 .. Arg_List_Len) of Char_Ptr;
2432 -- List with pointers to NUL-terminated strings of the Program_Name
2433 -- and the Args and terminated with a null pointer. We rely on the
2434 -- default initialization for the last null pointer.
2436 procedure Add_To_Command (S : String);
2437 -- Add S and a NUL character to Command, updating Last
2439 function Portable_Spawn (Args : Address) return Integer;
2440 pragma Import (C, Portable_Spawn, "__gnat_portable_spawn
");
2442 function Portable_No_Block_Spawn (Args : Address) return Process_Id;
2444 (C, Portable_No_Block_Spawn, "__gnat_portable_no_block_spawn
");
2446 --------------------
2447 -- Add_To_Command --
2448 --------------------
2450 procedure Add_To_Command (S : String) is
2451 First : constant Natural := Command_Last + 1;
2454 Command_Last := Command_Last + S'Length;
2456 -- Move characters one at a time, because Command has aliased
2459 -- But not volatile, so why is this necessary ???
2461 for J in S'Range loop
2462 Command (First + J - S'First) := S (J);
2465 Command_Last := Command_Last + 1;
2466 Command (Command_Last) := ASCII.NUL;
2468 Arg_List_Last := Arg_List_Last + 1;
2469 Arg_List (Arg_List_Last) := Command (First)'Access;
2472 -- Start of processing for Spawn
2475 Add_To_Command (Program_Name);
2477 for J in Args'Range loop
2478 Add_To_Command (Args (J).all);
2483 Result := Portable_Spawn (Arg_List'Address);
2485 Pid := Portable_No_Block_Spawn (Arg_List'Address);
2486 Result := Boolean'Pos (Pid /= Invalid_Pid);
2490 -- Start of processing for Spawn_Internal
2493 -- Copy arguments into a local structure
2495 for K in N_Args'Range loop
2496 N_Args (K) := new String'(Args (K).all);
2499 -- Normalize those arguments
2501 Normalize_Arguments (N_Args);
2503 -- Call spawn using the normalized arguments
2507 -- Free arguments list
2509 for K in N_Args'Range loop
2514 ---------------------------
2515 -- To_Path_String_Access --
2516 ---------------------------
2518 function To_Path_String_Access
2519 (Path_Addr : Address;
2520 Path_Len : Integer) return String_Access
2522 subtype Path_String is String (1 .. Path_Len);
2523 type Path_String_Access is access Path_String;
2525 function Address_To_Access is new
2526 Ada.Unchecked_Conversion (Source => Address,
2527 Target => Path_String_Access);
2529 Path_Access : constant Path_String_Access :=
2530 Address_To_Access (Path_Addr);
2532 Return_Val : String_Access;
2535 Return_Val := new String (1 .. Path_Len);
2537 for J in 1 .. Path_Len loop
2538 Return_Val (J) := Path_Access (J);
2542 end To_Path_String_Access;
2548 procedure Wait_Process (Pid : out Process_Id; Success : out Boolean) is
2551 function Portable_Wait (S : Address) return Process_Id;
2552 pragma Import (C, Portable_Wait, "__gnat_portable_wait
");
2555 Pid := Portable_Wait (Status'Address);
2556 Success := (Status = 0);
2564 (FD : File_Descriptor;
2566 N : Integer) return Integer
2569 return Integer (System.CRTL.write
2570 (System.CRTL.int (FD), System.CRTL.chars (A), System.CRTL.int (N)));