2014-10-10 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / s-os_lib.adb
blob3fad849b87adbade4f03b98db61e6c29d2c81c50
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S Y S T E M . O S _ L I B --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1995-2014, AdaCore --
10 -- --
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 3, 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. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 pragma Compiler_Unit_Warning;
34 with Ada.Unchecked_Conversion;
35 with Ada.Unchecked_Deallocation;
36 with System; use System;
37 with System.Case_Util;
38 with System.CRTL;
39 with System.Soft_Links;
41 package body System.OS_Lib is
43 subtype size_t is CRTL.size_t;
45 procedure Strncpy (dest, src : System.Address; n : size_t)
46 renames CRTL.strncpy;
48 -- Imported procedures Dup and Dup2 are used in procedures Spawn and
49 -- Non_Blocking_Spawn.
51 function Dup (Fd : File_Descriptor) return File_Descriptor;
52 pragma Import (C, Dup, "__gnat_dup");
54 procedure Dup2 (Old_Fd, New_Fd : File_Descriptor);
55 pragma Import (C, Dup2, "__gnat_dup2");
57 function Copy_Attributes
58 (From, To : System.Address;
59 Mode : Integer) return Integer;
60 pragma Import (C, Copy_Attributes, "__gnat_copy_attribs");
61 -- Mode = 0 - copy only time stamps.
62 -- Mode = 1 - copy time stamps and read/write/execute attributes
64 On_Windows : constant Boolean := Directory_Separator = '\';
65 -- An indication that we are on Windows. Used in Normalize_Pathname, to
66 -- deal with drive letters in the beginning of absolute paths.
68 package SSL renames System.Soft_Links;
70 -- The following are used by Create_Temp_File
72 First_Temp_File_Name : constant String := "GNAT-TEMP-000000.TMP";
73 -- Used to initialize Current_Temp_File_Name and Temp_File_Name_Last_Digit
75 Current_Temp_File_Name : String := First_Temp_File_Name;
76 -- Name of the temp file last created
78 Temp_File_Name_Last_Digit : constant Positive :=
79 First_Temp_File_Name'Last - 4;
80 -- Position of the last digit in Current_Temp_File_Name
82 Max_Attempts : constant := 100;
83 -- The maximum number of attempts to create a new temp file
85 -----------------------
86 -- Local Subprograms --
87 -----------------------
89 function Args_Length (Args : Argument_List) return Natural;
90 -- Returns total number of characters needed to create a string of all Args
91 -- terminated by ASCII.NUL characters.
93 procedure Create_Temp_File_Internal
94 (FD : out File_Descriptor;
95 Name : out String_Access;
96 Stdout : Boolean);
97 -- Internal routine to implement two Create_Temp_File routines. If Stdout
98 -- is set to True the created descriptor is stdout-compatible, otherwise
99 -- it might not be depending on the OS. The first two parameters are as
100 -- in Create_Temp_File.
102 function C_String_Length (S : Address) return Integer;
103 -- Returns the length of C (null-terminated) string at S, or 0 for
104 -- Null_Address.
106 procedure Spawn_Internal
107 (Program_Name : String;
108 Args : Argument_List;
109 Result : out Integer;
110 Pid : out Process_Id;
111 Blocking : Boolean);
112 -- Internal routine to implement the two Spawn (blocking/non blocking)
113 -- routines. If Blocking is set to True then the spawn is blocking
114 -- otherwise it is non blocking. In this latter case the Pid contains the
115 -- process id number. The first three parameters are as in Spawn. Note that
116 -- Spawn_Internal normalizes the argument list before calling the low level
117 -- system spawn routines (see Normalize_Arguments).
119 -- Note: Normalize_Arguments is designed to do nothing if it is called more
120 -- than once, so calling Normalize_Arguments before calling one of the
121 -- spawn routines is fine.
123 function To_Path_String_Access
124 (Path_Addr : Address;
125 Path_Len : Integer) return String_Access;
126 -- Converts a C String to an Ada String. We could do this making use of
127 -- Interfaces.C.Strings but we prefer not to import that entire package
129 ---------
130 -- "<" --
131 ---------
133 function "<" (X, Y : OS_Time) return Boolean is
134 begin
135 return Long_Integer (X) < Long_Integer (Y);
136 end "<";
138 ----------
139 -- "<=" --
140 ----------
142 function "<=" (X, Y : OS_Time) return Boolean is
143 begin
144 return Long_Integer (X) <= Long_Integer (Y);
145 end "<=";
147 ---------
148 -- ">" --
149 ---------
151 function ">" (X, Y : OS_Time) return Boolean is
152 begin
153 return Long_Integer (X) > Long_Integer (Y);
154 end ">";
156 ----------
157 -- ">=" --
158 ----------
160 function ">=" (X, Y : OS_Time) return Boolean is
161 begin
162 return Long_Integer (X) >= Long_Integer (Y);
163 end ">=";
165 -----------------
166 -- Args_Length --
167 -----------------
169 function Args_Length (Args : Argument_List) return Natural is
170 Len : Natural := 0;
172 begin
173 for J in Args'Range loop
174 Len := Len + Args (J)'Length + 1; -- One extra for ASCII.NUL
175 end loop;
177 return Len;
178 end Args_Length;
180 -----------------------------
181 -- Argument_String_To_List --
182 -----------------------------
184 function Argument_String_To_List
185 (Arg_String : String) return Argument_List_Access
187 Max_Args : constant Integer := Arg_String'Length;
188 New_Argv : Argument_List (1 .. Max_Args);
189 New_Argc : Natural := 0;
190 Idx : Integer;
192 begin
193 Idx := Arg_String'First;
195 loop
196 exit when Idx > Arg_String'Last;
198 declare
199 Quoted : Boolean := False;
200 Backqd : Boolean := False;
201 Old_Idx : Integer;
203 begin
204 Old_Idx := Idx;
206 loop
207 -- An unquoted space is the end of an argument
209 if not (Backqd or Quoted)
210 and then Arg_String (Idx) = ' '
211 then
212 exit;
214 -- Start of a quoted string
216 elsif not (Backqd or Quoted)
217 and then Arg_String (Idx) = '"'
218 then
219 Quoted := True;
221 -- End of a quoted string and end of an argument
223 elsif (Quoted and not Backqd)
224 and then Arg_String (Idx) = '"'
225 then
226 Idx := Idx + 1;
227 exit;
229 -- Following character is backquoted
231 elsif Arg_String (Idx) = '\' then
232 Backqd := True;
234 -- Turn off backquoting after advancing one character
236 elsif Backqd then
237 Backqd := False;
239 end if;
241 Idx := Idx + 1;
242 exit when Idx > Arg_String'Last;
243 end loop;
245 -- Found an argument
247 New_Argc := New_Argc + 1;
248 New_Argv (New_Argc) :=
249 new String'(Arg_String (Old_Idx .. Idx - 1));
251 -- Skip extraneous spaces
253 while Idx <= Arg_String'Last and then Arg_String (Idx) = ' ' loop
254 Idx := Idx + 1;
255 end loop;
256 end;
257 end loop;
259 return new Argument_List'(New_Argv (1 .. New_Argc));
260 end Argument_String_To_List;
262 ---------------------
263 -- C_String_Length --
264 ---------------------
266 function C_String_Length (S : Address) return Integer is
267 begin
268 if S = Null_Address then
269 return 0;
270 else
271 return Integer (CRTL.strlen (S));
272 end if;
273 end C_String_Length;
275 -----------
276 -- Close --
277 -----------
279 procedure Close (FD : File_Descriptor) is
280 use CRTL;
281 Discard : constant int := close (int (FD));
282 begin
283 null;
284 end Close;
286 procedure Close (FD : File_Descriptor; Status : out Boolean) is
287 use CRTL;
288 begin
289 Status := (close (int (FD)) = 0);
290 end Close;
292 ---------------
293 -- Copy_File --
294 ---------------
296 procedure Copy_File
297 (Name : String;
298 Pathname : String;
299 Success : out Boolean;
300 Mode : Copy_Mode := Copy;
301 Preserve : Attribute := Time_Stamps)
303 From : File_Descriptor;
304 To : File_Descriptor;
306 Copy_Error : exception;
307 -- Internal exception raised to signal error in copy
309 function Build_Path (Dir : String; File : String) return String;
310 -- Returns pathname Dir concatenated with File adding the directory
311 -- separator only if needed.
313 procedure Copy (From, To : File_Descriptor);
314 -- Read data from From and place them into To. In both cases the
315 -- operations uses the current file position. Raises Constraint_Error
316 -- if a problem occurs during the copy.
318 procedure Copy_To (To_Name : String);
319 -- Does a straight copy from source to designated destination file
321 ----------------
322 -- Build_Path --
323 ----------------
325 function Build_Path (Dir : String; File : String) return String is
326 Res : String (1 .. Dir'Length + File'Length + 1);
328 Base_File_Ptr : Integer;
329 -- The base file name is File (Base_File_Ptr + 1 .. File'Last)
331 function Is_Dirsep (C : Character) return Boolean;
332 pragma Inline (Is_Dirsep);
333 -- Returns True if C is a directory separator. On Windows we
334 -- handle both styles of directory separator.
336 ---------------
337 -- Is_Dirsep --
338 ---------------
340 function Is_Dirsep (C : Character) return Boolean is
341 begin
342 return C = Directory_Separator or else C = '/';
343 end Is_Dirsep;
345 -- Start of processing for Build_Path
347 begin
348 -- Find base file name
350 Base_File_Ptr := File'Last;
351 while Base_File_Ptr >= File'First loop
352 exit when Is_Dirsep (File (Base_File_Ptr));
353 Base_File_Ptr := Base_File_Ptr - 1;
354 end loop;
356 declare
357 Base_File : String renames
358 File (Base_File_Ptr + 1 .. File'Last);
360 begin
361 Res (1 .. Dir'Length) := Dir;
363 if Is_Dirsep (Dir (Dir'Last)) then
364 Res (Dir'Length + 1 .. Dir'Length + Base_File'Length) :=
365 Base_File;
366 return Res (1 .. Dir'Length + Base_File'Length);
368 else
369 Res (Dir'Length + 1) := Directory_Separator;
370 Res (Dir'Length + 2 .. Dir'Length + 1 + Base_File'Length) :=
371 Base_File;
372 return Res (1 .. Dir'Length + 1 + Base_File'Length);
373 end if;
374 end;
375 end Build_Path;
377 ----------
378 -- Copy --
379 ----------
381 procedure Copy (From, To : File_Descriptor) is
382 Buf_Size : constant := 200_000;
383 type Buf is array (1 .. Buf_Size) of Character;
384 type Buf_Ptr is access Buf;
386 Buffer : Buf_Ptr;
387 R : Integer;
388 W : Integer;
390 Status_From : Boolean;
391 Status_To : Boolean;
392 -- Statuses for the calls to Close
394 procedure Free is new Ada.Unchecked_Deallocation (Buf, Buf_Ptr);
396 begin
397 -- Check for invalid descriptors, making sure that we do not
398 -- accidentally leave an open file descriptor around.
400 if From = Invalid_FD then
401 if To /= Invalid_FD then
402 Close (To, Status_To);
403 end if;
405 raise Copy_Error;
407 elsif To = Invalid_FD then
408 Close (From, Status_From);
409 raise Copy_Error;
410 end if;
412 -- Allocate the buffer on the heap
414 Buffer := new Buf;
416 loop
417 R := Read (From, Buffer (1)'Address, Buf_Size);
419 -- On some systems, the buffer may not be full. So, we need to try
420 -- again until there is nothing to read.
422 exit when R = 0;
424 W := Write (To, Buffer (1)'Address, R);
426 if W < R then
428 -- Problem writing data, could be a disk full. Close files
429 -- without worrying about status, since we are raising a
430 -- Copy_Error exception in any case.
432 Close (From, Status_From);
433 Close (To, Status_To);
435 Free (Buffer);
437 raise Copy_Error;
438 end if;
439 end loop;
441 Close (From, Status_From);
442 Close (To, Status_To);
444 Free (Buffer);
446 if not (Status_From and Status_To) then
447 raise Copy_Error;
448 end if;
449 end Copy;
451 -------------
452 -- Copy_To --
453 -------------
455 procedure Copy_To (To_Name : String) is
456 C_From : String (1 .. Name'Length + 1);
457 C_To : String (1 .. To_Name'Length + 1);
459 begin
460 From := Open_Read (Name, Binary);
462 -- Do not clobber destination file if source file could not be opened
464 if From /= Invalid_FD then
465 To := Create_File (To_Name, Binary);
466 end if;
468 Copy (From, To);
470 -- Copy attributes
472 C_From (1 .. Name'Length) := Name;
473 C_From (C_From'Last) := ASCII.NUL;
475 C_To (1 .. To_Name'Length) := To_Name;
476 C_To (C_To'Last) := ASCII.NUL;
478 case Preserve is
480 when Time_Stamps =>
481 if Copy_Attributes (C_From'Address, C_To'Address, 0) = -1 then
482 raise Copy_Error;
483 end if;
485 when Full =>
486 if Copy_Attributes (C_From'Address, C_To'Address, 1) = -1 then
487 raise Copy_Error;
488 end if;
490 when None =>
491 null;
492 end case;
494 end Copy_To;
496 -- Start of processing for Copy_File
498 begin
499 Success := True;
501 -- The source file must exist
503 if not Is_Regular_File (Name) then
504 raise Copy_Error;
505 end if;
507 -- The source file exists
509 case Mode is
511 -- Copy case, target file must not exist
513 when Copy =>
515 -- If the target file exists, we have an error
517 if Is_Regular_File (Pathname) then
518 raise Copy_Error;
520 -- Case of target is a directory
522 elsif Is_Directory (Pathname) then
523 declare
524 Dest : constant String := Build_Path (Pathname, Name);
526 begin
527 -- If target file exists, we have an error, else do copy
529 if Is_Regular_File (Dest) then
530 raise Copy_Error;
531 else
532 Copy_To (Dest);
533 end if;
534 end;
536 -- Case of normal copy to file (destination does not exist)
538 else
539 Copy_To (Pathname);
540 end if;
542 -- Overwrite case (destination file may or may not exist)
544 when Overwrite =>
545 if Is_Directory (Pathname) then
546 Copy_To (Build_Path (Pathname, Name));
547 else
548 Copy_To (Pathname);
549 end if;
551 -- Append case (destination file may or may not exist)
553 when Append =>
555 -- Appending to existing file
557 if Is_Regular_File (Pathname) then
559 -- Append mode and destination file exists, append data at the
560 -- end of Pathname. But if we fail to open source file, do not
561 -- touch destination file at all.
563 From := Open_Read (Name, Binary);
564 if From /= Invalid_FD then
565 To := Open_Read_Write (Pathname, Binary);
566 end if;
568 Lseek (To, 0, Seek_End);
570 Copy (From, To);
572 -- Appending to directory, not allowed
574 elsif Is_Directory (Pathname) then
575 raise Copy_Error;
577 -- Appending when target file does not exist
579 else
580 Copy_To (Pathname);
581 end if;
582 end case;
584 -- All error cases are caught here
586 exception
587 when Copy_Error =>
588 Success := False;
589 end Copy_File;
591 procedure Copy_File
592 (Name : C_File_Name;
593 Pathname : C_File_Name;
594 Success : out Boolean;
595 Mode : Copy_Mode := Copy;
596 Preserve : Attribute := Time_Stamps)
598 Ada_Name : String_Access :=
599 To_Path_String_Access
600 (Name, C_String_Length (Name));
601 Ada_Pathname : String_Access :=
602 To_Path_String_Access
603 (Pathname, C_String_Length (Pathname));
604 begin
605 Copy_File (Ada_Name.all, Ada_Pathname.all, Success, Mode, Preserve);
606 Free (Ada_Name);
607 Free (Ada_Pathname);
608 end Copy_File;
610 ----------------------
611 -- Copy_Time_Stamps --
612 ----------------------
614 procedure Copy_Time_Stamps (Source, Dest : String; Success : out Boolean) is
615 begin
616 if Is_Regular_File (Source) and then Is_Writable_File (Dest) then
617 declare
618 C_Source : String (1 .. Source'Length + 1);
619 C_Dest : String (1 .. Dest'Length + 1);
621 begin
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
629 Success := False;
630 else
631 Success := True;
632 end if;
633 end;
635 else
636 Success := False;
637 end if;
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));
650 begin
651 Copy_Time_Stamps (Ada_Source.all, Ada_Dest.all, Success);
652 Free (Ada_Source);
653 Free (Ada_Dest);
654 end Copy_Time_Stamps;
656 -----------------
657 -- Create_File --
658 -----------------
660 function Create_File
661 (Name : C_File_Name;
662 Fmode : Mode) return File_Descriptor
664 function C_Create_File
665 (Name : C_File_Name;
666 Fmode : Mode) return File_Descriptor;
667 pragma Import (C, C_Create_File, "__gnat_open_create");
668 begin
669 return C_Create_File (Name, Fmode);
670 end Create_File;
672 function Create_File
673 (Name : String;
674 Fmode : Mode) return File_Descriptor
676 C_Name : String (1 .. Name'Length + 1);
677 begin
678 C_Name (1 .. Name'Length) := Name;
679 C_Name (C_Name'Last) := ASCII.NUL;
680 return Create_File (C_Name (C_Name'First)'Address, Fmode);
681 end Create_File;
683 ---------------------
684 -- Create_New_File --
685 ---------------------
687 function Create_New_File
688 (Name : C_File_Name;
689 Fmode : Mode) return File_Descriptor
691 function C_Create_New_File
692 (Name : C_File_Name;
693 Fmode : Mode) return File_Descriptor;
694 pragma Import (C, C_Create_New_File, "__gnat_open_new");
695 begin
696 return C_Create_New_File (Name, Fmode);
697 end Create_New_File;
699 function Create_New_File
700 (Name : String;
701 Fmode : Mode) return File_Descriptor
703 C_Name : String (1 .. Name'Length + 1);
704 begin
705 C_Name (1 .. Name'Length) := Name;
706 C_Name (C_Name'Last) := ASCII.NUL;
707 return Create_New_File (C_Name (C_Name'First)'Address, Fmode);
708 end Create_New_File;
710 -----------------------------
711 -- Create_Output_Text_File --
712 -----------------------------
714 function Create_Output_Text_File (Name : String) return File_Descriptor is
715 function C_Create_File
716 (Name : C_File_Name) return File_Descriptor;
717 pragma Import (C, C_Create_File, "__gnat_create_output_file");
718 C_Name : String (1 .. Name'Length + 1);
719 begin
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");
738 begin
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)
746 begin
747 Create_Temp_File_Internal (FD, Name, Stdout => False);
748 end Create_Temp_File;
750 -----------------------------
751 -- Create_Temp_Output_File --
752 -----------------------------
754 procedure Create_Temp_Output_File
755 (FD : out File_Descriptor;
756 Name : out String_Access)
758 begin
759 Create_Temp_File_Internal (FD, Name, Stdout => True);
760 end Create_Temp_Output_File;
762 -------------------------------
763 -- Create_Temp_File_Internal --
764 -------------------------------
766 procedure Create_Temp_File_Internal
767 (FD : out File_Descriptor;
768 Name : out String_Access;
769 Stdout : Boolean)
771 Pos : Positive;
772 Attempts : Natural := 0;
773 Current : String (Current_Temp_File_Name'Range);
775 function Create_New_Output_Text_File
776 (Name : String) return File_Descriptor;
777 -- Similar to Create_Output_Text_File, except it fails if the file
778 -- already exists. We need this behavior to ensure we don't accidentally
779 -- open a temp file that has just been created by a concurrently running
780 -- process. There is no point exposing this function, as it's generally
781 -- not particularly useful.
783 ---------------------------------
784 -- Create_New_Output_Text_File --
785 ---------------------------------
787 function Create_New_Output_Text_File
788 (Name : String) return File_Descriptor
790 function C_Create_File
791 (Name : C_File_Name) return File_Descriptor;
792 pragma Import (C, C_Create_File, "__gnat_create_output_file_new");
793 C_Name : String (1 .. Name'Length + 1);
794 begin
795 C_Name (1 .. Name'Length) := Name;
796 C_Name (C_Name'Last) := ASCII.NUL;
797 return C_Create_File (C_Name (C_Name'First)'Address);
798 end Create_New_Output_Text_File;
800 begin
801 -- Loop until a new temp file can be created
803 File_Loop : loop
804 Locked : begin
806 -- We need to protect global variable Current_Temp_File_Name
807 -- against concurrent access by different tasks.
809 SSL.Lock_Task.all;
811 -- Start at the last digit
813 Pos := Temp_File_Name_Last_Digit;
815 Digit_Loop :
816 loop
817 -- Increment the digit by one
819 case Current_Temp_File_Name (Pos) is
820 when '0' .. '8' =>
821 Current_Temp_File_Name (Pos) :=
822 Character'Succ (Current_Temp_File_Name (Pos));
823 exit Digit_Loop;
825 when '9' =>
827 -- For 9, set the digit to 0 and go to the previous digit
829 Current_Temp_File_Name (Pos) := '0';
830 Pos := Pos - 1;
832 when others =>
834 -- If it is not a digit, then there are no available
835 -- temp file names. Return Invalid_FD. There is almost no
836 -- chance that this code will be ever be executed, since
837 -- it would mean that there are one million temp files in
838 -- the same directory.
840 SSL.Unlock_Task.all;
841 FD := Invalid_FD;
842 Name := null;
843 exit File_Loop;
844 end case;
845 end loop Digit_Loop;
847 Current := Current_Temp_File_Name;
849 -- We can now release the lock, because we are no longer accessing
850 -- Current_Temp_File_Name.
852 SSL.Unlock_Task.all;
854 exception
855 when others =>
856 SSL.Unlock_Task.all;
857 raise;
858 end Locked;
860 -- Attempt to create the file
862 if Stdout then
863 FD := Create_New_Output_Text_File (Current);
864 else
865 FD := Create_New_File (Current, Binary);
866 end if;
868 if FD /= Invalid_FD then
869 Name := new String'(Current);
870 exit File_Loop;
871 end if;
873 if not Is_Regular_File (Current) then
875 -- If the file does not already exist and we are unable to create
876 -- it, we give up after Max_Attempts. Otherwise, we try again with
877 -- the next available file name.
879 Attempts := Attempts + 1;
881 if Attempts >= Max_Attempts then
882 FD := Invalid_FD;
883 Name := null;
884 exit File_Loop;
885 end if;
886 end if;
887 end loop File_Loop;
888 end Create_Temp_File_Internal;
890 -------------------------
891 -- Current_Time_String --
892 -------------------------
894 function Current_Time_String return String is
895 subtype S23 is String (1 .. 23);
896 -- Holds current time in ISO 8601 format YYYY-MM-DD HH:MM:SS.SS + NUL
898 procedure Current_Time_String (Time : System.Address);
899 pragma Import (C, Current_Time_String, "__gnat_current_time_string");
900 -- Puts current time into Time in above ISO 8601 format
902 Result23 : aliased S23;
903 -- Current time in ISO 8601 format
905 begin
906 Current_Time_String (Result23'Address);
907 return Result23 (1 .. 19);
908 end Current_Time_String;
910 -----------------
911 -- Delete_File --
912 -----------------
914 procedure Delete_File (Name : Address; Success : out Boolean) is
915 R : Integer;
916 begin
917 R := System.CRTL.unlink (Name);
918 Success := (R = 0);
919 end Delete_File;
921 procedure Delete_File (Name : String; Success : out Boolean) is
922 C_Name : String (1 .. Name'Length + 1);
923 begin
924 C_Name (1 .. Name'Length) := Name;
925 C_Name (C_Name'Last) := ASCII.NUL;
926 Delete_File (C_Name'Address, Success);
927 end Delete_File;
929 -------------------
930 -- Errno_Message --
931 -------------------
933 function Errno_Message
934 (Err : Integer := Errno;
935 Default : String := "") return String
937 function strerror (errnum : Integer) return System.Address;
938 pragma Import (C, strerror, "strerror");
940 C_Msg : constant System.Address := strerror (Err);
942 begin
943 if C_Msg = Null_Address then
944 if Default /= "" then
945 return Default;
947 else
948 -- Note: for bootstrap reasons, it is impractical
949 -- to use Integer'Image here.
951 declare
952 Val : Integer;
953 First : Integer;
955 Buf : String (1 .. 20);
956 -- Buffer large enough to hold image of largest Integer values
958 begin
959 Val := abs Err;
960 First := Buf'Last;
961 loop
962 Buf (First) :=
963 Character'Val (Character'Pos ('0') + Val mod 10);
964 Val := Val / 10;
965 exit when Val = 0;
966 First := First - 1;
967 end loop;
969 if Err < 0 then
970 First := First - 1;
971 Buf (First) := '-';
972 end if;
974 return "errno = " & Buf (First .. Buf'Last);
975 end;
976 end if;
978 else
979 declare
980 Msg : String (1 .. Integer (CRTL.strlen (C_Msg)));
981 for Msg'Address use C_Msg;
982 pragma Import (Ada, Msg);
983 begin
984 return Msg;
985 end;
986 end if;
987 end Errno_Message;
989 ---------------------
990 -- File_Time_Stamp --
991 ---------------------
993 function File_Time_Stamp (FD : File_Descriptor) return OS_Time is
994 function File_Time (FD : File_Descriptor) return OS_Time;
995 pragma Import (C, File_Time, "__gnat_file_time_fd");
996 begin
997 return File_Time (FD);
998 end File_Time_Stamp;
1000 function File_Time_Stamp (Name : C_File_Name) return OS_Time is
1001 function File_Time (Name : Address) return OS_Time;
1002 pragma Import (C, File_Time, "__gnat_file_time_name");
1003 begin
1004 return File_Time (Name);
1005 end File_Time_Stamp;
1007 function File_Time_Stamp (Name : String) return OS_Time is
1008 F_Name : String (1 .. Name'Length + 1);
1009 begin
1010 F_Name (1 .. Name'Length) := Name;
1011 F_Name (F_Name'Last) := ASCII.NUL;
1012 return File_Time_Stamp (F_Name'Address);
1013 end File_Time_Stamp;
1015 ---------------------------
1016 -- Get_Debuggable_Suffix --
1017 ---------------------------
1019 function Get_Debuggable_Suffix return String_Access is
1020 procedure Get_Suffix_Ptr (Length, Ptr : Address);
1021 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_debuggable_suffix_ptr");
1023 Suffix_Ptr : Address;
1024 Suffix_Length : Integer;
1025 Result : String_Access;
1027 begin
1028 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
1029 Result := new String (1 .. Suffix_Length);
1031 if Suffix_Length > 0 then
1032 Strncpy (Result.all'Address, Suffix_Ptr, size_t (Suffix_Length));
1033 end if;
1035 return Result;
1036 end Get_Debuggable_Suffix;
1038 ---------------------------
1039 -- Get_Executable_Suffix --
1040 ---------------------------
1042 function Get_Executable_Suffix return String_Access is
1043 procedure Get_Suffix_Ptr (Length, Ptr : Address);
1044 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_executable_suffix_ptr");
1046 Suffix_Ptr : Address;
1047 Suffix_Length : Integer;
1048 Result : String_Access;
1050 begin
1051 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
1052 Result := new String (1 .. Suffix_Length);
1054 if Suffix_Length > 0 then
1055 Strncpy (Result.all'Address, Suffix_Ptr, size_t (Suffix_Length));
1056 end if;
1058 return Result;
1059 end Get_Executable_Suffix;
1061 -----------------------
1062 -- Get_Object_Suffix --
1063 -----------------------
1065 function Get_Object_Suffix return String_Access is
1066 procedure Get_Suffix_Ptr (Length, Ptr : Address);
1067 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_object_suffix_ptr");
1069 Suffix_Ptr : Address;
1070 Suffix_Length : Integer;
1071 Result : String_Access;
1073 begin
1074 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
1075 Result := new String (1 .. Suffix_Length);
1077 if Suffix_Length > 0 then
1078 Strncpy (Result.all'Address, Suffix_Ptr, size_t (Suffix_Length));
1079 end if;
1081 return Result;
1082 end Get_Object_Suffix;
1084 ----------------------------------
1085 -- Get_Target_Debuggable_Suffix --
1086 ----------------------------------
1088 function Get_Target_Debuggable_Suffix return String_Access is
1089 Target_Exec_Ext_Ptr : Address;
1090 pragma Import
1091 (C, Target_Exec_Ext_Ptr, "__gnat_target_debuggable_extension");
1093 Suffix_Length : Integer;
1094 Result : String_Access;
1096 begin
1097 Suffix_Length := Integer (CRTL.strlen (Target_Exec_Ext_Ptr));
1098 Result := new String (1 .. Suffix_Length);
1100 if Suffix_Length > 0 then
1101 Strncpy
1102 (Result.all'Address, Target_Exec_Ext_Ptr, size_t (Suffix_Length));
1103 end if;
1105 return Result;
1106 end Get_Target_Debuggable_Suffix;
1108 ----------------------------------
1109 -- Get_Target_Executable_Suffix --
1110 ----------------------------------
1112 function Get_Target_Executable_Suffix return String_Access is
1113 Target_Exec_Ext_Ptr : Address;
1114 pragma Import
1115 (C, Target_Exec_Ext_Ptr, "__gnat_target_executable_extension");
1117 Suffix_Length : Integer;
1118 Result : String_Access;
1120 begin
1121 Suffix_Length := Integer (CRTL.strlen (Target_Exec_Ext_Ptr));
1122 Result := new String (1 .. Suffix_Length);
1124 if Suffix_Length > 0 then
1125 Strncpy
1126 (Result.all'Address, Target_Exec_Ext_Ptr, size_t (Suffix_Length));
1127 end if;
1129 return Result;
1130 end Get_Target_Executable_Suffix;
1132 ------------------------------
1133 -- Get_Target_Object_Suffix --
1134 ------------------------------
1136 function Get_Target_Object_Suffix return String_Access is
1137 Target_Object_Ext_Ptr : Address;
1138 pragma Import
1139 (C, Target_Object_Ext_Ptr, "__gnat_target_object_extension");
1141 Suffix_Length : Integer;
1142 Result : String_Access;
1144 begin
1145 Suffix_Length := Integer (CRTL.strlen (Target_Object_Ext_Ptr));
1146 Result := new String (1 .. Suffix_Length);
1148 if Suffix_Length > 0 then
1149 Strncpy
1150 (Result.all'Address, Target_Object_Ext_Ptr, size_t (Suffix_Length));
1151 end if;
1153 return Result;
1154 end Get_Target_Object_Suffix;
1156 ------------
1157 -- Getenv --
1158 ------------
1160 function Getenv (Name : String) return String_Access is
1161 procedure Get_Env_Value_Ptr (Name, Length, Ptr : Address);
1162 pragma Import (C, Get_Env_Value_Ptr, "__gnat_getenv");
1164 Env_Value_Ptr : aliased Address;
1165 Env_Value_Length : aliased Integer;
1166 F_Name : aliased String (1 .. Name'Length + 1);
1167 Result : String_Access;
1169 begin
1170 F_Name (1 .. Name'Length) := Name;
1171 F_Name (F_Name'Last) := ASCII.NUL;
1173 Get_Env_Value_Ptr
1174 (F_Name'Address, Env_Value_Length'Address, Env_Value_Ptr'Address);
1176 Result := new String (1 .. Env_Value_Length);
1178 if Env_Value_Length > 0 then
1179 Strncpy
1180 (Result.all'Address, Env_Value_Ptr, size_t (Env_Value_Length));
1181 end if;
1183 return Result;
1184 end Getenv;
1186 ------------
1187 -- GM_Day --
1188 ------------
1190 function GM_Day (Date : OS_Time) return Day_Type is
1191 D : Day_Type;
1193 Y : Year_Type;
1194 Mo : Month_Type;
1195 H : Hour_Type;
1196 Mn : Minute_Type;
1197 S : Second_Type;
1198 pragma Unreferenced (Y, Mo, H, Mn, S);
1200 begin
1201 GM_Split (Date, Y, Mo, D, H, Mn, S);
1202 return D;
1203 end GM_Day;
1205 -------------
1206 -- GM_Hour --
1207 -------------
1209 function GM_Hour (Date : OS_Time) return Hour_Type is
1210 H : Hour_Type;
1212 Y : Year_Type;
1213 Mo : Month_Type;
1214 D : Day_Type;
1215 Mn : Minute_Type;
1216 S : Second_Type;
1217 pragma Unreferenced (Y, Mo, D, Mn, S);
1219 begin
1220 GM_Split (Date, Y, Mo, D, H, Mn, S);
1221 return H;
1222 end GM_Hour;
1224 ---------------
1225 -- GM_Minute --
1226 ---------------
1228 function GM_Minute (Date : OS_Time) return Minute_Type is
1229 Mn : Minute_Type;
1231 Y : Year_Type;
1232 Mo : Month_Type;
1233 D : Day_Type;
1234 H : Hour_Type;
1235 S : Second_Type;
1236 pragma Unreferenced (Y, Mo, D, H, S);
1238 begin
1239 GM_Split (Date, Y, Mo, D, H, Mn, S);
1240 return Mn;
1241 end GM_Minute;
1243 --------------
1244 -- GM_Month --
1245 --------------
1247 function GM_Month (Date : OS_Time) return Month_Type is
1248 Mo : Month_Type;
1250 Y : Year_Type;
1251 D : Day_Type;
1252 H : Hour_Type;
1253 Mn : Minute_Type;
1254 S : Second_Type;
1255 pragma Unreferenced (Y, D, H, Mn, S);
1257 begin
1258 GM_Split (Date, Y, Mo, D, H, Mn, S);
1259 return Mo;
1260 end GM_Month;
1262 ---------------
1263 -- GM_Second --
1264 ---------------
1266 function GM_Second (Date : OS_Time) return Second_Type is
1267 S : Second_Type;
1269 Y : Year_Type;
1270 Mo : Month_Type;
1271 D : Day_Type;
1272 H : Hour_Type;
1273 Mn : Minute_Type;
1274 pragma Unreferenced (Y, Mo, D, H, Mn);
1276 begin
1277 GM_Split (Date, Y, Mo, D, H, Mn, S);
1278 return S;
1279 end GM_Second;
1281 --------------
1282 -- GM_Split --
1283 --------------
1285 procedure GM_Split
1286 (Date : OS_Time;
1287 Year : out Year_Type;
1288 Month : out Month_Type;
1289 Day : out Day_Type;
1290 Hour : out Hour_Type;
1291 Minute : out Minute_Type;
1292 Second : out Second_Type)
1294 procedure To_GM_Time
1295 (P_Time_T, P_Year, P_Month, P_Day, P_Hours, P_Mins, P_Secs : Address);
1296 pragma Import (C, To_GM_Time, "__gnat_to_gm_time");
1298 T : OS_Time := Date;
1299 Y : Integer;
1300 Mo : Integer;
1301 D : Integer;
1302 H : Integer;
1303 Mn : Integer;
1304 S : Integer;
1306 begin
1307 -- Use the global lock because To_GM_Time is not thread safe
1309 Locked_Processing : begin
1310 SSL.Lock_Task.all;
1311 To_GM_Time
1312 (T'Address, Y'Address, Mo'Address, D'Address,
1313 H'Address, Mn'Address, S'Address);
1314 SSL.Unlock_Task.all;
1316 exception
1317 when others =>
1318 SSL.Unlock_Task.all;
1319 raise;
1320 end Locked_Processing;
1322 Year := Y + 1900;
1323 Month := Mo + 1;
1324 Day := D;
1325 Hour := H;
1326 Minute := Mn;
1327 Second := S;
1328 end GM_Split;
1330 ----------------
1331 -- GM_Time_Of --
1332 ----------------
1334 function GM_Time_Of
1335 (Year : Year_Type;
1336 Month : Month_Type;
1337 Day : Day_Type;
1338 Hour : Hour_Type;
1339 Minute : Minute_Type;
1340 Second : Second_Type) return OS_Time
1342 procedure To_OS_Time
1343 (P_Time_T : Address; Year, Month, Day, Hours, Mins, Secs : Integer);
1344 pragma Import (C, To_OS_Time, "__gnat_to_os_time");
1345 Result : OS_Time;
1346 begin
1347 To_OS_Time
1348 (Result'Address, Year - 1900, Month - 1, Day, Hour, Minute, Second);
1349 return Result;
1350 end GM_Time_Of;
1352 -------------
1353 -- GM_Year --
1354 -------------
1356 function GM_Year (Date : OS_Time) return Year_Type is
1357 Y : Year_Type;
1359 Mo : Month_Type;
1360 D : Day_Type;
1361 H : Hour_Type;
1362 Mn : Minute_Type;
1363 S : Second_Type;
1364 pragma Unreferenced (Mo, D, H, Mn, S);
1366 begin
1367 GM_Split (Date, Y, Mo, D, H, Mn, S);
1368 return Y;
1369 end GM_Year;
1371 ----------------------
1372 -- Is_Absolute_Path --
1373 ----------------------
1375 function Is_Absolute_Path (Name : String) return Boolean is
1376 function Is_Absolute_Path
1377 (Name : Address;
1378 Length : Integer) return Integer;
1379 pragma Import (C, Is_Absolute_Path, "__gnat_is_absolute_path");
1380 begin
1381 return Is_Absolute_Path (Name'Address, Name'Length) /= 0;
1382 end Is_Absolute_Path;
1384 ------------------
1385 -- Is_Directory --
1386 ------------------
1388 function Is_Directory (Name : C_File_Name) return Boolean is
1389 function Is_Directory (Name : Address) return Integer;
1390 pragma Import (C, Is_Directory, "__gnat_is_directory");
1391 begin
1392 return Is_Directory (Name) /= 0;
1393 end Is_Directory;
1395 function Is_Directory (Name : String) return Boolean is
1396 F_Name : String (1 .. Name'Length + 1);
1397 begin
1398 F_Name (1 .. Name'Length) := Name;
1399 F_Name (F_Name'Last) := ASCII.NUL;
1400 return Is_Directory (F_Name'Address);
1401 end Is_Directory;
1403 ----------------------
1404 -- Is_Readable_File --
1405 ----------------------
1407 function Is_Readable_File (Name : C_File_Name) return Boolean is
1408 function Is_Readable_File (Name : Address) return Integer;
1409 pragma Import (C, Is_Readable_File, "__gnat_is_readable_file");
1410 begin
1411 return Is_Readable_File (Name) /= 0;
1412 end Is_Readable_File;
1414 function Is_Readable_File (Name : String) return Boolean is
1415 F_Name : String (1 .. Name'Length + 1);
1416 begin
1417 F_Name (1 .. Name'Length) := Name;
1418 F_Name (F_Name'Last) := ASCII.NUL;
1419 return Is_Readable_File (F_Name'Address);
1420 end Is_Readable_File;
1422 ------------------------
1423 -- Is_Executable_File --
1424 ------------------------
1426 function Is_Executable_File (Name : C_File_Name) return Boolean is
1427 function Is_Executable_File (Name : Address) return Integer;
1428 pragma Import (C, Is_Executable_File, "__gnat_is_executable_file");
1429 begin
1430 return Is_Executable_File (Name) /= 0;
1431 end Is_Executable_File;
1433 function Is_Executable_File (Name : String) return Boolean is
1434 F_Name : String (1 .. Name'Length + 1);
1435 begin
1436 F_Name (1 .. Name'Length) := Name;
1437 F_Name (F_Name'Last) := ASCII.NUL;
1438 return Is_Executable_File (F_Name'Address);
1439 end Is_Executable_File;
1441 ---------------------
1442 -- Is_Regular_File --
1443 ---------------------
1445 function Is_Regular_File (Name : C_File_Name) return Boolean is
1446 function Is_Regular_File (Name : Address) return Integer;
1447 pragma Import (C, Is_Regular_File, "__gnat_is_regular_file");
1448 begin
1449 return Is_Regular_File (Name) /= 0;
1450 end Is_Regular_File;
1452 function Is_Regular_File (Name : String) return Boolean is
1453 F_Name : String (1 .. Name'Length + 1);
1454 begin
1455 F_Name (1 .. Name'Length) := Name;
1456 F_Name (F_Name'Last) := ASCII.NUL;
1457 return Is_Regular_File (F_Name'Address);
1458 end Is_Regular_File;
1460 ----------------------
1461 -- Is_Symbolic_Link --
1462 ----------------------
1464 function Is_Symbolic_Link (Name : C_File_Name) return Boolean is
1465 function Is_Symbolic_Link (Name : Address) return Integer;
1466 pragma Import (C, Is_Symbolic_Link, "__gnat_is_symbolic_link");
1467 begin
1468 return Is_Symbolic_Link (Name) /= 0;
1469 end Is_Symbolic_Link;
1471 function Is_Symbolic_Link (Name : String) return Boolean is
1472 F_Name : String (1 .. Name'Length + 1);
1473 begin
1474 F_Name (1 .. Name'Length) := Name;
1475 F_Name (F_Name'Last) := ASCII.NUL;
1476 return Is_Symbolic_Link (F_Name'Address);
1477 end Is_Symbolic_Link;
1479 ----------------------
1480 -- Is_Writable_File --
1481 ----------------------
1483 function Is_Writable_File (Name : C_File_Name) return Boolean is
1484 function Is_Writable_File (Name : Address) return Integer;
1485 pragma Import (C, Is_Writable_File, "__gnat_is_writable_file");
1486 begin
1487 return Is_Writable_File (Name) /= 0;
1488 end Is_Writable_File;
1490 function Is_Writable_File (Name : String) return Boolean is
1491 F_Name : String (1 .. Name'Length + 1);
1492 begin
1493 F_Name (1 .. Name'Length) := Name;
1494 F_Name (F_Name'Last) := ASCII.NUL;
1495 return Is_Writable_File (F_Name'Address);
1496 end Is_Writable_File;
1498 -------------------------
1499 -- Locate_Exec_On_Path --
1500 -------------------------
1502 function Locate_Exec_On_Path
1503 (Exec_Name : String) return String_Access
1505 function Locate_Exec_On_Path (C_Exec_Name : Address) return Address;
1506 pragma Import (C, Locate_Exec_On_Path, "__gnat_locate_exec_on_path");
1508 C_Exec_Name : String (1 .. Exec_Name'Length + 1);
1509 Path_Addr : Address;
1510 Path_Len : Integer;
1511 Result : String_Access;
1513 begin
1514 C_Exec_Name (1 .. Exec_Name'Length) := Exec_Name;
1515 C_Exec_Name (C_Exec_Name'Last) := ASCII.NUL;
1517 Path_Addr := Locate_Exec_On_Path (C_Exec_Name'Address);
1518 Path_Len := C_String_Length (Path_Addr);
1520 if Path_Len = 0 then
1521 return null;
1523 else
1524 Result := To_Path_String_Access (Path_Addr, Path_Len);
1525 CRTL.free (Path_Addr);
1527 -- Always return an absolute path name
1529 if not Is_Absolute_Path (Result.all) then
1530 declare
1531 Absolute_Path : constant String :=
1532 Normalize_Pathname (Result.all, Resolve_Links => False);
1533 begin
1534 Free (Result);
1535 Result := new String'(Absolute_Path);
1536 end;
1537 end if;
1539 return Result;
1540 end if;
1541 end Locate_Exec_On_Path;
1543 -------------------------
1544 -- Locate_Regular_File --
1545 -------------------------
1547 function Locate_Regular_File
1548 (File_Name : C_File_Name;
1549 Path : C_File_Name) return String_Access
1551 function Locate_Regular_File
1552 (C_File_Name, Path_Val : Address) return Address;
1553 pragma Import (C, Locate_Regular_File, "__gnat_locate_regular_file");
1555 Path_Addr : Address;
1556 Path_Len : Integer;
1557 Result : String_Access;
1559 begin
1560 Path_Addr := Locate_Regular_File (File_Name, Path);
1561 Path_Len := C_String_Length (Path_Addr);
1563 if Path_Len = 0 then
1564 return null;
1566 else
1567 Result := To_Path_String_Access (Path_Addr, Path_Len);
1568 CRTL.free (Path_Addr);
1569 return Result;
1570 end if;
1571 end Locate_Regular_File;
1573 function Locate_Regular_File
1574 (File_Name : String;
1575 Path : String) return String_Access
1577 C_File_Name : String (1 .. File_Name'Length + 1);
1578 C_Path : String (1 .. Path'Length + 1);
1579 Result : String_Access;
1581 begin
1582 C_File_Name (1 .. File_Name'Length) := File_Name;
1583 C_File_Name (C_File_Name'Last) := ASCII.NUL;
1585 C_Path (1 .. Path'Length) := Path;
1586 C_Path (C_Path'Last) := ASCII.NUL;
1588 Result := Locate_Regular_File (C_File_Name'Address, C_Path'Address);
1590 -- Always return an absolute path name
1592 if Result /= null and then not Is_Absolute_Path (Result.all) then
1593 declare
1594 Absolute_Path : constant String := Normalize_Pathname (Result.all);
1595 begin
1596 Free (Result);
1597 Result := new String'(Absolute_Path);
1598 end;
1599 end if;
1601 return Result;
1602 end Locate_Regular_File;
1604 ------------------------
1605 -- Non_Blocking_Spawn --
1606 ------------------------
1608 function Non_Blocking_Spawn
1609 (Program_Name : String;
1610 Args : Argument_List) return Process_Id
1612 Pid : Process_Id;
1613 Junk : Integer;
1614 pragma Warnings (Off, Junk);
1615 begin
1616 Spawn_Internal (Program_Name, Args, Junk, Pid, Blocking => False);
1617 return Pid;
1618 end Non_Blocking_Spawn;
1620 function Non_Blocking_Spawn
1621 (Program_Name : String;
1622 Args : Argument_List;
1623 Output_File_Descriptor : File_Descriptor;
1624 Err_To_Out : Boolean := True) return Process_Id
1626 Saved_Output : File_Descriptor;
1627 Saved_Error : File_Descriptor := Invalid_FD; -- prevent warning
1628 Pid : Process_Id;
1630 begin
1631 if Output_File_Descriptor = Invalid_FD then
1632 return Invalid_Pid;
1633 end if;
1635 -- Set standard output and, if specified, error to the temporary file
1637 Saved_Output := Dup (Standout);
1638 Dup2 (Output_File_Descriptor, Standout);
1640 if Err_To_Out then
1641 Saved_Error := Dup (Standerr);
1642 Dup2 (Output_File_Descriptor, Standerr);
1643 end if;
1645 -- Spawn the program
1647 Pid := Non_Blocking_Spawn (Program_Name, Args);
1649 -- Restore the standard output and error
1651 Dup2 (Saved_Output, Standout);
1653 if Err_To_Out then
1654 Dup2 (Saved_Error, Standerr);
1655 end if;
1657 -- And close the saved standard output and error file descriptors
1659 Close (Saved_Output);
1661 if Err_To_Out then
1662 Close (Saved_Error);
1663 end if;
1665 return Pid;
1666 end Non_Blocking_Spawn;
1668 function Non_Blocking_Spawn
1669 (Program_Name : String;
1670 Args : Argument_List;
1671 Output_File : String;
1672 Err_To_Out : Boolean := True) return Process_Id
1674 Output_File_Descriptor : constant File_Descriptor :=
1675 Create_Output_Text_File (Output_File);
1676 Result : Process_Id;
1678 begin
1679 -- Do not attempt to spawn if the output file could not be created
1681 if Output_File_Descriptor = Invalid_FD then
1682 return Invalid_Pid;
1684 else
1685 Result := Non_Blocking_Spawn
1686 (Program_Name, Args, Output_File_Descriptor, Err_To_Out);
1688 -- Close the file just created for the output, as the file descriptor
1689 -- cannot be used anywhere, being a local value. It is safe to do
1690 -- that, as the file descriptor has been duplicated to form
1691 -- standard output and error of the spawned process.
1693 Close (Output_File_Descriptor);
1695 return Result;
1696 end if;
1697 end Non_Blocking_Spawn;
1699 -------------------------
1700 -- Normalize_Arguments --
1701 -------------------------
1703 procedure Normalize_Arguments (Args : in out Argument_List) is
1705 procedure Quote_Argument (Arg : in out String_Access);
1706 -- Add quote around argument if it contains spaces (or HT characters)
1708 C_Argument_Needs_Quote : Integer;
1709 pragma Import (C, C_Argument_Needs_Quote, "__gnat_argument_needs_quote");
1710 Argument_Needs_Quote : constant Boolean := C_Argument_Needs_Quote /= 0;
1712 --------------------
1713 -- Quote_Argument --
1714 --------------------
1716 procedure Quote_Argument (Arg : in out String_Access) is
1717 Res : String (1 .. Arg'Length * 2);
1718 J : Positive := 1;
1719 Quote_Needed : Boolean := False;
1721 begin
1722 if Arg (Arg'First) /= '"' or else Arg (Arg'Last) /= '"' then
1724 -- Starting quote
1726 Res (J) := '"';
1728 for K in Arg'Range loop
1730 J := J + 1;
1732 if Arg (K) = '"' then
1733 Res (J) := '\';
1734 J := J + 1;
1735 Res (J) := '"';
1736 Quote_Needed := True;
1738 elsif Arg (K) = ' ' or else Arg (K) = ASCII.HT then
1739 Res (J) := Arg (K);
1740 Quote_Needed := True;
1742 else
1743 Res (J) := Arg (K);
1744 end if;
1745 end loop;
1747 if Quote_Needed then
1749 -- Case of null terminated string
1751 if Res (J) = ASCII.NUL then
1753 -- If the string ends with \, double it
1755 if Res (J - 1) = '\' then
1756 Res (J) := '\';
1757 J := J + 1;
1758 end if;
1760 -- Put a quote just before the null at the end
1762 Res (J) := '"';
1763 J := J + 1;
1764 Res (J) := ASCII.NUL;
1766 -- If argument is terminated by '\', then double it. Otherwise
1767 -- the ending quote will be taken as-is. This is quite strange
1768 -- spawn behavior from Windows, but this is what we see.
1770 else
1771 if Res (J) = '\' then
1772 J := J + 1;
1773 Res (J) := '\';
1774 end if;
1776 -- Ending quote
1778 J := J + 1;
1779 Res (J) := '"';
1780 end if;
1782 declare
1783 Old : String_Access := Arg;
1785 begin
1786 Arg := new String'(Res (1 .. J));
1787 Free (Old);
1788 end;
1789 end if;
1791 end if;
1792 end Quote_Argument;
1794 -- Start of processing for Normalize_Arguments
1796 begin
1797 if Argument_Needs_Quote then
1798 for K in Args'Range loop
1799 if Args (K) /= null and then Args (K)'Length /= 0 then
1800 Quote_Argument (Args (K));
1801 end if;
1802 end loop;
1803 end if;
1804 end Normalize_Arguments;
1806 ------------------------
1807 -- Normalize_Pathname --
1808 ------------------------
1810 function Normalize_Pathname
1811 (Name : String;
1812 Directory : String := "";
1813 Resolve_Links : Boolean := True;
1814 Case_Sensitive : Boolean := True) return String
1816 Max_Path : Integer;
1817 pragma Import (C, Max_Path, "__gnat_max_path_len");
1818 -- Maximum length of a path name
1820 procedure Get_Current_Dir
1821 (Dir : System.Address;
1822 Length : System.Address);
1823 pragma Import (C, Get_Current_Dir, "__gnat_get_current_dir");
1825 Path_Buffer : String (1 .. Max_Path + Max_Path + 2);
1826 End_Path : Natural := 0;
1827 Link_Buffer : String (1 .. Max_Path + 2);
1828 Status : Integer;
1829 Last : Positive;
1830 Start : Natural;
1831 Finish : Positive;
1833 Max_Iterations : constant := 500;
1835 function Get_File_Names_Case_Sensitive return Integer;
1836 pragma Import
1837 (C, Get_File_Names_Case_Sensitive,
1838 "__gnat_get_file_names_case_sensitive");
1840 Fold_To_Lower_Case : constant Boolean :=
1841 not Case_Sensitive
1842 and then Get_File_Names_Case_Sensitive = 0;
1844 function Readlink
1845 (Path : System.Address;
1846 Buf : System.Address;
1847 Bufsiz : Integer) return Integer;
1848 pragma Import (C, Readlink, "__gnat_readlink");
1850 function To_Canonical_File_Spec
1851 (Host_File : System.Address) return System.Address;
1852 pragma Import
1853 (C, To_Canonical_File_Spec, "__gnat_to_canonical_file_spec");
1854 -- Convert possible foreign file syntax to canonical form
1856 The_Name : String (1 .. Name'Length + 1);
1857 Canonical_File_Addr : System.Address;
1858 Canonical_File_Len : Integer;
1860 function Final_Value (S : String) return String;
1861 -- Make final adjustment to the returned string. This function strips
1862 -- trailing directory separators, and folds returned string to lower
1863 -- case if required.
1865 function Get_Directory (Dir : String) return String;
1866 -- If Dir is not empty, return it, adding a directory separator
1867 -- if not already present, otherwise return current working directory
1868 -- with terminating directory separator.
1870 -----------------
1871 -- Final_Value --
1872 -----------------
1874 function Final_Value (S : String) return String is
1875 S1 : String := S;
1876 -- We may need to fold S to lower case, so we need a variable
1878 Last : Natural;
1880 begin
1881 if Fold_To_Lower_Case then
1882 System.Case_Util.To_Lower (S1);
1883 end if;
1885 -- Remove trailing directory separator, if any
1887 Last := S1'Last;
1889 if Last > 1
1890 and then (S1 (Last) = '/'
1891 or else
1892 S1 (Last) = Directory_Separator)
1893 then
1894 -- Special case for Windows: C:\
1896 if Last = 3
1897 and then S1 (1) /= Directory_Separator
1898 and then S1 (2) = ':'
1899 then
1900 null;
1902 else
1903 Last := Last - 1;
1904 end if;
1905 end if;
1907 return S1 (1 .. Last);
1908 end Final_Value;
1910 -------------------
1911 -- Get_Directory --
1912 -------------------
1914 function Get_Directory (Dir : String) return String is
1915 Result : String (1 .. Dir'Length + 1);
1916 Length : constant Natural := Dir'Length;
1918 begin
1919 -- Directory given, add directory separator if needed
1921 if Length > 0 then
1922 Result (1 .. Length) := Dir;
1924 -- On Windows, change all '/' to '\'
1926 if On_Windows then
1927 for J in 1 .. Length loop
1928 if Result (J) = '/' then
1929 Result (J) := Directory_Separator;
1930 end if;
1931 end loop;
1932 end if;
1934 -- Add directory separator, if needed
1936 if Result (Length) = Directory_Separator then
1937 return Result (1 .. Length);
1938 else
1939 Result (Result'Length) := Directory_Separator;
1940 return Result;
1941 end if;
1943 -- Directory name not given, get current directory
1945 else
1946 declare
1947 Buffer : String (1 .. Max_Path + 2);
1948 Path_Len : Natural := Max_Path;
1950 begin
1951 Get_Current_Dir (Buffer'Address, Path_Len'Address);
1953 if Buffer (Path_Len) /= Directory_Separator then
1954 Path_Len := Path_Len + 1;
1955 Buffer (Path_Len) := Directory_Separator;
1956 end if;
1958 -- By default, the drive letter on Windows is in upper case
1960 if On_Windows
1961 and then Path_Len >= 2
1962 and then Buffer (2) = ':'
1963 then
1964 System.Case_Util.To_Upper (Buffer (1 .. 1));
1965 end if;
1967 return Buffer (1 .. Path_Len);
1968 end;
1969 end if;
1970 end Get_Directory;
1972 -- Start of processing for Normalize_Pathname
1974 begin
1975 -- Special case, return null if name is null, or if it is bigger than
1976 -- the biggest name allowed.
1978 if Name'Length = 0 or else Name'Length > Max_Path then
1979 return "";
1980 end if;
1982 -- First, convert possible foreign file spec to Unix file spec. If no
1983 -- conversion is required, all this does is put Name at the beginning
1984 -- of Path_Buffer unchanged.
1986 File_Name_Conversion : begin
1987 The_Name (1 .. Name'Length) := Name;
1988 The_Name (The_Name'Last) := ASCII.NUL;
1990 Canonical_File_Addr := To_Canonical_File_Spec (The_Name'Address);
1991 Canonical_File_Len := Integer (CRTL.strlen (Canonical_File_Addr));
1993 -- If syntax conversion has failed, return an empty string to
1994 -- indicate the failure.
1996 if Canonical_File_Len = 0 then
1997 return "";
1998 end if;
2000 declare
2001 subtype Path_String is String (1 .. Canonical_File_Len);
2002 Canonical_File : Path_String;
2003 for Canonical_File'Address use Canonical_File_Addr;
2004 pragma Import (Ada, Canonical_File);
2006 begin
2007 Path_Buffer (1 .. Canonical_File_Len) := Canonical_File;
2008 End_Path := Canonical_File_Len;
2009 Last := 1;
2010 end;
2011 end File_Name_Conversion;
2013 -- Replace all '/' by Directory Separators (this is for Windows)
2015 if Directory_Separator /= '/' then
2016 for Index in 1 .. End_Path loop
2017 if Path_Buffer (Index) = '/' then
2018 Path_Buffer (Index) := Directory_Separator;
2019 end if;
2020 end loop;
2021 end if;
2023 -- Resolve directory names for Windows
2025 if On_Windows then
2027 -- On Windows, if we have an absolute path starting with a directory
2028 -- separator, we need to have the drive letter appended in front.
2030 -- On Windows, Get_Current_Dir will return a suitable directory name
2031 -- (path starting with a drive letter on Windows). So we take this
2032 -- drive letter and prepend it to the current path.
2034 if Path_Buffer (1) = Directory_Separator
2035 and then Path_Buffer (2) /= Directory_Separator
2036 then
2037 declare
2038 Cur_Dir : constant String := Get_Directory ("");
2039 -- Get the current directory to get the drive letter
2041 begin
2042 if Cur_Dir'Length > 2
2043 and then Cur_Dir (Cur_Dir'First + 1) = ':'
2044 then
2045 Path_Buffer (3 .. End_Path + 2) :=
2046 Path_Buffer (1 .. End_Path);
2047 Path_Buffer (1 .. 2) :=
2048 Cur_Dir (Cur_Dir'First .. Cur_Dir'First + 1);
2049 End_Path := End_Path + 2;
2050 end if;
2051 end;
2053 -- We have a drive letter, ensure it is upper-case
2055 elsif Path_Buffer (1) in 'a' .. 'z'
2056 and then Path_Buffer (2) = ':'
2057 then
2058 System.Case_Util.To_Upper (Path_Buffer (1 .. 1));
2059 end if;
2060 end if;
2062 -- On Windows, remove all double-quotes that are possibly part of the
2063 -- path but can cause problems with other methods.
2065 if On_Windows then
2066 declare
2067 Index : Natural;
2069 begin
2070 Index := Path_Buffer'First;
2071 for Current in Path_Buffer'First .. End_Path loop
2072 if Path_Buffer (Current) /= '"' then
2073 Path_Buffer (Index) := Path_Buffer (Current);
2074 Index := Index + 1;
2075 end if;
2076 end loop;
2078 End_Path := Index - 1;
2079 end;
2080 end if;
2082 -- Start the conversions
2084 -- If this is not finished after Max_Iterations, give up and return an
2085 -- empty string.
2087 for J in 1 .. Max_Iterations loop
2089 -- If we don't have an absolute pathname, prepend the directory
2090 -- Reference_Dir.
2092 if Last = 1
2093 and then not Is_Absolute_Path (Path_Buffer (1 .. End_Path))
2094 then
2095 declare
2096 Reference_Dir : constant String := Get_Directory (Directory);
2097 Ref_Dir_Len : constant Natural := Reference_Dir'Length;
2098 -- Current directory name specified and its length
2100 begin
2101 Path_Buffer (Ref_Dir_Len + 1 .. Ref_Dir_Len + End_Path) :=
2102 Path_Buffer (1 .. End_Path);
2103 End_Path := Ref_Dir_Len + End_Path;
2104 Path_Buffer (1 .. Ref_Dir_Len) := Reference_Dir;
2105 Last := Ref_Dir_Len;
2106 end;
2107 end if;
2109 Start := Last + 1;
2110 Finish := Last;
2112 -- Ensure that Windows network drives are kept, e.g: \\server\drive-c
2114 if Start = 2
2115 and then Directory_Separator = '\'
2116 and then Path_Buffer (1 .. 2) = "\\"
2117 then
2118 Start := 3;
2119 end if;
2121 -- If we have traversed the full pathname, return it
2123 if Start > End_Path then
2124 return Final_Value (Path_Buffer (1 .. End_Path));
2125 end if;
2127 -- Remove duplicate directory separators
2129 while Path_Buffer (Start) = Directory_Separator loop
2130 if Start = End_Path then
2131 return Final_Value (Path_Buffer (1 .. End_Path - 1));
2133 else
2134 Path_Buffer (Start .. End_Path - 1) :=
2135 Path_Buffer (Start + 1 .. End_Path);
2136 End_Path := End_Path - 1;
2137 end if;
2138 end loop;
2140 -- Find the end of the current field: last character or the one
2141 -- preceding the next directory separator.
2143 while Finish < End_Path
2144 and then Path_Buffer (Finish + 1) /= Directory_Separator
2145 loop
2146 Finish := Finish + 1;
2147 end loop;
2149 -- Remove "." field
2151 if Start = Finish and then Path_Buffer (Start) = '.' then
2152 if Start = End_Path then
2153 if Last = 1 then
2154 return (1 => Directory_Separator);
2155 else
2157 if Fold_To_Lower_Case then
2158 System.Case_Util.To_Lower (Path_Buffer (1 .. Last - 1));
2159 end if;
2161 return Path_Buffer (1 .. Last - 1);
2163 end if;
2165 else
2166 Path_Buffer (Last + 1 .. End_Path - 2) :=
2167 Path_Buffer (Last + 3 .. End_Path);
2168 End_Path := End_Path - 2;
2169 end if;
2171 -- Remove ".." fields
2173 elsif Finish = Start + 1
2174 and then Path_Buffer (Start .. Finish) = ".."
2175 then
2176 Start := Last;
2177 loop
2178 Start := Start - 1;
2179 exit when Start < 1
2180 or else Path_Buffer (Start) = Directory_Separator;
2181 end loop;
2183 if Start <= 1 then
2184 if Finish = End_Path then
2185 return (1 => Directory_Separator);
2187 else
2188 Path_Buffer (1 .. End_Path - Finish) :=
2189 Path_Buffer (Finish + 1 .. End_Path);
2190 End_Path := End_Path - Finish;
2191 Last := 1;
2192 end if;
2194 else
2195 if Finish = End_Path then
2196 return Final_Value (Path_Buffer (1 .. Start - 1));
2198 else
2199 Path_Buffer (Start + 1 .. Start + End_Path - Finish - 1) :=
2200 Path_Buffer (Finish + 2 .. End_Path);
2201 End_Path := Start + End_Path - Finish - 1;
2202 Last := Start;
2203 end if;
2204 end if;
2206 -- Check if current field is a symbolic link
2208 elsif Resolve_Links then
2209 declare
2210 Saved : constant Character := Path_Buffer (Finish + 1);
2212 begin
2213 Path_Buffer (Finish + 1) := ASCII.NUL;
2214 Status := Readlink (Path_Buffer'Address,
2215 Link_Buffer'Address,
2216 Link_Buffer'Length);
2217 Path_Buffer (Finish + 1) := Saved;
2218 end;
2220 -- Not a symbolic link, move to the next field, if any
2222 if Status <= 0 then
2223 Last := Finish + 1;
2225 -- Replace symbolic link with its value
2227 else
2228 if Is_Absolute_Path (Link_Buffer (1 .. Status)) then
2229 Path_Buffer (Status + 1 .. End_Path - (Finish - Status)) :=
2230 Path_Buffer (Finish + 1 .. End_Path);
2231 End_Path := End_Path - (Finish - Status);
2232 Path_Buffer (1 .. Status) := Link_Buffer (1 .. Status);
2233 Last := 1;
2235 else
2236 Path_Buffer
2237 (Last + Status + 1 .. End_Path - Finish + Last + Status) :=
2238 Path_Buffer (Finish + 1 .. End_Path);
2239 End_Path := End_Path - Finish + Last + Status;
2240 Path_Buffer (Last + 1 .. Last + Status) :=
2241 Link_Buffer (1 .. Status);
2242 end if;
2243 end if;
2245 else
2246 Last := Finish + 1;
2247 end if;
2248 end loop;
2250 -- Too many iterations: give up
2252 -- This can happen when there is a circularity in the symbolic links: A
2253 -- is a symbolic link for B, which itself is a symbolic link, and the
2254 -- target of B or of another symbolic link target of B is A. In this
2255 -- case, we return an empty string to indicate failure to resolve.
2257 return "";
2258 end Normalize_Pathname;
2260 -----------------
2261 -- Open_Append --
2262 -----------------
2264 function Open_Append
2265 (Name : C_File_Name;
2266 Fmode : Mode) return File_Descriptor
2268 function C_Open_Append
2269 (Name : C_File_Name;
2270 Fmode : Mode) return File_Descriptor;
2271 pragma Import (C, C_Open_Append, "__gnat_open_append");
2272 begin
2273 return C_Open_Append (Name, Fmode);
2274 end Open_Append;
2276 function Open_Append
2277 (Name : String;
2278 Fmode : Mode) return File_Descriptor
2280 C_Name : String (1 .. Name'Length + 1);
2281 begin
2282 C_Name (1 .. Name'Length) := Name;
2283 C_Name (C_Name'Last) := ASCII.NUL;
2284 return Open_Append (C_Name (C_Name'First)'Address, Fmode);
2285 end Open_Append;
2287 ---------------
2288 -- Open_Read --
2289 ---------------
2291 function Open_Read
2292 (Name : C_File_Name;
2293 Fmode : Mode) return File_Descriptor
2295 function C_Open_Read
2296 (Name : C_File_Name;
2297 Fmode : Mode) return File_Descriptor;
2298 pragma Import (C, C_Open_Read, "__gnat_open_read");
2299 begin
2300 return C_Open_Read (Name, Fmode);
2301 end Open_Read;
2303 function Open_Read
2304 (Name : String;
2305 Fmode : Mode) return File_Descriptor
2307 C_Name : String (1 .. Name'Length + 1);
2308 begin
2309 C_Name (1 .. Name'Length) := Name;
2310 C_Name (C_Name'Last) := ASCII.NUL;
2311 return Open_Read (C_Name (C_Name'First)'Address, Fmode);
2312 end Open_Read;
2314 ---------------------
2315 -- Open_Read_Write --
2316 ---------------------
2318 function Open_Read_Write
2319 (Name : C_File_Name;
2320 Fmode : Mode) return File_Descriptor
2322 function C_Open_Read_Write
2323 (Name : C_File_Name;
2324 Fmode : Mode) return File_Descriptor;
2325 pragma Import (C, C_Open_Read_Write, "__gnat_open_rw");
2326 begin
2327 return C_Open_Read_Write (Name, Fmode);
2328 end Open_Read_Write;
2330 function Open_Read_Write
2331 (Name : String;
2332 Fmode : Mode) return File_Descriptor
2334 C_Name : String (1 .. Name'Length + 1);
2335 begin
2336 C_Name (1 .. Name'Length) := Name;
2337 C_Name (C_Name'Last) := ASCII.NUL;
2338 return Open_Read_Write (C_Name (C_Name'First)'Address, Fmode);
2339 end Open_Read_Write;
2341 -------------
2342 -- OS_Exit --
2343 -------------
2345 procedure OS_Exit (Status : Integer) is
2346 begin
2347 OS_Exit_Ptr (Status);
2348 raise Program_Error;
2349 end OS_Exit;
2351 ---------------------
2352 -- OS_Exit_Default --
2353 ---------------------
2355 procedure OS_Exit_Default (Status : Integer) is
2356 procedure GNAT_OS_Exit (Status : Integer);
2357 pragma Import (C, GNAT_OS_Exit, "__gnat_os_exit");
2358 pragma No_Return (GNAT_OS_Exit);
2359 begin
2360 GNAT_OS_Exit (Status);
2361 end OS_Exit_Default;
2363 --------------------
2364 -- Pid_To_Integer --
2365 --------------------
2367 function Pid_To_Integer (Pid : Process_Id) return Integer is
2368 begin
2369 return Integer (Pid);
2370 end Pid_To_Integer;
2372 ----------
2373 -- Read --
2374 ----------
2376 function Read
2377 (FD : File_Descriptor;
2378 A : System.Address;
2379 N : Integer) return Integer
2381 begin
2382 return
2383 Integer (System.CRTL.read
2384 (System.CRTL.int (FD),
2385 System.CRTL.chars (A),
2386 System.CRTL.size_t (N)));
2387 end Read;
2389 -----------------
2390 -- Rename_File --
2391 -----------------
2393 procedure Rename_File
2394 (Old_Name : C_File_Name;
2395 New_Name : C_File_Name;
2396 Success : out Boolean)
2398 function rename (From, To : Address) return Integer;
2399 pragma Import (C, rename, "__gnat_rename");
2400 R : Integer;
2401 begin
2402 R := rename (Old_Name, New_Name);
2403 Success := (R = 0);
2404 end Rename_File;
2406 procedure Rename_File
2407 (Old_Name : String;
2408 New_Name : String;
2409 Success : out Boolean)
2411 C_Old_Name : String (1 .. Old_Name'Length + 1);
2412 C_New_Name : String (1 .. New_Name'Length + 1);
2413 begin
2414 C_Old_Name (1 .. Old_Name'Length) := Old_Name;
2415 C_Old_Name (C_Old_Name'Last) := ASCII.NUL;
2416 C_New_Name (1 .. New_Name'Length) := New_Name;
2417 C_New_Name (C_New_Name'Last) := ASCII.NUL;
2418 Rename_File (C_Old_Name'Address, C_New_Name'Address, Success);
2419 end Rename_File;
2421 -----------------------
2422 -- Set_Close_On_Exec --
2423 -----------------------
2425 procedure Set_Close_On_Exec
2426 (FD : File_Descriptor;
2427 Close_On_Exec : Boolean;
2428 Status : out Boolean)
2430 function C_Set_Close_On_Exec
2431 (FD : File_Descriptor; Close_On_Exec : System.CRTL.int)
2432 return System.CRTL.int;
2433 pragma Import (C, C_Set_Close_On_Exec, "__gnat_set_close_on_exec");
2434 begin
2435 Status := C_Set_Close_On_Exec (FD, Boolean'Pos (Close_On_Exec)) = 0;
2436 end Set_Close_On_Exec;
2438 --------------------
2439 -- Set_Executable --
2440 --------------------
2442 procedure Set_Executable (Name : String; Mode : Positive := S_Owner) is
2443 procedure C_Set_Executable (Name : C_File_Name; Mode : Integer);
2444 pragma Import (C, C_Set_Executable, "__gnat_set_executable");
2445 C_Name : aliased String (Name'First .. Name'Last + 1);
2446 begin
2447 C_Name (Name'Range) := Name;
2448 C_Name (C_Name'Last) := ASCII.NUL;
2449 C_Set_Executable (C_Name (C_Name'First)'Address, Mode);
2450 end Set_Executable;
2452 -------------------------------------
2453 -- Set_File_Last_Modify_Time_Stamp --
2454 -------------------------------------
2456 procedure Set_File_Last_Modify_Time_Stamp (Name : String; Time : OS_Time) is
2457 procedure C_Set_File_Time (Name : C_File_Name; Time : OS_Time);
2458 pragma Import (C, C_Set_File_Time, "__gnat_set_file_time_name");
2459 C_Name : aliased String (Name'First .. Name'Last + 1);
2460 begin
2461 C_Name (Name'Range) := Name;
2462 C_Name (C_Name'Last) := ASCII.NUL;
2463 C_Set_File_Time (C_Name'Address, Time);
2464 end Set_File_Last_Modify_Time_Stamp;
2466 ----------------------
2467 -- Set_Non_Readable --
2468 ----------------------
2470 procedure Set_Non_Readable (Name : String) is
2471 procedure C_Set_Non_Readable (Name : C_File_Name);
2472 pragma Import (C, C_Set_Non_Readable, "__gnat_set_non_readable");
2473 C_Name : aliased String (Name'First .. Name'Last + 1);
2474 begin
2475 C_Name (Name'Range) := Name;
2476 C_Name (C_Name'Last) := ASCII.NUL;
2477 C_Set_Non_Readable (C_Name (C_Name'First)'Address);
2478 end Set_Non_Readable;
2480 ----------------------
2481 -- Set_Non_Writable --
2482 ----------------------
2484 procedure Set_Non_Writable (Name : String) is
2485 procedure C_Set_Non_Writable (Name : C_File_Name);
2486 pragma Import (C, C_Set_Non_Writable, "__gnat_set_non_writable");
2487 C_Name : aliased String (Name'First .. Name'Last + 1);
2488 begin
2489 C_Name (Name'Range) := Name;
2490 C_Name (C_Name'Last) := ASCII.NUL;
2491 C_Set_Non_Writable (C_Name (C_Name'First)'Address);
2492 end Set_Non_Writable;
2494 ------------------
2495 -- Set_Readable --
2496 ------------------
2498 procedure Set_Readable (Name : String) is
2499 procedure C_Set_Readable (Name : C_File_Name);
2500 pragma Import (C, C_Set_Readable, "__gnat_set_readable");
2501 C_Name : aliased String (Name'First .. Name'Last + 1);
2502 begin
2503 C_Name (Name'Range) := Name;
2504 C_Name (C_Name'Last) := ASCII.NUL;
2505 C_Set_Readable (C_Name (C_Name'First)'Address);
2506 end Set_Readable;
2508 --------------------
2509 -- Set_Writable --
2510 --------------------
2512 procedure Set_Writable (Name : String) is
2513 procedure C_Set_Writable (Name : C_File_Name);
2514 pragma Import (C, C_Set_Writable, "__gnat_set_writable");
2515 C_Name : aliased String (Name'First .. Name'Last + 1);
2516 begin
2517 C_Name (Name'Range) := Name;
2518 C_Name (C_Name'Last) := ASCII.NUL;
2519 C_Set_Writable (C_Name (C_Name'First)'Address);
2520 end Set_Writable;
2522 ------------
2523 -- Setenv --
2524 ------------
2526 procedure Setenv (Name : String; Value : String) is
2527 F_Name : String (1 .. Name'Length + 1);
2528 F_Value : String (1 .. Value'Length + 1);
2530 procedure Set_Env_Value (Name, Value : System.Address);
2531 pragma Import (C, Set_Env_Value, "__gnat_setenv");
2533 begin
2534 F_Name (1 .. Name'Length) := Name;
2535 F_Name (F_Name'Last) := ASCII.NUL;
2537 F_Value (1 .. Value'Length) := Value;
2538 F_Value (F_Value'Last) := ASCII.NUL;
2540 Set_Env_Value (F_Name'Address, F_Value'Address);
2541 end Setenv;
2543 -----------
2544 -- Spawn --
2545 -----------
2547 function Spawn
2548 (Program_Name : String;
2549 Args : Argument_List) return Integer
2551 Result : Integer;
2552 Junk : Process_Id;
2553 pragma Warnings (Off, Junk);
2554 begin
2555 Spawn_Internal (Program_Name, Args, Result, Junk, Blocking => True);
2556 return Result;
2557 end Spawn;
2559 procedure Spawn
2560 (Program_Name : String;
2561 Args : Argument_List;
2562 Success : out Boolean)
2564 begin
2565 Success := (Spawn (Program_Name, Args) = 0);
2566 end Spawn;
2568 procedure Spawn
2569 (Program_Name : String;
2570 Args : Argument_List;
2571 Output_File_Descriptor : File_Descriptor;
2572 Return_Code : out Integer;
2573 Err_To_Out : Boolean := True)
2575 Saved_Output : File_Descriptor;
2576 Saved_Error : File_Descriptor := Invalid_FD; -- prevent compiler warning
2578 begin
2579 -- Set standard output and error to the temporary file
2581 Saved_Output := Dup (Standout);
2582 Dup2 (Output_File_Descriptor, Standout);
2584 if Err_To_Out then
2585 Saved_Error := Dup (Standerr);
2586 Dup2 (Output_File_Descriptor, Standerr);
2587 end if;
2589 -- Spawn the program
2591 Return_Code := Spawn (Program_Name, Args);
2593 -- Restore the standard output and error
2595 Dup2 (Saved_Output, Standout);
2597 if Err_To_Out then
2598 Dup2 (Saved_Error, Standerr);
2599 end if;
2601 -- And close the saved standard output and error file descriptors
2603 Close (Saved_Output);
2605 if Err_To_Out then
2606 Close (Saved_Error);
2607 end if;
2608 end Spawn;
2610 procedure Spawn
2611 (Program_Name : String;
2612 Args : Argument_List;
2613 Output_File : String;
2614 Success : out Boolean;
2615 Return_Code : out Integer;
2616 Err_To_Out : Boolean := True)
2618 FD : File_Descriptor;
2620 begin
2621 Success := True;
2622 Return_Code := 0;
2624 FD := Create_Output_Text_File (Output_File);
2626 if FD = Invalid_FD then
2627 Success := False;
2628 return;
2629 end if;
2631 Spawn (Program_Name, Args, FD, Return_Code, Err_To_Out);
2633 Close (FD, Success);
2634 end Spawn;
2636 --------------------
2637 -- Spawn_Internal --
2638 --------------------
2640 procedure Spawn_Internal
2641 (Program_Name : String;
2642 Args : Argument_List;
2643 Result : out Integer;
2644 Pid : out Process_Id;
2645 Blocking : Boolean)
2648 procedure Spawn (Args : Argument_List);
2649 -- Call Spawn with given argument list
2651 N_Args : Argument_List (Args'Range);
2652 -- Normalized arguments
2654 -----------
2655 -- Spawn --
2656 -----------
2658 procedure Spawn (Args : Argument_List) is
2659 type Chars is array (Positive range <>) of aliased Character;
2660 type Char_Ptr is access constant Character;
2662 Command_Len : constant Positive := Program_Name'Length + 1
2663 + Args_Length (Args);
2664 Command_Last : Natural := 0;
2665 Command : aliased Chars (1 .. Command_Len);
2666 -- Command contains all characters of the Program_Name and Args, all
2667 -- terminated by ASCII.NUL characters.
2669 Arg_List_Len : constant Positive := Args'Length + 2;
2670 Arg_List_Last : Natural := 0;
2671 Arg_List : aliased array (1 .. Arg_List_Len) of Char_Ptr;
2672 -- List with pointers to NUL-terminated strings of the Program_Name
2673 -- and the Args and terminated with a null pointer. We rely on the
2674 -- default initialization for the last null pointer.
2676 procedure Add_To_Command (S : String);
2677 -- Add S and a NUL character to Command, updating Last
2679 function Portable_Spawn (Args : Address) return Integer;
2680 pragma Import (C, Portable_Spawn, "__gnat_portable_spawn");
2682 function Portable_No_Block_Spawn (Args : Address) return Process_Id;
2683 pragma Import
2684 (C, Portable_No_Block_Spawn, "__gnat_portable_no_block_spawn");
2686 --------------------
2687 -- Add_To_Command --
2688 --------------------
2690 procedure Add_To_Command (S : String) is
2691 First : constant Natural := Command_Last + 1;
2693 begin
2694 Command_Last := Command_Last + S'Length;
2696 -- Move characters one at a time, because Command has aliased
2697 -- components.
2699 -- But not volatile, so why is this necessary ???
2701 for J in S'Range loop
2702 Command (First + J - S'First) := S (J);
2703 end loop;
2705 Command_Last := Command_Last + 1;
2706 Command (Command_Last) := ASCII.NUL;
2708 Arg_List_Last := Arg_List_Last + 1;
2709 Arg_List (Arg_List_Last) := Command (First)'Access;
2710 end Add_To_Command;
2712 -- Start of processing for Spawn
2714 begin
2715 Add_To_Command (Program_Name);
2717 for J in Args'Range loop
2718 Add_To_Command (Args (J).all);
2719 end loop;
2721 if Blocking then
2722 Pid := Invalid_Pid;
2723 Result := Portable_Spawn (Arg_List'Address);
2724 else
2725 Pid := Portable_No_Block_Spawn (Arg_List'Address);
2726 Result := Boolean'Pos (Pid /= Invalid_Pid);
2727 end if;
2728 end Spawn;
2730 -- Start of processing for Spawn_Internal
2732 begin
2733 -- Copy arguments into a local structure
2735 for K in N_Args'Range loop
2736 N_Args (K) := new String'(Args (K).all);
2737 end loop;
2739 -- Normalize those arguments
2741 Normalize_Arguments (N_Args);
2743 -- Call spawn using the normalized arguments
2745 Spawn (N_Args);
2747 -- Free arguments list
2749 for K in N_Args'Range loop
2750 Free (N_Args (K));
2751 end loop;
2752 end Spawn_Internal;
2754 ---------------------------
2755 -- To_Path_String_Access --
2756 ---------------------------
2758 function To_Path_String_Access
2759 (Path_Addr : Address;
2760 Path_Len : Integer) return String_Access
2762 subtype Path_String is String (1 .. Path_Len);
2763 type Path_String_Access is access Path_String;
2765 function Address_To_Access is new Ada.Unchecked_Conversion
2766 (Source => Address, Target => Path_String_Access);
2768 Path_Access : constant Path_String_Access :=
2769 Address_To_Access (Path_Addr);
2771 Return_Val : String_Access;
2773 begin
2774 Return_Val := new String (1 .. Path_Len);
2776 for J in 1 .. Path_Len loop
2777 Return_Val (J) := Path_Access (J);
2778 end loop;
2780 return Return_Val;
2781 end To_Path_String_Access;
2783 ------------------
2784 -- Wait_Process --
2785 ------------------
2787 procedure Wait_Process (Pid : out Process_Id; Success : out Boolean) is
2788 Status : Integer;
2790 function Portable_Wait (S : Address) return Process_Id;
2791 pragma Import (C, Portable_Wait, "__gnat_portable_wait");
2793 begin
2794 Pid := Portable_Wait (Status'Address);
2795 Success := (Status = 0);
2796 end Wait_Process;
2798 -----------
2799 -- Write --
2800 -----------
2802 function Write
2803 (FD : File_Descriptor;
2804 A : System.Address;
2805 N : Integer) return Integer
2807 begin
2808 return
2809 Integer (System.CRTL.write
2810 (System.CRTL.int (FD),
2811 System.CRTL.chars (A),
2812 System.CRTL.size_t (N)));
2813 end Write;
2815 end System.OS_Lib;