1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . O S _ L I B --
9 -- Copyright (C) 1995-2006, 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 with System.Case_Util;
36 with System.Soft_Links;
37 with Unchecked_Conversion;
38 with Unchecked_Deallocation;
39 with System; use System;
41 package body GNAT.OS_Lib is
43 -- Imported procedures Dup and Dup2 are used in procedures Spawn and
44 -- Non_Blocking_Spawn.
46 function Dup (Fd : File_Descriptor) return File_Descriptor;
47 pragma Import (C, Dup, "__gnat_dup");
49 procedure Dup2 (Old_Fd, New_Fd : File_Descriptor);
50 pragma Import (C, Dup2, "__gnat_dup2");
53 -- Note: OpenVMS should be a constant, but it cannot be, because it
54 -- prevents bootstrapping on some platforms.
56 pragma Import (Ada, OpenVMS, "system__openvms");
57 -- Needed to avoid doing useless checks when non on a VMS platform (see
58 -- Normalize_Pathname).
60 On_Windows : constant Boolean := Directory_Separator = '\';
61 -- An indication that we are on Windows. Used in Normalize_Pathname, to
62 -- deal with drive letters in the beginning of absolute paths.
64 package SSL renames System.Soft_Links;
66 -- The following are used by Create_Temp_File
68 First_Temp_File_Name : constant String := "GNAT-TEMP-000000.TMP";
69 -- Used to initialize Current_Temp_File_Name and Temp_File_Name_Last_Digit
71 Current_Temp_File_Name : String := First_Temp_File_Name;
72 -- Name of the temp file last created
74 Temp_File_Name_Last_Digit : constant Positive :=
75 First_Temp_File_Name'Last - 4;
76 -- Position of the last digit in Current_Temp_File_Name
78 Max_Attempts : constant := 100;
79 -- The maximum number of attempts to create a new temp file
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
85 function Args_Length (Args : Argument_List) return Natural;
86 -- Returns total number of characters needed to create a string
87 -- of all Args terminated by ASCII.NUL characters
89 function C_String_Length (S : Address) return Integer;
90 -- Returns the length of a C string. Does check for null address
93 procedure Spawn_Internal
94 (Program_Name : String;
99 -- Internal routine to implement the two Spawn (blocking/non blocking)
100 -- routines. If Blocking is set to True then the spawn is blocking
101 -- otherwise it is non blocking. In this latter case the Pid contains the
102 -- process id number. The first three parameters are as in Spawn. Note that
103 -- Spawn_Internal normalizes the argument list before calling the low level
104 -- system spawn routines (see Normalize_Arguments).
106 -- Note: Normalize_Arguments is designed to do nothing if it is called more
107 -- than once, so calling Normalize_Arguments before calling one of the
108 -- spawn routines is fine.
110 function To_Path_String_Access
111 (Path_Addr : Address;
112 Path_Len : Integer) return String_Access;
113 -- Converts a C String to an Ada String. We could do this making use of
114 -- Interfaces.C.Strings but we prefer not to import that entire package
120 function "<" (X, Y : OS_Time) return Boolean is
122 return Long_Integer (X) < Long_Integer (Y);
129 function "<=" (X, Y : OS_Time) return Boolean is
131 return Long_Integer (X) <= Long_Integer (Y);
138 function ">" (X, Y : OS_Time) return Boolean is
140 return Long_Integer (X) > Long_Integer (Y);
147 function ">=" (X, Y : OS_Time) return Boolean is
149 return Long_Integer (X) >= Long_Integer (Y);
156 function Args_Length (Args : Argument_List) return Natural is
160 for J in Args'Range loop
161 Len := Len + Args (J)'Length + 1; -- One extra for ASCII.NUL
167 -----------------------------
168 -- Argument_String_To_List --
169 -----------------------------
171 function Argument_String_To_List
172 (Arg_String : String) return Argument_List_Access
174 Max_Args : constant Integer := Arg_String'Length;
175 New_Argv : Argument_List (1 .. Max_Args);
176 New_Argc : Natural := 0;
180 Idx := Arg_String'First;
183 exit when Idx > Arg_String'Last;
186 Quoted : Boolean := False;
187 Backqd : Boolean := False;
194 -- An unquoted space is the end of an argument
196 if not (Backqd or Quoted)
197 and then Arg_String (Idx) = ' '
201 -- Start of a quoted string
203 elsif not (Backqd or Quoted)
204 and then Arg_String (Idx) = '"'
208 -- End of a quoted string and end of an argument
210 elsif (Quoted and not Backqd)
211 and then Arg_String (Idx) = '"'
216 -- Following character is backquoted
218 elsif Arg_String (Idx) = '\' then
221 -- Turn off backquoting after advancing one character
229 exit when Idx > Arg_String'Last;
234 New_Argc := New_Argc + 1;
235 New_Argv (New_Argc) :=
236 new String'(Arg_String (Old_Idx .. Idx - 1));
238 -- Skip extraneous spaces
240 while Idx <= Arg_String'Last and then Arg_String (Idx) = ' ' loop
246 return new Argument_List'(New_Argv (1 .. New_Argc));
247 end Argument_String_To_List;
249 ---------------------
250 -- C_String_Length --
251 ---------------------
253 function C_String_Length (S : Address) return Integer is
254 function Strlen (S : Address) return Integer;
255 pragma Import (C, Strlen, "strlen");
257 if S = Null_Address then
268 procedure Close (FD : File_Descriptor) is
269 procedure C_Close (FD : File_Descriptor);
270 pragma Import (C, C_Close, "close");
275 procedure Close (FD : File_Descriptor; Status : out Boolean) is
276 function C_Close (FD : File_Descriptor) return Integer;
277 pragma Import (C, C_Close, "close");
279 Status := (C_Close (FD) = 0);
289 Success : out Boolean;
290 Mode : Copy_Mode := Copy;
291 Preserve : Attribute := Time_Stamps)
293 From : File_Descriptor;
294 To : File_Descriptor;
296 Copy_Error : exception;
297 -- Internal exception raised to signal error in copy
299 function Build_Path (Dir : String; File : String) return String;
300 -- Returns pathname Dir catenated with File adding the directory
301 -- separator only if needed.
303 procedure Copy (From, To : File_Descriptor);
304 -- Read data from From and place them into To. In both cases the
305 -- operations uses the current file position. Raises Constraint_Error
306 -- if a problem occurs during the copy.
308 procedure Copy_To (To_Name : String);
309 -- Does a straight copy from source to designated destination file
315 function Build_Path (Dir : String; File : String) return String is
316 Res : String (1 .. Dir'Length + File'Length + 1);
318 Base_File_Ptr : Integer;
319 -- The base file name is File (Base_File_Ptr + 1 .. File'Last)
321 function Is_Dirsep (C : Character) return Boolean;
322 pragma Inline (Is_Dirsep);
323 -- Returns True if C is a directory separator. On Windows we
324 -- handle both styles of directory separator.
330 function Is_Dirsep (C : Character) return Boolean is
332 return C = Directory_Separator or else C = '/';
335 -- Start of processing for Build_Path
338 -- Find base file name
340 Base_File_Ptr := File'Last;
341 while Base_File_Ptr >= File'First loop
342 exit when Is_Dirsep (File (Base_File_Ptr));
343 Base_File_Ptr := Base_File_Ptr - 1;
347 Base_File : String renames
348 File (Base_File_Ptr + 1 .. File'Last);
351 Res (1 .. Dir'Length) := Dir;
353 if Is_Dirsep (Dir (Dir'Last)) then
354 Res (Dir'Length + 1 .. Dir'Length + Base_File'Length) :=
356 return Res (1 .. Dir'Length + Base_File'Length);
359 Res (Dir'Length + 1) := Directory_Separator;
360 Res (Dir'Length + 2 .. Dir'Length + 1 + Base_File'Length) :=
362 return Res (1 .. Dir'Length + 1 + Base_File'Length);
371 procedure Copy (From, To : File_Descriptor) is
372 Buf_Size : constant := 200_000;
373 type Buf is array (1 .. Buf_Size) of Character;
374 type Buf_Ptr is access Buf;
380 Status_From : Boolean;
382 -- Statuses for the calls to Close
384 procedure Free is new Unchecked_Deallocation (Buf, Buf_Ptr);
387 -- Check for invalid descriptors, making sure that we do not
388 -- accidentally leave an open file descriptor around.
390 if From = Invalid_FD then
391 if To /= Invalid_FD then
392 Close (To, Status_To);
397 elsif To = Invalid_FD then
398 Close (From, Status_From);
402 -- Allocate the buffer on the heap
407 R := Read (From, Buffer (1)'Address, Buf_Size);
409 -- For VMS, the buffer may not be full. So, we need to try again
410 -- until there is nothing to read.
414 W := Write (To, Buffer (1)'Address, R);
418 -- Problem writing data, could be a disk full. Close files
419 -- without worrying about status, since we are raising a
420 -- Copy_Error exception in any case.
422 Close (From, Status_From);
423 Close (To, Status_To);
431 Close (From, Status_From);
432 Close (To, Status_To);
436 if not (Status_From and Status_To) then
445 procedure Copy_To (To_Name : String) is
447 function Copy_Attributes
448 (From, To : System.Address;
449 Mode : Integer) return Integer;
450 pragma Import (C, Copy_Attributes, "__gnat_copy_attribs");
451 -- Mode = 0 - copy only time stamps.
452 -- Mode = 1 - copy time stamps and read/write/execute attributes
454 C_From : String (1 .. Name'Length + 1);
455 C_To : String (1 .. To_Name'Length + 1);
458 From := Open_Read (Name, Binary);
459 To := Create_File (To_Name, Binary);
464 C_From (1 .. Name'Length) := Name;
465 C_From (C_From'Last) := ASCII.Nul;
467 C_To (1 .. To_Name'Length) := To_Name;
468 C_To (C_To'Last) := ASCII.Nul;
473 if Copy_Attributes (C_From'Address, C_To'Address, 0) = -1 then
478 if Copy_Attributes (C_From'Address, C_To'Address, 1) = -1 then
488 -- Start of processing for Copy_File
493 -- The source file must exist
495 if not Is_Regular_File (Name) then
499 -- The source file exists
503 -- Copy case, target file must not exist
507 -- If the target file exists, we have an error
509 if Is_Regular_File (Pathname) then
512 -- Case of target is a directory
514 elsif Is_Directory (Pathname) then
516 Dest : constant String := Build_Path (Pathname, Name);
519 -- If target file exists, we have an error, else do copy
521 if Is_Regular_File (Dest) then
528 -- Case of normal copy to file (destination does not exist)
534 -- Overwrite case (destination file may or may not exist)
537 if Is_Directory (Pathname) then
538 Copy_To (Build_Path (Pathname, Name));
543 -- Append case (destination file may or may not exist)
547 -- Appending to existing file
549 if Is_Regular_File (Pathname) then
551 -- Append mode and destination file exists, append data at the
554 From := Open_Read (Name, Binary);
555 To := Open_Read_Write (Pathname, Binary);
556 Lseek (To, 0, Seek_End);
560 -- Appending to directory, not allowed
562 elsif Is_Directory (Pathname) then
565 -- Appending when target file does not exist
572 -- All error cases are caught here
581 Pathname : C_File_Name;
582 Success : out Boolean;
583 Mode : Copy_Mode := Copy;
584 Preserve : Attribute := Time_Stamps)
586 Ada_Name : String_Access :=
587 To_Path_String_Access
588 (Name, C_String_Length (Name));
590 Ada_Pathname : String_Access :=
591 To_Path_String_Access
592 (Pathname, C_String_Length (Pathname));
595 Copy_File (Ada_Name.all, Ada_Pathname.all, Success, Mode, Preserve);
600 ----------------------
601 -- Copy_Time_Stamps --
602 ----------------------
604 procedure Copy_Time_Stamps (Source, Dest : String; Success : out Boolean) is
606 function Copy_Attributes
607 (From, To : System.Address;
608 Mode : Integer) return Integer;
609 pragma Import (C, Copy_Attributes, "__gnat_copy_attribs");
610 -- Mode = 0 - copy only time stamps.
611 -- Mode = 1 - copy time stamps and read/write/execute attributes
614 if Is_Regular_File (Source) and then Is_Writable_File (Dest) then
616 C_Source : String (1 .. Source'Length + 1);
617 C_Dest : String (1 .. Dest'Length + 1);
619 C_Source (1 .. Source'Length) := Source;
620 C_Source (C_Source'Last) := ASCII.NUL;
622 C_Dest (1 .. Dest'Length) := Dest;
623 C_Dest (C_Dest'Last) := ASCII.NUL;
625 if Copy_Attributes (C_Source'Address, C_Dest'Address, 0) = -1 then
635 end Copy_Time_Stamps;
637 procedure Copy_Time_Stamps
638 (Source, Dest : C_File_Name;
639 Success : out Boolean)
641 Ada_Source : String_Access :=
642 To_Path_String_Access
643 (Source, C_String_Length (Source));
645 Ada_Dest : String_Access :=
646 To_Path_String_Access
647 (Dest, C_String_Length (Dest));
649 Copy_Time_Stamps (Ada_Source.all, Ada_Dest.all, Success);
652 end Copy_Time_Stamps;
660 Fmode : Mode) return File_Descriptor
662 function C_Create_File
664 Fmode : Mode) return File_Descriptor;
665 pragma Import (C, C_Create_File, "__gnat_open_create");
668 return C_Create_File (Name, Fmode);
673 Fmode : Mode) return File_Descriptor
675 C_Name : String (1 .. Name'Length + 1);
678 C_Name (1 .. Name'Length) := Name;
679 C_Name (C_Name'Last) := ASCII.NUL;
680 return Create_File (C_Name (C_Name'First)'Address, Fmode);
683 ---------------------
684 -- Create_New_File --
685 ---------------------
687 function Create_New_File
689 Fmode : Mode) return File_Descriptor
691 function C_Create_New_File
693 Fmode : Mode) return File_Descriptor;
694 pragma Import (C, C_Create_New_File, "__gnat_open_new");
697 return C_Create_New_File (Name, Fmode);
700 function Create_New_File
702 Fmode : Mode) return File_Descriptor
704 C_Name : String (1 .. Name'Length + 1);
707 C_Name (1 .. Name'Length) := Name;
708 C_Name (C_Name'Last) := ASCII.NUL;
709 return Create_New_File (C_Name (C_Name'First)'Address, Fmode);
712 -----------------------------
713 -- Create_Output_Text_File --
714 -----------------------------
716 function Create_Output_Text_File (Name : String) return File_Descriptor is
717 function C_Create_File
718 (Name : C_File_Name) return File_Descriptor;
719 pragma Import (C, C_Create_File, "__gnat_create_output_file");
721 C_Name : String (1 .. Name'Length + 1);
724 C_Name (1 .. Name'Length) := Name;
725 C_Name (C_Name'Last) := ASCII.NUL;
726 return C_Create_File (C_Name (C_Name'First)'Address);
727 end Create_Output_Text_File;
729 ----------------------
730 -- Create_Temp_File --
731 ----------------------
733 procedure Create_Temp_File
734 (FD : out File_Descriptor;
735 Name : out Temp_File_Name)
737 function Open_New_Temp
738 (Name : System.Address;
739 Fmode : Mode) return File_Descriptor;
740 pragma Import (C, Open_New_Temp, "__gnat_open_new_temp");
743 FD := Open_New_Temp (Name'Address, Binary);
744 end Create_Temp_File;
746 procedure Create_Temp_File
747 (FD : out File_Descriptor;
748 Name : out String_Access)
751 Attempts : Natural := 0;
752 Current : String (Current_Temp_File_Name'Range);
755 -- Loop until a new temp file can be created
759 -- We need to protect global variable Current_Temp_File_Name
760 -- against concurrent access by different tasks.
764 -- Start at the last digit
766 Pos := Temp_File_Name_Last_Digit;
770 -- Increment the digit by one
772 case Current_Temp_File_Name (Pos) is
774 Current_Temp_File_Name (Pos) :=
775 Character'Succ (Current_Temp_File_Name (Pos));
780 -- For 9, set the digit to 0 and go to the previous digit
782 Current_Temp_File_Name (Pos) := '0';
787 -- If it is not a digit, then there are no available
788 -- temp file names. Return Invalid_FD. There is almost
789 -- no that this code will be ever be executed, since
790 -- it would mean that there are one million temp files
791 -- in the same directory!
800 Current := Current_Temp_File_Name;
802 -- We can now release the lock, because we are no longer
803 -- accessing Current_Temp_File_Name.
813 -- Attempt to create the file
815 FD := Create_New_File (Current, Binary);
817 if FD /= Invalid_FD then
818 Name := new String'(Current);
822 if not Is_Regular_File (Current) then
824 -- If the file does not already exist and we are unable to create
825 -- it, we give up after Max_Attempts. Otherwise, we try again with
826 -- the next available file name.
828 Attempts := Attempts + 1;
830 if Attempts >= Max_Attempts then
837 end Create_Temp_File;
843 procedure Delete_File (Name : Address; Success : out Boolean) is
846 function unlink (A : Address) return Integer;
847 pragma Import (C, unlink, "unlink");
854 procedure Delete_File (Name : String; Success : out Boolean) is
855 C_Name : String (1 .. Name'Length + 1);
858 C_Name (1 .. Name'Length) := Name;
859 C_Name (C_Name'Last) := ASCII.NUL;
861 Delete_File (C_Name'Address, Success);
864 ---------------------
865 -- File_Time_Stamp --
866 ---------------------
868 function File_Time_Stamp (FD : File_Descriptor) return OS_Time is
869 function File_Time (FD : File_Descriptor) return OS_Time;
870 pragma Import (C, File_Time, "__gnat_file_time_fd");
872 return File_Time (FD);
875 function File_Time_Stamp (Name : C_File_Name) return OS_Time is
876 function File_Time (Name : Address) return OS_Time;
877 pragma Import (C, File_Time, "__gnat_file_time_name");
879 return File_Time (Name);
882 function File_Time_Stamp (Name : String) return OS_Time is
883 F_Name : String (1 .. Name'Length + 1);
885 F_Name (1 .. Name'Length) := Name;
886 F_Name (F_Name'Last) := ASCII.NUL;
887 return File_Time_Stamp (F_Name'Address);
890 ---------------------------
891 -- Get_Debuggable_Suffix --
892 ---------------------------
894 function Get_Debuggable_Suffix return String_Access is
895 procedure Get_Suffix_Ptr (Length, Ptr : Address);
896 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_debuggable_suffix_ptr");
898 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
899 pragma Import (C, Strncpy, "strncpy");
901 Suffix_Ptr : Address;
902 Suffix_Length : Integer;
903 Result : String_Access;
906 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
908 Result := new String (1 .. Suffix_Length);
910 if Suffix_Length > 0 then
911 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
915 end Get_Debuggable_Suffix;
917 ----------------------------------
918 -- Get_Target_Debuggable_Suffix --
919 ----------------------------------
921 function Get_Target_Debuggable_Suffix return String_Access is
922 Target_Exec_Ext_Ptr : Address;
924 (C, Target_Exec_Ext_Ptr, "__gnat_target_debuggable_extension");
926 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
927 pragma Import (C, Strncpy, "strncpy");
929 function Strlen (Cstring : Address) return Integer;
930 pragma Import (C, Strlen, "strlen");
932 Suffix_Length : Integer;
933 Result : String_Access;
936 Suffix_Length := Strlen (Target_Exec_Ext_Ptr);
938 Result := new String (1 .. Suffix_Length);
940 if Suffix_Length > 0 then
941 Strncpy (Result.all'Address, Target_Exec_Ext_Ptr, Suffix_Length);
945 end Get_Target_Debuggable_Suffix;
947 ---------------------------
948 -- Get_Executable_Suffix --
949 ---------------------------
951 function Get_Executable_Suffix return String_Access is
952 procedure Get_Suffix_Ptr (Length, Ptr : Address);
953 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_executable_suffix_ptr");
955 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
956 pragma Import (C, Strncpy, "strncpy");
958 Suffix_Ptr : Address;
959 Suffix_Length : Integer;
960 Result : String_Access;
963 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
965 Result := new String (1 .. Suffix_Length);
967 if Suffix_Length > 0 then
968 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
972 end Get_Executable_Suffix;
974 ----------------------------------
975 -- Get_Target_Executable_Suffix --
976 ----------------------------------
978 function Get_Target_Executable_Suffix return String_Access is
979 Target_Exec_Ext_Ptr : Address;
981 (C, Target_Exec_Ext_Ptr, "__gnat_target_executable_extension");
983 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
984 pragma Import (C, Strncpy, "strncpy");
986 function Strlen (Cstring : Address) return Integer;
987 pragma Import (C, Strlen, "strlen");
989 Suffix_Length : Integer;
990 Result : String_Access;
993 Suffix_Length := Strlen (Target_Exec_Ext_Ptr);
995 Result := new String (1 .. Suffix_Length);
997 if Suffix_Length > 0 then
998 Strncpy (Result.all'Address, Target_Exec_Ext_Ptr, Suffix_Length);
1002 end Get_Target_Executable_Suffix;
1004 -----------------------
1005 -- Get_Object_Suffix --
1006 -----------------------
1008 function Get_Object_Suffix return String_Access is
1009 procedure Get_Suffix_Ptr (Length, Ptr : Address);
1010 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_object_suffix_ptr");
1012 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1013 pragma Import (C, Strncpy, "strncpy");
1015 Suffix_Ptr : Address;
1016 Suffix_Length : Integer;
1017 Result : String_Access;
1020 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
1022 Result := new String (1 .. Suffix_Length);
1024 if Suffix_Length > 0 then
1025 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
1029 end Get_Object_Suffix;
1031 ------------------------------
1032 -- Get_Target_Object_Suffix --
1033 ------------------------------
1035 function Get_Target_Object_Suffix return String_Access is
1036 Target_Object_Ext_Ptr : Address;
1038 (C, Target_Object_Ext_Ptr, "__gnat_target_object_extension");
1040 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1041 pragma Import (C, Strncpy, "strncpy");
1043 function Strlen (Cstring : Address) return Integer;
1044 pragma Import (C, Strlen, "strlen");
1046 Suffix_Length : Integer;
1047 Result : String_Access;
1050 Suffix_Length := Strlen (Target_Object_Ext_Ptr);
1052 Result := new String (1 .. Suffix_Length);
1054 if Suffix_Length > 0 then
1055 Strncpy (Result.all'Address, Target_Object_Ext_Ptr, Suffix_Length);
1059 end Get_Target_Object_Suffix;
1065 function Getenv (Name : String) return String_Access is
1066 procedure Get_Env_Value_Ptr (Name, Length, Ptr : Address);
1067 pragma Import (C, Get_Env_Value_Ptr, "__gnat_getenv");
1069 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1070 pragma Import (C, Strncpy, "strncpy");
1072 Env_Value_Ptr : aliased Address;
1073 Env_Value_Length : aliased Integer;
1074 F_Name : aliased String (1 .. Name'Length + 1);
1075 Result : String_Access;
1078 F_Name (1 .. Name'Length) := Name;
1079 F_Name (F_Name'Last) := ASCII.NUL;
1082 (F_Name'Address, Env_Value_Length'Address, Env_Value_Ptr'Address);
1084 Result := new String (1 .. Env_Value_Length);
1086 if Env_Value_Length > 0 then
1087 Strncpy (Result.all'Address, Env_Value_Ptr, Env_Value_Length);
1097 function GM_Day (Date : OS_Time) return Day_Type is
1106 GM_Split (Date, Y, Mo, D, H, Mn, S);
1114 function GM_Hour (Date : OS_Time) return Hour_Type is
1123 GM_Split (Date, Y, Mo, D, H, Mn, S);
1131 function GM_Minute (Date : OS_Time) return Minute_Type is
1140 GM_Split (Date, Y, Mo, D, H, Mn, S);
1148 function GM_Month (Date : OS_Time) return Month_Type is
1157 GM_Split (Date, Y, Mo, D, H, Mn, S);
1165 function GM_Second (Date : OS_Time) return Second_Type is
1174 GM_Split (Date, Y, Mo, D, H, Mn, S);
1184 Year : out Year_Type;
1185 Month : out Month_Type;
1187 Hour : out Hour_Type;
1188 Minute : out Minute_Type;
1189 Second : out Second_Type)
1191 procedure To_GM_Time
1192 (P_Time_T, P_Year, P_Month, P_Day, P_Hours, P_Mins, P_Secs : Address);
1193 pragma Import (C, To_GM_Time, "__gnat_to_gm_time");
1195 T : OS_Time := Date;
1204 -- Use the global lock because To_GM_Time is not thread safe
1206 Locked_Processing : begin
1209 (T'Address, Y'Address, Mo'Address, D'Address,
1210 H'Address, Mn'Address, S'Address);
1211 SSL.Unlock_Task.all;
1215 SSL.Unlock_Task.all;
1217 end Locked_Processing;
1231 function GM_Year (Date : OS_Time) return Year_Type is
1240 GM_Split (Date, Y, Mo, D, H, Mn, S);
1244 ----------------------
1245 -- Is_Absolute_Path --
1246 ----------------------
1248 function Is_Absolute_Path (Name : String) return Boolean is
1249 function Is_Absolute_Path
1251 Length : Integer) return Integer;
1252 pragma Import (C, Is_Absolute_Path, "__gnat_is_absolute_path");
1254 return Is_Absolute_Path (Name'Address, Name'Length) /= 0;
1255 end Is_Absolute_Path;
1261 function Is_Directory (Name : C_File_Name) return Boolean is
1262 function Is_Directory (Name : Address) return Integer;
1263 pragma Import (C, Is_Directory, "__gnat_is_directory");
1265 return Is_Directory (Name) /= 0;
1268 function Is_Directory (Name : String) return Boolean is
1269 F_Name : String (1 .. Name'Length + 1);
1271 F_Name (1 .. Name'Length) := Name;
1272 F_Name (F_Name'Last) := ASCII.NUL;
1273 return Is_Directory (F_Name'Address);
1276 ---------------------
1277 -- Is_Regular_File --
1278 ---------------------
1280 function Is_Regular_File (Name : C_File_Name) return Boolean is
1281 function Is_Regular_File (Name : Address) return Integer;
1282 pragma Import (C, Is_Regular_File, "__gnat_is_regular_file");
1284 return Is_Regular_File (Name) /= 0;
1285 end Is_Regular_File;
1287 function Is_Regular_File (Name : String) return Boolean is
1288 F_Name : String (1 .. Name'Length + 1);
1290 F_Name (1 .. Name'Length) := Name;
1291 F_Name (F_Name'Last) := ASCII.NUL;
1292 return Is_Regular_File (F_Name'Address);
1293 end Is_Regular_File;
1295 ----------------------
1296 -- Is_Readable_File --
1297 ----------------------
1299 function Is_Readable_File (Name : C_File_Name) return Boolean is
1300 function Is_Readable_File (Name : Address) return Integer;
1301 pragma Import (C, Is_Readable_File, "__gnat_is_readable_file");
1303 return Is_Readable_File (Name) /= 0;
1304 end Is_Readable_File;
1306 function Is_Readable_File (Name : String) return Boolean is
1307 F_Name : String (1 .. Name'Length + 1);
1309 F_Name (1 .. Name'Length) := Name;
1310 F_Name (F_Name'Last) := ASCII.NUL;
1311 return Is_Readable_File (F_Name'Address);
1312 end Is_Readable_File;
1314 ----------------------
1315 -- Is_Writable_File --
1316 ----------------------
1318 function Is_Writable_File (Name : C_File_Name) return Boolean is
1319 function Is_Writable_File (Name : Address) return Integer;
1320 pragma Import (C, Is_Writable_File, "__gnat_is_writable_file");
1322 return Is_Writable_File (Name) /= 0;
1323 end Is_Writable_File;
1325 function Is_Writable_File (Name : String) return Boolean is
1326 F_Name : String (1 .. Name'Length + 1);
1328 F_Name (1 .. Name'Length) := Name;
1329 F_Name (F_Name'Last) := ASCII.NUL;
1330 return Is_Writable_File (F_Name'Address);
1331 end Is_Writable_File;
1333 ----------------------
1334 -- Is_Symbolic_Link --
1335 ----------------------
1337 function Is_Symbolic_Link (Name : C_File_Name) return Boolean is
1338 function Is_Symbolic_Link (Name : Address) return Integer;
1339 pragma Import (C, Is_Symbolic_Link, "__gnat_is_symbolic_link");
1341 return Is_Symbolic_Link (Name) /= 0;
1342 end Is_Symbolic_Link;
1344 function Is_Symbolic_Link (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_Symbolic_Link (F_Name'Address);
1350 end Is_Symbolic_Link;
1352 -------------------------
1353 -- Locate_Exec_On_Path --
1354 -------------------------
1356 function Locate_Exec_On_Path
1357 (Exec_Name : String) return String_Access
1359 function Locate_Exec_On_Path (C_Exec_Name : Address) return Address;
1360 pragma Import (C, Locate_Exec_On_Path, "__gnat_locate_exec_on_path");
1362 procedure Free (Ptr : System.Address);
1363 pragma Import (C, Free, "free");
1365 C_Exec_Name : String (1 .. Exec_Name'Length + 1);
1366 Path_Addr : Address;
1368 Result : String_Access;
1371 C_Exec_Name (1 .. Exec_Name'Length) := Exec_Name;
1372 C_Exec_Name (C_Exec_Name'Last) := ASCII.NUL;
1374 Path_Addr := Locate_Exec_On_Path (C_Exec_Name'Address);
1375 Path_Len := C_String_Length (Path_Addr);
1377 if Path_Len = 0 then
1381 Result := To_Path_String_Access (Path_Addr, Path_Len);
1385 end Locate_Exec_On_Path;
1387 -------------------------
1388 -- Locate_Regular_File --
1389 -------------------------
1391 function Locate_Regular_File
1392 (File_Name : C_File_Name;
1393 Path : C_File_Name) return String_Access
1395 function Locate_Regular_File
1396 (C_File_Name, Path_Val : Address) return Address;
1397 pragma Import (C, Locate_Regular_File, "__gnat_locate_regular_file");
1399 procedure Free (Ptr : System.Address);
1400 pragma Import (C, Free, "free");
1402 Path_Addr : Address;
1404 Result : String_Access;
1407 Path_Addr := Locate_Regular_File (File_Name, Path);
1408 Path_Len := C_String_Length (Path_Addr);
1410 if Path_Len = 0 then
1413 Result := To_Path_String_Access (Path_Addr, Path_Len);
1417 end Locate_Regular_File;
1419 function Locate_Regular_File
1420 (File_Name : String;
1421 Path : String) return String_Access
1423 C_File_Name : String (1 .. File_Name'Length + 1);
1424 C_Path : String (1 .. Path'Length + 1);
1427 C_File_Name (1 .. File_Name'Length) := File_Name;
1428 C_File_Name (C_File_Name'Last) := ASCII.NUL;
1430 C_Path (1 .. Path'Length) := Path;
1431 C_Path (C_Path'Last) := ASCII.NUL;
1433 return Locate_Regular_File (C_File_Name'Address, C_Path'Address);
1434 end Locate_Regular_File;
1436 ------------------------
1437 -- Non_Blocking_Spawn --
1438 ------------------------
1440 function Non_Blocking_Spawn
1441 (Program_Name : String;
1442 Args : Argument_List) return Process_Id
1448 Spawn_Internal (Program_Name, Args, Junk, Pid, Blocking => False);
1450 end Non_Blocking_Spawn;
1452 function Non_Blocking_Spawn
1453 (Program_Name : String;
1454 Args : Argument_List;
1455 Output_File_Descriptor : File_Descriptor;
1456 Err_To_Out : Boolean := True)
1459 Saved_Output : File_Descriptor;
1460 Saved_Error : File_Descriptor := Invalid_FD; -- prevent warning
1463 if Output_File_Descriptor = Invalid_FD then
1467 -- Set standard output and, if specified, error to the temporary file
1469 Saved_Output := Dup (Standout);
1470 Dup2 (Output_File_Descriptor, Standout);
1473 Saved_Error := Dup (Standerr);
1474 Dup2 (Output_File_Descriptor, Standerr);
1477 -- Spawn the program
1479 Pid := Non_Blocking_Spawn (Program_Name, Args);
1481 -- Restore the standard output and error
1483 Dup2 (Saved_Output, Standout);
1486 Dup2 (Saved_Error, Standerr);
1489 -- And close the saved standard output and error file descriptors
1491 Close (Saved_Output);
1494 Close (Saved_Error);
1498 end Non_Blocking_Spawn;
1500 function Non_Blocking_Spawn
1501 (Program_Name : String;
1502 Args : Argument_List;
1503 Output_File : String;
1504 Err_To_Out : Boolean := True) return Process_Id
1506 Output_File_Descriptor : constant File_Descriptor :=
1507 Create_Output_Text_File (Output_File);
1508 Result : Process_Id;
1511 -- Do not attempt to spawn if the output file could not be created
1513 if Output_File_Descriptor = Invalid_FD then
1517 Result := Non_Blocking_Spawn
1518 (Program_Name, Args, Output_File_Descriptor, Err_To_Out);
1520 -- Close the file just created for the output, as the file descriptor
1521 -- cannot be used anywhere, being a local value. It is safe to do
1522 -- that, as the file descriptor has been duplicated to form
1523 -- standard output and error of the spawned process.
1525 Close (Output_File_Descriptor);
1529 end Non_Blocking_Spawn;
1531 -------------------------
1532 -- Normalize_Arguments --
1533 -------------------------
1535 procedure Normalize_Arguments (Args : in out Argument_List) is
1537 procedure Quote_Argument (Arg : in out String_Access);
1538 -- Add quote around argument if it contains spaces
1540 C_Argument_Needs_Quote : Integer;
1541 pragma Import (C, C_Argument_Needs_Quote, "__gnat_argument_needs_quote");
1542 Argument_Needs_Quote : constant Boolean := C_Argument_Needs_Quote /= 0;
1544 --------------------
1545 -- Quote_Argument --
1546 --------------------
1548 procedure Quote_Argument (Arg : in out String_Access) is
1549 Res : String (1 .. Arg'Length * 2);
1551 Quote_Needed : Boolean := False;
1554 if Arg (Arg'First) /= '"' or else Arg (Arg'Last) /= '"' then
1560 for K in Arg'Range loop
1564 if Arg (K) = '"' then
1568 Quote_Needed := True;
1570 elsif Arg (K) = ' ' then
1572 Quote_Needed := True;
1580 if Quote_Needed then
1582 -- If null terminated string, put the quote before
1584 if Res (J) = ASCII.Nul then
1587 Res (J) := ASCII.Nul;
1589 -- If argument is terminated by '\', then double it. Otherwise
1590 -- the ending quote will be taken as-is. This is quite strange
1591 -- spawn behavior from Windows, but this is what we see!
1594 if Res (J) = '\' then
1606 Old : String_Access := Arg;
1609 Arg := new String'(Res (1 .. J));
1617 -- Start of processing for Normalize_Arguments
1620 if Argument_Needs_Quote then
1621 for K in Args'Range loop
1622 if Args (K) /= null and then Args (K)'Length /= 0 then
1623 Quote_Argument (Args (K));
1627 end Normalize_Arguments;
1629 ------------------------
1630 -- Normalize_Pathname --
1631 ------------------------
1633 function Normalize_Pathname
1635 Directory : String := "";
1636 Resolve_Links : Boolean := True;
1637 Case_Sensitive : Boolean := True) return String
1640 pragma Import (C, Max_Path, "__gnat_max_path_len");
1641 -- Maximum length of a path name
1643 procedure Get_Current_Dir
1644 (Dir : System.Address;
1645 Length : System.Address);
1646 pragma Import (C, Get_Current_Dir, "__gnat_get_current_dir");
1648 function Change_Dir (Dir_Name : String) return Integer;
1649 pragma Import (C, Change_Dir, "chdir");
1651 Path_Buffer : String (1 .. Max_Path + Max_Path + 2);
1652 End_Path : Natural := 0;
1653 Link_Buffer : String (1 .. Max_Path + 2);
1659 Max_Iterations : constant := 500;
1661 function Get_File_Names_Case_Sensitive return Integer;
1663 (C, Get_File_Names_Case_Sensitive,
1664 "__gnat_get_file_names_case_sensitive");
1666 Fold_To_Lower_Case : constant Boolean :=
1668 and then Get_File_Names_Case_Sensitive = 0;
1671 (Path : System.Address;
1672 Buf : System.Address;
1673 Bufsiz : Integer) return Integer;
1674 pragma Import (C, Readlink, "__gnat_readlink");
1676 function To_Canonical_File_Spec
1677 (Host_File : System.Address) return System.Address;
1679 (C, To_Canonical_File_Spec, "__gnat_to_canonical_file_spec");
1681 The_Name : String (1 .. Name'Length + 1);
1682 Canonical_File_Addr : System.Address;
1683 Canonical_File_Len : Integer;
1685 Need_To_Check_Drive_Letter : Boolean := False;
1686 -- Set to true if Name is an absolute path that starts with "//"
1688 function Strlen (S : System.Address) return Integer;
1689 pragma Import (C, Strlen, "strlen");
1691 function Get_Directory (Dir : String) return String;
1692 -- If Dir is not empty, return it, adding a directory separator
1693 -- if not already present, otherwise return current working directory
1694 -- with terminating directory separator.
1696 function Final_Value (S : String) return String;
1697 -- Make final adjustment to the returned string.
1698 -- To compensate for non standard path name in Interix,
1699 -- if S is "/x" or starts with "/x", where x is a capital
1700 -- letter 'A' to 'Z', add an additional '/' at the beginning
1701 -- so that the returned value starts with "//x".
1707 function Get_Directory (Dir : String) return String is
1709 -- Directory given, add directory separator if needed
1711 if Dir'Length > 0 then
1712 if Dir (Dir'Last) = Directory_Separator then
1716 Result : String (1 .. Dir'Length + 1);
1718 Result (1 .. Dir'Length) := Dir;
1719 Result (Result'Length) := Directory_Separator;
1724 -- Directory name not given, get current directory
1728 Buffer : String (1 .. Max_Path + 2);
1729 Path_Len : Natural := Max_Path;
1732 Get_Current_Dir (Buffer'Address, Path_Len'Address);
1734 if Buffer (Path_Len) /= Directory_Separator then
1735 Path_Len := Path_Len + 1;
1736 Buffer (Path_Len) := Directory_Separator;
1739 -- By default, the drive letter on Windows is in upper case
1741 if On_Windows and then Path_Len >= 2 and then
1744 System.Case_Util.To_Upper (Buffer (1 .. 1));
1747 return Buffer (1 .. Path_Len);
1752 Reference_Dir : constant String := Get_Directory (Directory);
1753 -- Current directory name specified
1759 function Final_Value (S : String) return String is
1761 -- We may need to fold S to lower case, so we need a variable
1766 -- Interix has the non standard notion of disk drive
1767 -- indicated by two '/' followed by a capital letter
1768 -- 'A' .. 'Z'. One of the two '/' may have been removed
1769 -- by Normalize_Pathname. It has to be added again.
1770 -- For other OSes, this should not make no difference.
1772 if Need_To_Check_Drive_Letter
1773 and then S'Length >= 2
1774 and then S (S'First) = '/'
1775 and then S (S'First + 1) in 'A' .. 'Z'
1776 and then (S'Length = 2 or else S (S'First + 2) = '/')
1779 Result : String (1 .. S'Length + 1);
1783 Result (2 .. Result'Last) := S;
1784 Last := Result'Last;
1786 if Fold_To_Lower_Case then
1787 System.Case_Util.To_Lower (Result);
1790 -- Remove trailing directory separator, if any
1792 if Last > 1 and then
1793 (Result (Last) = '/' or else
1794 Result (Last) = Directory_Separator)
1799 return Result (1 .. Last);
1803 if Fold_To_Lower_Case then
1804 System.Case_Util.To_Lower (S1);
1807 -- Remove trailing directory separator, if any
1812 and then (S1 (Last) = '/'
1814 S1 (Last) = Directory_Separator)
1816 -- Special case for Windows: C:\
1819 and then S1 (1) /= Directory_Separator
1820 and then S1 (2) = ':'
1829 return S1 (1 .. Last);
1833 -- Start of processing for Normalize_Pathname
1836 -- Special case, if name is null, then return null
1838 if Name'Length = 0 then
1842 -- First, convert VMS file spec to Unix file spec.
1843 -- If Name is not in VMS syntax, then this is equivalent
1844 -- to put Name at the begining of Path_Buffer.
1846 VMS_Conversion : begin
1847 The_Name (1 .. Name'Length) := Name;
1848 The_Name (The_Name'Last) := ASCII.NUL;
1850 Canonical_File_Addr := To_Canonical_File_Spec (The_Name'Address);
1851 Canonical_File_Len := Strlen (Canonical_File_Addr);
1853 -- If VMS syntax conversion has failed, return an empty string
1854 -- to indicate the failure.
1856 if Canonical_File_Len = 0 then
1861 subtype Path_String is String (1 .. Canonical_File_Len);
1862 type Path_String_Access is access Path_String;
1864 function Address_To_Access is new
1865 Unchecked_Conversion (Source => Address,
1866 Target => Path_String_Access);
1868 Path_Access : constant Path_String_Access :=
1869 Address_To_Access (Canonical_File_Addr);
1872 Path_Buffer (1 .. Canonical_File_Len) := Path_Access.all;
1873 End_Path := Canonical_File_Len;
1878 -- Replace all '/' by Directory Separators (this is for Windows)
1880 if Directory_Separator /= '/' then
1881 for Index in 1 .. End_Path loop
1882 if Path_Buffer (Index) = '/' then
1883 Path_Buffer (Index) := Directory_Separator;
1888 -- Resolve directory names for VMS and Windows
1890 -- On VMS, if we have a Unix path such as /temp/..., and TEMP is a
1891 -- logical name, we need to resolve this logical name.
1893 -- On Windows, if we have an absolute path starting with a directory
1894 -- separator, we need to have the drive letter appended in front.
1896 -- For both platforms, Get_Current_Dir will return a suitable
1897 -- directory name (logical names resolved on VMS, path starting with
1898 -- a drive letter on Windows). So we find the directory, change to it,
1899 -- call Get_Current_Dir and change the directory to the returned value.
1900 -- Then, of course, we return to the previous directory.
1902 if (OpenVMS or On_Windows)
1903 and then Path_Buffer (1) = Directory_Separator
1906 Cur_Dir : String := Get_Directory ("");
1907 -- Save the current directory, so that we can change dir back to
1908 -- it. It is not a constant, because the last character (a
1909 -- directory separator) is changed to ASCII.NUL to call the C
1912 Path : String := Path_Buffer (1 .. End_Path + 1);
1913 -- Copy of the current path. One character is added that may be
1914 -- set to ASCII.NUL to call chdir.
1916 Pos : Positive := End_Path;
1917 -- Position of the last directory separator
1920 -- Value returned by chdir
1923 -- Look for the last directory separator
1925 while Path (Pos) /= Directory_Separator loop
1929 -- Get the previous character that is not a directory separator
1931 while Pos > 1 and then Path (Pos) = Directory_Separator loop
1935 -- If we are at the start of the path, take the full path.
1936 -- It may be a file in the root directory, but it may also be
1937 -- a subdirectory of the root directory.
1943 -- Add the ASCII.NUL to be able to call the C function chdir
1945 Path (Pos + 1) := ASCII.NUL;
1947 Status := Change_Dir (Path (1 .. Pos + 1));
1949 -- If Status is not zero, then we do nothing: this is a file
1950 -- path or it is not a valid directory path.
1954 New_Dir : constant String := Get_Directory ("");
1955 -- The directory path
1957 New_Path : String (1 .. New_Dir'Length + End_Path - Pos);
1958 -- The new complete path, that is built below
1961 New_Path (1 .. New_Dir'Length) := New_Dir;
1962 New_Path (New_Dir'Length + 1 .. New_Path'Last) :=
1963 Path_Buffer (Pos + 1 .. End_Path);
1964 End_Path := New_Path'Length;
1965 Path_Buffer (1 .. End_Path) := New_Path;
1968 -- Back to where we were before
1970 Cur_Dir (Cur_Dir'Last) := ASCII.NUL;
1971 Status := Change_Dir (Cur_Dir);
1976 -- Start the conversions
1978 -- If this is not finished after Max_Iterations, give up and return an
1981 for J in 1 .. Max_Iterations loop
1983 -- If we don't have an absolute pathname, prepend the directory
1987 and then not Is_Absolute_Path (Path_Buffer (1 .. End_Path))
1990 (Reference_Dir'Last + 1 .. Reference_Dir'Length + End_Path) :=
1991 Path_Buffer (1 .. End_Path);
1992 End_Path := Reference_Dir'Length + End_Path;
1993 Path_Buffer (1 .. Reference_Dir'Length) := Reference_Dir;
1994 Last := Reference_Dir'Length;
1997 -- If name starts with "//", we may have a drive letter on Interix
1999 if Last = 1 and then End_Path >= 3 then
2000 Need_To_Check_Drive_Letter := (Path_Buffer (1 .. 2)) = "//";
2006 -- Ensure that Windows network drives are kept, e.g: \\server\drive-c
2009 and then Directory_Separator = '\'
2010 and then Path_Buffer (1 .. 2) = "\\"
2015 -- If we have traversed the full pathname, return it
2017 if Start > End_Path then
2018 return Final_Value (Path_Buffer (1 .. End_Path));
2021 -- Remove duplicate directory separators
2023 while Path_Buffer (Start) = Directory_Separator loop
2024 if Start = End_Path then
2025 return Final_Value (Path_Buffer (1 .. End_Path - 1));
2028 Path_Buffer (Start .. End_Path - 1) :=
2029 Path_Buffer (Start + 1 .. End_Path);
2030 End_Path := End_Path - 1;
2034 -- Find the end of the current field: last character or the one
2035 -- preceding the next directory separator.
2037 while Finish < End_Path
2038 and then Path_Buffer (Finish + 1) /= Directory_Separator
2040 Finish := Finish + 1;
2045 if Start = Finish and then Path_Buffer (Start) = '.' then
2046 if Start = End_Path then
2048 return (1 => Directory_Separator);
2051 if Fold_To_Lower_Case then
2052 System.Case_Util.To_Lower (Path_Buffer (1 .. Last - 1));
2055 return Path_Buffer (1 .. Last - 1);
2060 Path_Buffer (Last + 1 .. End_Path - 2) :=
2061 Path_Buffer (Last + 3 .. End_Path);
2062 End_Path := End_Path - 2;
2065 -- Remove ".." fields
2067 elsif Finish = Start + 1
2068 and then Path_Buffer (Start .. Finish) = ".."
2073 exit when Start < 1 or else
2074 Path_Buffer (Start) = Directory_Separator;
2078 if Finish = End_Path then
2079 return (1 => Directory_Separator);
2082 Path_Buffer (1 .. End_Path - Finish) :=
2083 Path_Buffer (Finish + 1 .. End_Path);
2084 End_Path := End_Path - Finish;
2089 if Finish = End_Path then
2090 return Final_Value (Path_Buffer (1 .. Start - 1));
2093 Path_Buffer (Start + 1 .. Start + End_Path - Finish - 1) :=
2094 Path_Buffer (Finish + 2 .. End_Path);
2095 End_Path := Start + End_Path - Finish - 1;
2100 -- Check if current field is a symbolic link
2102 elsif Resolve_Links then
2104 Saved : constant Character := Path_Buffer (Finish + 1);
2107 Path_Buffer (Finish + 1) := ASCII.NUL;
2108 Status := Readlink (Path_Buffer'Address,
2109 Link_Buffer'Address,
2110 Link_Buffer'Length);
2111 Path_Buffer (Finish + 1) := Saved;
2114 -- Not a symbolic link, move to the next field, if any
2119 -- Replace symbolic link with its value
2122 if Is_Absolute_Path (Link_Buffer (1 .. Status)) then
2123 Path_Buffer (Status + 1 .. End_Path - (Finish - Status)) :=
2124 Path_Buffer (Finish + 1 .. End_Path);
2125 End_Path := End_Path - (Finish - Status);
2126 Path_Buffer (1 .. Status) := Link_Buffer (1 .. Status);
2131 (Last + Status + 1 .. End_Path - Finish + Last + Status) :=
2132 Path_Buffer (Finish + 1 .. End_Path);
2133 End_Path := End_Path - Finish + Last + Status;
2134 Path_Buffer (Last + 1 .. Last + Status) :=
2135 Link_Buffer (1 .. Status);
2144 -- Too many iterations: give up
2146 -- This can happen when there is a circularity in the symbolic links: A
2147 -- is a symbolic link for B, which itself is a symbolic link, and the
2148 -- target of B or of another symbolic link target of B is A. In this
2149 -- case, we return an empty string to indicate failure to resolve.
2152 end Normalize_Pathname;
2159 (Name : C_File_Name;
2160 Fmode : Mode) return File_Descriptor
2162 function C_Open_Read
2163 (Name : C_File_Name;
2164 Fmode : Mode) return File_Descriptor;
2165 pragma Import (C, C_Open_Read, "__gnat_open_read");
2167 return C_Open_Read (Name, Fmode);
2172 Fmode : Mode) return File_Descriptor
2174 C_Name : String (1 .. Name'Length + 1);
2176 C_Name (1 .. Name'Length) := Name;
2177 C_Name (C_Name'Last) := ASCII.NUL;
2178 return Open_Read (C_Name (C_Name'First)'Address, Fmode);
2181 ---------------------
2182 -- Open_Read_Write --
2183 ---------------------
2185 function Open_Read_Write
2186 (Name : C_File_Name;
2187 Fmode : Mode) return File_Descriptor
2189 function C_Open_Read_Write
2190 (Name : C_File_Name;
2191 Fmode : Mode) return File_Descriptor;
2192 pragma Import (C, C_Open_Read_Write, "__gnat_open_rw");
2194 return C_Open_Read_Write (Name, Fmode);
2195 end Open_Read_Write;
2197 function Open_Read_Write
2199 Fmode : Mode) return File_Descriptor
2201 C_Name : String (1 .. Name'Length + 1);
2203 C_Name (1 .. Name'Length) := Name;
2204 C_Name (C_Name'Last) := ASCII.NUL;
2205 return Open_Read_Write (C_Name (C_Name'First)'Address, Fmode);
2206 end Open_Read_Write;
2213 (FD : File_Descriptor;
2215 N : Integer) return Integer
2218 return Integer (System.CRTL.read
2219 (System.CRTL.int (FD), System.CRTL.chars (A), System.CRTL.int (N)));
2226 procedure Rename_File
2227 (Old_Name : C_File_Name;
2228 New_Name : C_File_Name;
2229 Success : out Boolean)
2231 function rename (From, To : Address) return Integer;
2232 pragma Import (C, rename, "rename");
2235 R := rename (Old_Name, New_Name);
2239 procedure Rename_File
2242 Success : out Boolean)
2244 C_Old_Name : String (1 .. Old_Name'Length + 1);
2245 C_New_Name : String (1 .. New_Name'Length + 1);
2247 C_Old_Name (1 .. Old_Name'Length) := Old_Name;
2248 C_Old_Name (C_Old_Name'Last) := ASCII.NUL;
2249 C_New_Name (1 .. New_Name'Length) := New_Name;
2250 C_New_Name (C_New_Name'Last) := ASCII.NUL;
2251 Rename_File (C_Old_Name'Address, C_New_Name'Address, Success);
2254 -----------------------
2255 -- Set_Close_On_Exec --
2256 -----------------------
2258 procedure Set_Close_On_Exec
2259 (FD : File_Descriptor;
2260 Close_On_Exec : Boolean;
2261 Status : out Boolean)
2263 function C_Set_Close_On_Exec
2264 (FD : File_Descriptor; Close_On_Exec : System.CRTL.int)
2265 return System.CRTL.int;
2266 pragma Import (C, C_Set_Close_On_Exec, "__gnat_set_close_on_exec");
2268 Status := C_Set_Close_On_Exec (FD, Boolean'Pos (Close_On_Exec)) = 0;
2269 end Set_Close_On_Exec;
2271 --------------------
2272 -- Set_Executable --
2273 --------------------
2275 procedure Set_Executable (Name : String) is
2276 procedure C_Set_Executable (Name : C_File_Name);
2277 pragma Import (C, C_Set_Executable, "__gnat_set_executable");
2278 C_Name : aliased String (Name'First .. Name'Last + 1);
2280 C_Name (Name'Range) := Name;
2281 C_Name (C_Name'Last) := ASCII.NUL;
2282 C_Set_Executable (C_Name (C_Name'First)'Address);
2285 --------------------
2287 --------------------
2289 procedure Set_Read_Only (Name : String) is
2290 procedure C_Set_Read_Only (Name : C_File_Name);
2291 pragma Import (C, C_Set_Read_Only, "__gnat_set_readonly");
2292 C_Name : aliased String (Name'First .. Name'Last + 1);
2294 C_Name (Name'Range) := Name;
2295 C_Name (C_Name'Last) := ASCII.NUL;
2296 C_Set_Read_Only (C_Name (C_Name'First)'Address);
2299 --------------------
2301 --------------------
2303 procedure Set_Writable (Name : String) is
2304 procedure C_Set_Writable (Name : C_File_Name);
2305 pragma Import (C, C_Set_Writable, "__gnat_set_writable");
2306 C_Name : aliased String (Name'First .. Name'Last + 1);
2308 C_Name (Name'Range) := Name;
2309 C_Name (C_Name'Last) := ASCII.NUL;
2310 C_Set_Writable (C_Name (C_Name'First)'Address);
2317 procedure Setenv (Name : String; Value : String) is
2318 F_Name : String (1 .. Name'Length + 1);
2319 F_Value : String (1 .. Value'Length + 1);
2321 procedure Set_Env_Value (Name, Value : System.Address);
2322 pragma Import (C, Set_Env_Value, "__gnat_setenv");
2325 F_Name (1 .. Name'Length) := Name;
2326 F_Name (F_Name'Last) := ASCII.NUL;
2328 F_Value (1 .. Value'Length) := Value;
2329 F_Value (F_Value'Last) := ASCII.NUL;
2331 Set_Env_Value (F_Name'Address, F_Value'Address);
2339 (Program_Name : String;
2340 Args : Argument_List) return Integer
2345 Spawn_Internal (Program_Name, Args, Result, Junk, Blocking => True);
2350 (Program_Name : String;
2351 Args : Argument_List;
2352 Success : out Boolean)
2355 Success := (Spawn (Program_Name, Args) = 0);
2359 (Program_Name : String;
2360 Args : Argument_List;
2361 Output_File_Descriptor : File_Descriptor;
2362 Return_Code : out Integer;
2363 Err_To_Out : Boolean := True)
2365 Saved_Output : File_Descriptor;
2366 Saved_Error : File_Descriptor := Invalid_FD; -- prevent compiler warning
2369 -- Set standard output and error to the temporary file
2371 Saved_Output := Dup (Standout);
2372 Dup2 (Output_File_Descriptor, Standout);
2375 Saved_Error := Dup (Standerr);
2376 Dup2 (Output_File_Descriptor, Standerr);
2379 -- Spawn the program
2381 Return_Code := Spawn (Program_Name, Args);
2383 -- Restore the standard output and error
2385 Dup2 (Saved_Output, Standout);
2388 Dup2 (Saved_Error, Standerr);
2391 -- And close the saved standard output and error file descriptors
2393 Close (Saved_Output);
2396 Close (Saved_Error);
2401 (Program_Name : String;
2402 Args : Argument_List;
2403 Output_File : String;
2404 Success : out Boolean;
2405 Return_Code : out Integer;
2406 Err_To_Out : Boolean := True)
2408 FD : File_Descriptor;
2414 FD := Create_Output_Text_File (Output_File);
2416 if FD = Invalid_FD then
2421 Spawn (Program_Name, Args, FD, Return_Code, Err_To_Out);
2423 Close (FD, Success);
2426 --------------------
2427 -- Spawn_Internal --
2428 --------------------
2430 procedure Spawn_Internal
2431 (Program_Name : String;
2432 Args : Argument_List;
2433 Result : out Integer;
2434 Pid : out Process_Id;
2438 procedure Spawn (Args : Argument_List);
2439 -- Call Spawn with given argument list
2441 N_Args : Argument_List (Args'Range);
2442 -- Normalized arguments
2448 procedure Spawn (Args : Argument_List) is
2449 type Chars is array (Positive range <>) of aliased Character;
2450 type Char_Ptr is access constant Character;
2452 Command_Len : constant Positive := Program_Name'Length + 1
2453 + Args_Length (Args);
2454 Command_Last : Natural := 0;
2455 Command : aliased Chars (1 .. Command_Len);
2456 -- Command contains all characters of the Program_Name and Args, all
2457 -- terminated by ASCII.NUL characters
2459 Arg_List_Len : constant Positive := Args'Length + 2;
2460 Arg_List_Last : Natural := 0;
2461 Arg_List : aliased array (1 .. Arg_List_Len) of Char_Ptr;
2462 -- List with pointers to NUL-terminated strings of the Program_Name
2463 -- and the Args and terminated with a null pointer. We rely on the
2464 -- default initialization for the last null pointer.
2466 procedure Add_To_Command (S : String);
2467 -- Add S and a NUL character to Command, updating Last
2469 function Portable_Spawn (Args : Address) return Integer;
2470 pragma Import (C, Portable_Spawn, "__gnat_portable_spawn");
2472 function Portable_No_Block_Spawn (Args : Address) return Process_Id;
2474 (C, Portable_No_Block_Spawn, "__gnat_portable_no_block_spawn");
2476 --------------------
2477 -- Add_To_Command --
2478 --------------------
2480 procedure Add_To_Command (S : String) is
2481 First : constant Natural := Command_Last + 1;
2484 Command_Last := Command_Last + S'Length;
2486 -- Move characters one at a time, because Command has aliased
2489 -- But not volatile, so why is this necessary ???
2491 for J in S'Range loop
2492 Command (First + J - S'First) := S (J);
2495 Command_Last := Command_Last + 1;
2496 Command (Command_Last) := ASCII.NUL;
2498 Arg_List_Last := Arg_List_Last + 1;
2499 Arg_List (Arg_List_Last) := Command (First)'Access;
2502 -- Start of processing for Spawn
2505 Add_To_Command (Program_Name);
2507 for J in Args'Range loop
2508 Add_To_Command (Args (J).all);
2513 Result := Portable_Spawn (Arg_List'Address);
2515 Pid := Portable_No_Block_Spawn (Arg_List'Address);
2516 Result := Boolean'Pos (Pid /= Invalid_Pid);
2520 -- Start of processing for Spawn_Internal
2523 -- Copy arguments into a local structure
2525 for K in N_Args'Range loop
2526 N_Args (K) := new String'(Args (K).all);
2529 -- Normalize those arguments
2531 Normalize_Arguments (N_Args);
2533 -- Call spawn using the normalized arguments
2537 -- Free arguments list
2539 for K in N_Args'Range loop
2544 ---------------------------
2545 -- To_Path_String_Access --
2546 ---------------------------
2548 function To_Path_String_Access
2549 (Path_Addr : Address;
2550 Path_Len : Integer) return String_Access
2552 subtype Path_String is String (1 .. Path_Len);
2553 type Path_String_Access is access Path_String;
2555 function Address_To_Access is new
2556 Unchecked_Conversion (Source => Address,
2557 Target => Path_String_Access);
2559 Path_Access : constant Path_String_Access :=
2560 Address_To_Access (Path_Addr);
2562 Return_Val : String_Access;
2565 Return_Val := new String (1 .. Path_Len);
2567 for J in 1 .. Path_Len loop
2568 Return_Val (J) := Path_Access (J);
2572 end To_Path_String_Access;
2578 procedure Wait_Process (Pid : out Process_Id; Success : out Boolean) is
2581 function Portable_Wait (S : Address) return Process_Id;
2582 pragma Import (C, Portable_Wait, "__gnat_portable_wait");
2585 Pid := Portable_Wait (Status'Address);
2586 Success := (Status = 0);
2594 (FD : File_Descriptor;
2596 N : Integer) return Integer
2599 return Integer (System.CRTL.write
2600 (System.CRTL.int (FD), System.CRTL.chars (A), System.CRTL.int (N)));