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 procedure Create_Temp_File_Internal
84 (FD : out File_Descriptor;
85 Name : out String_Access;
87 -- Internal routine to implement two Create_Temp_File routines. If Stdout
88 -- is set to True the created descriptor is stdout-compatible, otherwise
89 -- it might not be depending on the OS (VMS is one example). The first two
90 -- parameters are as in Create_Temp_File.
92 function C_String_Length (S : Address) return Integer;
93 -- Returns the length of a C string. Does check for null address
96 procedure Spawn_Internal
97 (Program_Name : String;
100 Pid : out Process_Id;
102 -- Internal routine to implement the two Spawn (blocking/non blocking)
103 -- routines. If Blocking is set to True then the spawn is blocking
104 -- otherwise it is non blocking. In this latter case the Pid contains the
105 -- process id number. The first three parameters are as in Spawn. Note that
106 -- Spawn_Internal normalizes the argument list before calling the low level
107 -- system spawn routines (see Normalize_Arguments).
109 -- Note: Normalize_Arguments is designed to do nothing if it is called more
110 -- than once, so calling Normalize_Arguments before calling one of the
111 -- spawn routines is fine.
113 function To_Path_String_Access
114 (Path_Addr : Address;
115 Path_Len : Integer) return String_Access;
116 -- Converts a C String to an Ada String. We could do this making use of
117 -- Interfaces.C.Strings but we prefer not to import that entire package
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 ">=" (X, Y : OS_Time) return Boolean is
152 return Long_Integer (X) >= Long_Integer (Y);
159 function Args_Length (Args : Argument_List) return Natural is
163 for J in Args'Range loop
164 Len := Len + Args (J)'Length + 1; -- One extra for ASCII.NUL
170 -----------------------------
171 -- Argument_String_To_List --
172 -----------------------------
174 function Argument_String_To_List
175 (Arg_String : String) return Argument_List_Access
177 Max_Args : constant Integer := Arg_String'Length;
178 New_Argv : Argument_List (1 .. Max_Args);
179 New_Argc : Natural := 0;
183 Idx := Arg_String'First;
186 exit when Idx > Arg_String'Last;
189 Quoted : Boolean := False;
190 Backqd : Boolean := False;
197 -- An unquoted space is the end of an argument
199 if not (Backqd or Quoted)
200 and then Arg_String (Idx) = ' '
204 -- Start of a quoted string
206 elsif not (Backqd or Quoted)
207 and then Arg_String (Idx) = '"'
211 -- End of a quoted string and end of an argument
213 elsif (Quoted and not Backqd)
214 and then Arg_String (Idx) = '"'
219 -- Following character is backquoted
221 elsif Arg_String (Idx) = '\' then
224 -- Turn off backquoting after advancing one character
232 exit when Idx > Arg_String'Last;
237 New_Argc := New_Argc + 1;
238 New_Argv (New_Argc) :=
239 new String'(Arg_String (Old_Idx .. Idx - 1));
241 -- Skip extraneous spaces
243 while Idx <= Arg_String'Last and then Arg_String (Idx) = ' ' loop
249 return new Argument_List'(New_Argv (1 .. New_Argc));
250 end Argument_String_To_List;
252 ---------------------
253 -- C_String_Length --
254 ---------------------
256 function C_String_Length (S : Address) return Integer is
257 function Strlen (S : Address) return Integer;
258 pragma Import (C, Strlen, "strlen");
260 if S = Null_Address then
271 procedure Close (FD : File_Descriptor) is
272 procedure C_Close (FD : File_Descriptor);
273 pragma Import (C, C_Close, "close");
278 procedure Close (FD : File_Descriptor; Status : out Boolean) is
279 function C_Close (FD : File_Descriptor) return Integer;
280 pragma Import (C, C_Close, "close");
282 Status := (C_Close (FD) = 0);
292 Success : out Boolean;
293 Mode : Copy_Mode := Copy;
294 Preserve : Attribute := Time_Stamps)
296 From : File_Descriptor;
297 To : File_Descriptor;
299 Copy_Error : exception;
300 -- Internal exception raised to signal error in copy
302 function Build_Path (Dir : String; File : String) return String;
303 -- Returns pathname Dir concatenated with File adding the directory
304 -- separator only if needed.
306 procedure Copy (From, To : File_Descriptor);
307 -- Read data from From and place them into To. In both cases the
308 -- operations uses the current file position. Raises Constraint_Error
309 -- if a problem occurs during the copy.
311 procedure Copy_To (To_Name : String);
312 -- Does a straight copy from source to designated destination file
318 function Build_Path (Dir : String; File : String) return String is
319 Res : String (1 .. Dir'Length + File'Length + 1);
321 Base_File_Ptr : Integer;
322 -- The base file name is File (Base_File_Ptr + 1 .. File'Last)
324 function Is_Dirsep (C : Character) return Boolean;
325 pragma Inline (Is_Dirsep);
326 -- Returns True if C is a directory separator. On Windows we
327 -- handle both styles of directory separator.
333 function Is_Dirsep (C : Character) return Boolean is
335 return C = Directory_Separator or else C = '/';
338 -- Start of processing for Build_Path
341 -- Find base file name
343 Base_File_Ptr := File'Last;
344 while Base_File_Ptr >= File'First loop
345 exit when Is_Dirsep (File (Base_File_Ptr));
346 Base_File_Ptr := Base_File_Ptr - 1;
350 Base_File : String renames
351 File (Base_File_Ptr + 1 .. File'Last);
354 Res (1 .. Dir'Length) := Dir;
356 if Is_Dirsep (Dir (Dir'Last)) then
357 Res (Dir'Length + 1 .. Dir'Length + Base_File'Length) :=
359 return Res (1 .. Dir'Length + Base_File'Length);
362 Res (Dir'Length + 1) := Directory_Separator;
363 Res (Dir'Length + 2 .. Dir'Length + 1 + Base_File'Length) :=
365 return Res (1 .. Dir'Length + 1 + Base_File'Length);
374 procedure Copy (From, To : File_Descriptor) is
375 Buf_Size : constant := 200_000;
376 type Buf is array (1 .. Buf_Size) of Character;
377 type Buf_Ptr is access Buf;
383 Status_From : Boolean;
385 -- Statuses for the calls to Close
387 procedure Free is new Ada.Unchecked_Deallocation (Buf, Buf_Ptr);
390 -- Check for invalid descriptors, making sure that we do not
391 -- accidentally leave an open file descriptor around.
393 if From = Invalid_FD then
394 if To /= Invalid_FD then
395 Close (To, Status_To);
400 elsif To = Invalid_FD then
401 Close (From, Status_From);
405 -- Allocate the buffer on the heap
410 R := Read (From, Buffer (1)'Address, Buf_Size);
412 -- For VMS, the buffer may not be full. So, we need to try again
413 -- until there is nothing to read.
417 W := Write (To, Buffer (1)'Address, R);
421 -- Problem writing data, could be a disk full. Close files
422 -- without worrying about status, since we are raising a
423 -- Copy_Error exception in any case.
425 Close (From, Status_From);
426 Close (To, Status_To);
434 Close (From, Status_From);
435 Close (To, Status_To);
439 if not (Status_From and Status_To) then
448 procedure Copy_To (To_Name : String) is
450 function Copy_Attributes
451 (From, To : System.Address;
452 Mode : Integer) return Integer;
453 pragma Import (C, Copy_Attributes, "__gnat_copy_attribs");
454 -- Mode = 0 - copy only time stamps.
455 -- Mode = 1 - copy time stamps and read/write/execute attributes
457 C_From : String (1 .. Name'Length + 1);
458 C_To : String (1 .. To_Name'Length + 1);
461 From := Open_Read (Name, Binary);
463 -- Do not clobber destination file if source file could not be opened
465 if From /= Invalid_FD then
466 To := Create_File (To_Name, Binary);
473 C_From (1 .. Name'Length) := Name;
474 C_From (C_From'Last) := ASCII.NUL;
476 C_To (1 .. To_Name'Length) := To_Name;
477 C_To (C_To'Last) := ASCII.NUL;
482 if Copy_Attributes (C_From'Address, C_To'Address, 0) = -1 then
487 if Copy_Attributes (C_From'Address, C_To'Address, 1) = -1 then
497 -- Start of processing for Copy_File
502 -- The source file must exist
504 if not Is_Regular_File (Name) then
508 -- The source file exists
512 -- Copy case, target file must not exist
516 -- If the target file exists, we have an error
518 if Is_Regular_File (Pathname) then
521 -- Case of target is a directory
523 elsif Is_Directory (Pathname) then
525 Dest : constant String := Build_Path (Pathname, Name);
528 -- If target file exists, we have an error, else do copy
530 if Is_Regular_File (Dest) then
537 -- Case of normal copy to file (destination does not exist)
543 -- Overwrite case (destination file may or may not exist)
546 if Is_Directory (Pathname) then
547 Copy_To (Build_Path (Pathname, Name));
552 -- Append case (destination file may or may not exist)
556 -- Appending to existing file
558 if Is_Regular_File (Pathname) then
560 -- Append mode and destination file exists, append data at the
561 -- end of Pathname. But if we fail to open source file, do not
562 -- touch destination file at all.
564 From := Open_Read (Name, Binary);
565 if From /= Invalid_FD then
566 To := Open_Read_Write (Pathname, Binary);
569 Lseek (To, 0, Seek_End);
573 -- Appending to directory, not allowed
575 elsif Is_Directory (Pathname) then
578 -- Appending when target file does not exist
585 -- All error cases are caught here
594 Pathname : C_File_Name;
595 Success : out Boolean;
596 Mode : Copy_Mode := Copy;
597 Preserve : Attribute := Time_Stamps)
599 Ada_Name : String_Access :=
600 To_Path_String_Access
601 (Name, C_String_Length (Name));
602 Ada_Pathname : String_Access :=
603 To_Path_String_Access
604 (Pathname, C_String_Length (Pathname));
606 Copy_File (Ada_Name.all, Ada_Pathname.all, Success, Mode, Preserve);
611 ----------------------
612 -- Copy_Time_Stamps --
613 ----------------------
615 procedure Copy_Time_Stamps (Source, Dest : String; Success : out Boolean) is
617 function Copy_Attributes
618 (From, To : System.Address;
619 Mode : Integer) return Integer;
620 pragma Import (C, Copy_Attributes, "__gnat_copy_attribs");
621 -- Mode = 0 - copy only time stamps.
622 -- Mode = 1 - copy time stamps and read/write/execute attributes
625 if Is_Regular_File (Source) and then Is_Writable_File (Dest) then
627 C_Source : String (1 .. Source'Length + 1);
628 C_Dest : String (1 .. Dest'Length + 1);
631 C_Source (1 .. Source'Length) := Source;
632 C_Source (C_Source'Last) := ASCII.NUL;
634 C_Dest (1 .. Dest'Length) := Dest;
635 C_Dest (C_Dest'Last) := ASCII.NUL;
637 if Copy_Attributes (C_Source'Address, C_Dest'Address, 0) = -1 then
647 end Copy_Time_Stamps;
649 procedure Copy_Time_Stamps
650 (Source, Dest : C_File_Name;
651 Success : out Boolean)
653 Ada_Source : String_Access :=
654 To_Path_String_Access
655 (Source, C_String_Length (Source));
656 Ada_Dest : String_Access :=
657 To_Path_String_Access
658 (Dest, C_String_Length (Dest));
660 Copy_Time_Stamps (Ada_Source.all, Ada_Dest.all, Success);
663 end Copy_Time_Stamps;
671 Fmode : Mode) return File_Descriptor
673 function C_Create_File
675 Fmode : Mode) return File_Descriptor;
676 pragma Import (C, C_Create_File, "__gnat_open_create");
679 return C_Create_File (Name, Fmode);
684 Fmode : Mode) return File_Descriptor
686 C_Name : String (1 .. Name'Length + 1);
689 C_Name (1 .. Name'Length) := Name;
690 C_Name (C_Name'Last) := ASCII.NUL;
691 return Create_File (C_Name (C_Name'First)'Address, Fmode);
694 ---------------------
695 -- Create_New_File --
696 ---------------------
698 function Create_New_File
700 Fmode : Mode) return File_Descriptor
702 function C_Create_New_File
704 Fmode : Mode) return File_Descriptor;
705 pragma Import (C, C_Create_New_File, "__gnat_open_new");
708 return C_Create_New_File (Name, Fmode);
711 function Create_New_File
713 Fmode : Mode) return File_Descriptor
715 C_Name : String (1 .. Name'Length + 1);
718 C_Name (1 .. Name'Length) := Name;
719 C_Name (C_Name'Last) := ASCII.NUL;
720 return Create_New_File (C_Name (C_Name'First)'Address, Fmode);
723 -----------------------------
724 -- Create_Output_Text_File --
725 -----------------------------
727 function Create_Output_Text_File (Name : String) return File_Descriptor is
728 function C_Create_File
729 (Name : C_File_Name) return File_Descriptor;
730 pragma Import (C, C_Create_File, "__gnat_create_output_file");
732 C_Name : String (1 .. Name'Length + 1);
735 C_Name (1 .. Name'Length) := Name;
736 C_Name (C_Name'Last) := ASCII.NUL;
737 return C_Create_File (C_Name (C_Name'First)'Address);
738 end Create_Output_Text_File;
740 ----------------------
741 -- Create_Temp_File --
742 ----------------------
744 procedure Create_Temp_File
745 (FD : out File_Descriptor;
746 Name : out Temp_File_Name)
748 function Open_New_Temp
749 (Name : System.Address;
750 Fmode : Mode) return File_Descriptor;
751 pragma Import (C, Open_New_Temp, "__gnat_open_new_temp");
754 FD := Open_New_Temp (Name'Address, Binary);
755 end Create_Temp_File;
757 procedure Create_Temp_File
758 (FD : out File_Descriptor;
759 Name : out String_Access)
762 Create_Temp_File_Internal (FD, Name, Stdout => False);
763 end Create_Temp_File;
765 procedure Create_Temp_Output_File
766 (FD : out File_Descriptor;
767 Name : out String_Access)
770 Create_Temp_File_Internal (FD, Name, Stdout => True);
771 end Create_Temp_Output_File;
773 -------------------------------
774 -- Create_Temp_File_Internal --
775 -------------------------------
777 procedure Create_Temp_File_Internal
778 (FD : out File_Descriptor;
779 Name : out String_Access;
783 Attempts : Natural := 0;
784 Current : String (Current_Temp_File_Name'Range);
786 ---------------------------------
787 -- Create_New_Output_Text_File --
788 ---------------------------------
790 function Create_New_Output_Text_File
791 (Name : String) return File_Descriptor;
792 -- Similar to Create_Output_Text_File, except it fails if the file
793 -- already exists. We need this behavior to ensure we don't accidentally
794 -- open a temp file that has just been created by a concurrently running
795 -- process. There is no point exposing this function, as it's generally
796 -- not particularly useful.
798 function Create_New_Output_Text_File
799 (Name : String) return File_Descriptor is
800 function C_Create_File
801 (Name : C_File_Name) return File_Descriptor;
802 pragma Import (C, C_Create_File, "__gnat_create_output_file_new");
804 C_Name : String (1 .. Name'Length + 1);
807 C_Name (1 .. Name'Length) := Name;
808 C_Name (C_Name'Last) := ASCII.NUL;
809 return C_Create_File (C_Name (C_Name'First)'Address);
810 end Create_New_Output_Text_File;
813 -- Loop until a new temp file can be created
817 -- We need to protect global variable Current_Temp_File_Name
818 -- against concurrent access by different tasks.
822 -- Start at the last digit
824 Pos := Temp_File_Name_Last_Digit;
828 -- Increment the digit by one
830 case Current_Temp_File_Name (Pos) is
832 Current_Temp_File_Name (Pos) :=
833 Character'Succ (Current_Temp_File_Name (Pos));
838 -- For 9, set the digit to 0 and go to the previous digit
840 Current_Temp_File_Name (Pos) := '0';
845 -- If it is not a digit, then there are no available
846 -- temp file names. Return Invalid_FD. There is almost
847 -- no chance that this code will be ever be executed,
848 -- since it would mean that there are one million temp
849 -- files in the same directory!
858 Current := Current_Temp_File_Name;
860 -- We can now release the lock, because we are no longer
861 -- accessing Current_Temp_File_Name.
871 -- Attempt to create the file
874 FD := Create_New_Output_Text_File (Current);
876 FD := Create_New_File (Current, Binary);
879 if FD /= Invalid_FD then
880 Name := new String'(Current);
884 if not Is_Regular_File (Current) then
886 -- If the file does not already exist and we are unable to create
887 -- it, we give up after Max_Attempts. Otherwise, we try again with
888 -- the next available file name.
890 Attempts := Attempts + 1;
892 if Attempts >= Max_Attempts then
899 end Create_Temp_File_Internal;
905 procedure Delete_File (Name : Address; Success : out Boolean) is
908 R := System.CRTL.unlink (Name);
912 procedure Delete_File (Name : String; Success : out Boolean) is
913 C_Name : String (1 .. Name'Length + 1);
916 C_Name (1 .. Name'Length) := Name;
917 C_Name (C_Name'Last) := ASCII.NUL;
919 Delete_File (C_Name'Address, Success);
922 ---------------------
923 -- File_Time_Stamp --
924 ---------------------
926 function File_Time_Stamp (FD : File_Descriptor) return OS_Time is
927 function File_Time (FD : File_Descriptor) return OS_Time;
928 pragma Import (C, File_Time, "__gnat_file_time_fd");
930 return File_Time (FD);
933 function File_Time_Stamp (Name : C_File_Name) return OS_Time is
934 function File_Time (Name : Address) return OS_Time;
935 pragma Import (C, File_Time, "__gnat_file_time_name");
937 return File_Time (Name);
940 function File_Time_Stamp (Name : String) return OS_Time is
941 F_Name : String (1 .. Name'Length + 1);
943 F_Name (1 .. Name'Length) := Name;
944 F_Name (F_Name'Last) := ASCII.NUL;
945 return File_Time_Stamp (F_Name'Address);
948 ---------------------------
949 -- Get_Debuggable_Suffix --
950 ---------------------------
952 function Get_Debuggable_Suffix return String_Access is
953 procedure Get_Suffix_Ptr (Length, Ptr : Address);
954 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_debuggable_suffix_ptr");
956 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
957 pragma Import (C, Strncpy, "strncpy");
959 Suffix_Ptr : Address;
960 Suffix_Length : Integer;
961 Result : String_Access;
964 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
966 Result := new String (1 .. Suffix_Length);
968 if Suffix_Length > 0 then
969 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
973 end Get_Debuggable_Suffix;
975 ---------------------------
976 -- Get_Executable_Suffix --
977 ---------------------------
979 function Get_Executable_Suffix return String_Access is
980 procedure Get_Suffix_Ptr (Length, Ptr : Address);
981 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_executable_suffix_ptr");
983 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
984 pragma Import (C, Strncpy, "strncpy");
986 Suffix_Ptr : Address;
987 Suffix_Length : Integer;
988 Result : String_Access;
991 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
993 Result := new String (1 .. Suffix_Length);
995 if Suffix_Length > 0 then
996 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
1000 end Get_Executable_Suffix;
1002 -----------------------
1003 -- Get_Object_Suffix --
1004 -----------------------
1006 function Get_Object_Suffix return String_Access is
1007 procedure Get_Suffix_Ptr (Length, Ptr : Address);
1008 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_object_suffix_ptr");
1010 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1011 pragma Import (C, Strncpy, "strncpy");
1013 Suffix_Ptr : Address;
1014 Suffix_Length : Integer;
1015 Result : String_Access;
1018 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
1020 Result := new String (1 .. Suffix_Length);
1022 if Suffix_Length > 0 then
1023 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
1027 end Get_Object_Suffix;
1029 ----------------------------------
1030 -- Get_Target_Debuggable_Suffix --
1031 ----------------------------------
1033 function Get_Target_Debuggable_Suffix return String_Access is
1034 Target_Exec_Ext_Ptr : Address;
1036 (C, Target_Exec_Ext_Ptr, "__gnat_target_debuggable_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_Exec_Ext_Ptr);
1050 Result := new String (1 .. Suffix_Length);
1052 if Suffix_Length > 0 then
1053 Strncpy (Result.all'Address, Target_Exec_Ext_Ptr, Suffix_Length);
1057 end Get_Target_Debuggable_Suffix;
1059 ----------------------------------
1060 -- Get_Target_Executable_Suffix --
1061 ----------------------------------
1063 function Get_Target_Executable_Suffix return String_Access is
1064 Target_Exec_Ext_Ptr : Address;
1066 (C, Target_Exec_Ext_Ptr, "__gnat_target_executable_extension");
1068 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1069 pragma Import (C, Strncpy, "strncpy");
1071 function Strlen (Cstring : Address) return Integer;
1072 pragma Import (C, Strlen, "strlen");
1074 Suffix_Length : Integer;
1075 Result : String_Access;
1078 Suffix_Length := Strlen (Target_Exec_Ext_Ptr);
1080 Result := new String (1 .. Suffix_Length);
1082 if Suffix_Length > 0 then
1083 Strncpy (Result.all'Address, Target_Exec_Ext_Ptr, Suffix_Length);
1087 end Get_Target_Executable_Suffix;
1089 ------------------------------
1090 -- Get_Target_Object_Suffix --
1091 ------------------------------
1093 function Get_Target_Object_Suffix return String_Access is
1094 Target_Object_Ext_Ptr : Address;
1096 (C, Target_Object_Ext_Ptr, "__gnat_target_object_extension");
1098 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1099 pragma Import (C, Strncpy, "strncpy");
1101 function Strlen (Cstring : Address) return Integer;
1102 pragma Import (C, Strlen, "strlen");
1104 Suffix_Length : Integer;
1105 Result : String_Access;
1108 Suffix_Length := Strlen (Target_Object_Ext_Ptr);
1110 Result := new String (1 .. Suffix_Length);
1112 if Suffix_Length > 0 then
1113 Strncpy (Result.all'Address, Target_Object_Ext_Ptr, Suffix_Length);
1117 end Get_Target_Object_Suffix;
1123 function Getenv (Name : String) return String_Access is
1124 procedure Get_Env_Value_Ptr (Name, Length, Ptr : Address);
1125 pragma Import (C, Get_Env_Value_Ptr, "__gnat_getenv");
1127 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1128 pragma Import (C, Strncpy, "strncpy");
1130 Env_Value_Ptr : aliased Address;
1131 Env_Value_Length : aliased Integer;
1132 F_Name : aliased String (1 .. Name'Length + 1);
1133 Result : String_Access;
1136 F_Name (1 .. Name'Length) := Name;
1137 F_Name (F_Name'Last) := ASCII.NUL;
1140 (F_Name'Address, Env_Value_Length'Address, Env_Value_Ptr'Address);
1142 Result := new String (1 .. Env_Value_Length);
1144 if Env_Value_Length > 0 then
1145 Strncpy (Result.all'Address, Env_Value_Ptr, Env_Value_Length);
1155 function GM_Day (Date : OS_Time) return Day_Type is
1158 pragma Warnings (Off);
1164 pragma Warnings (On);
1167 GM_Split (Date, Y, Mo, D, H, Mn, S);
1175 function GM_Hour (Date : OS_Time) return Hour_Type is
1178 pragma Warnings (Off);
1184 pragma Warnings (On);
1187 GM_Split (Date, Y, Mo, D, H, Mn, S);
1195 function GM_Minute (Date : OS_Time) return Minute_Type is
1198 pragma Warnings (Off);
1204 pragma Warnings (On);
1207 GM_Split (Date, Y, Mo, D, H, Mn, S);
1215 function GM_Month (Date : OS_Time) return Month_Type is
1218 pragma Warnings (Off);
1224 pragma Warnings (On);
1227 GM_Split (Date, Y, Mo, D, H, Mn, S);
1235 function GM_Second (Date : OS_Time) return Second_Type is
1238 pragma Warnings (Off);
1244 pragma Warnings (On);
1247 GM_Split (Date, Y, Mo, D, H, Mn, S);
1257 Year : out Year_Type;
1258 Month : out Month_Type;
1260 Hour : out Hour_Type;
1261 Minute : out Minute_Type;
1262 Second : out Second_Type)
1264 procedure To_GM_Time
1265 (P_Time_T, P_Year, P_Month, P_Day, P_Hours, P_Mins, P_Secs : Address);
1266 pragma Import (C, To_GM_Time, "__gnat_to_gm_time");
1268 T : OS_Time := Date;
1277 -- Use the global lock because To_GM_Time is not thread safe
1279 Locked_Processing : begin
1282 (T'Address, Y'Address, Mo'Address, D'Address,
1283 H'Address, Mn'Address, S'Address);
1284 SSL.Unlock_Task.all;
1288 SSL.Unlock_Task.all;
1290 end Locked_Processing;
1304 function GM_Year (Date : OS_Time) return Year_Type is
1307 pragma Warnings (Off);
1313 pragma Warnings (On);
1316 GM_Split (Date, Y, Mo, D, H, Mn, S);
1320 ----------------------
1321 -- Is_Absolute_Path --
1322 ----------------------
1324 function Is_Absolute_Path (Name : String) return Boolean is
1325 function Is_Absolute_Path
1327 Length : Integer) return Integer;
1328 pragma Import (C, Is_Absolute_Path, "__gnat_is_absolute_path");
1330 return Is_Absolute_Path (Name'Address, Name'Length) /= 0;
1331 end Is_Absolute_Path;
1337 function Is_Directory (Name : C_File_Name) return Boolean is
1338 function Is_Directory (Name : Address) return Integer;
1339 pragma Import (C, Is_Directory, "__gnat_is_directory");
1341 return Is_Directory (Name) /= 0;
1344 function Is_Directory (Name : String) return Boolean is
1345 F_Name : String (1 .. Name'Length + 1);
1347 F_Name (1 .. Name'Length) := Name;
1348 F_Name (F_Name'Last) := ASCII.NUL;
1349 return Is_Directory (F_Name'Address);
1352 ----------------------
1353 -- Is_Readable_File --
1354 ----------------------
1356 function Is_Readable_File (Name : C_File_Name) return Boolean is
1357 function Is_Readable_File (Name : Address) return Integer;
1358 pragma Import (C, Is_Readable_File, "__gnat_is_readable_file");
1360 return Is_Readable_File (Name) /= 0;
1361 end Is_Readable_File;
1363 function Is_Readable_File (Name : String) return Boolean is
1364 F_Name : String (1 .. Name'Length + 1);
1366 F_Name (1 .. Name'Length) := Name;
1367 F_Name (F_Name'Last) := ASCII.NUL;
1368 return Is_Readable_File (F_Name'Address);
1369 end Is_Readable_File;
1371 ------------------------
1372 -- Is_Executable_File --
1373 ------------------------
1375 function Is_Executable_File (Name : C_File_Name) return Boolean is
1376 function Is_Executable_File (Name : Address) return Integer;
1377 pragma Import (C, Is_Executable_File, "__gnat_is_executable_file");
1379 return Is_Executable_File (Name) /= 0;
1380 end Is_Executable_File;
1382 function Is_Executable_File (Name : String) return Boolean is
1383 F_Name : String (1 .. Name'Length + 1);
1385 F_Name (1 .. Name'Length) := Name;
1386 F_Name (F_Name'Last) := ASCII.NUL;
1387 return Is_Executable_File (F_Name'Address);
1388 end Is_Executable_File;
1390 ---------------------
1391 -- Is_Regular_File --
1392 ---------------------
1394 function Is_Regular_File (Name : C_File_Name) return Boolean is
1395 function Is_Regular_File (Name : Address) return Integer;
1396 pragma Import (C, Is_Regular_File, "__gnat_is_regular_file");
1398 return Is_Regular_File (Name) /= 0;
1399 end Is_Regular_File;
1401 function Is_Regular_File (Name : String) return Boolean is
1402 F_Name : String (1 .. Name'Length + 1);
1404 F_Name (1 .. Name'Length) := Name;
1405 F_Name (F_Name'Last) := ASCII.NUL;
1406 return Is_Regular_File (F_Name'Address);
1407 end Is_Regular_File;
1409 ----------------------
1410 -- Is_Symbolic_Link --
1411 ----------------------
1413 function Is_Symbolic_Link (Name : C_File_Name) return Boolean is
1414 function Is_Symbolic_Link (Name : Address) return Integer;
1415 pragma Import (C, Is_Symbolic_Link, "__gnat_is_symbolic_link");
1417 return Is_Symbolic_Link (Name) /= 0;
1418 end Is_Symbolic_Link;
1420 function Is_Symbolic_Link (Name : String) return Boolean is
1421 F_Name : String (1 .. Name'Length + 1);
1423 F_Name (1 .. Name'Length) := Name;
1424 F_Name (F_Name'Last) := ASCII.NUL;
1425 return Is_Symbolic_Link (F_Name'Address);
1426 end Is_Symbolic_Link;
1428 ----------------------
1429 -- Is_Writable_File --
1430 ----------------------
1432 function Is_Writable_File (Name : C_File_Name) return Boolean is
1433 function Is_Writable_File (Name : Address) return Integer;
1434 pragma Import (C, Is_Writable_File, "__gnat_is_writable_file");
1436 return Is_Writable_File (Name) /= 0;
1437 end Is_Writable_File;
1439 function Is_Writable_File (Name : String) return Boolean is
1440 F_Name : String (1 .. Name'Length + 1);
1442 F_Name (1 .. Name'Length) := Name;
1443 F_Name (F_Name'Last) := ASCII.NUL;
1444 return Is_Writable_File (F_Name'Address);
1445 end Is_Writable_File;
1447 -------------------------
1448 -- Locate_Exec_On_Path --
1449 -------------------------
1451 function Locate_Exec_On_Path
1452 (Exec_Name : String) return String_Access
1454 function Locate_Exec_On_Path (C_Exec_Name : Address) return Address;
1455 pragma Import (C, Locate_Exec_On_Path, "__gnat_locate_exec_on_path");
1457 procedure Free (Ptr : System.Address);
1458 pragma Import (C, Free, "free");
1460 C_Exec_Name : String (1 .. Exec_Name'Length + 1);
1461 Path_Addr : Address;
1463 Result : String_Access;
1466 C_Exec_Name (1 .. Exec_Name'Length) := Exec_Name;
1467 C_Exec_Name (C_Exec_Name'Last) := ASCII.NUL;
1469 Path_Addr := Locate_Exec_On_Path (C_Exec_Name'Address);
1470 Path_Len := C_String_Length (Path_Addr);
1472 if Path_Len = 0 then
1476 Result := To_Path_String_Access (Path_Addr, Path_Len);
1479 -- Always return an absolute path name
1481 if not Is_Absolute_Path (Result.all) then
1483 Absolute_Path : constant String :=
1484 Normalize_Pathname (Result.all);
1487 Result := new String'(Absolute_Path);
1493 end Locate_Exec_On_Path;
1495 -------------------------
1496 -- Locate_Regular_File --
1497 -------------------------
1499 function Locate_Regular_File
1500 (File_Name : C_File_Name;
1501 Path : C_File_Name) return String_Access
1503 function Locate_Regular_File
1504 (C_File_Name, Path_Val : Address) return Address;
1505 pragma Import (C, Locate_Regular_File, "__gnat_locate_regular_file");
1507 procedure Free (Ptr : System.Address);
1508 pragma Import (C, Free, "free");
1510 Path_Addr : Address;
1512 Result : String_Access;
1515 Path_Addr := Locate_Regular_File (File_Name, Path);
1516 Path_Len := C_String_Length (Path_Addr);
1518 if Path_Len = 0 then
1522 Result := To_Path_String_Access (Path_Addr, Path_Len);
1526 end Locate_Regular_File;
1528 function Locate_Regular_File
1529 (File_Name : String;
1530 Path : String) return String_Access
1532 C_File_Name : String (1 .. File_Name'Length + 1);
1533 C_Path : String (1 .. Path'Length + 1);
1534 Result : String_Access;
1537 C_File_Name (1 .. File_Name'Length) := File_Name;
1538 C_File_Name (C_File_Name'Last) := ASCII.NUL;
1540 C_Path (1 .. Path'Length) := Path;
1541 C_Path (C_Path'Last) := ASCII.NUL;
1543 Result := Locate_Regular_File (C_File_Name'Address, C_Path'Address);
1545 -- Always return an absolute path name
1547 if Result /= null and then not Is_Absolute_Path (Result.all) then
1549 Absolute_Path : constant String := Normalize_Pathname (Result.all);
1552 Result := new String'(Absolute_Path);
1557 end Locate_Regular_File;
1559 ------------------------
1560 -- Non_Blocking_Spawn --
1561 ------------------------
1563 function Non_Blocking_Spawn
1564 (Program_Name : String;
1565 Args : Argument_List) return Process_Id
1569 pragma Warnings (Off, Junk);
1571 Spawn_Internal (Program_Name, Args, Junk, Pid, Blocking => False);
1573 end Non_Blocking_Spawn;
1575 function Non_Blocking_Spawn
1576 (Program_Name : String;
1577 Args : Argument_List;
1578 Output_File_Descriptor : File_Descriptor;
1579 Err_To_Out : Boolean := True) return Process_Id
1581 Saved_Output : File_Descriptor;
1582 Saved_Error : File_Descriptor := Invalid_FD; -- prevent warning
1586 if Output_File_Descriptor = Invalid_FD then
1590 -- Set standard output and, if specified, error to the temporary file
1592 Saved_Output := Dup (Standout);
1593 Dup2 (Output_File_Descriptor, Standout);
1596 Saved_Error := Dup (Standerr);
1597 Dup2 (Output_File_Descriptor, Standerr);
1600 -- Spawn the program
1602 Pid := Non_Blocking_Spawn (Program_Name, Args);
1604 -- Restore the standard output and error
1606 Dup2 (Saved_Output, Standout);
1609 Dup2 (Saved_Error, Standerr);
1612 -- And close the saved standard output and error file descriptors
1614 Close (Saved_Output);
1617 Close (Saved_Error);
1621 end Non_Blocking_Spawn;
1623 function Non_Blocking_Spawn
1624 (Program_Name : String;
1625 Args : Argument_List;
1626 Output_File : String;
1627 Err_To_Out : Boolean := True) return Process_Id
1629 Output_File_Descriptor : constant File_Descriptor :=
1630 Create_Output_Text_File (Output_File);
1631 Result : Process_Id;
1634 -- Do not attempt to spawn if the output file could not be created
1636 if Output_File_Descriptor = Invalid_FD then
1640 Result := Non_Blocking_Spawn
1641 (Program_Name, Args, Output_File_Descriptor, Err_To_Out);
1643 -- Close the file just created for the output, as the file descriptor
1644 -- cannot be used anywhere, being a local value. It is safe to do
1645 -- that, as the file descriptor has been duplicated to form
1646 -- standard output and error of the spawned process.
1648 Close (Output_File_Descriptor);
1652 end Non_Blocking_Spawn;
1654 -------------------------
1655 -- Normalize_Arguments --
1656 -------------------------
1658 procedure Normalize_Arguments (Args : in out Argument_List) is
1660 procedure Quote_Argument (Arg : in out String_Access);
1661 -- Add quote around argument if it contains spaces
1663 C_Argument_Needs_Quote : Integer;
1664 pragma Import (C, C_Argument_Needs_Quote, "__gnat_argument_needs_quote");
1665 Argument_Needs_Quote : constant Boolean := C_Argument_Needs_Quote /= 0;
1667 --------------------
1668 -- Quote_Argument --
1669 --------------------
1671 procedure Quote_Argument (Arg : in out String_Access) is
1672 Res : String (1 .. Arg'Length * 2);
1674 Quote_Needed : Boolean := False;
1677 if Arg (Arg'First) /= '"' or else Arg (Arg'Last) /= '"' then
1683 for K in Arg'Range loop
1687 if Arg (K) = '"' then
1691 Quote_Needed := True;
1693 elsif Arg (K) = ' ' then
1695 Quote_Needed := True;
1703 if Quote_Needed then
1705 -- If null terminated string, put the quote before
1707 if Res (J) = ASCII.NUL then
1710 Res (J) := ASCII.NUL;
1712 -- If argument is terminated by '\', then double it. Otherwise
1713 -- the ending quote will be taken as-is. This is quite strange
1714 -- spawn behavior from Windows, but this is what we see!
1717 if Res (J) = '\' then
1729 Old : String_Access := Arg;
1732 Arg := new String'(Res (1 .. J));
1740 -- Start of processing for Normalize_Arguments
1743 if Argument_Needs_Quote then
1744 for K in Args'Range loop
1745 if Args (K) /= null and then Args (K)'Length /= 0 then
1746 Quote_Argument (Args (K));
1750 end Normalize_Arguments;
1752 ------------------------
1753 -- Normalize_Pathname --
1754 ------------------------
1756 function Normalize_Pathname
1758 Directory : String := "";
1759 Resolve_Links : Boolean := True;
1760 Case_Sensitive : Boolean := True) return String
1763 pragma Import (C, Max_Path, "__gnat_max_path_len");
1764 -- Maximum length of a path name
1766 procedure Get_Current_Dir
1767 (Dir : System.Address;
1768 Length : System.Address);
1769 pragma Import (C, Get_Current_Dir, "__gnat_get_current_dir");
1771 Path_Buffer : String (1 .. Max_Path + Max_Path + 2);
1772 End_Path : Natural := 0;
1773 Link_Buffer : String (1 .. Max_Path + 2);
1779 Max_Iterations : constant := 500;
1781 function Get_File_Names_Case_Sensitive return Integer;
1783 (C, Get_File_Names_Case_Sensitive,
1784 "__gnat_get_file_names_case_sensitive");
1786 Fold_To_Lower_Case : constant Boolean :=
1788 and then Get_File_Names_Case_Sensitive = 0;
1791 (Path : System.Address;
1792 Buf : System.Address;
1793 Bufsiz : Integer) return Integer;
1794 pragma Import (C, Readlink, "__gnat_readlink");
1796 function To_Canonical_File_Spec
1797 (Host_File : System.Address) return System.Address;
1799 (C, To_Canonical_File_Spec, "__gnat_to_canonical_file_spec");
1801 The_Name : String (1 .. Name'Length + 1);
1802 Canonical_File_Addr : System.Address;
1803 Canonical_File_Len : Integer;
1805 function Strlen (S : System.Address) return Integer;
1806 pragma Import (C, Strlen, "strlen");
1808 function Final_Value (S : String) return String;
1809 -- Make final adjustment to the returned string. This function strips
1810 -- trailing directory separators, and folds returned string to lower
1811 -- case if required.
1813 function Get_Directory (Dir : String) return String;
1814 -- If Dir is not empty, return it, adding a directory separator
1815 -- if not already present, otherwise return current working directory
1816 -- with terminating directory separator.
1822 function Final_Value (S : String) return String is
1824 -- We may need to fold S to lower case, so we need a variable
1829 if Fold_To_Lower_Case then
1830 System.Case_Util.To_Lower (S1);
1833 -- Remove trailing directory separator, if any
1838 and then (S1 (Last) = '/'
1840 S1 (Last) = Directory_Separator)
1842 -- Special case for Windows: C:\
1845 and then S1 (1) /= Directory_Separator
1846 and then S1 (2) = ':'
1855 return S1 (1 .. Last);
1862 function Get_Directory (Dir : String) return String is
1863 Result : String (1 .. Dir'Length + 1);
1864 Length : constant Natural := Dir'Length;
1867 -- Directory given, add directory separator if needed
1870 Result (1 .. Length) := Dir;
1872 -- On Windows, change all '/' to '\'
1875 for J in 1 .. Length loop
1876 if Result (J) = '/' then
1877 Result (J) := Directory_Separator;
1882 -- Add directory separator, if needed
1884 if Result (Length) = Directory_Separator then
1885 return Result (1 .. Length);
1887 Result (Result'Length) := Directory_Separator;
1891 -- Directory name not given, get current directory
1895 Buffer : String (1 .. Max_Path + 2);
1896 Path_Len : Natural := Max_Path;
1899 Get_Current_Dir (Buffer'Address, Path_Len'Address);
1901 if Buffer (Path_Len) /= Directory_Separator then
1902 Path_Len := Path_Len + 1;
1903 Buffer (Path_Len) := Directory_Separator;
1906 -- By default, the drive letter on Windows is in upper case
1909 and then Path_Len >= 2
1910 and then Buffer (2) = ':'
1912 System.Case_Util.To_Upper (Buffer (1 .. 1));
1915 return Buffer (1 .. Path_Len);
1920 -- Start of processing for Normalize_Pathname
1923 -- Special case, if name is null, then return null
1925 if Name'Length = 0 then
1929 -- First, convert VMS file spec to Unix file spec.
1930 -- If Name is not in VMS syntax, then this is equivalent
1931 -- to put Name at the beginning of Path_Buffer.
1933 VMS_Conversion : begin
1934 The_Name (1 .. Name'Length) := Name;
1935 The_Name (The_Name'Last) := ASCII.NUL;
1937 Canonical_File_Addr := To_Canonical_File_Spec (The_Name'Address);
1938 Canonical_File_Len := Strlen (Canonical_File_Addr);
1940 -- If VMS syntax conversion has failed, return an empty string
1941 -- to indicate the failure.
1943 if Canonical_File_Len = 0 then
1948 subtype Path_String is String (1 .. Canonical_File_Len);
1949 type Path_String_Access is access Path_String;
1951 function Address_To_Access is new
1952 Ada.Unchecked_Conversion (Source => Address,
1953 Target => Path_String_Access);
1955 Path_Access : constant Path_String_Access :=
1956 Address_To_Access (Canonical_File_Addr);
1959 Path_Buffer (1 .. Canonical_File_Len) := Path_Access.all;
1960 End_Path := Canonical_File_Len;
1965 -- Replace all '/' by Directory Separators (this is for Windows)
1967 if Directory_Separator /= '/' then
1968 for Index in 1 .. End_Path loop
1969 if Path_Buffer (Index) = '/' then
1970 Path_Buffer (Index) := Directory_Separator;
1975 -- Resolve directory names for Windows (formerly also VMS)
1977 -- On VMS, if we have a Unix path such as /temp/..., and TEMP is a
1978 -- logical name, we must not try to resolve this logical name, because
1979 -- it may have multiple equivalences and if resolved we will only
1980 -- get the first one.
1984 -- On Windows, if we have an absolute path starting with a directory
1985 -- separator, we need to have the drive letter appended in front.
1987 -- On Windows, Get_Current_Dir will return a suitable directory name
1988 -- (path starting with a drive letter on Windows). So we take this
1989 -- drive letter and prepend it to the current path.
1991 if Path_Buffer (1) = Directory_Separator
1992 and then Path_Buffer (2) /= Directory_Separator
1995 Cur_Dir : constant String := Get_Directory ("");
1996 -- Get the current directory to get the drive letter
1999 if Cur_Dir'Length > 2
2000 and then Cur_Dir (Cur_Dir'First + 1) = ':'
2002 Path_Buffer (3 .. End_Path + 2) :=
2003 Path_Buffer (1 .. End_Path);
2004 Path_Buffer (1 .. 2) :=
2005 Cur_Dir (Cur_Dir'First .. Cur_Dir'First + 1);
2006 End_Path := End_Path + 2;
2010 -- We have a drive letter, ensure it is upper-case
2012 elsif Path_Buffer (1) in 'a' .. 'z'
2013 and then Path_Buffer (2) = ':'
2015 System.Case_Util.To_Upper (Path_Buffer (1 .. 1));
2019 -- On Windows, remove all double-quotes that are possibly part of the
2020 -- path but can cause problems with other methods.
2027 Index := Path_Buffer'First;
2028 for Current in Path_Buffer'First .. End_Path loop
2029 if Path_Buffer (Current) /= '"' then
2030 Path_Buffer (Index) := Path_Buffer (Current);
2035 End_Path := Index - 1;
2039 -- Start the conversions
2041 -- If this is not finished after Max_Iterations, give up and return an
2044 for J in 1 .. Max_Iterations loop
2046 -- If we don't have an absolute pathname, prepend the directory
2050 and then not Is_Absolute_Path (Path_Buffer (1 .. End_Path))
2053 Reference_Dir : constant String := Get_Directory (Directory);
2054 Ref_Dir_Len : constant Natural := Reference_Dir'Length;
2055 -- Current directory name specified and its length
2058 Path_Buffer (Ref_Dir_Len + 1 .. Ref_Dir_Len + End_Path) :=
2059 Path_Buffer (1 .. End_Path);
2060 End_Path := Ref_Dir_Len + End_Path;
2061 Path_Buffer (1 .. Ref_Dir_Len) := Reference_Dir;
2062 Last := Ref_Dir_Len;
2069 -- Ensure that Windows network drives are kept, e.g: \\server\drive-c
2072 and then Directory_Separator = '\'
2073 and then Path_Buffer (1 .. 2) = "\\"
2078 -- If we have traversed the full pathname, return it
2080 if Start > End_Path then
2081 return Final_Value (Path_Buffer (1 .. End_Path));
2084 -- Remove duplicate directory separators
2086 while Path_Buffer (Start) = Directory_Separator loop
2087 if Start = End_Path then
2088 return Final_Value (Path_Buffer (1 .. End_Path - 1));
2091 Path_Buffer (Start .. End_Path - 1) :=
2092 Path_Buffer (Start + 1 .. End_Path);
2093 End_Path := End_Path - 1;
2097 -- Find the end of the current field: last character or the one
2098 -- preceding the next directory separator.
2100 while Finish < End_Path
2101 and then Path_Buffer (Finish + 1) /= Directory_Separator
2103 Finish := Finish + 1;
2108 if Start = Finish and then Path_Buffer (Start) = '.' then
2109 if Start = End_Path then
2111 return (1 => Directory_Separator);
2114 if Fold_To_Lower_Case then
2115 System.Case_Util.To_Lower (Path_Buffer (1 .. Last - 1));
2118 return Path_Buffer (1 .. Last - 1);
2123 Path_Buffer (Last + 1 .. End_Path - 2) :=
2124 Path_Buffer (Last + 3 .. End_Path);
2125 End_Path := End_Path - 2;
2128 -- Remove ".." fields
2130 elsif Finish = Start + 1
2131 and then Path_Buffer (Start .. Finish) = ".."
2136 exit when Start < 1 or else
2137 Path_Buffer (Start) = Directory_Separator;
2141 if Finish = End_Path then
2142 return (1 => Directory_Separator);
2145 Path_Buffer (1 .. End_Path - Finish) :=
2146 Path_Buffer (Finish + 1 .. End_Path);
2147 End_Path := End_Path - Finish;
2152 if Finish = End_Path then
2153 return Final_Value (Path_Buffer (1 .. Start - 1));
2156 Path_Buffer (Start + 1 .. Start + End_Path - Finish - 1) :=
2157 Path_Buffer (Finish + 2 .. End_Path);
2158 End_Path := Start + End_Path - Finish - 1;
2163 -- Check if current field is a symbolic link
2165 elsif Resolve_Links then
2167 Saved : constant Character := Path_Buffer (Finish + 1);
2170 Path_Buffer (Finish + 1) := ASCII.NUL;
2171 Status := Readlink (Path_Buffer'Address,
2172 Link_Buffer'Address,
2173 Link_Buffer'Length);
2174 Path_Buffer (Finish + 1) := Saved;
2177 -- Not a symbolic link, move to the next field, if any
2182 -- Replace symbolic link with its value
2185 if Is_Absolute_Path (Link_Buffer (1 .. Status)) then
2186 Path_Buffer (Status + 1 .. End_Path - (Finish - Status)) :=
2187 Path_Buffer (Finish + 1 .. End_Path);
2188 End_Path := End_Path - (Finish - Status);
2189 Path_Buffer (1 .. Status) := Link_Buffer (1 .. Status);
2194 (Last + Status + 1 .. End_Path - Finish + Last + Status) :=
2195 Path_Buffer (Finish + 1 .. End_Path);
2196 End_Path := End_Path - Finish + Last + Status;
2197 Path_Buffer (Last + 1 .. Last + Status) :=
2198 Link_Buffer (1 .. Status);
2207 -- Too many iterations: give up
2209 -- This can happen when there is a circularity in the symbolic links: A
2210 -- is a symbolic link for B, which itself is a symbolic link, and the
2211 -- target of B or of another symbolic link target of B is A. In this
2212 -- case, we return an empty string to indicate failure to resolve.
2215 end Normalize_Pathname;
2222 (Name : C_File_Name;
2223 Fmode : Mode) return File_Descriptor
2225 function C_Open_Read
2226 (Name : C_File_Name;
2227 Fmode : Mode) return File_Descriptor;
2228 pragma Import (C, C_Open_Read, "__gnat_open_read");
2230 return C_Open_Read (Name, Fmode);
2235 Fmode : Mode) return File_Descriptor
2237 C_Name : String (1 .. Name'Length + 1);
2239 C_Name (1 .. Name'Length) := Name;
2240 C_Name (C_Name'Last) := ASCII.NUL;
2241 return Open_Read (C_Name (C_Name'First)'Address, Fmode);
2244 ---------------------
2245 -- Open_Read_Write --
2246 ---------------------
2248 function Open_Read_Write
2249 (Name : C_File_Name;
2250 Fmode : Mode) return File_Descriptor
2252 function C_Open_Read_Write
2253 (Name : C_File_Name;
2254 Fmode : Mode) return File_Descriptor;
2255 pragma Import (C, C_Open_Read_Write, "__gnat_open_rw");
2257 return C_Open_Read_Write (Name, Fmode);
2258 end Open_Read_Write;
2260 function Open_Read_Write
2262 Fmode : Mode) return File_Descriptor
2264 C_Name : String (1 .. Name'Length + 1);
2266 C_Name (1 .. Name'Length) := Name;
2267 C_Name (C_Name'Last) := ASCII.NUL;
2268 return Open_Read_Write (C_Name (C_Name'First)'Address, Fmode);
2269 end Open_Read_Write;
2275 procedure OS_Exit (Status : Integer) is
2277 OS_Exit_Ptr (Status);
2278 raise Program_Error;
2281 ---------------------
2282 -- OS_Exit_Default --
2283 ---------------------
2285 procedure OS_Exit_Default (Status : Integer) is
2286 procedure GNAT_OS_Exit (Status : Integer);
2287 pragma Import (C, GNAT_OS_Exit, "__gnat_os_exit");
2288 pragma No_Return (GNAT_OS_Exit);
2290 GNAT_OS_Exit (Status);
2291 end OS_Exit_Default;
2293 --------------------
2294 -- Pid_To_Integer --
2295 --------------------
2297 function Pid_To_Integer (Pid : Process_Id) return Integer is
2299 return Integer (Pid);
2307 (FD : File_Descriptor;
2309 N : Integer) return Integer
2312 return Integer (System.CRTL.read
2313 (System.CRTL.int (FD), System.CRTL.chars (A), System.CRTL.int (N)));
2320 procedure Rename_File
2321 (Old_Name : C_File_Name;
2322 New_Name : C_File_Name;
2323 Success : out Boolean)
2325 function rename (From, To : Address) return Integer;
2326 pragma Import (C, rename, "__gnat_rename");
2329 R := rename (Old_Name, New_Name);
2333 procedure Rename_File
2336 Success : out Boolean)
2338 C_Old_Name : String (1 .. Old_Name'Length + 1);
2339 C_New_Name : String (1 .. New_Name'Length + 1);
2341 C_Old_Name (1 .. Old_Name'Length) := Old_Name;
2342 C_Old_Name (C_Old_Name'Last) := ASCII.NUL;
2343 C_New_Name (1 .. New_Name'Length) := New_Name;
2344 C_New_Name (C_New_Name'Last) := ASCII.NUL;
2345 Rename_File (C_Old_Name'Address, C_New_Name'Address, Success);
2348 -----------------------
2349 -- Set_Close_On_Exec --
2350 -----------------------
2352 procedure Set_Close_On_Exec
2353 (FD : File_Descriptor;
2354 Close_On_Exec : Boolean;
2355 Status : out Boolean)
2357 function C_Set_Close_On_Exec
2358 (FD : File_Descriptor; Close_On_Exec : System.CRTL.int)
2359 return System.CRTL.int;
2360 pragma Import (C, C_Set_Close_On_Exec, "__gnat_set_close_on_exec");
2362 Status := C_Set_Close_On_Exec (FD, Boolean'Pos (Close_On_Exec)) = 0;
2363 end Set_Close_On_Exec;
2365 --------------------
2366 -- Set_Executable --
2367 --------------------
2369 procedure Set_Executable (Name : String) is
2370 procedure C_Set_Executable (Name : C_File_Name);
2371 pragma Import (C, C_Set_Executable, "__gnat_set_executable");
2372 C_Name : aliased String (Name'First .. Name'Last + 1);
2374 C_Name (Name'Range) := Name;
2375 C_Name (C_Name'Last) := ASCII.NUL;
2376 C_Set_Executable (C_Name (C_Name'First)'Address);
2379 ----------------------
2380 -- Set_Non_Readable --
2381 ----------------------
2383 procedure Set_Non_Readable (Name : String) is
2384 procedure C_Set_Non_Readable (Name : C_File_Name);
2385 pragma Import (C, C_Set_Non_Readable, "__gnat_set_non_readable");
2386 C_Name : aliased String (Name'First .. Name'Last + 1);
2388 C_Name (Name'Range) := Name;
2389 C_Name (C_Name'Last) := ASCII.NUL;
2390 C_Set_Non_Readable (C_Name (C_Name'First)'Address);
2391 end Set_Non_Readable;
2393 ----------------------
2394 -- Set_Non_Writable --
2395 ----------------------
2397 procedure Set_Non_Writable (Name : String) is
2398 procedure C_Set_Non_Writable (Name : C_File_Name);
2399 pragma Import (C, C_Set_Non_Writable, "__gnat_set_non_writable");
2400 C_Name : aliased String (Name'First .. Name'Last + 1);
2402 C_Name (Name'Range) := Name;
2403 C_Name (C_Name'Last) := ASCII.NUL;
2404 C_Set_Non_Writable (C_Name (C_Name'First)'Address);
2405 end Set_Non_Writable;
2411 procedure Set_Readable (Name : String) is
2412 procedure C_Set_Readable (Name : C_File_Name);
2413 pragma Import (C, C_Set_Readable, "__gnat_set_readable");
2414 C_Name : aliased String (Name'First .. Name'Last + 1);
2416 C_Name (Name'Range) := Name;
2417 C_Name (C_Name'Last) := ASCII.NUL;
2418 C_Set_Readable (C_Name (C_Name'First)'Address);
2421 --------------------
2423 --------------------
2425 procedure Set_Writable (Name : String) is
2426 procedure C_Set_Writable (Name : C_File_Name);
2427 pragma Import (C, C_Set_Writable, "__gnat_set_writable");
2428 C_Name : aliased String (Name'First .. Name'Last + 1);
2430 C_Name (Name'Range) := Name;
2431 C_Name (C_Name'Last) := ASCII.NUL;
2432 C_Set_Writable (C_Name (C_Name'First)'Address);
2439 procedure Setenv (Name : String; Value : String) is
2440 F_Name : String (1 .. Name'Length + 1);
2441 F_Value : String (1 .. Value'Length + 1);
2443 procedure Set_Env_Value (Name, Value : System.Address);
2444 pragma Import (C, Set_Env_Value, "__gnat_setenv");
2447 F_Name (1 .. Name'Length) := Name;
2448 F_Name (F_Name'Last) := ASCII.NUL;
2450 F_Value (1 .. Value'Length) := Value;
2451 F_Value (F_Value'Last) := ASCII.NUL;
2453 Set_Env_Value (F_Name'Address, F_Value'Address);
2461 (Program_Name : String;
2462 Args : Argument_List) return Integer
2466 pragma Warnings (Off, Junk);
2468 Spawn_Internal (Program_Name, Args, Result, Junk, Blocking => True);
2473 (Program_Name : String;
2474 Args : Argument_List;
2475 Success : out Boolean)
2478 Success := (Spawn (Program_Name, Args) = 0);
2482 (Program_Name : String;
2483 Args : Argument_List;
2484 Output_File_Descriptor : File_Descriptor;
2485 Return_Code : out Integer;
2486 Err_To_Out : Boolean := True)
2488 Saved_Output : File_Descriptor;
2489 Saved_Error : File_Descriptor := Invalid_FD; -- prevent compiler warning
2492 -- Set standard output and error to the temporary file
2494 Saved_Output := Dup (Standout);
2495 Dup2 (Output_File_Descriptor, Standout);
2498 Saved_Error := Dup (Standerr);
2499 Dup2 (Output_File_Descriptor, Standerr);
2502 -- Spawn the program
2504 Return_Code := Spawn (Program_Name, Args);
2506 -- Restore the standard output and error
2508 Dup2 (Saved_Output, Standout);
2511 Dup2 (Saved_Error, Standerr);
2514 -- And close the saved standard output and error file descriptors
2516 Close (Saved_Output);
2519 Close (Saved_Error);
2524 (Program_Name : String;
2525 Args : Argument_List;
2526 Output_File : String;
2527 Success : out Boolean;
2528 Return_Code : out Integer;
2529 Err_To_Out : Boolean := True)
2531 FD : File_Descriptor;
2537 FD := Create_Output_Text_File (Output_File);
2539 if FD = Invalid_FD then
2544 Spawn (Program_Name, Args, FD, Return_Code, Err_To_Out);
2546 Close (FD, Success);
2549 --------------------
2550 -- Spawn_Internal --
2551 --------------------
2553 procedure Spawn_Internal
2554 (Program_Name : String;
2555 Args : Argument_List;
2556 Result : out Integer;
2557 Pid : out Process_Id;
2561 procedure Spawn (Args : Argument_List);
2562 -- Call Spawn with given argument list
2564 N_Args : Argument_List (Args'Range);
2565 -- Normalized arguments
2571 procedure Spawn (Args : Argument_List) is
2572 type Chars is array (Positive range <>) of aliased Character;
2573 type Char_Ptr is access constant Character;
2575 Command_Len : constant Positive := Program_Name'Length + 1
2576 + Args_Length (Args);
2577 Command_Last : Natural := 0;
2578 Command : aliased Chars (1 .. Command_Len);
2579 -- Command contains all characters of the Program_Name and Args, all
2580 -- terminated by ASCII.NUL characters.
2582 Arg_List_Len : constant Positive := Args'Length + 2;
2583 Arg_List_Last : Natural := 0;
2584 Arg_List : aliased array (1 .. Arg_List_Len) of Char_Ptr;
2585 -- List with pointers to NUL-terminated strings of the Program_Name
2586 -- and the Args and terminated with a null pointer. We rely on the
2587 -- default initialization for the last null pointer.
2589 procedure Add_To_Command (S : String);
2590 -- Add S and a NUL character to Command, updating Last
2592 function Portable_Spawn (Args : Address) return Integer;
2593 pragma Import (C, Portable_Spawn, "__gnat_portable_spawn");
2595 function Portable_No_Block_Spawn (Args : Address) return Process_Id;
2597 (C, Portable_No_Block_Spawn, "__gnat_portable_no_block_spawn");
2599 --------------------
2600 -- Add_To_Command --
2601 --------------------
2603 procedure Add_To_Command (S : String) is
2604 First : constant Natural := Command_Last + 1;
2607 Command_Last := Command_Last + S'Length;
2609 -- Move characters one at a time, because Command has aliased
2612 -- But not volatile, so why is this necessary ???
2614 for J in S'Range loop
2615 Command (First + J - S'First) := S (J);
2618 Command_Last := Command_Last + 1;
2619 Command (Command_Last) := ASCII.NUL;
2621 Arg_List_Last := Arg_List_Last + 1;
2622 Arg_List (Arg_List_Last) := Command (First)'Access;
2625 -- Start of processing for Spawn
2628 Add_To_Command (Program_Name);
2630 for J in Args'Range loop
2631 Add_To_Command (Args (J).all);
2636 Result := Portable_Spawn (Arg_List'Address);
2638 Pid := Portable_No_Block_Spawn (Arg_List'Address);
2639 Result := Boolean'Pos (Pid /= Invalid_Pid);
2643 -- Start of processing for Spawn_Internal
2646 -- Copy arguments into a local structure
2648 for K in N_Args'Range loop
2649 N_Args (K) := new String'(Args (K).all);
2652 -- Normalize those arguments
2654 Normalize_Arguments (N_Args);
2656 -- Call spawn using the normalized arguments
2660 -- Free arguments list
2662 for K in N_Args'Range loop
2667 ---------------------------
2668 -- To_Path_String_Access --
2669 ---------------------------
2671 function To_Path_String_Access
2672 (Path_Addr : Address;
2673 Path_Len : Integer) return String_Access
2675 subtype Path_String is String (1 .. Path_Len);
2676 type Path_String_Access is access Path_String;
2678 function Address_To_Access is new Ada.Unchecked_Conversion
2679 (Source => Address, Target => Path_String_Access);
2681 Path_Access : constant Path_String_Access :=
2682 Address_To_Access (Path_Addr);
2684 Return_Val : String_Access;
2687 Return_Val := new String (1 .. Path_Len);
2689 for J in 1 .. Path_Len loop
2690 Return_Val (J) := Path_Access (J);
2694 end To_Path_String_Access;
2700 procedure Wait_Process (Pid : out Process_Id; Success : out Boolean) is
2703 function Portable_Wait (S : Address) return Process_Id;
2704 pragma Import (C, Portable_Wait, "__gnat_portable_wait");
2707 Pid := Portable_Wait (Status'Address);
2708 Success := (Status = 0);
2716 (FD : File_Descriptor;
2718 N : Integer) return Integer
2721 return Integer (System.CRTL.write
2722 (System.CRTL.int (FD), System.CRTL.chars (A), System.CRTL.int (N)));