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 ------------------------------------------------------------------------------
36 with System.Case_Util;
38 with System.Soft_Links;
39 with Ada.Unchecked_Conversion;
40 with Ada.Unchecked_Deallocation;
41 with System; use System;
43 package body System.OS_Lib is
45 -- Imported procedures Dup and Dup2 are used in procedures Spawn and
46 -- Non_Blocking_Spawn.
48 function Dup (Fd : File_Descriptor) return File_Descriptor;
49 pragma Import (C, Dup, "__gnat_dup");
51 procedure Dup2 (Old_Fd, New_Fd : File_Descriptor);
52 pragma Import (C, Dup2, "__gnat_dup2");
54 On_Windows : constant Boolean := Directory_Separator = '\';
55 -- An indication that we are on Windows. Used in Normalize_Pathname, to
56 -- deal with drive letters in the beginning of absolute paths.
58 package SSL renames System.Soft_Links;
60 -- The following are used by Create_Temp_File
62 First_Temp_File_Name : constant String := "GNAT-TEMP-000000.TMP";
63 -- Used to initialize Current_Temp_File_Name and Temp_File_Name_Last_Digit
65 Current_Temp_File_Name : String := First_Temp_File_Name;
66 -- Name of the temp file last created
68 Temp_File_Name_Last_Digit : constant Positive :=
69 First_Temp_File_Name'Last - 4;
70 -- Position of the last digit in Current_Temp_File_Name
72 Max_Attempts : constant := 100;
73 -- The maximum number of attempts to create a new temp file
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 function Args_Length (Args : Argument_List) return Natural;
80 -- Returns total number of characters needed to create a string
81 -- of all Args terminated by ASCII.NUL characters
83 function C_String_Length (S : Address) return Integer;
84 -- Returns the length of a C string. Does check for null address
87 procedure Spawn_Internal
88 (Program_Name : String;
93 -- Internal routine to implement the two Spawn (blocking/non blocking)
94 -- routines. If Blocking is set to True then the spawn is blocking
95 -- otherwise it is non blocking. In this latter case the Pid contains the
96 -- process id number. The first three parameters are as in Spawn. Note that
97 -- Spawn_Internal normalizes the argument list before calling the low level
98 -- system spawn routines (see Normalize_Arguments).
100 -- Note: Normalize_Arguments is designed to do nothing if it is called more
101 -- than once, so calling Normalize_Arguments before calling one of the
102 -- spawn routines is fine.
104 function To_Path_String_Access
105 (Path_Addr : Address;
106 Path_Len : Integer) return String_Access;
107 -- Converts a C String to an Ada String. We could do this making use of
108 -- Interfaces.C.Strings but we prefer not to import that entire package
114 function "<" (X, Y : OS_Time) return Boolean is
116 return Long_Integer (X) < Long_Integer (Y);
123 function "<=" (X, Y : OS_Time) return Boolean is
125 return Long_Integer (X) <= Long_Integer (Y);
132 function ">" (X, Y : OS_Time) return Boolean is
134 return Long_Integer (X) > Long_Integer (Y);
141 function ">=" (X, Y : OS_Time) return Boolean is
143 return Long_Integer (X) >= Long_Integer (Y);
150 function Args_Length (Args : Argument_List) return Natural is
154 for J in Args'Range loop
155 Len := Len + Args (J)'Length + 1; -- One extra for ASCII.NUL
161 -----------------------------
162 -- Argument_String_To_List --
163 -----------------------------
165 function Argument_String_To_List
166 (Arg_String : String) return Argument_List_Access
168 Max_Args : constant Integer := Arg_String'Length;
169 New_Argv : Argument_List (1 .. Max_Args);
170 New_Argc : Natural := 0;
174 Idx := Arg_String'First;
177 exit when Idx > Arg_String'Last;
180 Quoted : Boolean := False;
181 Backqd : Boolean := False;
188 -- An unquoted space is the end of an argument
190 if not (Backqd or Quoted)
191 and then Arg_String (Idx) = ' '
195 -- Start of a quoted string
197 elsif not (Backqd or Quoted)
198 and then Arg_String (Idx) = '"'
202 -- End of a quoted string and end of an argument
204 elsif (Quoted and not Backqd)
205 and then Arg_String (Idx) = '"'
210 -- Following character is backquoted
212 elsif Arg_String (Idx) = '\' then
215 -- Turn off backquoting after advancing one character
223 exit when Idx > Arg_String'Last;
228 New_Argc := New_Argc + 1;
229 New_Argv (New_Argc) :=
230 new String'(Arg_String (Old_Idx .. Idx - 1));
232 -- Skip extraneous spaces
234 while Idx <= Arg_String'Last and then Arg_String (Idx) = ' ' loop
240 return new Argument_List'(New_Argv (1 .. New_Argc));
241 end Argument_String_To_List;
243 ---------------------
244 -- C_String_Length --
245 ---------------------
247 function C_String_Length (S : Address) return Integer is
248 function Strlen (S : Address) return Integer;
249 pragma Import (C, Strlen, "strlen");
251 if S = Null_Address then
262 procedure Close (FD : File_Descriptor) is
263 procedure C_Close (FD : File_Descriptor);
264 pragma Import (C, C_Close, "close");
269 procedure Close (FD : File_Descriptor; Status : out Boolean) is
270 function C_Close (FD : File_Descriptor) return Integer;
271 pragma Import (C, C_Close, "close");
273 Status := (C_Close (FD) = 0);
283 Success : out Boolean;
284 Mode : Copy_Mode := Copy;
285 Preserve : Attribute := Time_Stamps)
287 From : File_Descriptor;
288 To : File_Descriptor;
290 Copy_Error : exception;
291 -- Internal exception raised to signal error in copy
293 function Build_Path (Dir : String; File : String) return String;
294 -- Returns pathname Dir concatenated with File adding the directory
295 -- separator only if needed.
297 procedure Copy (From, To : File_Descriptor);
298 -- Read data from From and place them into To. In both cases the
299 -- operations uses the current file position. Raises Constraint_Error
300 -- if a problem occurs during the copy.
302 procedure Copy_To (To_Name : String);
303 -- Does a straight copy from source to designated destination file
309 function Build_Path (Dir : String; File : String) return String is
310 Res : String (1 .. Dir'Length + File'Length + 1);
312 Base_File_Ptr : Integer;
313 -- The base file name is File (Base_File_Ptr + 1 .. File'Last)
315 function Is_Dirsep (C : Character) return Boolean;
316 pragma Inline (Is_Dirsep);
317 -- Returns True if C is a directory separator. On Windows we
318 -- handle both styles of directory separator.
324 function Is_Dirsep (C : Character) return Boolean is
326 return C = Directory_Separator or else C = '/';
329 -- Start of processing for Build_Path
332 -- Find base file name
334 Base_File_Ptr := File'Last;
335 while Base_File_Ptr >= File'First loop
336 exit when Is_Dirsep (File (Base_File_Ptr));
337 Base_File_Ptr := Base_File_Ptr - 1;
341 Base_File : String renames
342 File (Base_File_Ptr + 1 .. File'Last);
345 Res (1 .. Dir'Length) := Dir;
347 if Is_Dirsep (Dir (Dir'Last)) then
348 Res (Dir'Length + 1 .. Dir'Length + Base_File'Length) :=
350 return Res (1 .. Dir'Length + Base_File'Length);
353 Res (Dir'Length + 1) := Directory_Separator;
354 Res (Dir'Length + 2 .. Dir'Length + 1 + Base_File'Length) :=
356 return Res (1 .. Dir'Length + 1 + Base_File'Length);
365 procedure Copy (From, To : File_Descriptor) is
366 Buf_Size : constant := 200_000;
367 type Buf is array (1 .. Buf_Size) of Character;
368 type Buf_Ptr is access Buf;
374 Status_From : Boolean;
376 -- Statuses for the calls to Close
378 procedure Free is new Ada.Unchecked_Deallocation (Buf, Buf_Ptr);
381 -- Check for invalid descriptors, making sure that we do not
382 -- accidentally leave an open file descriptor around.
384 if From = Invalid_FD then
385 if To /= Invalid_FD then
386 Close (To, Status_To);
391 elsif To = Invalid_FD then
392 Close (From, Status_From);
396 -- Allocate the buffer on the heap
401 R := Read (From, Buffer (1)'Address, Buf_Size);
403 -- For VMS, the buffer may not be full. So, we need to try again
404 -- until there is nothing to read.
408 W := Write (To, Buffer (1)'Address, R);
412 -- Problem writing data, could be a disk full. Close files
413 -- without worrying about status, since we are raising a
414 -- Copy_Error exception in any case.
416 Close (From, Status_From);
417 Close (To, Status_To);
425 Close (From, Status_From);
426 Close (To, Status_To);
430 if not (Status_From and Status_To) then
439 procedure Copy_To (To_Name : String) is
441 function Copy_Attributes
442 (From, To : System.Address;
443 Mode : Integer) return Integer;
444 pragma Import (C, Copy_Attributes, "__gnat_copy_attribs");
445 -- Mode = 0 - copy only time stamps.
446 -- Mode = 1 - copy time stamps and read/write/execute attributes
448 C_From : String (1 .. Name'Length + 1);
449 C_To : String (1 .. To_Name'Length + 1);
452 From := Open_Read (Name, Binary);
454 -- Do not clobber destination file if source file could not be opened
456 if From /= Invalid_FD then
457 To := Create_File (To_Name, Binary);
464 C_From (1 .. Name'Length) := Name;
465 C_From (C_From'Last) := ASCII.NUL;
467 C_To (1 .. To_Name'Length) := To_Name;
468 C_To (C_To'Last) := ASCII.NUL;
473 if Copy_Attributes (C_From'Address, C_To'Address, 0) = -1 then
478 if Copy_Attributes (C_From'Address, C_To'Address, 1) = -1 then
488 -- Start of processing for Copy_File
493 -- The source file must exist
495 if not Is_Regular_File (Name) then
499 -- The source file exists
503 -- Copy case, target file must not exist
507 -- If the target file exists, we have an error
509 if Is_Regular_File (Pathname) then
512 -- Case of target is a directory
514 elsif Is_Directory (Pathname) then
516 Dest : constant String := Build_Path (Pathname, Name);
519 -- If target file exists, we have an error, else do copy
521 if Is_Regular_File (Dest) then
528 -- Case of normal copy to file (destination does not exist)
534 -- Overwrite case (destination file may or may not exist)
537 if Is_Directory (Pathname) then
538 Copy_To (Build_Path (Pathname, Name));
543 -- Append case (destination file may or may not exist)
547 -- Appending to existing file
549 if Is_Regular_File (Pathname) then
551 -- Append mode and destination file exists, append data at the
552 -- end of Pathname. But if we fail to open source file, do not
553 -- touch destination file at all.
555 From := Open_Read (Name, Binary);
556 if From /= Invalid_FD then
557 To := Open_Read_Write (Pathname, Binary);
560 Lseek (To, 0, Seek_End);
564 -- Appending to directory, not allowed
566 elsif Is_Directory (Pathname) then
569 -- Appending when target file does not exist
576 -- All error cases are caught here
585 Pathname : C_File_Name;
586 Success : out Boolean;
587 Mode : Copy_Mode := Copy;
588 Preserve : Attribute := Time_Stamps)
590 Ada_Name : String_Access :=
591 To_Path_String_Access
592 (Name, C_String_Length (Name));
593 Ada_Pathname : String_Access :=
594 To_Path_String_Access
595 (Pathname, C_String_Length (Pathname));
597 Copy_File (Ada_Name.all, Ada_Pathname.all, Success, Mode, Preserve);
602 ----------------------
603 -- Copy_Time_Stamps --
604 ----------------------
606 procedure Copy_Time_Stamps (Source, Dest : String; Success : out Boolean) is
608 function Copy_Attributes
609 (From, To : System.Address;
610 Mode : Integer) return Integer;
611 pragma Import (C, Copy_Attributes, "__gnat_copy_attribs");
612 -- Mode = 0 - copy only time stamps.
613 -- Mode = 1 - copy time stamps and read/write/execute attributes
616 if Is_Regular_File (Source) and then Is_Writable_File (Dest) then
618 C_Source : String (1 .. Source'Length + 1);
619 C_Dest : String (1 .. Dest'Length + 1);
622 C_Source (1 .. Source'Length) := Source;
623 C_Source (C_Source'Last) := ASCII.NUL;
625 C_Dest (1 .. Dest'Length) := Dest;
626 C_Dest (C_Dest'Last) := ASCII.NUL;
628 if Copy_Attributes (C_Source'Address, C_Dest'Address, 0) = -1 then
638 end Copy_Time_Stamps;
640 procedure Copy_Time_Stamps
641 (Source, Dest : C_File_Name;
642 Success : out Boolean)
644 Ada_Source : String_Access :=
645 To_Path_String_Access
646 (Source, C_String_Length (Source));
647 Ada_Dest : String_Access :=
648 To_Path_String_Access
649 (Dest, C_String_Length (Dest));
651 Copy_Time_Stamps (Ada_Source.all, Ada_Dest.all, Success);
654 end Copy_Time_Stamps;
662 Fmode : Mode) return File_Descriptor
664 function C_Create_File
666 Fmode : Mode) return File_Descriptor;
667 pragma Import (C, C_Create_File, "__gnat_open_create");
670 return C_Create_File (Name, Fmode);
675 Fmode : Mode) return File_Descriptor
677 C_Name : String (1 .. Name'Length + 1);
680 C_Name (1 .. Name'Length) := Name;
681 C_Name (C_Name'Last) := ASCII.NUL;
682 return Create_File (C_Name (C_Name'First)'Address, Fmode);
685 ---------------------
686 -- Create_New_File --
687 ---------------------
689 function Create_New_File
691 Fmode : Mode) return File_Descriptor
693 function C_Create_New_File
695 Fmode : Mode) return File_Descriptor;
696 pragma Import (C, C_Create_New_File, "__gnat_open_new");
699 return C_Create_New_File (Name, Fmode);
702 function Create_New_File
704 Fmode : Mode) return File_Descriptor
706 C_Name : String (1 .. Name'Length + 1);
709 C_Name (1 .. Name'Length) := Name;
710 C_Name (C_Name'Last) := ASCII.NUL;
711 return Create_New_File (C_Name (C_Name'First)'Address, Fmode);
714 -----------------------------
715 -- Create_Output_Text_File --
716 -----------------------------
718 function Create_Output_Text_File (Name : String) return File_Descriptor is
719 function C_Create_File
720 (Name : C_File_Name) return File_Descriptor;
721 pragma Import (C, C_Create_File, "__gnat_create_output_file");
723 C_Name : String (1 .. Name'Length + 1);
726 C_Name (1 .. Name'Length) := Name;
727 C_Name (C_Name'Last) := ASCII.NUL;
728 return C_Create_File (C_Name (C_Name'First)'Address);
729 end Create_Output_Text_File;
731 ----------------------
732 -- Create_Temp_File --
733 ----------------------
735 procedure Create_Temp_File
736 (FD : out File_Descriptor;
737 Name : out Temp_File_Name)
739 function Open_New_Temp
740 (Name : System.Address;
741 Fmode : Mode) return File_Descriptor;
742 pragma Import (C, Open_New_Temp, "__gnat_open_new_temp");
745 FD := Open_New_Temp (Name'Address, Binary);
746 end Create_Temp_File;
748 procedure Create_Temp_File
749 (FD : out File_Descriptor;
750 Name : out String_Access)
753 Attempts : Natural := 0;
754 Current : String (Current_Temp_File_Name'Range);
757 -- Loop until a new temp file can be created
761 -- We need to protect global variable Current_Temp_File_Name
762 -- against concurrent access by different tasks.
766 -- Start at the last digit
768 Pos := Temp_File_Name_Last_Digit;
772 -- Increment the digit by one
774 case Current_Temp_File_Name (Pos) is
776 Current_Temp_File_Name (Pos) :=
777 Character'Succ (Current_Temp_File_Name (Pos));
782 -- For 9, set the digit to 0 and go to the previous digit
784 Current_Temp_File_Name (Pos) := '0';
789 -- If it is not a digit, then there are no available
790 -- temp file names. Return Invalid_FD. There is almost
791 -- no chance that this code will be ever be executed,
792 -- since it would mean that there are one million temp
793 -- files in the same directory!
802 Current := Current_Temp_File_Name;
804 -- We can now release the lock, because we are no longer
805 -- accessing Current_Temp_File_Name.
815 -- Attempt to create the file
817 FD := Create_New_File (Current, Binary);
819 if FD /= Invalid_FD then
820 Name := new String'(Current);
824 if not Is_Regular_File (Current) then
826 -- If the file does not already exist and we are unable to create
827 -- it, we give up after Max_Attempts. Otherwise, we try again with
828 -- the next available file name.
830 Attempts := Attempts + 1;
832 if Attempts >= Max_Attempts then
839 end Create_Temp_File;
845 procedure Delete_File (Name : Address; Success : out Boolean) is
848 R := System.CRTL.unlink (Name);
852 procedure Delete_File (Name : String; Success : out Boolean) is
853 C_Name : String (1 .. Name'Length + 1);
856 C_Name (1 .. Name'Length) := Name;
857 C_Name (C_Name'Last) := ASCII.NUL;
859 Delete_File (C_Name'Address, Success);
862 ---------------------
863 -- File_Time_Stamp --
864 ---------------------
866 function File_Time_Stamp (FD : File_Descriptor) return OS_Time is
867 function File_Time (FD : File_Descriptor) return OS_Time;
868 pragma Import (C, File_Time, "__gnat_file_time_fd");
870 return File_Time (FD);
873 function File_Time_Stamp (Name : C_File_Name) return OS_Time is
874 function File_Time (Name : Address) return OS_Time;
875 pragma Import (C, File_Time, "__gnat_file_time_name");
877 return File_Time (Name);
880 function File_Time_Stamp (Name : String) return OS_Time is
881 F_Name : String (1 .. Name'Length + 1);
883 F_Name (1 .. Name'Length) := Name;
884 F_Name (F_Name'Last) := ASCII.NUL;
885 return File_Time_Stamp (F_Name'Address);
888 ---------------------------
889 -- Get_Debuggable_Suffix --
890 ---------------------------
892 function Get_Debuggable_Suffix return String_Access is
893 procedure Get_Suffix_Ptr (Length, Ptr : Address);
894 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_debuggable_suffix_ptr");
896 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
897 pragma Import (C, Strncpy, "strncpy");
899 Suffix_Ptr : Address;
900 Suffix_Length : Integer;
901 Result : String_Access;
904 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
906 Result := new String (1 .. Suffix_Length);
908 if Suffix_Length > 0 then
909 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
913 end Get_Debuggable_Suffix;
915 ---------------------------
916 -- Get_Executable_Suffix --
917 ---------------------------
919 function Get_Executable_Suffix return String_Access is
920 procedure Get_Suffix_Ptr (Length, Ptr : Address);
921 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_executable_suffix_ptr");
923 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
924 pragma Import (C, Strncpy, "strncpy");
926 Suffix_Ptr : Address;
927 Suffix_Length : Integer;
928 Result : String_Access;
931 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
933 Result := new String (1 .. Suffix_Length);
935 if Suffix_Length > 0 then
936 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
940 end Get_Executable_Suffix;
942 -----------------------
943 -- Get_Object_Suffix --
944 -----------------------
946 function Get_Object_Suffix return String_Access is
947 procedure Get_Suffix_Ptr (Length, Ptr : Address);
948 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_object_suffix_ptr");
950 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
951 pragma Import (C, Strncpy, "strncpy");
953 Suffix_Ptr : Address;
954 Suffix_Length : Integer;
955 Result : String_Access;
958 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
960 Result := new String (1 .. Suffix_Length);
962 if Suffix_Length > 0 then
963 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
967 end Get_Object_Suffix;
969 ----------------------------------
970 -- Get_Target_Debuggable_Suffix --
971 ----------------------------------
973 function Get_Target_Debuggable_Suffix return String_Access is
974 Target_Exec_Ext_Ptr : Address;
976 (C, Target_Exec_Ext_Ptr, "__gnat_target_debuggable_extension");
978 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
979 pragma Import (C, Strncpy, "strncpy");
981 function Strlen (Cstring : Address) return Integer;
982 pragma Import (C, Strlen, "strlen");
984 Suffix_Length : Integer;
985 Result : String_Access;
988 Suffix_Length := Strlen (Target_Exec_Ext_Ptr);
990 Result := new String (1 .. Suffix_Length);
992 if Suffix_Length > 0 then
993 Strncpy (Result.all'Address, Target_Exec_Ext_Ptr, Suffix_Length);
997 end Get_Target_Debuggable_Suffix;
999 ----------------------------------
1000 -- Get_Target_Executable_Suffix --
1001 ----------------------------------
1003 function Get_Target_Executable_Suffix return String_Access is
1004 Target_Exec_Ext_Ptr : Address;
1006 (C, Target_Exec_Ext_Ptr, "__gnat_target_executable_extension");
1008 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1009 pragma Import (C, Strncpy, "strncpy");
1011 function Strlen (Cstring : Address) return Integer;
1012 pragma Import (C, Strlen, "strlen");
1014 Suffix_Length : Integer;
1015 Result : String_Access;
1018 Suffix_Length := Strlen (Target_Exec_Ext_Ptr);
1020 Result := new String (1 .. Suffix_Length);
1022 if Suffix_Length > 0 then
1023 Strncpy (Result.all'Address, Target_Exec_Ext_Ptr, Suffix_Length);
1027 end Get_Target_Executable_Suffix;
1029 ------------------------------
1030 -- Get_Target_Object_Suffix --
1031 ------------------------------
1033 function Get_Target_Object_Suffix return String_Access is
1034 Target_Object_Ext_Ptr : Address;
1036 (C, Target_Object_Ext_Ptr, "__gnat_target_object_extension");
1038 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1039 pragma Import (C, Strncpy, "strncpy");
1041 function Strlen (Cstring : Address) return Integer;
1042 pragma Import (C, Strlen, "strlen");
1044 Suffix_Length : Integer;
1045 Result : String_Access;
1048 Suffix_Length := Strlen (Target_Object_Ext_Ptr);
1050 Result := new String (1 .. Suffix_Length);
1052 if Suffix_Length > 0 then
1053 Strncpy (Result.all'Address, Target_Object_Ext_Ptr, Suffix_Length);
1057 end Get_Target_Object_Suffix;
1063 function Getenv (Name : String) return String_Access is
1064 procedure Get_Env_Value_Ptr (Name, Length, Ptr : Address);
1065 pragma Import (C, Get_Env_Value_Ptr, "__gnat_getenv");
1067 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1068 pragma Import (C, Strncpy, "strncpy");
1070 Env_Value_Ptr : aliased Address;
1071 Env_Value_Length : aliased Integer;
1072 F_Name : aliased String (1 .. Name'Length + 1);
1073 Result : String_Access;
1076 F_Name (1 .. Name'Length) := Name;
1077 F_Name (F_Name'Last) := ASCII.NUL;
1080 (F_Name'Address, Env_Value_Length'Address, Env_Value_Ptr'Address);
1082 Result := new String (1 .. Env_Value_Length);
1084 if Env_Value_Length > 0 then
1085 Strncpy (Result.all'Address, Env_Value_Ptr, Env_Value_Length);
1095 function GM_Day (Date : OS_Time) return Day_Type is
1098 pragma Warnings (Off);
1104 pragma Warnings (On);
1107 GM_Split (Date, Y, Mo, D, H, Mn, S);
1115 function GM_Hour (Date : OS_Time) return Hour_Type is
1118 pragma Warnings (Off);
1124 pragma Warnings (On);
1127 GM_Split (Date, Y, Mo, D, H, Mn, S);
1135 function GM_Minute (Date : OS_Time) return Minute_Type is
1138 pragma Warnings (Off);
1144 pragma Warnings (On);
1147 GM_Split (Date, Y, Mo, D, H, Mn, S);
1155 function GM_Month (Date : OS_Time) return Month_Type is
1158 pragma Warnings (Off);
1164 pragma Warnings (On);
1167 GM_Split (Date, Y, Mo, D, H, Mn, S);
1175 function GM_Second (Date : OS_Time) return Second_Type is
1178 pragma Warnings (Off);
1184 pragma Warnings (On);
1187 GM_Split (Date, Y, Mo, D, H, Mn, S);
1197 Year : out Year_Type;
1198 Month : out Month_Type;
1200 Hour : out Hour_Type;
1201 Minute : out Minute_Type;
1202 Second : out Second_Type)
1204 procedure To_GM_Time
1205 (P_Time_T, P_Year, P_Month, P_Day, P_Hours, P_Mins, P_Secs : Address);
1206 pragma Import (C, To_GM_Time, "__gnat_to_gm_time");
1208 T : OS_Time := Date;
1217 -- Use the global lock because To_GM_Time is not thread safe
1219 Locked_Processing : begin
1222 (T'Address, Y'Address, Mo'Address, D'Address,
1223 H'Address, Mn'Address, S'Address);
1224 SSL.Unlock_Task.all;
1228 SSL.Unlock_Task.all;
1230 end Locked_Processing;
1244 function GM_Year (Date : OS_Time) return Year_Type is
1247 pragma Warnings (Off);
1253 pragma Warnings (On);
1256 GM_Split (Date, Y, Mo, D, H, Mn, S);
1260 ----------------------
1261 -- Is_Absolute_Path --
1262 ----------------------
1264 function Is_Absolute_Path (Name : String) return Boolean is
1265 function Is_Absolute_Path
1267 Length : Integer) return Integer;
1268 pragma Import (C, Is_Absolute_Path, "__gnat_is_absolute_path");
1270 return Is_Absolute_Path (Name'Address, Name'Length) /= 0;
1271 end Is_Absolute_Path;
1277 function Is_Directory (Name : C_File_Name) return Boolean is
1278 function Is_Directory (Name : Address) return Integer;
1279 pragma Import (C, Is_Directory, "__gnat_is_directory");
1281 return Is_Directory (Name) /= 0;
1284 function Is_Directory (Name : String) return Boolean is
1285 F_Name : String (1 .. Name'Length + 1);
1287 F_Name (1 .. Name'Length) := Name;
1288 F_Name (F_Name'Last) := ASCII.NUL;
1289 return Is_Directory (F_Name'Address);
1292 ----------------------
1293 -- Is_Readable_File --
1294 ----------------------
1296 function Is_Readable_File (Name : C_File_Name) return Boolean is
1297 function Is_Readable_File (Name : Address) return Integer;
1298 pragma Import (C, Is_Readable_File, "__gnat_is_readable_file");
1300 return Is_Readable_File (Name) /= 0;
1301 end Is_Readable_File;
1303 function Is_Readable_File (Name : String) return Boolean is
1304 F_Name : String (1 .. Name'Length + 1);
1306 F_Name (1 .. Name'Length) := Name;
1307 F_Name (F_Name'Last) := ASCII.NUL;
1308 return Is_Readable_File (F_Name'Address);
1309 end Is_Readable_File;
1311 ------------------------
1312 -- Is_Executable_File --
1313 ------------------------
1315 function Is_Executable_File (Name : C_File_Name) return Boolean is
1316 function Is_Executable_File (Name : Address) return Integer;
1317 pragma Import (C, Is_Executable_File, "__gnat_is_executable_file");
1319 return Is_Executable_File (Name) /= 0;
1320 end Is_Executable_File;
1322 function Is_Executable_File (Name : String) return Boolean is
1323 F_Name : String (1 .. Name'Length + 1);
1325 F_Name (1 .. Name'Length) := Name;
1326 F_Name (F_Name'Last) := ASCII.NUL;
1327 return Is_Executable_File (F_Name'Address);
1328 end Is_Executable_File;
1330 ---------------------
1331 -- Is_Regular_File --
1332 ---------------------
1334 function Is_Regular_File (Name : C_File_Name) return Boolean is
1335 function Is_Regular_File (Name : Address) return Integer;
1336 pragma Import (C, Is_Regular_File, "__gnat_is_regular_file");
1338 return Is_Regular_File (Name) /= 0;
1339 end Is_Regular_File;
1341 function Is_Regular_File (Name : String) return Boolean is
1342 F_Name : String (1 .. Name'Length + 1);
1344 F_Name (1 .. Name'Length) := Name;
1345 F_Name (F_Name'Last) := ASCII.NUL;
1346 return Is_Regular_File (F_Name'Address);
1347 end Is_Regular_File;
1349 ----------------------
1350 -- Is_Symbolic_Link --
1351 ----------------------
1353 function Is_Symbolic_Link (Name : C_File_Name) return Boolean is
1354 function Is_Symbolic_Link (Name : Address) return Integer;
1355 pragma Import (C, Is_Symbolic_Link, "__gnat_is_symbolic_link");
1357 return Is_Symbolic_Link (Name) /= 0;
1358 end Is_Symbolic_Link;
1360 function Is_Symbolic_Link (Name : String) return Boolean is
1361 F_Name : String (1 .. Name'Length + 1);
1363 F_Name (1 .. Name'Length) := Name;
1364 F_Name (F_Name'Last) := ASCII.NUL;
1365 return Is_Symbolic_Link (F_Name'Address);
1366 end Is_Symbolic_Link;
1368 ----------------------
1369 -- Is_Writable_File --
1370 ----------------------
1372 function Is_Writable_File (Name : C_File_Name) return Boolean is
1373 function Is_Writable_File (Name : Address) return Integer;
1374 pragma Import (C, Is_Writable_File, "__gnat_is_writable_file");
1376 return Is_Writable_File (Name) /= 0;
1377 end Is_Writable_File;
1379 function Is_Writable_File (Name : String) return Boolean is
1380 F_Name : String (1 .. Name'Length + 1);
1382 F_Name (1 .. Name'Length) := Name;
1383 F_Name (F_Name'Last) := ASCII.NUL;
1384 return Is_Writable_File (F_Name'Address);
1385 end Is_Writable_File;
1387 -------------------------
1388 -- Locate_Exec_On_Path --
1389 -------------------------
1391 function Locate_Exec_On_Path
1392 (Exec_Name : String) return String_Access
1394 function Locate_Exec_On_Path (C_Exec_Name : Address) return Address;
1395 pragma Import (C, Locate_Exec_On_Path, "__gnat_locate_exec_on_path");
1397 procedure Free (Ptr : System.Address);
1398 pragma Import (C, Free, "free");
1400 C_Exec_Name : String (1 .. Exec_Name'Length + 1);
1401 Path_Addr : Address;
1403 Result : String_Access;
1406 C_Exec_Name (1 .. Exec_Name'Length) := Exec_Name;
1407 C_Exec_Name (C_Exec_Name'Last) := ASCII.NUL;
1409 Path_Addr := Locate_Exec_On_Path (C_Exec_Name'Address);
1410 Path_Len := C_String_Length (Path_Addr);
1412 if Path_Len = 0 then
1416 Result := To_Path_String_Access (Path_Addr, Path_Len);
1419 -- Always return an absolute path name
1421 if not Is_Absolute_Path (Result.all) then
1423 Absolute_Path : constant String :=
1424 Normalize_Pathname (Result.all);
1427 Result := new String'(Absolute_Path);
1433 end Locate_Exec_On_Path;
1435 -------------------------
1436 -- Locate_Regular_File --
1437 -------------------------
1439 function Locate_Regular_File
1440 (File_Name : C_File_Name;
1441 Path : C_File_Name) return String_Access
1443 function Locate_Regular_File
1444 (C_File_Name, Path_Val : Address) return Address;
1445 pragma Import (C, Locate_Regular_File, "__gnat_locate_regular_file");
1447 procedure Free (Ptr : System.Address);
1448 pragma Import (C, Free, "free");
1450 Path_Addr : Address;
1452 Result : String_Access;
1455 Path_Addr := Locate_Regular_File (File_Name, Path);
1456 Path_Len := C_String_Length (Path_Addr);
1458 if Path_Len = 0 then
1462 Result := To_Path_String_Access (Path_Addr, Path_Len);
1466 end Locate_Regular_File;
1468 function Locate_Regular_File
1469 (File_Name : String;
1470 Path : String) return String_Access
1472 C_File_Name : String (1 .. File_Name'Length + 1);
1473 C_Path : String (1 .. Path'Length + 1);
1474 Result : String_Access;
1477 C_File_Name (1 .. File_Name'Length) := File_Name;
1478 C_File_Name (C_File_Name'Last) := ASCII.NUL;
1480 C_Path (1 .. Path'Length) := Path;
1481 C_Path (C_Path'Last) := ASCII.NUL;
1483 Result := Locate_Regular_File (C_File_Name'Address, C_Path'Address);
1485 -- Always return an absolute path name
1487 if Result /= null and then not Is_Absolute_Path (Result.all) then
1489 Absolute_Path : constant String := Normalize_Pathname (Result.all);
1492 Result := new String'(Absolute_Path);
1497 end Locate_Regular_File;
1499 ------------------------
1500 -- Non_Blocking_Spawn --
1501 ------------------------
1503 function Non_Blocking_Spawn
1504 (Program_Name : String;
1505 Args : Argument_List) return Process_Id
1509 pragma Warnings (Off, Junk);
1511 Spawn_Internal (Program_Name, Args, Junk, Pid, Blocking => False);
1513 end Non_Blocking_Spawn;
1515 function Non_Blocking_Spawn
1516 (Program_Name : String;
1517 Args : Argument_List;
1518 Output_File_Descriptor : File_Descriptor;
1519 Err_To_Out : Boolean := True) return Process_Id
1521 Saved_Output : File_Descriptor;
1522 Saved_Error : File_Descriptor := Invalid_FD; -- prevent warning
1526 if Output_File_Descriptor = Invalid_FD then
1530 -- Set standard output and, if specified, error to the temporary file
1532 Saved_Output := Dup (Standout);
1533 Dup2 (Output_File_Descriptor, Standout);
1536 Saved_Error := Dup (Standerr);
1537 Dup2 (Output_File_Descriptor, Standerr);
1540 -- Spawn the program
1542 Pid := Non_Blocking_Spawn (Program_Name, Args);
1544 -- Restore the standard output and error
1546 Dup2 (Saved_Output, Standout);
1549 Dup2 (Saved_Error, Standerr);
1552 -- And close the saved standard output and error file descriptors
1554 Close (Saved_Output);
1557 Close (Saved_Error);
1561 end Non_Blocking_Spawn;
1563 function Non_Blocking_Spawn
1564 (Program_Name : String;
1565 Args : Argument_List;
1566 Output_File : String;
1567 Err_To_Out : Boolean := True) return Process_Id
1569 Output_File_Descriptor : constant File_Descriptor :=
1570 Create_Output_Text_File (Output_File);
1571 Result : Process_Id;
1574 -- Do not attempt to spawn if the output file could not be created
1576 if Output_File_Descriptor = Invalid_FD then
1580 Result := Non_Blocking_Spawn
1581 (Program_Name, Args, Output_File_Descriptor, Err_To_Out);
1583 -- Close the file just created for the output, as the file descriptor
1584 -- cannot be used anywhere, being a local value. It is safe to do
1585 -- that, as the file descriptor has been duplicated to form
1586 -- standard output and error of the spawned process.
1588 Close (Output_File_Descriptor);
1592 end Non_Blocking_Spawn;
1594 -------------------------
1595 -- Normalize_Arguments --
1596 -------------------------
1598 procedure Normalize_Arguments (Args : in out Argument_List) is
1600 procedure Quote_Argument (Arg : in out String_Access);
1601 -- Add quote around argument if it contains spaces
1603 C_Argument_Needs_Quote : Integer;
1604 pragma Import (C, C_Argument_Needs_Quote, "__gnat_argument_needs_quote");
1605 Argument_Needs_Quote : constant Boolean := C_Argument_Needs_Quote /= 0;
1607 --------------------
1608 -- Quote_Argument --
1609 --------------------
1611 procedure Quote_Argument (Arg : in out String_Access) is
1612 Res : String (1 .. Arg'Length * 2);
1614 Quote_Needed : Boolean := False;
1617 if Arg (Arg'First) /= '"' or else Arg (Arg'Last) /= '"' then
1623 for K in Arg'Range loop
1627 if Arg (K) = '"' then
1631 Quote_Needed := True;
1633 elsif Arg (K) = ' ' then
1635 Quote_Needed := True;
1643 if Quote_Needed then
1645 -- If null terminated string, put the quote before
1647 if Res (J) = ASCII.NUL then
1650 Res (J) := ASCII.NUL;
1652 -- If argument is terminated by '\', then double it. Otherwise
1653 -- the ending quote will be taken as-is. This is quite strange
1654 -- spawn behavior from Windows, but this is what we see!
1657 if Res (J) = '\' then
1669 Old : String_Access := Arg;
1672 Arg := new String'(Res (1 .. J));
1680 -- Start of processing for Normalize_Arguments
1683 if Argument_Needs_Quote then
1684 for K in Args'Range loop
1685 if Args (K) /= null and then Args (K)'Length /= 0 then
1686 Quote_Argument (Args (K));
1690 end Normalize_Arguments;
1692 ------------------------
1693 -- Normalize_Pathname --
1694 ------------------------
1696 function Normalize_Pathname
1698 Directory : String := "";
1699 Resolve_Links : Boolean := True;
1700 Case_Sensitive : Boolean := True) return String
1703 pragma Import (C, Max_Path, "__gnat_max_path_len");
1704 -- Maximum length of a path name
1706 procedure Get_Current_Dir
1707 (Dir : System.Address;
1708 Length : System.Address);
1709 pragma Import (C, Get_Current_Dir, "__gnat_get_current_dir");
1711 Path_Buffer : String (1 .. Max_Path + Max_Path + 2);
1712 End_Path : Natural := 0;
1713 Link_Buffer : String (1 .. Max_Path + 2);
1719 Max_Iterations : constant := 500;
1721 function Get_File_Names_Case_Sensitive return Integer;
1723 (C, Get_File_Names_Case_Sensitive,
1724 "__gnat_get_file_names_case_sensitive");
1726 Fold_To_Lower_Case : constant Boolean :=
1728 and then Get_File_Names_Case_Sensitive = 0;
1731 (Path : System.Address;
1732 Buf : System.Address;
1733 Bufsiz : Integer) return Integer;
1734 pragma Import (C, Readlink, "__gnat_readlink");
1736 function To_Canonical_File_Spec
1737 (Host_File : System.Address) return System.Address;
1739 (C, To_Canonical_File_Spec, "__gnat_to_canonical_file_spec");
1741 The_Name : String (1 .. Name'Length + 1);
1742 Canonical_File_Addr : System.Address;
1743 Canonical_File_Len : Integer;
1745 function Strlen (S : System.Address) return Integer;
1746 pragma Import (C, Strlen, "strlen");
1748 function Final_Value (S : String) return String;
1749 -- Make final adjustment to the returned string. This function strips
1750 -- trailing directory separators, and folds returned string to lower
1751 -- case if required.
1753 function Get_Directory (Dir : String) return String;
1754 -- If Dir is not empty, return it, adding a directory separator
1755 -- if not already present, otherwise return current working directory
1756 -- with terminating directory separator.
1762 function Final_Value (S : String) return String is
1764 -- We may need to fold S to lower case, so we need a variable
1769 if Fold_To_Lower_Case then
1770 System.Case_Util.To_Lower (S1);
1773 -- Remove trailing directory separator, if any
1778 and then (S1 (Last) = '/'
1780 S1 (Last) = Directory_Separator)
1782 -- Special case for Windows: C:\
1785 and then S1 (1) /= Directory_Separator
1786 and then S1 (2) = ':'
1795 return S1 (1 .. Last);
1802 function Get_Directory (Dir : String) return String is
1804 -- Directory given, add directory separator if needed
1806 if Dir'Length > 0 then
1807 if Dir (Dir'Last) = Directory_Separator then
1811 Result : String (1 .. Dir'Length + 1);
1813 Result (1 .. Dir'Length) := Dir;
1814 Result (Result'Length) := Directory_Separator;
1819 -- Directory name not given, get current directory
1823 Buffer : String (1 .. Max_Path + 2);
1824 Path_Len : Natural := Max_Path;
1827 Get_Current_Dir (Buffer'Address, Path_Len'Address);
1829 if Buffer (Path_Len) /= Directory_Separator then
1830 Path_Len := Path_Len + 1;
1831 Buffer (Path_Len) := Directory_Separator;
1834 -- By default, the drive letter on Windows is in upper case
1836 if On_Windows and then Path_Len >= 2 and then
1839 System.Case_Util.To_Upper (Buffer (1 .. 1));
1842 return Buffer (1 .. Path_Len);
1847 -- Start of processing for Normalize_Pathname
1850 -- Special case, if name is null, then return null
1852 if Name'Length = 0 then
1856 -- First, convert VMS file spec to Unix file spec.
1857 -- If Name is not in VMS syntax, then this is equivalent
1858 -- to put Name at the beginning of Path_Buffer.
1860 VMS_Conversion : begin
1861 The_Name (1 .. Name'Length) := Name;
1862 The_Name (The_Name'Last) := ASCII.NUL;
1864 Canonical_File_Addr := To_Canonical_File_Spec (The_Name'Address);
1865 Canonical_File_Len := Strlen (Canonical_File_Addr);
1867 -- If VMS syntax conversion has failed, return an empty string
1868 -- to indicate the failure.
1870 if Canonical_File_Len = 0 then
1875 subtype Path_String is String (1 .. Canonical_File_Len);
1876 type Path_String_Access is access Path_String;
1878 function Address_To_Access is new
1879 Ada.Unchecked_Conversion (Source => Address,
1880 Target => Path_String_Access);
1882 Path_Access : constant Path_String_Access :=
1883 Address_To_Access (Canonical_File_Addr);
1886 Path_Buffer (1 .. Canonical_File_Len) := Path_Access.all;
1887 End_Path := Canonical_File_Len;
1892 -- Replace all '/' by Directory Separators (this is for Windows)
1894 if Directory_Separator /= '/' then
1895 for Index in 1 .. End_Path loop
1896 if Path_Buffer (Index) = '/' then
1897 Path_Buffer (Index) := Directory_Separator;
1902 -- Resolve directory names for Windows (formerly also VMS)
1904 -- On VMS, if we have a Unix path such as /temp/..., and TEMP is a
1905 -- logical name, we must not try to resolve this logical name, because
1906 -- it may have multiple equivalences and if resolved we will only
1907 -- get the first one.
1909 -- On Windows, if we have an absolute path starting with a directory
1910 -- separator, we need to have the drive letter appended in front.
1912 -- On Windows, Get_Current_Dir will return a suitable directory
1913 -- name (path starting with a drive letter on Windows). So we take this
1914 -- drive letter and prepend it to the current path.
1917 and then Path_Buffer (1) = Directory_Separator
1918 and then Path_Buffer (2) /= Directory_Separator
1921 Cur_Dir : constant String := Get_Directory ("");
1922 -- Get the current directory to get the drive letter
1925 if Cur_Dir'Length > 2
1926 and then Cur_Dir (Cur_Dir'First + 1) = ':'
1928 Path_Buffer (3 .. End_Path + 2) := Path_Buffer (1 .. End_Path);
1929 Path_Buffer (1 .. 2) :=
1930 Cur_Dir (Cur_Dir'First .. Cur_Dir'First + 1);
1931 End_Path := End_Path + 2;
1936 -- On Windows, remove all double-quotes that are possibly part of the
1937 -- path but can cause problems with other methods.
1944 Index := Path_Buffer'First;
1945 for Current in Path_Buffer'First .. End_Path loop
1946 if Path_Buffer (Current) /= '"' then
1947 Path_Buffer (Index) := Path_Buffer (Current);
1952 End_Path := Index - 1;
1956 -- Start the conversions
1958 -- If this is not finished after Max_Iterations, give up and return an
1961 for J in 1 .. Max_Iterations loop
1963 -- If we don't have an absolute pathname, prepend the directory
1967 and then not Is_Absolute_Path (Path_Buffer (1 .. End_Path))
1970 Reference_Dir : constant String := Get_Directory (Directory);
1971 Ref_Dir_Len : constant Natural := Reference_Dir'Length;
1972 -- Current directory name specified and its length
1975 Path_Buffer (Ref_Dir_Len + 1 .. Ref_Dir_Len + End_Path) :=
1976 Path_Buffer (1 .. End_Path);
1977 End_Path := Ref_Dir_Len + End_Path;
1978 Path_Buffer (1 .. Ref_Dir_Len) := Reference_Dir;
1979 Last := Ref_Dir_Len;
1986 -- Ensure that Windows network drives are kept, e.g: \\server\drive-c
1989 and then Directory_Separator = '\'
1990 and then Path_Buffer (1 .. 2) = "\\"
1995 -- If we have traversed the full pathname, return it
1997 if Start > End_Path then
1998 return Final_Value (Path_Buffer (1 .. End_Path));
2001 -- Remove duplicate directory separators
2003 while Path_Buffer (Start) = Directory_Separator loop
2004 if Start = End_Path then
2005 return Final_Value (Path_Buffer (1 .. End_Path - 1));
2008 Path_Buffer (Start .. End_Path - 1) :=
2009 Path_Buffer (Start + 1 .. End_Path);
2010 End_Path := End_Path - 1;
2014 -- Find the end of the current field: last character or the one
2015 -- preceding the next directory separator.
2017 while Finish < End_Path
2018 and then Path_Buffer (Finish + 1) /= Directory_Separator
2020 Finish := Finish + 1;
2025 if Start = Finish and then Path_Buffer (Start) = '.' then
2026 if Start = End_Path then
2028 return (1 => Directory_Separator);
2031 if Fold_To_Lower_Case then
2032 System.Case_Util.To_Lower (Path_Buffer (1 .. Last - 1));
2035 return Path_Buffer (1 .. Last - 1);
2040 Path_Buffer (Last + 1 .. End_Path - 2) :=
2041 Path_Buffer (Last + 3 .. End_Path);
2042 End_Path := End_Path - 2;
2045 -- Remove ".." fields
2047 elsif Finish = Start + 1
2048 and then Path_Buffer (Start .. Finish) = ".."
2053 exit when Start < 1 or else
2054 Path_Buffer (Start) = Directory_Separator;
2058 if Finish = End_Path then
2059 return (1 => Directory_Separator);
2062 Path_Buffer (1 .. End_Path - Finish) :=
2063 Path_Buffer (Finish + 1 .. End_Path);
2064 End_Path := End_Path - Finish;
2069 if Finish = End_Path then
2070 return Final_Value (Path_Buffer (1 .. Start - 1));
2073 Path_Buffer (Start + 1 .. Start + End_Path - Finish - 1) :=
2074 Path_Buffer (Finish + 2 .. End_Path);
2075 End_Path := Start + End_Path - Finish - 1;
2080 -- Check if current field is a symbolic link
2082 elsif Resolve_Links then
2084 Saved : constant Character := Path_Buffer (Finish + 1);
2087 Path_Buffer (Finish + 1) := ASCII.NUL;
2088 Status := Readlink (Path_Buffer'Address,
2089 Link_Buffer'Address,
2090 Link_Buffer'Length);
2091 Path_Buffer (Finish + 1) := Saved;
2094 -- Not a symbolic link, move to the next field, if any
2099 -- Replace symbolic link with its value
2102 if Is_Absolute_Path (Link_Buffer (1 .. Status)) then
2103 Path_Buffer (Status + 1 .. End_Path - (Finish - Status)) :=
2104 Path_Buffer (Finish + 1 .. End_Path);
2105 End_Path := End_Path - (Finish - Status);
2106 Path_Buffer (1 .. Status) := Link_Buffer (1 .. Status);
2111 (Last + Status + 1 .. End_Path - Finish + Last + Status) :=
2112 Path_Buffer (Finish + 1 .. End_Path);
2113 End_Path := End_Path - Finish + Last + Status;
2114 Path_Buffer (Last + 1 .. Last + Status) :=
2115 Link_Buffer (1 .. Status);
2124 -- Too many iterations: give up
2126 -- This can happen when there is a circularity in the symbolic links: A
2127 -- is a symbolic link for B, which itself is a symbolic link, and the
2128 -- target of B or of another symbolic link target of B is A. In this
2129 -- case, we return an empty string to indicate failure to resolve.
2132 end Normalize_Pathname;
2139 (Name : C_File_Name;
2140 Fmode : Mode) return File_Descriptor
2142 function C_Open_Read
2143 (Name : C_File_Name;
2144 Fmode : Mode) return File_Descriptor;
2145 pragma Import (C, C_Open_Read, "__gnat_open_read");
2147 return C_Open_Read (Name, Fmode);
2152 Fmode : Mode) return File_Descriptor
2154 C_Name : String (1 .. Name'Length + 1);
2156 C_Name (1 .. Name'Length) := Name;
2157 C_Name (C_Name'Last) := ASCII.NUL;
2158 return Open_Read (C_Name (C_Name'First)'Address, Fmode);
2161 ---------------------
2162 -- Open_Read_Write --
2163 ---------------------
2165 function Open_Read_Write
2166 (Name : C_File_Name;
2167 Fmode : Mode) return File_Descriptor
2169 function C_Open_Read_Write
2170 (Name : C_File_Name;
2171 Fmode : Mode) return File_Descriptor;
2172 pragma Import (C, C_Open_Read_Write, "__gnat_open_rw");
2174 return C_Open_Read_Write (Name, Fmode);
2175 end Open_Read_Write;
2177 function Open_Read_Write
2179 Fmode : Mode) return File_Descriptor
2181 C_Name : String (1 .. Name'Length + 1);
2183 C_Name (1 .. Name'Length) := Name;
2184 C_Name (C_Name'Last) := ASCII.NUL;
2185 return Open_Read_Write (C_Name (C_Name'First)'Address, Fmode);
2186 end Open_Read_Write;
2192 procedure OS_Exit (Status : Integer) is
2194 OS_Exit_Ptr (Status);
2195 raise Program_Error;
2198 ---------------------
2199 -- OS_Exit_Default --
2200 ---------------------
2202 procedure OS_Exit_Default (Status : Integer) is
2203 procedure GNAT_OS_Exit (Status : Integer);
2204 pragma Import (C, GNAT_OS_Exit, "__gnat_os_exit");
2205 pragma No_Return (GNAT_OS_Exit);
2207 GNAT_OS_Exit (Status);
2208 end OS_Exit_Default;
2210 --------------------
2211 -- Pid_To_Integer --
2212 --------------------
2214 function Pid_To_Integer (Pid : Process_Id) return Integer is
2216 return Integer (Pid);
2224 (FD : File_Descriptor;
2226 N : Integer) return Integer
2229 return Integer (System.CRTL.read
2230 (System.CRTL.int (FD), System.CRTL.chars (A), System.CRTL.int (N)));
2237 procedure Rename_File
2238 (Old_Name : C_File_Name;
2239 New_Name : C_File_Name;
2240 Success : out Boolean)
2242 function rename (From, To : Address) return Integer;
2243 pragma Import (C, rename, "__gnat_rename");
2246 R := rename (Old_Name, New_Name);
2250 procedure Rename_File
2253 Success : out Boolean)
2255 C_Old_Name : String (1 .. Old_Name'Length + 1);
2256 C_New_Name : String (1 .. New_Name'Length + 1);
2258 C_Old_Name (1 .. Old_Name'Length) := Old_Name;
2259 C_Old_Name (C_Old_Name'Last) := ASCII.NUL;
2260 C_New_Name (1 .. New_Name'Length) := New_Name;
2261 C_New_Name (C_New_Name'Last) := ASCII.NUL;
2262 Rename_File (C_Old_Name'Address, C_New_Name'Address, Success);
2265 -----------------------
2266 -- Set_Close_On_Exec --
2267 -----------------------
2269 procedure Set_Close_On_Exec
2270 (FD : File_Descriptor;
2271 Close_On_Exec : Boolean;
2272 Status : out Boolean)
2274 function C_Set_Close_On_Exec
2275 (FD : File_Descriptor; Close_On_Exec : System.CRTL.int)
2276 return System.CRTL.int;
2277 pragma Import (C, C_Set_Close_On_Exec, "__gnat_set_close_on_exec");
2279 Status := C_Set_Close_On_Exec (FD, Boolean'Pos (Close_On_Exec)) = 0;
2280 end Set_Close_On_Exec;
2282 --------------------
2283 -- Set_Executable --
2284 --------------------
2286 procedure Set_Executable (Name : String) is
2287 procedure C_Set_Executable (Name : C_File_Name);
2288 pragma Import (C, C_Set_Executable, "__gnat_set_executable");
2289 C_Name : aliased String (Name'First .. Name'Last + 1);
2291 C_Name (Name'Range) := Name;
2292 C_Name (C_Name'Last) := ASCII.NUL;
2293 C_Set_Executable (C_Name (C_Name'First)'Address);
2296 ----------------------
2297 -- Set_Non_Readable --
2298 ----------------------
2300 procedure Set_Non_Readable (Name : String) is
2301 procedure C_Set_Non_Readable (Name : C_File_Name);
2302 pragma Import (C, C_Set_Non_Readable, "__gnat_set_non_readable");
2303 C_Name : aliased String (Name'First .. Name'Last + 1);
2305 C_Name (Name'Range) := Name;
2306 C_Name (C_Name'Last) := ASCII.NUL;
2307 C_Set_Non_Readable (C_Name (C_Name'First)'Address);
2308 end Set_Non_Readable;
2310 ----------------------
2311 -- Set_Non_Writable --
2312 ----------------------
2314 procedure Set_Non_Writable (Name : String) is
2315 procedure C_Set_Non_Writable (Name : C_File_Name);
2316 pragma Import (C, C_Set_Non_Writable, "__gnat_set_non_writable");
2317 C_Name : aliased String (Name'First .. Name'Last + 1);
2319 C_Name (Name'Range) := Name;
2320 C_Name (C_Name'Last) := ASCII.NUL;
2321 C_Set_Non_Writable (C_Name (C_Name'First)'Address);
2322 end Set_Non_Writable;
2328 procedure Set_Readable (Name : String) is
2329 procedure C_Set_Readable (Name : C_File_Name);
2330 pragma Import (C, C_Set_Readable, "__gnat_set_readable");
2331 C_Name : aliased String (Name'First .. Name'Last + 1);
2333 C_Name (Name'Range) := Name;
2334 C_Name (C_Name'Last) := ASCII.NUL;
2335 C_Set_Readable (C_Name (C_Name'First)'Address);
2338 --------------------
2340 --------------------
2342 procedure Set_Writable (Name : String) is
2343 procedure C_Set_Writable (Name : C_File_Name);
2344 pragma Import (C, C_Set_Writable, "__gnat_set_writable");
2345 C_Name : aliased String (Name'First .. Name'Last + 1);
2347 C_Name (Name'Range) := Name;
2348 C_Name (C_Name'Last) := ASCII.NUL;
2349 C_Set_Writable (C_Name (C_Name'First)'Address);
2356 procedure Setenv (Name : String; Value : String) is
2357 F_Name : String (1 .. Name'Length + 1);
2358 F_Value : String (1 .. Value'Length + 1);
2360 procedure Set_Env_Value (Name, Value : System.Address);
2361 pragma Import (C, Set_Env_Value, "__gnat_setenv");
2364 F_Name (1 .. Name'Length) := Name;
2365 F_Name (F_Name'Last) := ASCII.NUL;
2367 F_Value (1 .. Value'Length) := Value;
2368 F_Value (F_Value'Last) := ASCII.NUL;
2370 Set_Env_Value (F_Name'Address, F_Value'Address);
2378 (Program_Name : String;
2379 Args : Argument_List) return Integer
2383 pragma Warnings (Off, Junk);
2385 Spawn_Internal (Program_Name, Args, Result, Junk, Blocking => True);
2390 (Program_Name : String;
2391 Args : Argument_List;
2392 Success : out Boolean)
2395 Success := (Spawn (Program_Name, Args) = 0);
2399 (Program_Name : String;
2400 Args : Argument_List;
2401 Output_File_Descriptor : File_Descriptor;
2402 Return_Code : out Integer;
2403 Err_To_Out : Boolean := True)
2405 Saved_Output : File_Descriptor;
2406 Saved_Error : File_Descriptor := Invalid_FD; -- prevent compiler warning
2409 -- Set standard output and error to the temporary file
2411 Saved_Output := Dup (Standout);
2412 Dup2 (Output_File_Descriptor, Standout);
2415 Saved_Error := Dup (Standerr);
2416 Dup2 (Output_File_Descriptor, Standerr);
2419 -- Spawn the program
2421 Return_Code := Spawn (Program_Name, Args);
2423 -- Restore the standard output and error
2425 Dup2 (Saved_Output, Standout);
2428 Dup2 (Saved_Error, Standerr);
2431 -- And close the saved standard output and error file descriptors
2433 Close (Saved_Output);
2436 Close (Saved_Error);
2441 (Program_Name : String;
2442 Args : Argument_List;
2443 Output_File : String;
2444 Success : out Boolean;
2445 Return_Code : out Integer;
2446 Err_To_Out : Boolean := True)
2448 FD : File_Descriptor;
2454 FD := Create_Output_Text_File (Output_File);
2456 if FD = Invalid_FD then
2461 Spawn (Program_Name, Args, FD, Return_Code, Err_To_Out);
2463 Close (FD, Success);
2466 --------------------
2467 -- Spawn_Internal --
2468 --------------------
2470 procedure Spawn_Internal
2471 (Program_Name : String;
2472 Args : Argument_List;
2473 Result : out Integer;
2474 Pid : out Process_Id;
2478 procedure Spawn (Args : Argument_List);
2479 -- Call Spawn with given argument list
2481 N_Args : Argument_List (Args'Range);
2482 -- Normalized arguments
2488 procedure Spawn (Args : Argument_List) is
2489 type Chars is array (Positive range <>) of aliased Character;
2490 type Char_Ptr is access constant Character;
2492 Command_Len : constant Positive := Program_Name'Length + 1
2493 + Args_Length (Args);
2494 Command_Last : Natural := 0;
2495 Command : aliased Chars (1 .. Command_Len);
2496 -- Command contains all characters of the Program_Name and Args, all
2497 -- terminated by ASCII.NUL characters.
2499 Arg_List_Len : constant Positive := Args'Length + 2;
2500 Arg_List_Last : Natural := 0;
2501 Arg_List : aliased array (1 .. Arg_List_Len) of Char_Ptr;
2502 -- List with pointers to NUL-terminated strings of the Program_Name
2503 -- and the Args and terminated with a null pointer. We rely on the
2504 -- default initialization for the last null pointer.
2506 procedure Add_To_Command (S : String);
2507 -- Add S and a NUL character to Command, updating Last
2509 function Portable_Spawn (Args : Address) return Integer;
2510 pragma Import (C, Portable_Spawn, "__gnat_portable_spawn");
2512 function Portable_No_Block_Spawn (Args : Address) return Process_Id;
2514 (C, Portable_No_Block_Spawn, "__gnat_portable_no_block_spawn");
2516 --------------------
2517 -- Add_To_Command --
2518 --------------------
2520 procedure Add_To_Command (S : String) is
2521 First : constant Natural := Command_Last + 1;
2524 Command_Last := Command_Last + S'Length;
2526 -- Move characters one at a time, because Command has aliased
2529 -- But not volatile, so why is this necessary ???
2531 for J in S'Range loop
2532 Command (First + J - S'First) := S (J);
2535 Command_Last := Command_Last + 1;
2536 Command (Command_Last) := ASCII.NUL;
2538 Arg_List_Last := Arg_List_Last + 1;
2539 Arg_List (Arg_List_Last) := Command (First)'Access;
2542 -- Start of processing for Spawn
2545 Add_To_Command (Program_Name);
2547 for J in Args'Range loop
2548 Add_To_Command (Args (J).all);
2553 Result := Portable_Spawn (Arg_List'Address);
2555 Pid := Portable_No_Block_Spawn (Arg_List'Address);
2556 Result := Boolean'Pos (Pid /= Invalid_Pid);
2560 -- Start of processing for Spawn_Internal
2563 -- Copy arguments into a local structure
2565 for K in N_Args'Range loop
2566 N_Args (K) := new String'(Args (K).all);
2569 -- Normalize those arguments
2571 Normalize_Arguments (N_Args);
2573 -- Call spawn using the normalized arguments
2577 -- Free arguments list
2579 for K in N_Args'Range loop
2584 ---------------------------
2585 -- To_Path_String_Access --
2586 ---------------------------
2588 function To_Path_String_Access
2589 (Path_Addr : Address;
2590 Path_Len : Integer) return String_Access
2592 subtype Path_String is String (1 .. Path_Len);
2593 type Path_String_Access is access Path_String;
2595 function Address_To_Access is new Ada.Unchecked_Conversion
2596 (Source => Address, Target => Path_String_Access);
2598 Path_Access : constant Path_String_Access :=
2599 Address_To_Access (Path_Addr);
2601 Return_Val : String_Access;
2604 Return_Val := new String (1 .. Path_Len);
2606 for J in 1 .. Path_Len loop
2607 Return_Val (J) := Path_Access (J);
2611 end To_Path_String_Access;
2617 procedure Wait_Process (Pid : out Process_Id; Success : out Boolean) is
2620 function Portable_Wait (S : Address) return Process_Id;
2621 pragma Import (C, Portable_Wait, "__gnat_portable_wait");
2624 Pid := Portable_Wait (Status'Address);
2625 Success := (Status = 0);
2633 (FD : File_Descriptor;
2635 N : Integer) return Integer
2638 return Integer (System.CRTL.write
2639 (System.CRTL.int (FD), System.CRTL.chars (A), System.CRTL.int (N)));