1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . O S _ L I B --
9 -- Copyright (C) 1995-2008, 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 concatenated 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);
456 -- Do not clobber destination file if source file could not be opened
458 if From /= Invalid_FD then
459 To := Create_File (To_Name, Binary);
466 C_From (1 .. Name'Length) := Name;
467 C_From (C_From'Last) := ASCII.NUL;
469 C_To (1 .. To_Name'Length) := To_Name;
470 C_To (C_To'Last) := ASCII.NUL;
475 if Copy_Attributes (C_From'Address, C_To'Address, 0) = -1 then
480 if Copy_Attributes (C_From'Address, C_To'Address, 1) = -1 then
490 -- Start of processing for Copy_File
495 -- The source file must exist
497 if not Is_Regular_File (Name) then
501 -- The source file exists
505 -- Copy case, target file must not exist
509 -- If the target file exists, we have an error
511 if Is_Regular_File (Pathname) then
514 -- Case of target is a directory
516 elsif Is_Directory (Pathname) then
518 Dest : constant String := Build_Path (Pathname, Name);
521 -- If target file exists, we have an error, else do copy
523 if Is_Regular_File (Dest) then
530 -- Case of normal copy to file (destination does not exist)
536 -- Overwrite case (destination file may or may not exist)
539 if Is_Directory (Pathname) then
540 Copy_To (Build_Path (Pathname, Name));
545 -- Append case (destination file may or may not exist)
549 -- Appending to existing file
551 if Is_Regular_File (Pathname) then
553 -- Append mode and destination file exists, append data at the
554 -- end of Pathname. But if we fail to open source file, do not
555 -- touch destination file at all.
557 From := Open_Read (Name, Binary);
558 if From /= Invalid_FD then
559 To := Open_Read_Write (Pathname, Binary);
562 Lseek (To, 0, Seek_End);
566 -- Appending to directory, not allowed
568 elsif Is_Directory (Pathname) then
571 -- Appending when target file does not exist
578 -- All error cases are caught here
587 Pathname : C_File_Name;
588 Success : out Boolean;
589 Mode : Copy_Mode := Copy;
590 Preserve : Attribute := Time_Stamps)
592 Ada_Name : String_Access :=
593 To_Path_String_Access
594 (Name, C_String_Length (Name));
595 Ada_Pathname : String_Access :=
596 To_Path_String_Access
597 (Pathname, C_String_Length (Pathname));
599 Copy_File (Ada_Name.all, Ada_Pathname.all, Success, Mode, Preserve);
604 ----------------------
605 -- Copy_Time_Stamps --
606 ----------------------
608 procedure Copy_Time_Stamps (Source, Dest : String; Success : out Boolean) is
610 function Copy_Attributes
611 (From, To : System.Address;
612 Mode : Integer) return Integer;
613 pragma Import (C, Copy_Attributes, "__gnat_copy_attribs");
614 -- Mode = 0 - copy only time stamps.
615 -- Mode = 1 - copy time stamps and read/write/execute attributes
618 if Is_Regular_File (Source) and then Is_Writable_File (Dest) then
620 C_Source : String (1 .. Source'Length + 1);
621 C_Dest : String (1 .. Dest'Length + 1);
624 C_Source (1 .. Source'Length) := Source;
625 C_Source (C_Source'Last) := ASCII.NUL;
627 C_Dest (1 .. Dest'Length) := Dest;
628 C_Dest (C_Dest'Last) := ASCII.NUL;
630 if Copy_Attributes (C_Source'Address, C_Dest'Address, 0) = -1 then
640 end Copy_Time_Stamps;
642 procedure Copy_Time_Stamps
643 (Source, Dest : C_File_Name;
644 Success : out Boolean)
646 Ada_Source : String_Access :=
647 To_Path_String_Access
648 (Source, C_String_Length (Source));
649 Ada_Dest : String_Access :=
650 To_Path_String_Access
651 (Dest, C_String_Length (Dest));
653 Copy_Time_Stamps (Ada_Source.all, Ada_Dest.all, Success);
656 end Copy_Time_Stamps;
664 Fmode : Mode) return File_Descriptor
666 function C_Create_File
668 Fmode : Mode) return File_Descriptor;
669 pragma Import (C, C_Create_File, "__gnat_open_create");
672 return C_Create_File (Name, Fmode);
677 Fmode : Mode) return File_Descriptor
679 C_Name : String (1 .. Name'Length + 1);
682 C_Name (1 .. Name'Length) := Name;
683 C_Name (C_Name'Last) := ASCII.NUL;
684 return Create_File (C_Name (C_Name'First)'Address, Fmode);
687 ---------------------
688 -- Create_New_File --
689 ---------------------
691 function Create_New_File
693 Fmode : Mode) return File_Descriptor
695 function C_Create_New_File
697 Fmode : Mode) return File_Descriptor;
698 pragma Import (C, C_Create_New_File, "__gnat_open_new");
701 return C_Create_New_File (Name, Fmode);
704 function Create_New_File
706 Fmode : Mode) return File_Descriptor
708 C_Name : String (1 .. Name'Length + 1);
711 C_Name (1 .. Name'Length) := Name;
712 C_Name (C_Name'Last) := ASCII.NUL;
713 return Create_New_File (C_Name (C_Name'First)'Address, Fmode);
716 -----------------------------
717 -- Create_Output_Text_File --
718 -----------------------------
720 function Create_Output_Text_File (Name : String) return File_Descriptor is
721 function C_Create_File
722 (Name : C_File_Name) return File_Descriptor;
723 pragma Import (C, C_Create_File, "__gnat_create_output_file");
725 C_Name : String (1 .. Name'Length + 1);
728 C_Name (1 .. Name'Length) := Name;
729 C_Name (C_Name'Last) := ASCII.NUL;
730 return C_Create_File (C_Name (C_Name'First)'Address);
731 end Create_Output_Text_File;
733 ----------------------
734 -- Create_Temp_File --
735 ----------------------
737 procedure Create_Temp_File
738 (FD : out File_Descriptor;
739 Name : out Temp_File_Name)
741 function Open_New_Temp
742 (Name : System.Address;
743 Fmode : Mode) return File_Descriptor;
744 pragma Import (C, Open_New_Temp, "__gnat_open_new_temp");
747 FD := Open_New_Temp (Name'Address, Binary);
748 end Create_Temp_File;
750 procedure Create_Temp_File
751 (FD : out File_Descriptor;
752 Name : out String_Access)
755 Attempts : Natural := 0;
756 Current : String (Current_Temp_File_Name'Range);
759 -- Loop until a new temp file can be created
763 -- We need to protect global variable Current_Temp_File_Name
764 -- against concurrent access by different tasks.
768 -- Start at the last digit
770 Pos := Temp_File_Name_Last_Digit;
774 -- Increment the digit by one
776 case Current_Temp_File_Name (Pos) is
778 Current_Temp_File_Name (Pos) :=
779 Character'Succ (Current_Temp_File_Name (Pos));
784 -- For 9, set the digit to 0 and go to the previous digit
786 Current_Temp_File_Name (Pos) := '0';
791 -- If it is not a digit, then there are no available
792 -- temp file names. Return Invalid_FD. There is almost
793 -- no chance that this code will be ever be executed,
794 -- since it would mean that there are one million temp
795 -- files in the same directory!
804 Current := Current_Temp_File_Name;
806 -- We can now release the lock, because we are no longer
807 -- accessing Current_Temp_File_Name.
817 -- Attempt to create the file
819 FD := Create_New_File (Current, Binary);
821 if FD /= Invalid_FD then
822 Name := new String'(Current);
826 if not Is_Regular_File (Current) then
828 -- If the file does not already exist and we are unable to create
829 -- it, we give up after Max_Attempts. Otherwise, we try again with
830 -- the next available file name.
832 Attempts := Attempts + 1;
834 if Attempts >= Max_Attempts then
841 end Create_Temp_File;
847 procedure Delete_File (Name : Address; Success : out Boolean) is
850 function unlink (A : Address) return Integer;
851 pragma Import (C, unlink, "unlink");
858 procedure Delete_File (Name : String; Success : out Boolean) is
859 C_Name : String (1 .. Name'Length + 1);
862 C_Name (1 .. Name'Length) := Name;
863 C_Name (C_Name'Last) := ASCII.NUL;
865 Delete_File (C_Name'Address, Success);
868 ---------------------
869 -- File_Time_Stamp --
870 ---------------------
872 function File_Time_Stamp (FD : File_Descriptor) return OS_Time is
873 function File_Time (FD : File_Descriptor) return OS_Time;
874 pragma Import (C, File_Time, "__gnat_file_time_fd");
876 return File_Time (FD);
879 function File_Time_Stamp (Name : C_File_Name) return OS_Time is
880 function File_Time (Name : Address) return OS_Time;
881 pragma Import (C, File_Time, "__gnat_file_time_name");
883 return File_Time (Name);
886 function File_Time_Stamp (Name : String) return OS_Time is
887 F_Name : String (1 .. Name'Length + 1);
889 F_Name (1 .. Name'Length) := Name;
890 F_Name (F_Name'Last) := ASCII.NUL;
891 return File_Time_Stamp (F_Name'Address);
894 ---------------------------
895 -- Get_Debuggable_Suffix --
896 ---------------------------
898 function Get_Debuggable_Suffix return String_Access is
899 procedure Get_Suffix_Ptr (Length, Ptr : Address);
900 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_debuggable_suffix_ptr");
902 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
903 pragma Import (C, Strncpy, "strncpy");
905 Suffix_Ptr : Address;
906 Suffix_Length : Integer;
907 Result : String_Access;
910 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
912 Result := new String (1 .. Suffix_Length);
914 if Suffix_Length > 0 then
915 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
919 end Get_Debuggable_Suffix;
921 ---------------------------
922 -- Get_Executable_Suffix --
923 ---------------------------
925 function Get_Executable_Suffix return String_Access is
926 procedure Get_Suffix_Ptr (Length, Ptr : Address);
927 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_executable_suffix_ptr");
929 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
930 pragma Import (C, Strncpy, "strncpy");
932 Suffix_Ptr : Address;
933 Suffix_Length : Integer;
934 Result : String_Access;
937 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
939 Result := new String (1 .. Suffix_Length);
941 if Suffix_Length > 0 then
942 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
946 end Get_Executable_Suffix;
948 -----------------------
949 -- Get_Object_Suffix --
950 -----------------------
952 function Get_Object_Suffix return String_Access is
953 procedure Get_Suffix_Ptr (Length, Ptr : Address);
954 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_object_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_Object_Suffix;
975 ----------------------------------
976 -- Get_Target_Debuggable_Suffix --
977 ----------------------------------
979 function Get_Target_Debuggable_Suffix return String_Access is
980 Target_Exec_Ext_Ptr : Address;
982 (C, Target_Exec_Ext_Ptr, "__gnat_target_debuggable_extension");
984 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
985 pragma Import (C, Strncpy, "strncpy");
987 function Strlen (Cstring : Address) return Integer;
988 pragma Import (C, Strlen, "strlen");
990 Suffix_Length : Integer;
991 Result : String_Access;
994 Suffix_Length := Strlen (Target_Exec_Ext_Ptr);
996 Result := new String (1 .. Suffix_Length);
998 if Suffix_Length > 0 then
999 Strncpy (Result.all'Address, Target_Exec_Ext_Ptr, Suffix_Length);
1003 end Get_Target_Debuggable_Suffix;
1005 ----------------------------------
1006 -- Get_Target_Executable_Suffix --
1007 ----------------------------------
1009 function Get_Target_Executable_Suffix return String_Access is
1010 Target_Exec_Ext_Ptr : Address;
1012 (C, Target_Exec_Ext_Ptr, "__gnat_target_executable_extension");
1014 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1015 pragma Import (C, Strncpy, "strncpy");
1017 function Strlen (Cstring : Address) return Integer;
1018 pragma Import (C, Strlen, "strlen");
1020 Suffix_Length : Integer;
1021 Result : String_Access;
1024 Suffix_Length := Strlen (Target_Exec_Ext_Ptr);
1026 Result := new String (1 .. Suffix_Length);
1028 if Suffix_Length > 0 then
1029 Strncpy (Result.all'Address, Target_Exec_Ext_Ptr, Suffix_Length);
1033 end Get_Target_Executable_Suffix;
1035 ------------------------------
1036 -- Get_Target_Object_Suffix --
1037 ------------------------------
1039 function Get_Target_Object_Suffix return String_Access is
1040 Target_Object_Ext_Ptr : Address;
1042 (C, Target_Object_Ext_Ptr, "__gnat_target_object_extension");
1044 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1045 pragma Import (C, Strncpy, "strncpy");
1047 function Strlen (Cstring : Address) return Integer;
1048 pragma Import (C, Strlen, "strlen");
1050 Suffix_Length : Integer;
1051 Result : String_Access;
1054 Suffix_Length := Strlen (Target_Object_Ext_Ptr);
1056 Result := new String (1 .. Suffix_Length);
1058 if Suffix_Length > 0 then
1059 Strncpy (Result.all'Address, Target_Object_Ext_Ptr, Suffix_Length);
1063 end Get_Target_Object_Suffix;
1069 function Getenv (Name : String) return String_Access is
1070 procedure Get_Env_Value_Ptr (Name, Length, Ptr : Address);
1071 pragma Import (C, Get_Env_Value_Ptr, "__gnat_getenv");
1073 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1074 pragma Import (C, Strncpy, "strncpy");
1076 Env_Value_Ptr : aliased Address;
1077 Env_Value_Length : aliased Integer;
1078 F_Name : aliased String (1 .. Name'Length + 1);
1079 Result : String_Access;
1082 F_Name (1 .. Name'Length) := Name;
1083 F_Name (F_Name'Last) := ASCII.NUL;
1086 (F_Name'Address, Env_Value_Length'Address, Env_Value_Ptr'Address);
1088 Result := new String (1 .. Env_Value_Length);
1090 if Env_Value_Length > 0 then
1091 Strncpy (Result.all'Address, Env_Value_Ptr, Env_Value_Length);
1101 function GM_Day (Date : OS_Time) return Day_Type is
1104 pragma Warnings (Off);
1110 pragma Warnings (On);
1113 GM_Split (Date, Y, Mo, D, H, Mn, S);
1121 function GM_Hour (Date : OS_Time) return Hour_Type is
1124 pragma Warnings (Off);
1130 pragma Warnings (On);
1133 GM_Split (Date, Y, Mo, D, H, Mn, S);
1141 function GM_Minute (Date : OS_Time) return Minute_Type is
1144 pragma Warnings (Off);
1150 pragma Warnings (On);
1153 GM_Split (Date, Y, Mo, D, H, Mn, S);
1161 function GM_Month (Date : OS_Time) return Month_Type is
1164 pragma Warnings (Off);
1170 pragma Warnings (On);
1173 GM_Split (Date, Y, Mo, D, H, Mn, S);
1181 function GM_Second (Date : OS_Time) return Second_Type is
1184 pragma Warnings (Off);
1190 pragma Warnings (On);
1193 GM_Split (Date, Y, Mo, D, H, Mn, S);
1203 Year : out Year_Type;
1204 Month : out Month_Type;
1206 Hour : out Hour_Type;
1207 Minute : out Minute_Type;
1208 Second : out Second_Type)
1210 procedure To_GM_Time
1211 (P_Time_T, P_Year, P_Month, P_Day, P_Hours, P_Mins, P_Secs : Address);
1212 pragma Import (C, To_GM_Time, "__gnat_to_gm_time");
1214 T : OS_Time := Date;
1223 -- Use the global lock because To_GM_Time is not thread safe
1225 Locked_Processing : begin
1228 (T'Address, Y'Address, Mo'Address, D'Address,
1229 H'Address, Mn'Address, S'Address);
1230 SSL.Unlock_Task.all;
1234 SSL.Unlock_Task.all;
1236 end Locked_Processing;
1250 function GM_Year (Date : OS_Time) return Year_Type is
1253 pragma Warnings (Off);
1259 pragma Warnings (On);
1262 GM_Split (Date, Y, Mo, D, H, Mn, S);
1266 ----------------------
1267 -- Is_Absolute_Path --
1268 ----------------------
1270 function Is_Absolute_Path (Name : String) return Boolean is
1271 function Is_Absolute_Path
1273 Length : Integer) return Integer;
1274 pragma Import (C, Is_Absolute_Path, "__gnat_is_absolute_path");
1276 return Is_Absolute_Path (Name'Address, Name'Length) /= 0;
1277 end Is_Absolute_Path;
1283 function Is_Directory (Name : C_File_Name) return Boolean is
1284 function Is_Directory (Name : Address) return Integer;
1285 pragma Import (C, Is_Directory, "__gnat_is_directory");
1287 return Is_Directory (Name) /= 0;
1290 function Is_Directory (Name : String) return Boolean is
1291 F_Name : String (1 .. Name'Length + 1);
1293 F_Name (1 .. Name'Length) := Name;
1294 F_Name (F_Name'Last) := ASCII.NUL;
1295 return Is_Directory (F_Name'Address);
1298 ----------------------
1299 -- Is_Readable_File --
1300 ----------------------
1302 function Is_Readable_File (Name : C_File_Name) return Boolean is
1303 function Is_Readable_File (Name : Address) return Integer;
1304 pragma Import (C, Is_Readable_File, "__gnat_is_readable_file");
1306 return Is_Readable_File (Name) /= 0;
1307 end Is_Readable_File;
1309 function Is_Readable_File (Name : String) return Boolean is
1310 F_Name : String (1 .. Name'Length + 1);
1312 F_Name (1 .. Name'Length) := Name;
1313 F_Name (F_Name'Last) := ASCII.NUL;
1314 return Is_Readable_File (F_Name'Address);
1315 end Is_Readable_File;
1317 ------------------------
1318 -- Is_Executable_File --
1319 ------------------------
1321 function Is_Executable_File (Name : C_File_Name) return Boolean is
1322 function Is_Executable_File (Name : Address) return Integer;
1323 pragma Import (C, Is_Executable_File, "__gnat_is_executable_file");
1325 return Is_Executable_File (Name) /= 0;
1326 end Is_Executable_File;
1328 function Is_Executable_File (Name : String) return Boolean is
1329 F_Name : String (1 .. Name'Length + 1);
1331 F_Name (1 .. Name'Length) := Name;
1332 F_Name (F_Name'Last) := ASCII.NUL;
1333 return Is_Executable_File (F_Name'Address);
1334 end Is_Executable_File;
1336 ---------------------
1337 -- Is_Regular_File --
1338 ---------------------
1340 function Is_Regular_File (Name : C_File_Name) return Boolean is
1341 function Is_Regular_File (Name : Address) return Integer;
1342 pragma Import (C, Is_Regular_File, "__gnat_is_regular_file");
1344 return Is_Regular_File (Name) /= 0;
1345 end Is_Regular_File;
1347 function Is_Regular_File (Name : String) return Boolean is
1348 F_Name : String (1 .. Name'Length + 1);
1350 F_Name (1 .. Name'Length) := Name;
1351 F_Name (F_Name'Last) := ASCII.NUL;
1352 return Is_Regular_File (F_Name'Address);
1353 end Is_Regular_File;
1355 ----------------------
1356 -- Is_Symbolic_Link --
1357 ----------------------
1359 function Is_Symbolic_Link (Name : C_File_Name) return Boolean is
1360 function Is_Symbolic_Link (Name : Address) return Integer;
1361 pragma Import (C, Is_Symbolic_Link, "__gnat_is_symbolic_link");
1363 return Is_Symbolic_Link (Name) /= 0;
1364 end Is_Symbolic_Link;
1366 function Is_Symbolic_Link (Name : String) return Boolean is
1367 F_Name : String (1 .. Name'Length + 1);
1369 F_Name (1 .. Name'Length) := Name;
1370 F_Name (F_Name'Last) := ASCII.NUL;
1371 return Is_Symbolic_Link (F_Name'Address);
1372 end Is_Symbolic_Link;
1374 ----------------------
1375 -- Is_Writable_File --
1376 ----------------------
1378 function Is_Writable_File (Name : C_File_Name) return Boolean is
1379 function Is_Writable_File (Name : Address) return Integer;
1380 pragma Import (C, Is_Writable_File, "__gnat_is_writable_file");
1382 return Is_Writable_File (Name) /= 0;
1383 end Is_Writable_File;
1385 function Is_Writable_File (Name : String) return Boolean is
1386 F_Name : String (1 .. Name'Length + 1);
1388 F_Name (1 .. Name'Length) := Name;
1389 F_Name (F_Name'Last) := ASCII.NUL;
1390 return Is_Writable_File (F_Name'Address);
1391 end Is_Writable_File;
1393 -------------------------
1394 -- Locate_Exec_On_Path --
1395 -------------------------
1397 function Locate_Exec_On_Path
1398 (Exec_Name : String) return String_Access
1400 function Locate_Exec_On_Path (C_Exec_Name : Address) return Address;
1401 pragma Import (C, Locate_Exec_On_Path, "__gnat_locate_exec_on_path");
1403 procedure Free (Ptr : System.Address);
1404 pragma Import (C, Free, "free");
1406 C_Exec_Name : String (1 .. Exec_Name'Length + 1);
1407 Path_Addr : Address;
1409 Result : String_Access;
1412 C_Exec_Name (1 .. Exec_Name'Length) := Exec_Name;
1413 C_Exec_Name (C_Exec_Name'Last) := ASCII.NUL;
1415 Path_Addr := Locate_Exec_On_Path (C_Exec_Name'Address);
1416 Path_Len := C_String_Length (Path_Addr);
1418 if Path_Len = 0 then
1422 Result := To_Path_String_Access (Path_Addr, Path_Len);
1425 -- Always return an absolute path name
1427 if not Is_Absolute_Path (Result.all) then
1429 Absolute_Path : constant String :=
1430 Normalize_Pathname (Result.all);
1433 Result := new String'(Absolute_Path);
1439 end Locate_Exec_On_Path;
1441 -------------------------
1442 -- Locate_Regular_File --
1443 -------------------------
1445 function Locate_Regular_File
1446 (File_Name : C_File_Name;
1447 Path : C_File_Name) return String_Access
1449 function Locate_Regular_File
1450 (C_File_Name, Path_Val : Address) return Address;
1451 pragma Import (C, Locate_Regular_File, "__gnat_locate_regular_file");
1453 procedure Free (Ptr : System.Address);
1454 pragma Import (C, Free, "free");
1456 Path_Addr : Address;
1458 Result : String_Access;
1461 Path_Addr := Locate_Regular_File (File_Name, Path);
1462 Path_Len := C_String_Length (Path_Addr);
1464 if Path_Len = 0 then
1468 Result := To_Path_String_Access (Path_Addr, Path_Len);
1472 end Locate_Regular_File;
1474 function Locate_Regular_File
1475 (File_Name : String;
1476 Path : String) return String_Access
1478 C_File_Name : String (1 .. File_Name'Length + 1);
1479 C_Path : String (1 .. Path'Length + 1);
1480 Result : String_Access;
1483 C_File_Name (1 .. File_Name'Length) := File_Name;
1484 C_File_Name (C_File_Name'Last) := ASCII.NUL;
1486 C_Path (1 .. Path'Length) := Path;
1487 C_Path (C_Path'Last) := ASCII.NUL;
1489 Result := Locate_Regular_File (C_File_Name'Address, C_Path'Address);
1491 -- Always return an absolute path name
1493 if Result /= null and then not Is_Absolute_Path (Result.all) then
1495 Absolute_Path : constant String := Normalize_Pathname (Result.all);
1498 Result := new String'(Absolute_Path);
1503 end Locate_Regular_File;
1505 ------------------------
1506 -- Non_Blocking_Spawn --
1507 ------------------------
1509 function Non_Blocking_Spawn
1510 (Program_Name : String;
1511 Args : Argument_List) return Process_Id
1515 pragma Warnings (Off, Junk);
1517 Spawn_Internal (Program_Name, Args, Junk, Pid, Blocking => False);
1519 end Non_Blocking_Spawn;
1521 function Non_Blocking_Spawn
1522 (Program_Name : String;
1523 Args : Argument_List;
1524 Output_File_Descriptor : File_Descriptor;
1525 Err_To_Out : Boolean := True) return Process_Id
1527 Saved_Output : File_Descriptor;
1528 Saved_Error : File_Descriptor := Invalid_FD; -- prevent warning
1532 if Output_File_Descriptor = Invalid_FD then
1536 -- Set standard output and, if specified, error to the temporary file
1538 Saved_Output := Dup (Standout);
1539 Dup2 (Output_File_Descriptor, Standout);
1542 Saved_Error := Dup (Standerr);
1543 Dup2 (Output_File_Descriptor, Standerr);
1546 -- Spawn the program
1548 Pid := Non_Blocking_Spawn (Program_Name, Args);
1550 -- Restore the standard output and error
1552 Dup2 (Saved_Output, Standout);
1555 Dup2 (Saved_Error, Standerr);
1558 -- And close the saved standard output and error file descriptors
1560 Close (Saved_Output);
1563 Close (Saved_Error);
1567 end Non_Blocking_Spawn;
1569 function Non_Blocking_Spawn
1570 (Program_Name : String;
1571 Args : Argument_List;
1572 Output_File : String;
1573 Err_To_Out : Boolean := True) return Process_Id
1575 Output_File_Descriptor : constant File_Descriptor :=
1576 Create_Output_Text_File (Output_File);
1577 Result : Process_Id;
1580 -- Do not attempt to spawn if the output file could not be created
1582 if Output_File_Descriptor = Invalid_FD then
1586 Result := Non_Blocking_Spawn
1587 (Program_Name, Args, Output_File_Descriptor, Err_To_Out);
1589 -- Close the file just created for the output, as the file descriptor
1590 -- cannot be used anywhere, being a local value. It is safe to do
1591 -- that, as the file descriptor has been duplicated to form
1592 -- standard output and error of the spawned process.
1594 Close (Output_File_Descriptor);
1598 end Non_Blocking_Spawn;
1600 -------------------------
1601 -- Normalize_Arguments --
1602 -------------------------
1604 procedure Normalize_Arguments (Args : in out Argument_List) is
1606 procedure Quote_Argument (Arg : in out String_Access);
1607 -- Add quote around argument if it contains spaces
1609 C_Argument_Needs_Quote : Integer;
1610 pragma Import (C, C_Argument_Needs_Quote, "__gnat_argument_needs_quote");
1611 Argument_Needs_Quote : constant Boolean := C_Argument_Needs_Quote /= 0;
1613 --------------------
1614 -- Quote_Argument --
1615 --------------------
1617 procedure Quote_Argument (Arg : in out String_Access) is
1618 Res : String (1 .. Arg'Length * 2);
1620 Quote_Needed : Boolean := False;
1623 if Arg (Arg'First) /= '"' or else Arg (Arg'Last) /= '"' then
1629 for K in Arg'Range loop
1633 if Arg (K) = '"' then
1637 Quote_Needed := True;
1639 elsif Arg (K) = ' ' then
1641 Quote_Needed := True;
1649 if Quote_Needed then
1651 -- If null terminated string, put the quote before
1653 if Res (J) = ASCII.NUL then
1656 Res (J) := ASCII.NUL;
1658 -- If argument is terminated by '\', then double it. Otherwise
1659 -- the ending quote will be taken as-is. This is quite strange
1660 -- spawn behavior from Windows, but this is what we see!
1663 if Res (J) = '\' then
1675 Old : String_Access := Arg;
1678 Arg := new String'(Res (1 .. J));
1686 -- Start of processing for Normalize_Arguments
1689 if Argument_Needs_Quote then
1690 for K in Args'Range loop
1691 if Args (K) /= null and then Args (K)'Length /= 0 then
1692 Quote_Argument (Args (K));
1696 end Normalize_Arguments;
1698 ------------------------
1699 -- Normalize_Pathname --
1700 ------------------------
1702 function Normalize_Pathname
1704 Directory : String := "";
1705 Resolve_Links : Boolean := True;
1706 Case_Sensitive : Boolean := True) return String
1709 pragma Import (C, Max_Path, "__gnat_max_path_len");
1710 -- Maximum length of a path name
1712 procedure Get_Current_Dir
1713 (Dir : System.Address;
1714 Length : System.Address);
1715 pragma Import (C, Get_Current_Dir, "__gnat_get_current_dir");
1717 Path_Buffer : String (1 .. Max_Path + Max_Path + 2);
1718 End_Path : Natural := 0;
1719 Link_Buffer : String (1 .. Max_Path + 2);
1725 Max_Iterations : constant := 500;
1727 function Get_File_Names_Case_Sensitive return Integer;
1729 (C, Get_File_Names_Case_Sensitive,
1730 "__gnat_get_file_names_case_sensitive");
1732 Fold_To_Lower_Case : constant Boolean :=
1734 and then Get_File_Names_Case_Sensitive = 0;
1737 (Path : System.Address;
1738 Buf : System.Address;
1739 Bufsiz : Integer) return Integer;
1740 pragma Import (C, Readlink, "__gnat_readlink");
1742 function To_Canonical_File_Spec
1743 (Host_File : System.Address) return System.Address;
1745 (C, To_Canonical_File_Spec, "__gnat_to_canonical_file_spec");
1747 The_Name : String (1 .. Name'Length + 1);
1748 Canonical_File_Addr : System.Address;
1749 Canonical_File_Len : Integer;
1751 function Strlen (S : System.Address) return Integer;
1752 pragma Import (C, Strlen, "strlen");
1754 function Final_Value (S : String) return String;
1755 -- Make final adjustment to the returned string. This function strips
1756 -- trailing directory separators, and folds returned string to lower
1757 -- case if required.
1759 function Get_Directory (Dir : String) return String;
1760 -- If Dir is not empty, return it, adding a directory separator
1761 -- if not already present, otherwise return current working directory
1762 -- with terminating directory separator.
1768 function Final_Value (S : String) return String is
1770 -- We may need to fold S to lower case, so we need a variable
1775 if Fold_To_Lower_Case then
1776 System.Case_Util.To_Lower (S1);
1779 -- Remove trailing directory separator, if any
1784 and then (S1 (Last) = '/'
1786 S1 (Last) = Directory_Separator)
1788 -- Special case for Windows: C:\
1791 and then S1 (1) /= Directory_Separator
1792 and then S1 (2) = ':'
1801 return S1 (1 .. Last);
1808 function Get_Directory (Dir : String) return String is
1810 -- Directory given, add directory separator if needed
1812 if Dir'Length > 0 then
1813 if Dir (Dir'Last) = Directory_Separator then
1817 Result : String (1 .. Dir'Length + 1);
1819 Result (1 .. Dir'Length) := Dir;
1820 Result (Result'Length) := Directory_Separator;
1825 -- Directory name not given, get current directory
1829 Buffer : String (1 .. Max_Path + 2);
1830 Path_Len : Natural := Max_Path;
1833 Get_Current_Dir (Buffer'Address, Path_Len'Address);
1835 if Buffer (Path_Len) /= Directory_Separator then
1836 Path_Len := Path_Len + 1;
1837 Buffer (Path_Len) := Directory_Separator;
1840 -- By default, the drive letter on Windows is in upper case
1842 if On_Windows and then Path_Len >= 2 and then
1845 System.Case_Util.To_Upper (Buffer (1 .. 1));
1848 return Buffer (1 .. Path_Len);
1853 -- Start of processing for Normalize_Pathname
1856 -- Special case, if name is null, then return null
1858 if Name'Length = 0 then
1862 -- First, convert VMS file spec to Unix file spec.
1863 -- If Name is not in VMS syntax, then this is equivalent
1864 -- to put Name at the beginning of Path_Buffer.
1866 VMS_Conversion : begin
1867 The_Name (1 .. Name'Length) := Name;
1868 The_Name (The_Name'Last) := ASCII.NUL;
1870 Canonical_File_Addr := To_Canonical_File_Spec (The_Name'Address);
1871 Canonical_File_Len := Strlen (Canonical_File_Addr);
1873 -- If VMS syntax conversion has failed, return an empty string
1874 -- to indicate the failure.
1876 if Canonical_File_Len = 0 then
1881 subtype Path_String is String (1 .. Canonical_File_Len);
1882 type Path_String_Access is access Path_String;
1884 function Address_To_Access is new
1885 Ada.Unchecked_Conversion (Source => Address,
1886 Target => Path_String_Access);
1888 Path_Access : constant Path_String_Access :=
1889 Address_To_Access (Canonical_File_Addr);
1892 Path_Buffer (1 .. Canonical_File_Len) := Path_Access.all;
1893 End_Path := Canonical_File_Len;
1898 -- Replace all '/' by Directory Separators (this is for Windows)
1900 if Directory_Separator /= '/' then
1901 for Index in 1 .. End_Path loop
1902 if Path_Buffer (Index) = '/' then
1903 Path_Buffer (Index) := Directory_Separator;
1908 -- Resolve directory names for Windows (formerly also VMS)
1910 -- On VMS, if we have a Unix path such as /temp/..., and TEMP is a
1911 -- logical name, we must not try to resolve this logical name, because
1912 -- it may have multiple equivalences and if resolved we will only
1913 -- get the first one.
1915 -- On Windows, if we have an absolute path starting with a directory
1916 -- separator, we need to have the drive letter appended in front.
1918 -- On Windows, Get_Current_Dir will return a suitable directory
1919 -- name (path starting with a drive letter on Windows). So we take this
1920 -- drive letter and prepend it to the current path.
1923 and then Path_Buffer (1) = Directory_Separator
1924 and then Path_Buffer (2) /= Directory_Separator
1927 Cur_Dir : constant String := Get_Directory ("");
1928 -- Get the current directory to get the drive letter
1931 if Cur_Dir'Length > 2
1932 and then Cur_Dir (Cur_Dir'First + 1) = ':'
1934 Path_Buffer (3 .. End_Path + 2) := Path_Buffer (1 .. End_Path);
1935 Path_Buffer (1 .. 2) :=
1936 Cur_Dir (Cur_Dir'First .. Cur_Dir'First + 1);
1937 End_Path := End_Path + 2;
1942 -- On Windows, remove all double-quotes that are possibly part of the
1943 -- path but can cause problems with other methods.
1950 Index := Path_Buffer'First;
1951 for Current in Path_Buffer'First .. End_Path loop
1952 if Path_Buffer (Current) /= '"' then
1953 Path_Buffer (Index) := Path_Buffer (Current);
1958 End_Path := Index - 1;
1962 -- Start the conversions
1964 -- If this is not finished after Max_Iterations, give up and return an
1967 for J in 1 .. Max_Iterations loop
1969 -- If we don't have an absolute pathname, prepend the directory
1973 and then not Is_Absolute_Path (Path_Buffer (1 .. End_Path))
1976 Reference_Dir : constant String := Get_Directory (Directory);
1977 Ref_Dir_Len : constant Natural := Reference_Dir'Length;
1978 -- Current directory name specified and its length
1981 Path_Buffer (Ref_Dir_Len + 1 .. Ref_Dir_Len + End_Path) :=
1982 Path_Buffer (1 .. End_Path);
1983 End_Path := Ref_Dir_Len + End_Path;
1984 Path_Buffer (1 .. Ref_Dir_Len) := Reference_Dir;
1985 Last := Ref_Dir_Len;
1992 -- Ensure that Windows network drives are kept, e.g: \\server\drive-c
1995 and then Directory_Separator = '\'
1996 and then Path_Buffer (1 .. 2) = "\\"
2001 -- If we have traversed the full pathname, return it
2003 if Start > End_Path then
2004 return Final_Value (Path_Buffer (1 .. End_Path));
2007 -- Remove duplicate directory separators
2009 while Path_Buffer (Start) = Directory_Separator loop
2010 if Start = End_Path then
2011 return Final_Value (Path_Buffer (1 .. End_Path - 1));
2014 Path_Buffer (Start .. End_Path - 1) :=
2015 Path_Buffer (Start + 1 .. End_Path);
2016 End_Path := End_Path - 1;
2020 -- Find the end of the current field: last character or the one
2021 -- preceding the next directory separator.
2023 while Finish < End_Path
2024 and then Path_Buffer (Finish + 1) /= Directory_Separator
2026 Finish := Finish + 1;
2031 if Start = Finish and then Path_Buffer (Start) = '.' then
2032 if Start = End_Path then
2034 return (1 => Directory_Separator);
2037 if Fold_To_Lower_Case then
2038 System.Case_Util.To_Lower (Path_Buffer (1 .. Last - 1));
2041 return Path_Buffer (1 .. Last - 1);
2046 Path_Buffer (Last + 1 .. End_Path - 2) :=
2047 Path_Buffer (Last + 3 .. End_Path);
2048 End_Path := End_Path - 2;
2051 -- Remove ".." fields
2053 elsif Finish = Start + 1
2054 and then Path_Buffer (Start .. Finish) = ".."
2059 exit when Start < 1 or else
2060 Path_Buffer (Start) = Directory_Separator;
2064 if Finish = End_Path then
2065 return (1 => Directory_Separator);
2068 Path_Buffer (1 .. End_Path - Finish) :=
2069 Path_Buffer (Finish + 1 .. End_Path);
2070 End_Path := End_Path - Finish;
2075 if Finish = End_Path then
2076 return Final_Value (Path_Buffer (1 .. Start - 1));
2079 Path_Buffer (Start + 1 .. Start + End_Path - Finish - 1) :=
2080 Path_Buffer (Finish + 2 .. End_Path);
2081 End_Path := Start + End_Path - Finish - 1;
2086 -- Check if current field is a symbolic link
2088 elsif Resolve_Links then
2090 Saved : constant Character := Path_Buffer (Finish + 1);
2093 Path_Buffer (Finish + 1) := ASCII.NUL;
2094 Status := Readlink (Path_Buffer'Address,
2095 Link_Buffer'Address,
2096 Link_Buffer'Length);
2097 Path_Buffer (Finish + 1) := Saved;
2100 -- Not a symbolic link, move to the next field, if any
2105 -- Replace symbolic link with its value
2108 if Is_Absolute_Path (Link_Buffer (1 .. Status)) then
2109 Path_Buffer (Status + 1 .. End_Path - (Finish - Status)) :=
2110 Path_Buffer (Finish + 1 .. End_Path);
2111 End_Path := End_Path - (Finish - Status);
2112 Path_Buffer (1 .. Status) := Link_Buffer (1 .. Status);
2117 (Last + Status + 1 .. End_Path - Finish + Last + Status) :=
2118 Path_Buffer (Finish + 1 .. End_Path);
2119 End_Path := End_Path - Finish + Last + Status;
2120 Path_Buffer (Last + 1 .. Last + Status) :=
2121 Link_Buffer (1 .. Status);
2130 -- Too many iterations: give up
2132 -- This can happen when there is a circularity in the symbolic links: A
2133 -- is a symbolic link for B, which itself is a symbolic link, and the
2134 -- target of B or of another symbolic link target of B is A. In this
2135 -- case, we return an empty string to indicate failure to resolve.
2138 end Normalize_Pathname;
2145 (Name : C_File_Name;
2146 Fmode : Mode) return File_Descriptor
2148 function C_Open_Read
2149 (Name : C_File_Name;
2150 Fmode : Mode) return File_Descriptor;
2151 pragma Import (C, C_Open_Read, "__gnat_open_read");
2153 return C_Open_Read (Name, Fmode);
2158 Fmode : Mode) return File_Descriptor
2160 C_Name : String (1 .. Name'Length + 1);
2162 C_Name (1 .. Name'Length) := Name;
2163 C_Name (C_Name'Last) := ASCII.NUL;
2164 return Open_Read (C_Name (C_Name'First)'Address, Fmode);
2167 ---------------------
2168 -- Open_Read_Write --
2169 ---------------------
2171 function Open_Read_Write
2172 (Name : C_File_Name;
2173 Fmode : Mode) return File_Descriptor
2175 function C_Open_Read_Write
2176 (Name : C_File_Name;
2177 Fmode : Mode) return File_Descriptor;
2178 pragma Import (C, C_Open_Read_Write, "__gnat_open_rw");
2180 return C_Open_Read_Write (Name, Fmode);
2181 end Open_Read_Write;
2183 function Open_Read_Write
2185 Fmode : Mode) return File_Descriptor
2187 C_Name : String (1 .. Name'Length + 1);
2189 C_Name (1 .. Name'Length) := Name;
2190 C_Name (C_Name'Last) := ASCII.NUL;
2191 return Open_Read_Write (C_Name (C_Name'First)'Address, Fmode);
2192 end Open_Read_Write;
2198 procedure OS_Exit (Status : Integer) is
2200 OS_Exit_Ptr (Status);
2201 raise Program_Error;
2204 ---------------------
2205 -- OS_Exit_Default --
2206 ---------------------
2208 procedure OS_Exit_Default (Status : Integer) is
2209 procedure GNAT_OS_Exit (Status : Integer);
2210 pragma Import (C, GNAT_OS_Exit, "__gnat_os_exit");
2211 pragma No_Return (GNAT_OS_Exit);
2213 GNAT_OS_Exit (Status);
2214 end OS_Exit_Default;
2216 --------------------
2217 -- Pid_To_Integer --
2218 --------------------
2220 function Pid_To_Integer (Pid : Process_Id) return Integer is
2222 return Integer (Pid);
2230 (FD : File_Descriptor;
2232 N : Integer) return Integer
2235 return Integer (System.CRTL.read
2236 (System.CRTL.int (FD), System.CRTL.chars (A), System.CRTL.int (N)));
2243 procedure Rename_File
2244 (Old_Name : C_File_Name;
2245 New_Name : C_File_Name;
2246 Success : out Boolean)
2248 function rename (From, To : Address) return Integer;
2249 pragma Import (C, rename, "rename");
2252 R := rename (Old_Name, New_Name);
2256 procedure Rename_File
2259 Success : out Boolean)
2261 C_Old_Name : String (1 .. Old_Name'Length + 1);
2262 C_New_Name : String (1 .. New_Name'Length + 1);
2264 C_Old_Name (1 .. Old_Name'Length) := Old_Name;
2265 C_Old_Name (C_Old_Name'Last) := ASCII.NUL;
2266 C_New_Name (1 .. New_Name'Length) := New_Name;
2267 C_New_Name (C_New_Name'Last) := ASCII.NUL;
2268 Rename_File (C_Old_Name'Address, C_New_Name'Address, Success);
2271 -----------------------
2272 -- Set_Close_On_Exec --
2273 -----------------------
2275 procedure Set_Close_On_Exec
2276 (FD : File_Descriptor;
2277 Close_On_Exec : Boolean;
2278 Status : out Boolean)
2280 function C_Set_Close_On_Exec
2281 (FD : File_Descriptor; Close_On_Exec : System.CRTL.int)
2282 return System.CRTL.int;
2283 pragma Import (C, C_Set_Close_On_Exec, "__gnat_set_close_on_exec");
2285 Status := C_Set_Close_On_Exec (FD, Boolean'Pos (Close_On_Exec)) = 0;
2286 end Set_Close_On_Exec;
2288 --------------------
2289 -- Set_Executable --
2290 --------------------
2292 procedure Set_Executable (Name : String) is
2293 procedure C_Set_Executable (Name : C_File_Name);
2294 pragma Import (C, C_Set_Executable, "__gnat_set_executable");
2295 C_Name : aliased String (Name'First .. Name'Last + 1);
2297 C_Name (Name'Range) := Name;
2298 C_Name (C_Name'Last) := ASCII.NUL;
2299 C_Set_Executable (C_Name (C_Name'First)'Address);
2302 ----------------------
2303 -- Set_Non_Readable --
2304 ----------------------
2306 procedure Set_Non_Readable (Name : String) is
2307 procedure C_Set_Non_Readable (Name : C_File_Name);
2308 pragma Import (C, C_Set_Non_Readable, "__gnat_set_non_readable");
2309 C_Name : aliased String (Name'First .. Name'Last + 1);
2311 C_Name (Name'Range) := Name;
2312 C_Name (C_Name'Last) := ASCII.NUL;
2313 C_Set_Non_Readable (C_Name (C_Name'First)'Address);
2314 end Set_Non_Readable;
2316 ----------------------
2317 -- Set_Non_Writable --
2318 ----------------------
2320 procedure Set_Non_Writable (Name : String) is
2321 procedure C_Set_Non_Writable (Name : C_File_Name);
2322 pragma Import (C, C_Set_Non_Writable, "__gnat_set_non_writable");
2323 C_Name : aliased String (Name'First .. Name'Last + 1);
2325 C_Name (Name'Range) := Name;
2326 C_Name (C_Name'Last) := ASCII.NUL;
2327 C_Set_Non_Writable (C_Name (C_Name'First)'Address);
2328 end Set_Non_Writable;
2334 procedure Set_Readable (Name : String) is
2335 procedure C_Set_Readable (Name : C_File_Name);
2336 pragma Import (C, C_Set_Readable, "__gnat_set_readable");
2337 C_Name : aliased String (Name'First .. Name'Last + 1);
2339 C_Name (Name'Range) := Name;
2340 C_Name (C_Name'Last) := ASCII.NUL;
2341 C_Set_Readable (C_Name (C_Name'First)'Address);
2344 --------------------
2346 --------------------
2348 procedure Set_Writable (Name : String) is
2349 procedure C_Set_Writable (Name : C_File_Name);
2350 pragma Import (C, C_Set_Writable, "__gnat_set_writable");
2351 C_Name : aliased String (Name'First .. Name'Last + 1);
2353 C_Name (Name'Range) := Name;
2354 C_Name (C_Name'Last) := ASCII.NUL;
2355 C_Set_Writable (C_Name (C_Name'First)'Address);
2362 procedure Setenv (Name : String; Value : String) is
2363 F_Name : String (1 .. Name'Length + 1);
2364 F_Value : String (1 .. Value'Length + 1);
2366 procedure Set_Env_Value (Name, Value : System.Address);
2367 pragma Import (C, Set_Env_Value, "__gnat_setenv");
2370 F_Name (1 .. Name'Length) := Name;
2371 F_Name (F_Name'Last) := ASCII.NUL;
2373 F_Value (1 .. Value'Length) := Value;
2374 F_Value (F_Value'Last) := ASCII.NUL;
2376 Set_Env_Value (F_Name'Address, F_Value'Address);
2384 (Program_Name : String;
2385 Args : Argument_List) return Integer
2389 pragma Warnings (Off, Junk);
2391 Spawn_Internal (Program_Name, Args, Result, Junk, Blocking => True);
2396 (Program_Name : String;
2397 Args : Argument_List;
2398 Success : out Boolean)
2401 Success := (Spawn (Program_Name, Args) = 0);
2405 (Program_Name : String;
2406 Args : Argument_List;
2407 Output_File_Descriptor : File_Descriptor;
2408 Return_Code : out Integer;
2409 Err_To_Out : Boolean := True)
2411 Saved_Output : File_Descriptor;
2412 Saved_Error : File_Descriptor := Invalid_FD; -- prevent compiler warning
2415 -- Set standard output and error to the temporary file
2417 Saved_Output := Dup (Standout);
2418 Dup2 (Output_File_Descriptor, Standout);
2421 Saved_Error := Dup (Standerr);
2422 Dup2 (Output_File_Descriptor, Standerr);
2425 -- Spawn the program
2427 Return_Code := Spawn (Program_Name, Args);
2429 -- Restore the standard output and error
2431 Dup2 (Saved_Output, Standout);
2434 Dup2 (Saved_Error, Standerr);
2437 -- And close the saved standard output and error file descriptors
2439 Close (Saved_Output);
2442 Close (Saved_Error);
2447 (Program_Name : String;
2448 Args : Argument_List;
2449 Output_File : String;
2450 Success : out Boolean;
2451 Return_Code : out Integer;
2452 Err_To_Out : Boolean := True)
2454 FD : File_Descriptor;
2460 FD := Create_Output_Text_File (Output_File);
2462 if FD = Invalid_FD then
2467 Spawn (Program_Name, Args, FD, Return_Code, Err_To_Out);
2469 Close (FD, Success);
2472 --------------------
2473 -- Spawn_Internal --
2474 --------------------
2476 procedure Spawn_Internal
2477 (Program_Name : String;
2478 Args : Argument_List;
2479 Result : out Integer;
2480 Pid : out Process_Id;
2484 procedure Spawn (Args : Argument_List);
2485 -- Call Spawn with given argument list
2487 N_Args : Argument_List (Args'Range);
2488 -- Normalized arguments
2494 procedure Spawn (Args : Argument_List) is
2495 type Chars is array (Positive range <>) of aliased Character;
2496 type Char_Ptr is access constant Character;
2498 Command_Len : constant Positive := Program_Name'Length + 1
2499 + Args_Length (Args);
2500 Command_Last : Natural := 0;
2501 Command : aliased Chars (1 .. Command_Len);
2502 -- Command contains all characters of the Program_Name and Args, all
2503 -- terminated by ASCII.NUL characters.
2505 Arg_List_Len : constant Positive := Args'Length + 2;
2506 Arg_List_Last : Natural := 0;
2507 Arg_List : aliased array (1 .. Arg_List_Len) of Char_Ptr;
2508 -- List with pointers to NUL-terminated strings of the Program_Name
2509 -- and the Args and terminated with a null pointer. We rely on the
2510 -- default initialization for the last null pointer.
2512 procedure Add_To_Command (S : String);
2513 -- Add S and a NUL character to Command, updating Last
2515 function Portable_Spawn (Args : Address) return Integer;
2516 pragma Import (C, Portable_Spawn, "__gnat_portable_spawn");
2518 function Portable_No_Block_Spawn (Args : Address) return Process_Id;
2520 (C, Portable_No_Block_Spawn, "__gnat_portable_no_block_spawn");
2522 --------------------
2523 -- Add_To_Command --
2524 --------------------
2526 procedure Add_To_Command (S : String) is
2527 First : constant Natural := Command_Last + 1;
2530 Command_Last := Command_Last + S'Length;
2532 -- Move characters one at a time, because Command has aliased
2535 -- But not volatile, so why is this necessary ???
2537 for J in S'Range loop
2538 Command (First + J - S'First) := S (J);
2541 Command_Last := Command_Last + 1;
2542 Command (Command_Last) := ASCII.NUL;
2544 Arg_List_Last := Arg_List_Last + 1;
2545 Arg_List (Arg_List_Last) := Command (First)'Access;
2548 -- Start of processing for Spawn
2551 Add_To_Command (Program_Name);
2553 for J in Args'Range loop
2554 Add_To_Command (Args (J).all);
2559 Result := Portable_Spawn (Arg_List'Address);
2561 Pid := Portable_No_Block_Spawn (Arg_List'Address);
2562 Result := Boolean'Pos (Pid /= Invalid_Pid);
2566 -- Start of processing for Spawn_Internal
2569 -- Copy arguments into a local structure
2571 for K in N_Args'Range loop
2572 N_Args (K) := new String'(Args (K).all);
2575 -- Normalize those arguments
2577 Normalize_Arguments (N_Args);
2579 -- Call spawn using the normalized arguments
2583 -- Free arguments list
2585 for K in N_Args'Range loop
2590 ---------------------------
2591 -- To_Path_String_Access --
2592 ---------------------------
2594 function To_Path_String_Access
2595 (Path_Addr : Address;
2596 Path_Len : Integer) return String_Access
2598 subtype Path_String is String (1 .. Path_Len);
2599 type Path_String_Access is access Path_String;
2601 function Address_To_Access is new Ada.Unchecked_Conversion
2602 (Source => Address, Target => Path_String_Access);
2604 Path_Access : constant Path_String_Access :=
2605 Address_To_Access (Path_Addr);
2607 Return_Val : String_Access;
2610 Return_Val := new String (1 .. Path_Len);
2612 for J in 1 .. Path_Len loop
2613 Return_Val (J) := Path_Access (J);
2617 end To_Path_String_Access;
2623 procedure Wait_Process (Pid : out Process_Id; Success : out Boolean) is
2626 function Portable_Wait (S : Address) return Process_Id;
2627 pragma Import (C, Portable_Wait, "__gnat_portable_wait");
2630 Pid := Portable_Wait (Status'Address);
2631 Success := (Status = 0);
2639 (FD : File_Descriptor;
2641 N : Integer) return Integer
2644 return Integer (System.CRTL.write
2645 (System.CRTL.int (FD), System.CRTL.chars (A), System.CRTL.int (N)));