1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . O S _ L I B --
9 -- Copyright (C) 1995-2007, AdaCore --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 pragma Warnings (Off);
38 with System.Case_Util;
40 with System.Soft_Links;
41 with Ada.Unchecked_Conversion;
42 with Ada.Unchecked_Deallocation;
43 with System; use System;
45 package body System.OS_Lib is
47 -- Imported procedures Dup and Dup2 are used in procedures Spawn and
48 -- Non_Blocking_Spawn.
50 function Dup (Fd : File_Descriptor) return File_Descriptor;
51 pragma Import (C, Dup, "__gnat_dup");
53 procedure Dup2 (Old_Fd, New_Fd : File_Descriptor);
54 pragma Import (C, Dup2, "__gnat_dup2");
56 On_Windows : constant Boolean := Directory_Separator = '\';
57 -- An indication that we are on Windows. Used in Normalize_Pathname, to
58 -- deal with drive letters in the beginning of absolute paths.
60 package SSL renames System.Soft_Links;
62 -- The following are used by Create_Temp_File
64 First_Temp_File_Name : constant String := "GNAT-TEMP-000000.TMP";
65 -- Used to initialize Current_Temp_File_Name and Temp_File_Name_Last_Digit
67 Current_Temp_File_Name : String := First_Temp_File_Name;
68 -- Name of the temp file last created
70 Temp_File_Name_Last_Digit : constant Positive :=
71 First_Temp_File_Name'Last - 4;
72 -- Position of the last digit in Current_Temp_File_Name
74 Max_Attempts : constant := 100;
75 -- The maximum number of attempts to create a new temp file
77 -----------------------
78 -- Local Subprograms --
79 -----------------------
81 function Args_Length (Args : Argument_List) return Natural;
82 -- Returns total number of characters needed to create a string
83 -- of all Args terminated by ASCII.NUL characters
85 function C_String_Length (S : Address) return Integer;
86 -- Returns the length of a C string. Does check for null address
89 procedure Spawn_Internal
90 (Program_Name : String;
95 -- Internal routine to implement the two Spawn (blocking/non blocking)
96 -- routines. If Blocking is set to True then the spawn is blocking
97 -- otherwise it is non blocking. In this latter case the Pid contains the
98 -- process id number. The first three parameters are as in Spawn. Note that
99 -- Spawn_Internal normalizes the argument list before calling the low level
100 -- system spawn routines (see Normalize_Arguments).
102 -- Note: Normalize_Arguments is designed to do nothing if it is called more
103 -- than once, so calling Normalize_Arguments before calling one of the
104 -- spawn routines is fine.
106 function To_Path_String_Access
107 (Path_Addr : Address;
108 Path_Len : Integer) return String_Access;
109 -- Converts a C String to an Ada String. We could do this making use of
110 -- Interfaces.C.Strings but we prefer not to import that entire package
116 function "<" (X, Y : OS_Time) return Boolean is
118 return Long_Integer (X) < Long_Integer (Y);
125 function "<=" (X, Y : OS_Time) return Boolean is
127 return Long_Integer (X) <= Long_Integer (Y);
134 function ">" (X, Y : OS_Time) return Boolean is
136 return Long_Integer (X) > Long_Integer (Y);
143 function ">=" (X, Y : OS_Time) return Boolean is
145 return Long_Integer (X) >= Long_Integer (Y);
152 function Args_Length (Args : Argument_List) return Natural is
156 for J in Args'Range loop
157 Len := Len + Args (J)'Length + 1; -- One extra for ASCII.NUL
163 -----------------------------
164 -- Argument_String_To_List --
165 -----------------------------
167 function Argument_String_To_List
168 (Arg_String : String) return Argument_List_Access
170 Max_Args : constant Integer := Arg_String'Length;
171 New_Argv : Argument_List (1 .. Max_Args);
172 New_Argc : Natural := 0;
176 Idx := Arg_String'First;
179 exit when Idx > Arg_String'Last;
182 Quoted : Boolean := False;
183 Backqd : Boolean := False;
190 -- An unquoted space is the end of an argument
192 if not (Backqd or Quoted)
193 and then Arg_String (Idx) = ' '
197 -- Start of a quoted string
199 elsif not (Backqd or Quoted)
200 and then Arg_String (Idx) = '"'
204 -- End of a quoted string and end of an argument
206 elsif (Quoted and not Backqd)
207 and then Arg_String (Idx) = '"'
212 -- Following character is backquoted
214 elsif Arg_String (Idx) = '\' then
217 -- Turn off backquoting after advancing one character
225 exit when Idx > Arg_String'Last;
230 New_Argc := New_Argc + 1;
231 New_Argv (New_Argc) :=
232 new String'(Arg_String (Old_Idx .. Idx - 1));
234 -- Skip extraneous spaces
236 while Idx <= Arg_String'Last and then Arg_String (Idx) = ' ' loop
242 return new Argument_List'(New_Argv (1 .. New_Argc));
243 end Argument_String_To_List;
245 ---------------------
246 -- C_String_Length --
247 ---------------------
249 function C_String_Length (S : Address) return Integer is
250 function Strlen (S : Address) return Integer;
251 pragma Import (C, Strlen, "strlen");
253 if S = Null_Address then
264 procedure Close (FD : File_Descriptor) is
265 procedure C_Close (FD : File_Descriptor);
266 pragma Import (C, C_Close, "close");
271 procedure Close (FD : File_Descriptor; Status : out Boolean) is
272 function C_Close (FD : File_Descriptor) return Integer;
273 pragma Import (C, C_Close, "close");
275 Status := (C_Close (FD) = 0);
285 Success : out Boolean;
286 Mode : Copy_Mode := Copy;
287 Preserve : Attribute := Time_Stamps)
289 From : File_Descriptor;
290 To : File_Descriptor;
292 Copy_Error : exception;
293 -- Internal exception raised to signal error in copy
295 function Build_Path (Dir : String; File : String) return String;
296 -- Returns pathname Dir catenated with File adding the directory
297 -- separator only if needed.
299 procedure Copy (From, To : File_Descriptor);
300 -- Read data from From and place them into To. In both cases the
301 -- operations uses the current file position. Raises Constraint_Error
302 -- if a problem occurs during the copy.
304 procedure Copy_To (To_Name : String);
305 -- Does a straight copy from source to designated destination file
311 function Build_Path (Dir : String; File : String) return String is
312 Res : String (1 .. Dir'Length + File'Length + 1);
314 Base_File_Ptr : Integer;
315 -- The base file name is File (Base_File_Ptr + 1 .. File'Last)
317 function Is_Dirsep (C : Character) return Boolean;
318 pragma Inline (Is_Dirsep);
319 -- Returns True if C is a directory separator. On Windows we
320 -- handle both styles of directory separator.
326 function Is_Dirsep (C : Character) return Boolean is
328 return C = Directory_Separator or else C = '/';
331 -- Start of processing for Build_Path
334 -- Find base file name
336 Base_File_Ptr := File'Last;
337 while Base_File_Ptr >= File'First loop
338 exit when Is_Dirsep (File (Base_File_Ptr));
339 Base_File_Ptr := Base_File_Ptr - 1;
343 Base_File : String renames
344 File (Base_File_Ptr + 1 .. File'Last);
347 Res (1 .. Dir'Length) := Dir;
349 if Is_Dirsep (Dir (Dir'Last)) then
350 Res (Dir'Length + 1 .. Dir'Length + Base_File'Length) :=
352 return Res (1 .. Dir'Length + Base_File'Length);
355 Res (Dir'Length + 1) := Directory_Separator;
356 Res (Dir'Length + 2 .. Dir'Length + 1 + Base_File'Length) :=
358 return Res (1 .. Dir'Length + 1 + Base_File'Length);
367 procedure Copy (From, To : File_Descriptor) is
368 Buf_Size : constant := 200_000;
369 type Buf is array (1 .. Buf_Size) of Character;
370 type Buf_Ptr is access Buf;
376 Status_From : Boolean;
378 -- Statuses for the calls to Close
380 procedure Free is new Ada.Unchecked_Deallocation (Buf, Buf_Ptr);
383 -- Check for invalid descriptors, making sure that we do not
384 -- accidentally leave an open file descriptor around.
386 if From = Invalid_FD then
387 if To /= Invalid_FD then
388 Close (To, Status_To);
393 elsif To = Invalid_FD then
394 Close (From, Status_From);
398 -- Allocate the buffer on the heap
403 R := Read (From, Buffer (1)'Address, Buf_Size);
405 -- For VMS, the buffer may not be full. So, we need to try again
406 -- until there is nothing to read.
410 W := Write (To, Buffer (1)'Address, R);
414 -- Problem writing data, could be a disk full. Close files
415 -- without worrying about status, since we are raising a
416 -- Copy_Error exception in any case.
418 Close (From, Status_From);
419 Close (To, Status_To);
427 Close (From, Status_From);
428 Close (To, Status_To);
432 if not (Status_From and Status_To) then
441 procedure Copy_To (To_Name : String) is
443 function Copy_Attributes
444 (From, To : System.Address;
445 Mode : Integer) return Integer;
446 pragma Import (C, Copy_Attributes, "__gnat_copy_attribs");
447 -- Mode = 0 - copy only time stamps.
448 -- Mode = 1 - copy time stamps and read/write/execute attributes
450 C_From : String (1 .. Name'Length + 1);
451 C_To : String (1 .. To_Name'Length + 1);
454 From := Open_Read (Name, Binary);
455 To := Create_File (To_Name, Binary);
460 C_From (1 .. Name'Length) := Name;
461 C_From (C_From'Last) := ASCII.Nul;
463 C_To (1 .. To_Name'Length) := To_Name;
464 C_To (C_To'Last) := ASCII.Nul;
469 if Copy_Attributes (C_From'Address, C_To'Address, 0) = -1 then
474 if Copy_Attributes (C_From'Address, C_To'Address, 1) = -1 then
484 -- Start of processing for Copy_File
489 -- The source file must exist
491 if not Is_Regular_File (Name) then
495 -- The source file exists
499 -- Copy case, target file must not exist
503 -- If the target file exists, we have an error
505 if Is_Regular_File (Pathname) then
508 -- Case of target is a directory
510 elsif Is_Directory (Pathname) then
512 Dest : constant String := Build_Path (Pathname, Name);
515 -- If target file exists, we have an error, else do copy
517 if Is_Regular_File (Dest) then
524 -- Case of normal copy to file (destination does not exist)
530 -- Overwrite case (destination file may or may not exist)
533 if Is_Directory (Pathname) then
534 Copy_To (Build_Path (Pathname, Name));
539 -- Append case (destination file may or may not exist)
543 -- Appending to existing file
545 if Is_Regular_File (Pathname) then
547 -- Append mode and destination file exists, append data at the
550 From := Open_Read (Name, Binary);
551 To := Open_Read_Write (Pathname, Binary);
552 Lseek (To, 0, Seek_End);
556 -- Appending to directory, not allowed
558 elsif Is_Directory (Pathname) then
561 -- Appending when target file does not exist
568 -- All error cases are caught here
577 Pathname : C_File_Name;
578 Success : out Boolean;
579 Mode : Copy_Mode := Copy;
580 Preserve : Attribute := Time_Stamps)
582 Ada_Name : String_Access :=
583 To_Path_String_Access
584 (Name, C_String_Length (Name));
586 Ada_Pathname : String_Access :=
587 To_Path_String_Access
588 (Pathname, C_String_Length (Pathname));
591 Copy_File (Ada_Name.all, Ada_Pathname.all, Success, Mode, Preserve);
596 ----------------------
597 -- Copy_Time_Stamps --
598 ----------------------
600 procedure Copy_Time_Stamps (Source, Dest : String; Success : out Boolean) is
602 function Copy_Attributes
603 (From, To : System.Address;
604 Mode : Integer) return Integer;
605 pragma Import (C, Copy_Attributes, "__gnat_copy_attribs");
606 -- Mode = 0 - copy only time stamps.
607 -- Mode = 1 - copy time stamps and read/write/execute attributes
610 if Is_Regular_File (Source) and then Is_Writable_File (Dest) then
612 C_Source : String (1 .. Source'Length + 1);
613 C_Dest : String (1 .. Dest'Length + 1);
615 C_Source (1 .. Source'Length) := Source;
616 C_Source (C_Source'Last) := ASCII.NUL;
618 C_Dest (1 .. Dest'Length) := Dest;
619 C_Dest (C_Dest'Last) := ASCII.NUL;
621 if Copy_Attributes (C_Source'Address, C_Dest'Address, 0) = -1 then
631 end Copy_Time_Stamps;
633 procedure Copy_Time_Stamps
634 (Source, Dest : C_File_Name;
635 Success : out Boolean)
637 Ada_Source : String_Access :=
638 To_Path_String_Access
639 (Source, C_String_Length (Source));
641 Ada_Dest : String_Access :=
642 To_Path_String_Access
643 (Dest, C_String_Length (Dest));
645 Copy_Time_Stamps (Ada_Source.all, Ada_Dest.all, Success);
648 end Copy_Time_Stamps;
656 Fmode : Mode) return File_Descriptor
658 function C_Create_File
660 Fmode : Mode) return File_Descriptor;
661 pragma Import (C, C_Create_File, "__gnat_open_create");
664 return C_Create_File (Name, Fmode);
669 Fmode : Mode) return File_Descriptor
671 C_Name : String (1 .. Name'Length + 1);
674 C_Name (1 .. Name'Length) := Name;
675 C_Name (C_Name'Last) := ASCII.NUL;
676 return Create_File (C_Name (C_Name'First)'Address, Fmode);
679 ---------------------
680 -- Create_New_File --
681 ---------------------
683 function Create_New_File
685 Fmode : Mode) return File_Descriptor
687 function C_Create_New_File
689 Fmode : Mode) return File_Descriptor;
690 pragma Import (C, C_Create_New_File, "__gnat_open_new");
693 return C_Create_New_File (Name, Fmode);
696 function Create_New_File
698 Fmode : Mode) return File_Descriptor
700 C_Name : String (1 .. Name'Length + 1);
703 C_Name (1 .. Name'Length) := Name;
704 C_Name (C_Name'Last) := ASCII.NUL;
705 return Create_New_File (C_Name (C_Name'First)'Address, Fmode);
708 -----------------------------
709 -- Create_Output_Text_File --
710 -----------------------------
712 function Create_Output_Text_File (Name : String) return File_Descriptor is
713 function C_Create_File
714 (Name : C_File_Name) return File_Descriptor;
715 pragma Import (C, C_Create_File, "__gnat_create_output_file");
717 C_Name : String (1 .. Name'Length + 1);
720 C_Name (1 .. Name'Length) := Name;
721 C_Name (C_Name'Last) := ASCII.NUL;
722 return C_Create_File (C_Name (C_Name'First)'Address);
723 end Create_Output_Text_File;
725 ----------------------
726 -- Create_Temp_File --
727 ----------------------
729 procedure Create_Temp_File
730 (FD : out File_Descriptor;
731 Name : out Temp_File_Name)
733 function Open_New_Temp
734 (Name : System.Address;
735 Fmode : Mode) return File_Descriptor;
736 pragma Import (C, Open_New_Temp, "__gnat_open_new_temp");
739 FD := Open_New_Temp (Name'Address, Binary);
740 end Create_Temp_File;
742 procedure Create_Temp_File
743 (FD : out File_Descriptor;
744 Name : out String_Access)
747 Attempts : Natural := 0;
748 Current : String (Current_Temp_File_Name'Range);
751 -- Loop until a new temp file can be created
755 -- We need to protect global variable Current_Temp_File_Name
756 -- against concurrent access by different tasks.
760 -- Start at the last digit
762 Pos := Temp_File_Name_Last_Digit;
766 -- Increment the digit by one
768 case Current_Temp_File_Name (Pos) is
770 Current_Temp_File_Name (Pos) :=
771 Character'Succ (Current_Temp_File_Name (Pos));
776 -- For 9, set the digit to 0 and go to the previous digit
778 Current_Temp_File_Name (Pos) := '0';
783 -- If it is not a digit, then there are no available
784 -- temp file names. Return Invalid_FD. There is almost
785 -- no that this code will be ever be executed, since
786 -- it would mean that there are one million temp files
787 -- in the same directory!
796 Current := Current_Temp_File_Name;
798 -- We can now release the lock, because we are no longer
799 -- accessing Current_Temp_File_Name.
809 -- Attempt to create the file
811 FD := Create_New_File (Current, Binary);
813 if FD /= Invalid_FD then
814 Name := new String'(Current);
818 if not Is_Regular_File (Current) then
820 -- If the file does not already exist and we are unable to create
821 -- it, we give up after Max_Attempts. Otherwise, we try again with
822 -- the next available file name.
824 Attempts := Attempts + 1;
826 if Attempts >= Max_Attempts then
833 end Create_Temp_File;
839 procedure Delete_File (Name : Address; Success : out Boolean) is
842 function unlink (A : Address) return Integer;
843 pragma Import (C, unlink, "unlink");
850 procedure Delete_File (Name : String; Success : out Boolean) is
851 C_Name : String (1 .. Name'Length + 1);
854 C_Name (1 .. Name'Length) := Name;
855 C_Name (C_Name'Last) := ASCII.NUL;
857 Delete_File (C_Name'Address, Success);
860 ---------------------
861 -- File_Time_Stamp --
862 ---------------------
864 function File_Time_Stamp (FD : File_Descriptor) return OS_Time is
865 function File_Time (FD : File_Descriptor) return OS_Time;
866 pragma Import (C, File_Time, "__gnat_file_time_fd");
868 return File_Time (FD);
871 function File_Time_Stamp (Name : C_File_Name) return OS_Time is
872 function File_Time (Name : Address) return OS_Time;
873 pragma Import (C, File_Time, "__gnat_file_time_name");
875 return File_Time (Name);
878 function File_Time_Stamp (Name : String) return OS_Time is
879 F_Name : String (1 .. Name'Length + 1);
881 F_Name (1 .. Name'Length) := Name;
882 F_Name (F_Name'Last) := ASCII.NUL;
883 return File_Time_Stamp (F_Name'Address);
886 ---------------------------
887 -- Get_Debuggable_Suffix --
888 ---------------------------
890 function Get_Debuggable_Suffix return String_Access is
891 procedure Get_Suffix_Ptr (Length, Ptr : Address);
892 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_debuggable_suffix_ptr");
894 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
895 pragma Import (C, Strncpy, "strncpy");
897 Suffix_Ptr : Address;
898 Suffix_Length : Integer;
899 Result : String_Access;
902 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
904 Result := new String (1 .. Suffix_Length);
906 if Suffix_Length > 0 then
907 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
911 end Get_Debuggable_Suffix;
913 ---------------------------
914 -- Get_Executable_Suffix --
915 ---------------------------
917 function Get_Executable_Suffix return String_Access is
918 procedure Get_Suffix_Ptr (Length, Ptr : Address);
919 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_executable_suffix_ptr");
921 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
922 pragma Import (C, Strncpy, "strncpy");
924 Suffix_Ptr : Address;
925 Suffix_Length : Integer;
926 Result : String_Access;
929 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
931 Result := new String (1 .. Suffix_Length);
933 if Suffix_Length > 0 then
934 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
938 end Get_Executable_Suffix;
940 -----------------------
941 -- Get_Object_Suffix --
942 -----------------------
944 function Get_Object_Suffix return String_Access is
945 procedure Get_Suffix_Ptr (Length, Ptr : Address);
946 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_object_suffix_ptr");
948 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
949 pragma Import (C, Strncpy, "strncpy");
951 Suffix_Ptr : Address;
952 Suffix_Length : Integer;
953 Result : String_Access;
956 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
958 Result := new String (1 .. Suffix_Length);
960 if Suffix_Length > 0 then
961 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
965 end Get_Object_Suffix;
967 ----------------------------------
968 -- Get_Target_Debuggable_Suffix --
969 ----------------------------------
971 function Get_Target_Debuggable_Suffix return String_Access is
972 Target_Exec_Ext_Ptr : Address;
974 (C, Target_Exec_Ext_Ptr, "__gnat_target_debuggable_extension");
976 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
977 pragma Import (C, Strncpy, "strncpy");
979 function Strlen (Cstring : Address) return Integer;
980 pragma Import (C, Strlen, "strlen");
982 Suffix_Length : Integer;
983 Result : String_Access;
986 Suffix_Length := Strlen (Target_Exec_Ext_Ptr);
988 Result := new String (1 .. Suffix_Length);
990 if Suffix_Length > 0 then
991 Strncpy (Result.all'Address, Target_Exec_Ext_Ptr, Suffix_Length);
995 end Get_Target_Debuggable_Suffix;
997 ----------------------------------
998 -- Get_Target_Executable_Suffix --
999 ----------------------------------
1001 function Get_Target_Executable_Suffix return String_Access is
1002 Target_Exec_Ext_Ptr : Address;
1004 (C, Target_Exec_Ext_Ptr, "__gnat_target_executable_extension");
1006 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1007 pragma Import (C, Strncpy, "strncpy");
1009 function Strlen (Cstring : Address) return Integer;
1010 pragma Import (C, Strlen, "strlen");
1012 Suffix_Length : Integer;
1013 Result : String_Access;
1016 Suffix_Length := Strlen (Target_Exec_Ext_Ptr);
1018 Result := new String (1 .. Suffix_Length);
1020 if Suffix_Length > 0 then
1021 Strncpy (Result.all'Address, Target_Exec_Ext_Ptr, Suffix_Length);
1025 end Get_Target_Executable_Suffix;
1027 ------------------------------
1028 -- Get_Target_Object_Suffix --
1029 ------------------------------
1031 function Get_Target_Object_Suffix return String_Access is
1032 Target_Object_Ext_Ptr : Address;
1034 (C, Target_Object_Ext_Ptr, "__gnat_target_object_extension");
1036 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1037 pragma Import (C, Strncpy, "strncpy");
1039 function Strlen (Cstring : Address) return Integer;
1040 pragma Import (C, Strlen, "strlen");
1042 Suffix_Length : Integer;
1043 Result : String_Access;
1046 Suffix_Length := Strlen (Target_Object_Ext_Ptr);
1048 Result := new String (1 .. Suffix_Length);
1050 if Suffix_Length > 0 then
1051 Strncpy (Result.all'Address, Target_Object_Ext_Ptr, Suffix_Length);
1055 end Get_Target_Object_Suffix;
1061 function Getenv (Name : String) return String_Access is
1062 procedure Get_Env_Value_Ptr (Name, Length, Ptr : Address);
1063 pragma Import (C, Get_Env_Value_Ptr, "__gnat_getenv");
1065 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1066 pragma Import (C, Strncpy, "strncpy");
1068 Env_Value_Ptr : aliased Address;
1069 Env_Value_Length : aliased Integer;
1070 F_Name : aliased String (1 .. Name'Length + 1);
1071 Result : String_Access;
1074 F_Name (1 .. Name'Length) := Name;
1075 F_Name (F_Name'Last) := ASCII.NUL;
1078 (F_Name'Address, Env_Value_Length'Address, Env_Value_Ptr'Address);
1080 Result := new String (1 .. Env_Value_Length);
1082 if Env_Value_Length > 0 then
1083 Strncpy (Result.all'Address, Env_Value_Ptr, Env_Value_Length);
1093 function GM_Day (Date : OS_Time) return Day_Type is
1102 GM_Split (Date, Y, Mo, D, H, Mn, S);
1110 function GM_Hour (Date : OS_Time) return Hour_Type is
1119 GM_Split (Date, Y, Mo, D, H, Mn, S);
1127 function GM_Minute (Date : OS_Time) return Minute_Type is
1136 GM_Split (Date, Y, Mo, D, H, Mn, S);
1144 function GM_Month (Date : OS_Time) return Month_Type is
1153 GM_Split (Date, Y, Mo, D, H, Mn, S);
1161 function GM_Second (Date : OS_Time) return Second_Type is
1170 GM_Split (Date, Y, Mo, D, H, Mn, S);
1180 Year : out Year_Type;
1181 Month : out Month_Type;
1183 Hour : out Hour_Type;
1184 Minute : out Minute_Type;
1185 Second : out Second_Type)
1187 procedure To_GM_Time
1188 (P_Time_T, P_Year, P_Month, P_Day, P_Hours, P_Mins, P_Secs : Address);
1189 pragma Import (C, To_GM_Time, "__gnat_to_gm_time");
1191 T : OS_Time := Date;
1200 -- Use the global lock because To_GM_Time is not thread safe
1202 Locked_Processing : begin
1205 (T'Address, Y'Address, Mo'Address, D'Address,
1206 H'Address, Mn'Address, S'Address);
1207 SSL.Unlock_Task.all;
1211 SSL.Unlock_Task.all;
1213 end Locked_Processing;
1227 function GM_Year (Date : OS_Time) return Year_Type is
1236 GM_Split (Date, Y, Mo, D, H, Mn, S);
1240 ----------------------
1241 -- Is_Absolute_Path --
1242 ----------------------
1244 function Is_Absolute_Path (Name : String) return Boolean is
1245 function Is_Absolute_Path
1247 Length : Integer) return Integer;
1248 pragma Import (C, Is_Absolute_Path, "__gnat_is_absolute_path");
1250 return Is_Absolute_Path (Name'Address, Name'Length) /= 0;
1251 end Is_Absolute_Path;
1257 function Is_Directory (Name : C_File_Name) return Boolean is
1258 function Is_Directory (Name : Address) return Integer;
1259 pragma Import (C, Is_Directory, "__gnat_is_directory");
1261 return Is_Directory (Name) /= 0;
1264 function Is_Directory (Name : String) return Boolean is
1265 F_Name : String (1 .. Name'Length + 1);
1267 F_Name (1 .. Name'Length) := Name;
1268 F_Name (F_Name'Last) := ASCII.NUL;
1269 return Is_Directory (F_Name'Address);
1272 ----------------------
1273 -- Is_Readable_File --
1274 ----------------------
1276 function Is_Readable_File (Name : C_File_Name) return Boolean is
1277 function Is_Readable_File (Name : Address) return Integer;
1278 pragma Import (C, Is_Readable_File, "__gnat_is_readable_file");
1280 return Is_Readable_File (Name) /= 0;
1281 end Is_Readable_File;
1283 function Is_Readable_File (Name : String) return Boolean is
1284 F_Name : String (1 .. Name'Length + 1);
1286 F_Name (1 .. Name'Length) := Name;
1287 F_Name (F_Name'Last) := ASCII.NUL;
1288 return Is_Readable_File (F_Name'Address);
1289 end Is_Readable_File;
1291 ---------------------
1292 -- Is_Regular_File --
1293 ---------------------
1295 function Is_Regular_File (Name : C_File_Name) return Boolean is
1296 function Is_Regular_File (Name : Address) return Integer;
1297 pragma Import (C, Is_Regular_File, "__gnat_is_regular_file");
1299 return Is_Regular_File (Name) /= 0;
1300 end Is_Regular_File;
1302 function Is_Regular_File (Name : String) return Boolean is
1303 F_Name : String (1 .. Name'Length + 1);
1305 F_Name (1 .. Name'Length) := Name;
1306 F_Name (F_Name'Last) := ASCII.NUL;
1307 return Is_Regular_File (F_Name'Address);
1308 end Is_Regular_File;
1310 ----------------------
1311 -- Is_Symbolic_Link --
1312 ----------------------
1314 function Is_Symbolic_Link (Name : C_File_Name) return Boolean is
1315 function Is_Symbolic_Link (Name : Address) return Integer;
1316 pragma Import (C, Is_Symbolic_Link, "__gnat_is_symbolic_link");
1318 return Is_Symbolic_Link (Name) /= 0;
1319 end Is_Symbolic_Link;
1321 function Is_Symbolic_Link (Name : String) return Boolean is
1322 F_Name : String (1 .. Name'Length + 1);
1324 F_Name (1 .. Name'Length) := Name;
1325 F_Name (F_Name'Last) := ASCII.NUL;
1326 return Is_Symbolic_Link (F_Name'Address);
1327 end Is_Symbolic_Link;
1329 ----------------------
1330 -- Is_Writable_File --
1331 ----------------------
1333 function Is_Writable_File (Name : C_File_Name) return Boolean is
1334 function Is_Writable_File (Name : Address) return Integer;
1335 pragma Import (C, Is_Writable_File, "__gnat_is_writable_file");
1337 return Is_Writable_File (Name) /= 0;
1338 end Is_Writable_File;
1340 function Is_Writable_File (Name : String) return Boolean is
1341 F_Name : String (1 .. Name'Length + 1);
1343 F_Name (1 .. Name'Length) := Name;
1344 F_Name (F_Name'Last) := ASCII.NUL;
1345 return Is_Writable_File (F_Name'Address);
1346 end Is_Writable_File;
1348 -------------------------
1349 -- Locate_Exec_On_Path --
1350 -------------------------
1352 function Locate_Exec_On_Path
1353 (Exec_Name : String) return String_Access
1355 function Locate_Exec_On_Path (C_Exec_Name : Address) return Address;
1356 pragma Import (C, Locate_Exec_On_Path, "__gnat_locate_exec_on_path");
1358 procedure Free (Ptr : System.Address);
1359 pragma Import (C, Free, "free");
1361 C_Exec_Name : String (1 .. Exec_Name'Length + 1);
1362 Path_Addr : Address;
1364 Result : String_Access;
1367 C_Exec_Name (1 .. Exec_Name'Length) := Exec_Name;
1368 C_Exec_Name (C_Exec_Name'Last) := ASCII.NUL;
1370 Path_Addr := Locate_Exec_On_Path (C_Exec_Name'Address);
1371 Path_Len := C_String_Length (Path_Addr);
1373 if Path_Len = 0 then
1377 Result := To_Path_String_Access (Path_Addr, Path_Len);
1380 -- Always return an absolute path name
1382 if not Is_Absolute_Path (Result.all) then
1384 Absolute_Path : constant String :=
1385 Normalize_Pathname (Result.all);
1388 Result := new String'(Absolute_Path);
1394 end Locate_Exec_On_Path;
1396 -------------------------
1397 -- Locate_Regular_File --
1398 -------------------------
1400 function Locate_Regular_File
1401 (File_Name : C_File_Name;
1402 Path : C_File_Name) return String_Access
1404 function Locate_Regular_File
1405 (C_File_Name, Path_Val : Address) return Address;
1406 pragma Import (C, Locate_Regular_File, "__gnat_locate_regular_file");
1408 procedure Free (Ptr : System.Address);
1409 pragma Import (C, Free, "free");
1411 Path_Addr : Address;
1413 Result : String_Access;
1416 Path_Addr := Locate_Regular_File (File_Name, Path);
1417 Path_Len := C_String_Length (Path_Addr);
1419 if Path_Len = 0 then
1422 Result := To_Path_String_Access (Path_Addr, Path_Len);
1426 end Locate_Regular_File;
1428 function Locate_Regular_File
1429 (File_Name : String;
1430 Path : String) return String_Access
1432 C_File_Name : String (1 .. File_Name'Length + 1);
1433 C_Path : String (1 .. Path'Length + 1);
1434 Result : String_Access;
1437 C_File_Name (1 .. File_Name'Length) := File_Name;
1438 C_File_Name (C_File_Name'Last) := ASCII.NUL;
1440 C_Path (1 .. Path'Length) := Path;
1441 C_Path (C_Path'Last) := ASCII.NUL;
1443 Result := Locate_Regular_File (C_File_Name'Address, C_Path'Address);
1445 -- Always return an absolute path name
1447 if Result /= null and then not Is_Absolute_Path (Result.all) then
1449 Absolute_Path : constant String := Normalize_Pathname (Result.all);
1452 Result := new String'(Absolute_Path);
1457 end Locate_Regular_File;
1459 ------------------------
1460 -- Non_Blocking_Spawn --
1461 ------------------------
1463 function Non_Blocking_Spawn
1464 (Program_Name : String;
1465 Args : Argument_List) return Process_Id
1471 Spawn_Internal (Program_Name, Args, Junk, Pid, Blocking => False);
1473 end Non_Blocking_Spawn;
1475 function Non_Blocking_Spawn
1476 (Program_Name : String;
1477 Args : Argument_List;
1478 Output_File_Descriptor : File_Descriptor;
1479 Err_To_Out : Boolean := True) return Process_Id
1481 Saved_Output : File_Descriptor;
1482 Saved_Error : File_Descriptor := Invalid_FD; -- prevent warning
1486 if Output_File_Descriptor = Invalid_FD then
1490 -- Set standard output and, if specified, error to the temporary file
1492 Saved_Output := Dup (Standout);
1493 Dup2 (Output_File_Descriptor, Standout);
1496 Saved_Error := Dup (Standerr);
1497 Dup2 (Output_File_Descriptor, Standerr);
1500 -- Spawn the program
1502 Pid := Non_Blocking_Spawn (Program_Name, Args);
1504 -- Restore the standard output and error
1506 Dup2 (Saved_Output, Standout);
1509 Dup2 (Saved_Error, Standerr);
1512 -- And close the saved standard output and error file descriptors
1514 Close (Saved_Output);
1517 Close (Saved_Error);
1521 end Non_Blocking_Spawn;
1523 function Non_Blocking_Spawn
1524 (Program_Name : String;
1525 Args : Argument_List;
1526 Output_File : String;
1527 Err_To_Out : Boolean := True) return Process_Id
1529 Output_File_Descriptor : constant File_Descriptor :=
1530 Create_Output_Text_File (Output_File);
1531 Result : Process_Id;
1534 -- Do not attempt to spawn if the output file could not be created
1536 if Output_File_Descriptor = Invalid_FD then
1540 Result := Non_Blocking_Spawn
1541 (Program_Name, Args, Output_File_Descriptor, Err_To_Out);
1543 -- Close the file just created for the output, as the file descriptor
1544 -- cannot be used anywhere, being a local value. It is safe to do
1545 -- that, as the file descriptor has been duplicated to form
1546 -- standard output and error of the spawned process.
1548 Close (Output_File_Descriptor);
1552 end Non_Blocking_Spawn;
1554 -------------------------
1555 -- Normalize_Arguments --
1556 -------------------------
1558 procedure Normalize_Arguments (Args : in out Argument_List) is
1560 procedure Quote_Argument (Arg : in out String_Access);
1561 -- Add quote around argument if it contains spaces
1563 C_Argument_Needs_Quote : Integer;
1564 pragma Import (C, C_Argument_Needs_Quote, "__gnat_argument_needs_quote");
1565 Argument_Needs_Quote : constant Boolean := C_Argument_Needs_Quote /= 0;
1567 --------------------
1568 -- Quote_Argument --
1569 --------------------
1571 procedure Quote_Argument (Arg : in out String_Access) is
1572 Res : String (1 .. Arg'Length * 2);
1574 Quote_Needed : Boolean := False;
1577 if Arg (Arg'First) /= '"' or else Arg (Arg'Last) /= '"' then
1583 for K in Arg'Range loop
1587 if Arg (K) = '"' then
1591 Quote_Needed := True;
1593 elsif Arg (K) = ' ' then
1595 Quote_Needed := True;
1603 if Quote_Needed then
1605 -- If null terminated string, put the quote before
1607 if Res (J) = ASCII.Nul then
1610 Res (J) := ASCII.Nul;
1612 -- If argument is terminated by '\', then double it. Otherwise
1613 -- the ending quote will be taken as-is. This is quite strange
1614 -- spawn behavior from Windows, but this is what we see!
1617 if Res (J) = '\' then
1629 Old : String_Access := Arg;
1632 Arg := new String'(Res (1 .. J));
1640 -- Start of processing for Normalize_Arguments
1643 if Argument_Needs_Quote then
1644 for K in Args'Range loop
1645 if Args (K) /= null and then Args (K)'Length /= 0 then
1646 Quote_Argument (Args (K));
1650 end Normalize_Arguments;
1652 ------------------------
1653 -- Normalize_Pathname --
1654 ------------------------
1656 function Normalize_Pathname
1658 Directory : String := "";
1659 Resolve_Links : Boolean := True;
1660 Case_Sensitive : Boolean := True) return String
1663 pragma Import (C, Max_Path, "__gnat_max_path_len");
1664 -- Maximum length of a path name
1666 procedure Get_Current_Dir
1667 (Dir : System.Address;
1668 Length : System.Address);
1669 pragma Import (C, Get_Current_Dir, "__gnat_get_current_dir");
1671 Path_Buffer : String (1 .. Max_Path + Max_Path + 2);
1672 End_Path : Natural := 0;
1673 Link_Buffer : String (1 .. Max_Path + 2);
1679 Max_Iterations : constant := 500;
1681 function Get_File_Names_Case_Sensitive return Integer;
1683 (C, Get_File_Names_Case_Sensitive,
1684 "__gnat_get_file_names_case_sensitive");
1686 Fold_To_Lower_Case : constant Boolean :=
1688 and then Get_File_Names_Case_Sensitive = 0;
1691 (Path : System.Address;
1692 Buf : System.Address;
1693 Bufsiz : Integer) return Integer;
1694 pragma Import (C, Readlink, "__gnat_readlink");
1696 function To_Canonical_File_Spec
1697 (Host_File : System.Address) return System.Address;
1699 (C, To_Canonical_File_Spec, "__gnat_to_canonical_file_spec");
1701 The_Name : String (1 .. Name'Length + 1);
1702 Canonical_File_Addr : System.Address;
1703 Canonical_File_Len : Integer;
1705 function Strlen (S : System.Address) return Integer;
1706 pragma Import (C, Strlen, "strlen");
1708 function Final_Value (S : String) return String;
1709 -- Make final adjustment to the returned string. This function strips
1710 -- trailing directory separators, and folds returned string to lower
1711 -- case if required.
1713 function Get_Directory (Dir : String) return String;
1714 -- If Dir is not empty, return it, adding a directory separator
1715 -- if not already present, otherwise return current working directory
1716 -- with terminating directory separator.
1722 function Final_Value (S : String) return String is
1724 -- We may need to fold S to lower case, so we need a variable
1729 if Fold_To_Lower_Case then
1730 System.Case_Util.To_Lower (S1);
1733 -- Remove trailing directory separator, if any
1738 and then (S1 (Last) = '/'
1740 S1 (Last) = Directory_Separator)
1742 -- Special case for Windows: C:\
1745 and then S1 (1) /= Directory_Separator
1746 and then S1 (2) = ':'
1755 return S1 (1 .. Last);
1762 function Get_Directory (Dir : String) return String is
1764 -- Directory given, add directory separator if needed
1766 if Dir'Length > 0 then
1767 if Dir (Dir'Last) = Directory_Separator then
1771 Result : String (1 .. Dir'Length + 1);
1773 Result (1 .. Dir'Length) := Dir;
1774 Result (Result'Length) := Directory_Separator;
1779 -- Directory name not given, get current directory
1783 Buffer : String (1 .. Max_Path + 2);
1784 Path_Len : Natural := Max_Path;
1787 Get_Current_Dir (Buffer'Address, Path_Len'Address);
1789 if Buffer (Path_Len) /= Directory_Separator then
1790 Path_Len := Path_Len + 1;
1791 Buffer (Path_Len) := Directory_Separator;
1794 -- By default, the drive letter on Windows is in upper case
1796 if On_Windows and then Path_Len >= 2 and then
1799 System.Case_Util.To_Upper (Buffer (1 .. 1));
1802 return Buffer (1 .. Path_Len);
1807 Reference_Dir : constant String := Get_Directory (Directory);
1808 -- Current directory name specified
1810 -- Start of processing for Normalize_Pathname
1813 -- Special case, if name is null, then return null
1815 if Name'Length = 0 then
1819 -- First, convert VMS file spec to Unix file spec.
1820 -- If Name is not in VMS syntax, then this is equivalent
1821 -- to put Name at the begining of Path_Buffer.
1823 VMS_Conversion : begin
1824 The_Name (1 .. Name'Length) := Name;
1825 The_Name (The_Name'Last) := ASCII.NUL;
1827 Canonical_File_Addr := To_Canonical_File_Spec (The_Name'Address);
1828 Canonical_File_Len := Strlen (Canonical_File_Addr);
1830 -- If VMS syntax conversion has failed, return an empty string
1831 -- to indicate the failure.
1833 if Canonical_File_Len = 0 then
1838 subtype Path_String is String (1 .. Canonical_File_Len);
1839 type Path_String_Access is access Path_String;
1841 function Address_To_Access is new
1842 Ada.Unchecked_Conversion (Source => Address,
1843 Target => Path_String_Access);
1845 Path_Access : constant Path_String_Access :=
1846 Address_To_Access (Canonical_File_Addr);
1849 Path_Buffer (1 .. Canonical_File_Len) := Path_Access.all;
1850 End_Path := Canonical_File_Len;
1855 -- Replace all '/' by Directory Separators (this is for Windows)
1857 if Directory_Separator /= '/' then
1858 for Index in 1 .. End_Path loop
1859 if Path_Buffer (Index) = '/' then
1860 Path_Buffer (Index) := Directory_Separator;
1865 -- Resolve directory names for Windows (formerly also VMS)
1867 -- On VMS, if we have a Unix path such as /temp/..., and TEMP is a
1868 -- logical name, we must not try to resolve this logical name, because
1869 -- it may have multiple equivalences and if resolved we will only
1870 -- get the first one.
1872 -- On Windows, if we have an absolute path starting with a directory
1873 -- separator, we need to have the drive letter appended in front.
1875 -- On Windows, Get_Current_Dir will return a suitable directory
1876 -- name (path starting with a drive letter on Windows). So we take this
1877 -- drive letter and prepend it to the current path.
1880 and then Path_Buffer (1) = Directory_Separator
1881 and then Path_Buffer (2) /= Directory_Separator
1884 Cur_Dir : String := Get_Directory ("");
1885 -- Get the current directory to get the drive letter
1888 if Cur_Dir'Length > 2
1889 and then Cur_Dir (Cur_Dir'First + 1) = ':'
1891 Path_Buffer (3 .. End_Path + 2) := Path_Buffer (1 .. End_Path);
1892 Path_Buffer (1 .. 2) :=
1893 Cur_Dir (Cur_Dir'First .. Cur_Dir'First + 1);
1894 End_Path := End_Path + 2;
1899 -- Start the conversions
1901 -- If this is not finished after Max_Iterations, give up and return an
1904 for J in 1 .. Max_Iterations loop
1906 -- If we don't have an absolute pathname, prepend the directory
1910 and then not Is_Absolute_Path (Path_Buffer (1 .. End_Path))
1913 (Reference_Dir'Length + 1 .. Reference_Dir'Length + End_Path) :=
1914 Path_Buffer (1 .. End_Path);
1915 End_Path := Reference_Dir'Length + End_Path;
1916 Path_Buffer (1 .. Reference_Dir'Length) := Reference_Dir;
1917 Last := Reference_Dir'Length;
1923 -- Ensure that Windows network drives are kept, e.g: \\server\drive-c
1926 and then Directory_Separator = '\'
1927 and then Path_Buffer (1 .. 2) = "\\"
1932 -- If we have traversed the full pathname, return it
1934 if Start > End_Path then
1935 return Final_Value (Path_Buffer (1 .. End_Path));
1938 -- Remove duplicate directory separators
1940 while Path_Buffer (Start) = Directory_Separator loop
1941 if Start = End_Path then
1942 return Final_Value (Path_Buffer (1 .. End_Path - 1));
1945 Path_Buffer (Start .. End_Path - 1) :=
1946 Path_Buffer (Start + 1 .. End_Path);
1947 End_Path := End_Path - 1;
1951 -- Find the end of the current field: last character or the one
1952 -- preceding the next directory separator.
1954 while Finish < End_Path
1955 and then Path_Buffer (Finish + 1) /= Directory_Separator
1957 Finish := Finish + 1;
1962 if Start = Finish and then Path_Buffer (Start) = '.' then
1963 if Start = End_Path then
1965 return (1 => Directory_Separator);
1968 if Fold_To_Lower_Case then
1969 System.Case_Util.To_Lower (Path_Buffer (1 .. Last - 1));
1972 return Path_Buffer (1 .. Last - 1);
1977 Path_Buffer (Last + 1 .. End_Path - 2) :=
1978 Path_Buffer (Last + 3 .. End_Path);
1979 End_Path := End_Path - 2;
1982 -- Remove ".." fields
1984 elsif Finish = Start + 1
1985 and then Path_Buffer (Start .. Finish) = ".."
1990 exit when Start < 1 or else
1991 Path_Buffer (Start) = Directory_Separator;
1995 if Finish = End_Path then
1996 return (1 => Directory_Separator);
1999 Path_Buffer (1 .. End_Path - Finish) :=
2000 Path_Buffer (Finish + 1 .. End_Path);
2001 End_Path := End_Path - Finish;
2006 if Finish = End_Path then
2007 return Final_Value (Path_Buffer (1 .. Start - 1));
2010 Path_Buffer (Start + 1 .. Start + End_Path - Finish - 1) :=
2011 Path_Buffer (Finish + 2 .. End_Path);
2012 End_Path := Start + End_Path - Finish - 1;
2017 -- Check if current field is a symbolic link
2019 elsif Resolve_Links then
2021 Saved : constant Character := Path_Buffer (Finish + 1);
2024 Path_Buffer (Finish + 1) := ASCII.NUL;
2025 Status := Readlink (Path_Buffer'Address,
2026 Link_Buffer'Address,
2027 Link_Buffer'Length);
2028 Path_Buffer (Finish + 1) := Saved;
2031 -- Not a symbolic link, move to the next field, if any
2036 -- Replace symbolic link with its value
2039 if Is_Absolute_Path (Link_Buffer (1 .. Status)) then
2040 Path_Buffer (Status + 1 .. End_Path - (Finish - Status)) :=
2041 Path_Buffer (Finish + 1 .. End_Path);
2042 End_Path := End_Path - (Finish - Status);
2043 Path_Buffer (1 .. Status) := Link_Buffer (1 .. Status);
2048 (Last + Status + 1 .. End_Path - Finish + Last + Status) :=
2049 Path_Buffer (Finish + 1 .. End_Path);
2050 End_Path := End_Path - Finish + Last + Status;
2051 Path_Buffer (Last + 1 .. Last + Status) :=
2052 Link_Buffer (1 .. Status);
2061 -- Too many iterations: give up
2063 -- This can happen when there is a circularity in the symbolic links: A
2064 -- is a symbolic link for B, which itself is a symbolic link, and the
2065 -- target of B or of another symbolic link target of B is A. In this
2066 -- case, we return an empty string to indicate failure to resolve.
2069 end Normalize_Pathname;
2076 (Name : C_File_Name;
2077 Fmode : Mode) return File_Descriptor
2079 function C_Open_Read
2080 (Name : C_File_Name;
2081 Fmode : Mode) return File_Descriptor;
2082 pragma Import (C, C_Open_Read, "__gnat_open_read");
2084 return C_Open_Read (Name, Fmode);
2089 Fmode : Mode) return File_Descriptor
2091 C_Name : String (1 .. Name'Length + 1);
2093 C_Name (1 .. Name'Length) := Name;
2094 C_Name (C_Name'Last) := ASCII.NUL;
2095 return Open_Read (C_Name (C_Name'First)'Address, Fmode);
2098 ---------------------
2099 -- Open_Read_Write --
2100 ---------------------
2102 function Open_Read_Write
2103 (Name : C_File_Name;
2104 Fmode : Mode) return File_Descriptor
2106 function C_Open_Read_Write
2107 (Name : C_File_Name;
2108 Fmode : Mode) return File_Descriptor;
2109 pragma Import (C, C_Open_Read_Write, "__gnat_open_rw");
2111 return C_Open_Read_Write (Name, Fmode);
2112 end Open_Read_Write;
2114 function Open_Read_Write
2116 Fmode : Mode) return File_Descriptor
2118 C_Name : String (1 .. Name'Length + 1);
2120 C_Name (1 .. Name'Length) := Name;
2121 C_Name (C_Name'Last) := ASCII.NUL;
2122 return Open_Read_Write (C_Name (C_Name'First)'Address, Fmode);
2123 end Open_Read_Write;
2129 procedure OS_Exit (Status : Integer) is
2131 OS_Exit_Ptr (Status);
2132 raise Program_Error;
2135 ---------------------
2136 -- OS_Exit_Default --
2137 ---------------------
2139 procedure OS_Exit_Default (Status : Integer) is
2140 procedure GNAT_OS_Exit (Status : Integer);
2141 pragma Import (C, GNAT_OS_Exit, "__gnat_os_exit");
2142 pragma No_Return (GNAT_OS_Exit);
2144 GNAT_OS_Exit (Status);
2145 end OS_Exit_Default;
2147 --------------------
2148 -- Pid_To_Integer --
2149 --------------------
2151 function Pid_To_Integer (Pid : Process_Id) return Integer is
2153 return Integer (Pid);
2161 (FD : File_Descriptor;
2163 N : Integer) return Integer
2166 return Integer (System.CRTL.read
2167 (System.CRTL.int (FD), System.CRTL.chars (A), System.CRTL.int (N)));
2174 procedure Rename_File
2175 (Old_Name : C_File_Name;
2176 New_Name : C_File_Name;
2177 Success : out Boolean)
2179 function rename (From, To : Address) return Integer;
2180 pragma Import (C, rename, "rename");
2183 R := rename (Old_Name, New_Name);
2187 procedure Rename_File
2190 Success : out Boolean)
2192 C_Old_Name : String (1 .. Old_Name'Length + 1);
2193 C_New_Name : String (1 .. New_Name'Length + 1);
2195 C_Old_Name (1 .. Old_Name'Length) := Old_Name;
2196 C_Old_Name (C_Old_Name'Last) := ASCII.NUL;
2197 C_New_Name (1 .. New_Name'Length) := New_Name;
2198 C_New_Name (C_New_Name'Last) := ASCII.NUL;
2199 Rename_File (C_Old_Name'Address, C_New_Name'Address, Success);
2202 -----------------------
2203 -- Set_Close_On_Exec --
2204 -----------------------
2206 procedure Set_Close_On_Exec
2207 (FD : File_Descriptor;
2208 Close_On_Exec : Boolean;
2209 Status : out Boolean)
2211 function C_Set_Close_On_Exec
2212 (FD : File_Descriptor; Close_On_Exec : System.CRTL.int)
2213 return System.CRTL.int;
2214 pragma Import (C, C_Set_Close_On_Exec, "__gnat_set_close_on_exec");
2216 Status := C_Set_Close_On_Exec (FD, Boolean'Pos (Close_On_Exec)) = 0;
2217 end Set_Close_On_Exec;
2219 --------------------
2220 -- Set_Executable --
2221 --------------------
2223 procedure Set_Executable (Name : String) is
2224 procedure C_Set_Executable (Name : C_File_Name);
2225 pragma Import (C, C_Set_Executable, "__gnat_set_executable");
2226 C_Name : aliased String (Name'First .. Name'Last + 1);
2228 C_Name (Name'Range) := Name;
2229 C_Name (C_Name'Last) := ASCII.NUL;
2230 C_Set_Executable (C_Name (C_Name'First)'Address);
2233 --------------------
2235 --------------------
2237 procedure Set_Read_Only (Name : String) is
2238 procedure C_Set_Read_Only (Name : C_File_Name);
2239 pragma Import (C, C_Set_Read_Only, "__gnat_set_readonly");
2240 C_Name : aliased String (Name'First .. Name'Last + 1);
2242 C_Name (Name'Range) := Name;
2243 C_Name (C_Name'Last) := ASCII.NUL;
2244 C_Set_Read_Only (C_Name (C_Name'First)'Address);
2247 --------------------
2249 --------------------
2251 procedure Set_Writable (Name : String) is
2252 procedure C_Set_Writable (Name : C_File_Name);
2253 pragma Import (C, C_Set_Writable, "__gnat_set_writable");
2254 C_Name : aliased String (Name'First .. Name'Last + 1);
2256 C_Name (Name'Range) := Name;
2257 C_Name (C_Name'Last) := ASCII.NUL;
2258 C_Set_Writable (C_Name (C_Name'First)'Address);
2265 procedure Setenv (Name : String; Value : String) is
2266 F_Name : String (1 .. Name'Length + 1);
2267 F_Value : String (1 .. Value'Length + 1);
2269 procedure Set_Env_Value (Name, Value : System.Address);
2270 pragma Import (C, Set_Env_Value, "__gnat_setenv");
2273 F_Name (1 .. Name'Length) := Name;
2274 F_Name (F_Name'Last) := ASCII.NUL;
2276 F_Value (1 .. Value'Length) := Value;
2277 F_Value (F_Value'Last) := ASCII.NUL;
2279 Set_Env_Value (F_Name'Address, F_Value'Address);
2287 (Program_Name : String;
2288 Args : Argument_List) return Integer
2293 Spawn_Internal (Program_Name, Args, Result, Junk, Blocking => True);
2298 (Program_Name : String;
2299 Args : Argument_List;
2300 Success : out Boolean)
2303 Success := (Spawn (Program_Name, Args) = 0);
2307 (Program_Name : String;
2308 Args : Argument_List;
2309 Output_File_Descriptor : File_Descriptor;
2310 Return_Code : out Integer;
2311 Err_To_Out : Boolean := True)
2313 Saved_Output : File_Descriptor;
2314 Saved_Error : File_Descriptor := Invalid_FD; -- prevent compiler warning
2317 -- Set standard output and error to the temporary file
2319 Saved_Output := Dup (Standout);
2320 Dup2 (Output_File_Descriptor, Standout);
2323 Saved_Error := Dup (Standerr);
2324 Dup2 (Output_File_Descriptor, Standerr);
2327 -- Spawn the program
2329 Return_Code := Spawn (Program_Name, Args);
2331 -- Restore the standard output and error
2333 Dup2 (Saved_Output, Standout);
2336 Dup2 (Saved_Error, Standerr);
2339 -- And close the saved standard output and error file descriptors
2341 Close (Saved_Output);
2344 Close (Saved_Error);
2349 (Program_Name : String;
2350 Args : Argument_List;
2351 Output_File : String;
2352 Success : out Boolean;
2353 Return_Code : out Integer;
2354 Err_To_Out : Boolean := True)
2356 FD : File_Descriptor;
2362 FD := Create_Output_Text_File (Output_File);
2364 if FD = Invalid_FD then
2369 Spawn (Program_Name, Args, FD, Return_Code, Err_To_Out);
2371 Close (FD, Success);
2374 --------------------
2375 -- Spawn_Internal --
2376 --------------------
2378 procedure Spawn_Internal
2379 (Program_Name : String;
2380 Args : Argument_List;
2381 Result : out Integer;
2382 Pid : out Process_Id;
2386 procedure Spawn (Args : Argument_List);
2387 -- Call Spawn with given argument list
2389 N_Args : Argument_List (Args'Range);
2390 -- Normalized arguments
2396 procedure Spawn (Args : Argument_List) is
2397 type Chars is array (Positive range <>) of aliased Character;
2398 type Char_Ptr is access constant Character;
2400 Command_Len : constant Positive := Program_Name'Length + 1
2401 + Args_Length (Args);
2402 Command_Last : Natural := 0;
2403 Command : aliased Chars (1 .. Command_Len);
2404 -- Command contains all characters of the Program_Name and Args, all
2405 -- terminated by ASCII.NUL characters
2407 Arg_List_Len : constant Positive := Args'Length + 2;
2408 Arg_List_Last : Natural := 0;
2409 Arg_List : aliased array (1 .. Arg_List_Len) of Char_Ptr;
2410 -- List with pointers to NUL-terminated strings of the Program_Name
2411 -- and the Args and terminated with a null pointer. We rely on the
2412 -- default initialization for the last null pointer.
2414 procedure Add_To_Command (S : String);
2415 -- Add S and a NUL character to Command, updating Last
2417 function Portable_Spawn (Args : Address) return Integer;
2418 pragma Import (C, Portable_Spawn, "__gnat_portable_spawn");
2420 function Portable_No_Block_Spawn (Args : Address) return Process_Id;
2422 (C, Portable_No_Block_Spawn, "__gnat_portable_no_block_spawn");
2424 --------------------
2425 -- Add_To_Command --
2426 --------------------
2428 procedure Add_To_Command (S : String) is
2429 First : constant Natural := Command_Last + 1;
2432 Command_Last := Command_Last + S'Length;
2434 -- Move characters one at a time, because Command has aliased
2437 -- But not volatile, so why is this necessary ???
2439 for J in S'Range loop
2440 Command (First + J - S'First) := S (J);
2443 Command_Last := Command_Last + 1;
2444 Command (Command_Last) := ASCII.NUL;
2446 Arg_List_Last := Arg_List_Last + 1;
2447 Arg_List (Arg_List_Last) := Command (First)'Access;
2450 -- Start of processing for Spawn
2453 Add_To_Command (Program_Name);
2455 for J in Args'Range loop
2456 Add_To_Command (Args (J).all);
2461 Result := Portable_Spawn (Arg_List'Address);
2463 Pid := Portable_No_Block_Spawn (Arg_List'Address);
2464 Result := Boolean'Pos (Pid /= Invalid_Pid);
2468 -- Start of processing for Spawn_Internal
2471 -- Copy arguments into a local structure
2473 for K in N_Args'Range loop
2474 N_Args (K) := new String'(Args (K).all);
2477 -- Normalize those arguments
2479 Normalize_Arguments (N_Args);
2481 -- Call spawn using the normalized arguments
2485 -- Free arguments list
2487 for K in N_Args'Range loop
2492 ---------------------------
2493 -- To_Path_String_Access --
2494 ---------------------------
2496 function To_Path_String_Access
2497 (Path_Addr : Address;
2498 Path_Len : Integer) return String_Access
2500 subtype Path_String is String (1 .. Path_Len);
2501 type Path_String_Access is access Path_String;
2503 function Address_To_Access is new
2504 Ada.Unchecked_Conversion (Source => Address,
2505 Target => Path_String_Access);
2507 Path_Access : constant Path_String_Access :=
2508 Address_To_Access (Path_Addr);
2510 Return_Val : String_Access;
2513 Return_Val := new String (1 .. Path_Len);
2515 for J in 1 .. Path_Len loop
2516 Return_Val (J) := Path_Access (J);
2520 end To_Path_String_Access;
2526 procedure Wait_Process (Pid : out Process_Id; Success : out Boolean) is
2529 function Portable_Wait (S : Address) return Process_Id;
2530 pragma Import (C, Portable_Wait, "__gnat_portable_wait");
2533 Pid := Portable_Wait (Status'Address);
2534 Success := (Status = 0);
2542 (FD : File_Descriptor;
2544 N : Integer) return Integer
2547 return Integer (System.CRTL.write
2548 (System.CRTL.int (FD), System.CRTL.chars (A), System.CRTL.int (N)));