2009-06-03 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / ada / s-os_lib.adb
blobe24a02e8895216ed57a3a0fe6edb990f41cb760a
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-2008, 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 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. --
21 -- --
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. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 pragma Compiler_Unit;
36 with System.Case_Util;
37 with System.CRTL;
38 with System.Soft_Links;
39 with Ada.Unchecked_Conversion;
40 with Ada.Unchecked_Deallocation;
41 with System; use System;
43 package body System.OS_Lib is
45 -- Imported procedures Dup and Dup2 are used in procedures Spawn and
46 -- Non_Blocking_Spawn.
48 function Dup (Fd : File_Descriptor) return File_Descriptor;
49 pragma Import (C, Dup, "__gnat_dup");
51 procedure Dup2 (Old_Fd, New_Fd : File_Descriptor);
52 pragma Import (C, Dup2, "__gnat_dup2");
54 On_Windows : constant Boolean := Directory_Separator = '\';
55 -- An indication that we are on Windows. Used in Normalize_Pathname, to
56 -- deal with drive letters in the beginning of absolute paths.
58 package SSL renames System.Soft_Links;
60 -- The following are used by Create_Temp_File
62 First_Temp_File_Name : constant String := "GNAT-TEMP-000000.TMP";
63 -- Used to initialize Current_Temp_File_Name and Temp_File_Name_Last_Digit
65 Current_Temp_File_Name : String := First_Temp_File_Name;
66 -- Name of the temp file last created
68 Temp_File_Name_Last_Digit : constant Positive :=
69 First_Temp_File_Name'Last - 4;
70 -- Position of the last digit in Current_Temp_File_Name
72 Max_Attempts : constant := 100;
73 -- The maximum number of attempts to create a new temp file
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 function Args_Length (Args : Argument_List) return Natural;
80 -- Returns total number of characters needed to create a string
81 -- of all Args terminated by ASCII.NUL characters
83 function C_String_Length (S : Address) return Integer;
84 -- Returns the length of a C string. Does check for null address
85 -- (returns 0).
87 procedure Spawn_Internal
88 (Program_Name : String;
89 Args : Argument_List;
90 Result : out Integer;
91 Pid : out Process_Id;
92 Blocking : Boolean);
93 -- Internal routine to implement the two Spawn (blocking/non blocking)
94 -- routines. If Blocking is set to True then the spawn is blocking
95 -- otherwise it is non blocking. In this latter case the Pid contains the
96 -- process id number. The first three parameters are as in Spawn. Note that
97 -- Spawn_Internal normalizes the argument list before calling the low level
98 -- system spawn routines (see Normalize_Arguments).
100 -- Note: Normalize_Arguments is designed to do nothing if it is called more
101 -- than once, so calling Normalize_Arguments before calling one of the
102 -- spawn routines is fine.
104 function To_Path_String_Access
105 (Path_Addr : Address;
106 Path_Len : Integer) return String_Access;
107 -- Converts a C String to an Ada String. We could do this making use of
108 -- Interfaces.C.Strings but we prefer not to import that entire package
110 ---------
111 -- "<" --
112 ---------
114 function "<" (X, Y : OS_Time) return Boolean is
115 begin
116 return Long_Integer (X) < Long_Integer (Y);
117 end "<";
119 ----------
120 -- "<=" --
121 ----------
123 function "<=" (X, Y : OS_Time) return Boolean is
124 begin
125 return Long_Integer (X) <= Long_Integer (Y);
126 end "<=";
128 ---------
129 -- ">" --
130 ---------
132 function ">" (X, Y : OS_Time) return Boolean is
133 begin
134 return Long_Integer (X) > Long_Integer (Y);
135 end ">";
137 ----------
138 -- ">=" --
139 ----------
141 function ">=" (X, Y : OS_Time) return Boolean is
142 begin
143 return Long_Integer (X) >= Long_Integer (Y);
144 end ">=";
146 -----------------
147 -- Args_Length --
148 -----------------
150 function Args_Length (Args : Argument_List) return Natural is
151 Len : Natural := 0;
153 begin
154 for J in Args'Range loop
155 Len := Len + Args (J)'Length + 1; -- One extra for ASCII.NUL
156 end loop;
158 return Len;
159 end Args_Length;
161 -----------------------------
162 -- Argument_String_To_List --
163 -----------------------------
165 function Argument_String_To_List
166 (Arg_String : String) return Argument_List_Access
168 Max_Args : constant Integer := Arg_String'Length;
169 New_Argv : Argument_List (1 .. Max_Args);
170 New_Argc : Natural := 0;
171 Idx : Integer;
173 begin
174 Idx := Arg_String'First;
176 loop
177 exit when Idx > Arg_String'Last;
179 declare
180 Quoted : Boolean := False;
181 Backqd : Boolean := False;
182 Old_Idx : Integer;
184 begin
185 Old_Idx := Idx;
187 loop
188 -- An unquoted space is the end of an argument
190 if not (Backqd or Quoted)
191 and then Arg_String (Idx) = ' '
192 then
193 exit;
195 -- Start of a quoted string
197 elsif not (Backqd or Quoted)
198 and then Arg_String (Idx) = '"'
199 then
200 Quoted := True;
202 -- End of a quoted string and end of an argument
204 elsif (Quoted and not Backqd)
205 and then Arg_String (Idx) = '"'
206 then
207 Idx := Idx + 1;
208 exit;
210 -- Following character is backquoted
212 elsif Arg_String (Idx) = '\' then
213 Backqd := True;
215 -- Turn off backquoting after advancing one character
217 elsif Backqd then
218 Backqd := False;
220 end if;
222 Idx := Idx + 1;
223 exit when Idx > Arg_String'Last;
224 end loop;
226 -- Found an argument
228 New_Argc := New_Argc + 1;
229 New_Argv (New_Argc) :=
230 new String'(Arg_String (Old_Idx .. Idx - 1));
232 -- Skip extraneous spaces
234 while Idx <= Arg_String'Last and then Arg_String (Idx) = ' ' loop
235 Idx := Idx + 1;
236 end loop;
237 end;
238 end loop;
240 return new Argument_List'(New_Argv (1 .. New_Argc));
241 end Argument_String_To_List;
243 ---------------------
244 -- C_String_Length --
245 ---------------------
247 function C_String_Length (S : Address) return Integer is
248 function Strlen (S : Address) return Integer;
249 pragma Import (C, Strlen, "strlen");
250 begin
251 if S = Null_Address then
252 return 0;
253 else
254 return Strlen (S);
255 end if;
256 end C_String_Length;
258 -----------
259 -- Close --
260 -----------
262 procedure Close (FD : File_Descriptor) is
263 procedure C_Close (FD : File_Descriptor);
264 pragma Import (C, C_Close, "close");
265 begin
266 C_Close (FD);
267 end Close;
269 procedure Close (FD : File_Descriptor; Status : out Boolean) is
270 function C_Close (FD : File_Descriptor) return Integer;
271 pragma Import (C, C_Close, "close");
272 begin
273 Status := (C_Close (FD) = 0);
274 end Close;
276 ---------------
277 -- Copy_File --
278 ---------------
280 procedure Copy_File
281 (Name : String;
282 Pathname : String;
283 Success : out Boolean;
284 Mode : Copy_Mode := Copy;
285 Preserve : Attribute := Time_Stamps)
287 From : File_Descriptor;
288 To : File_Descriptor;
290 Copy_Error : exception;
291 -- Internal exception raised to signal error in copy
293 function Build_Path (Dir : String; File : String) return String;
294 -- Returns pathname Dir concatenated with File adding the directory
295 -- separator only if needed.
297 procedure Copy (From, To : File_Descriptor);
298 -- Read data from From and place them into To. In both cases the
299 -- operations uses the current file position. Raises Constraint_Error
300 -- if a problem occurs during the copy.
302 procedure Copy_To (To_Name : String);
303 -- Does a straight copy from source to designated destination file
305 ----------------
306 -- Build_Path --
307 ----------------
309 function Build_Path (Dir : String; File : String) return String is
310 Res : String (1 .. Dir'Length + File'Length + 1);
312 Base_File_Ptr : Integer;
313 -- The base file name is File (Base_File_Ptr + 1 .. File'Last)
315 function Is_Dirsep (C : Character) return Boolean;
316 pragma Inline (Is_Dirsep);
317 -- Returns True if C is a directory separator. On Windows we
318 -- handle both styles of directory separator.
320 ---------------
321 -- Is_Dirsep --
322 ---------------
324 function Is_Dirsep (C : Character) return Boolean is
325 begin
326 return C = Directory_Separator or else C = '/';
327 end Is_Dirsep;
329 -- Start of processing for Build_Path
331 begin
332 -- Find base file name
334 Base_File_Ptr := File'Last;
335 while Base_File_Ptr >= File'First loop
336 exit when Is_Dirsep (File (Base_File_Ptr));
337 Base_File_Ptr := Base_File_Ptr - 1;
338 end loop;
340 declare
341 Base_File : String renames
342 File (Base_File_Ptr + 1 .. File'Last);
344 begin
345 Res (1 .. Dir'Length) := Dir;
347 if Is_Dirsep (Dir (Dir'Last)) then
348 Res (Dir'Length + 1 .. Dir'Length + Base_File'Length) :=
349 Base_File;
350 return Res (1 .. Dir'Length + Base_File'Length);
352 else
353 Res (Dir'Length + 1) := Directory_Separator;
354 Res (Dir'Length + 2 .. Dir'Length + 1 + Base_File'Length) :=
355 Base_File;
356 return Res (1 .. Dir'Length + 1 + Base_File'Length);
357 end if;
358 end;
359 end Build_Path;
361 ----------
362 -- Copy --
363 ----------
365 procedure Copy (From, To : File_Descriptor) is
366 Buf_Size : constant := 200_000;
367 type Buf is array (1 .. Buf_Size) of Character;
368 type Buf_Ptr is access Buf;
370 Buffer : Buf_Ptr;
371 R : Integer;
372 W : Integer;
374 Status_From : Boolean;
375 Status_To : Boolean;
376 -- Statuses for the calls to Close
378 procedure Free is new Ada.Unchecked_Deallocation (Buf, Buf_Ptr);
380 begin
381 -- Check for invalid descriptors, making sure that we do not
382 -- accidentally leave an open file descriptor around.
384 if From = Invalid_FD then
385 if To /= Invalid_FD then
386 Close (To, Status_To);
387 end if;
389 raise Copy_Error;
391 elsif To = Invalid_FD then
392 Close (From, Status_From);
393 raise Copy_Error;
394 end if;
396 -- Allocate the buffer on the heap
398 Buffer := new Buf;
400 loop
401 R := Read (From, Buffer (1)'Address, Buf_Size);
403 -- For VMS, the buffer may not be full. So, we need to try again
404 -- until there is nothing to read.
406 exit when R = 0;
408 W := Write (To, Buffer (1)'Address, R);
410 if W < R then
412 -- Problem writing data, could be a disk full. Close files
413 -- without worrying about status, since we are raising a
414 -- Copy_Error exception in any case.
416 Close (From, Status_From);
417 Close (To, Status_To);
419 Free (Buffer);
421 raise Copy_Error;
422 end if;
423 end loop;
425 Close (From, Status_From);
426 Close (To, Status_To);
428 Free (Buffer);
430 if not (Status_From and Status_To) then
431 raise Copy_Error;
432 end if;
433 end Copy;
435 -------------
436 -- Copy_To --
437 -------------
439 procedure Copy_To (To_Name : String) is
441 function Copy_Attributes
442 (From, To : System.Address;
443 Mode : Integer) return Integer;
444 pragma Import (C, Copy_Attributes, "__gnat_copy_attribs");
445 -- Mode = 0 - copy only time stamps.
446 -- Mode = 1 - copy time stamps and read/write/execute attributes
448 C_From : String (1 .. Name'Length + 1);
449 C_To : String (1 .. To_Name'Length + 1);
451 begin
452 From := Open_Read (Name, Binary);
454 -- Do not clobber destination file if source file could not be opened
456 if From /= Invalid_FD then
457 To := Create_File (To_Name, Binary);
458 end if;
460 Copy (From, To);
462 -- Copy attributes
464 C_From (1 .. Name'Length) := Name;
465 C_From (C_From'Last) := ASCII.NUL;
467 C_To (1 .. To_Name'Length) := To_Name;
468 C_To (C_To'Last) := ASCII.NUL;
470 case Preserve is
472 when Time_Stamps =>
473 if Copy_Attributes (C_From'Address, C_To'Address, 0) = -1 then
474 raise Copy_Error;
475 end if;
477 when Full =>
478 if Copy_Attributes (C_From'Address, C_To'Address, 1) = -1 then
479 raise Copy_Error;
480 end if;
482 when None =>
483 null;
484 end case;
486 end Copy_To;
488 -- Start of processing for Copy_File
490 begin
491 Success := True;
493 -- The source file must exist
495 if not Is_Regular_File (Name) then
496 raise Copy_Error;
497 end if;
499 -- The source file exists
501 case Mode is
503 -- Copy case, target file must not exist
505 when Copy =>
507 -- If the target file exists, we have an error
509 if Is_Regular_File (Pathname) then
510 raise Copy_Error;
512 -- Case of target is a directory
514 elsif Is_Directory (Pathname) then
515 declare
516 Dest : constant String := Build_Path (Pathname, Name);
518 begin
519 -- If target file exists, we have an error, else do copy
521 if Is_Regular_File (Dest) then
522 raise Copy_Error;
523 else
524 Copy_To (Dest);
525 end if;
526 end;
528 -- Case of normal copy to file (destination does not exist)
530 else
531 Copy_To (Pathname);
532 end if;
534 -- Overwrite case (destination file may or may not exist)
536 when Overwrite =>
537 if Is_Directory (Pathname) then
538 Copy_To (Build_Path (Pathname, Name));
539 else
540 Copy_To (Pathname);
541 end if;
543 -- Append case (destination file may or may not exist)
545 when Append =>
547 -- Appending to existing file
549 if Is_Regular_File (Pathname) then
551 -- Append mode and destination file exists, append data at the
552 -- end of Pathname. But if we fail to open source file, do not
553 -- touch destination file at all.
555 From := Open_Read (Name, Binary);
556 if From /= Invalid_FD then
557 To := Open_Read_Write (Pathname, Binary);
558 end if;
560 Lseek (To, 0, Seek_End);
562 Copy (From, To);
564 -- Appending to directory, not allowed
566 elsif Is_Directory (Pathname) then
567 raise Copy_Error;
569 -- Appending when target file does not exist
571 else
572 Copy_To (Pathname);
573 end if;
574 end case;
576 -- All error cases are caught here
578 exception
579 when Copy_Error =>
580 Success := False;
581 end Copy_File;
583 procedure Copy_File
584 (Name : C_File_Name;
585 Pathname : C_File_Name;
586 Success : out Boolean;
587 Mode : Copy_Mode := Copy;
588 Preserve : Attribute := Time_Stamps)
590 Ada_Name : String_Access :=
591 To_Path_String_Access
592 (Name, C_String_Length (Name));
593 Ada_Pathname : String_Access :=
594 To_Path_String_Access
595 (Pathname, C_String_Length (Pathname));
596 begin
597 Copy_File (Ada_Name.all, Ada_Pathname.all, Success, Mode, Preserve);
598 Free (Ada_Name);
599 Free (Ada_Pathname);
600 end Copy_File;
602 ----------------------
603 -- Copy_Time_Stamps --
604 ----------------------
606 procedure Copy_Time_Stamps (Source, Dest : String; Success : out Boolean) is
608 function Copy_Attributes
609 (From, To : System.Address;
610 Mode : Integer) return Integer;
611 pragma Import (C, Copy_Attributes, "__gnat_copy_attribs");
612 -- Mode = 0 - copy only time stamps.
613 -- Mode = 1 - copy time stamps and read/write/execute attributes
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");
669 begin
670 return C_Create_File (Name, Fmode);
671 end Create_File;
673 function Create_File
674 (Name : String;
675 Fmode : Mode) return File_Descriptor
677 C_Name : String (1 .. Name'Length + 1);
679 begin
680 C_Name (1 .. Name'Length) := Name;
681 C_Name (C_Name'Last) := ASCII.NUL;
682 return Create_File (C_Name (C_Name'First)'Address, Fmode);
683 end Create_File;
685 ---------------------
686 -- Create_New_File --
687 ---------------------
689 function Create_New_File
690 (Name : C_File_Name;
691 Fmode : Mode) return File_Descriptor
693 function C_Create_New_File
694 (Name : C_File_Name;
695 Fmode : Mode) return File_Descriptor;
696 pragma Import (C, C_Create_New_File, "__gnat_open_new");
698 begin
699 return C_Create_New_File (Name, Fmode);
700 end Create_New_File;
702 function Create_New_File
703 (Name : String;
704 Fmode : Mode) return File_Descriptor
706 C_Name : String (1 .. Name'Length + 1);
708 begin
709 C_Name (1 .. Name'Length) := Name;
710 C_Name (C_Name'Last) := ASCII.NUL;
711 return Create_New_File (C_Name (C_Name'First)'Address, Fmode);
712 end Create_New_File;
714 -----------------------------
715 -- Create_Output_Text_File --
716 -----------------------------
718 function Create_Output_Text_File (Name : String) return File_Descriptor is
719 function C_Create_File
720 (Name : C_File_Name) return File_Descriptor;
721 pragma Import (C, C_Create_File, "__gnat_create_output_file");
723 C_Name : String (1 .. Name'Length + 1);
725 begin
726 C_Name (1 .. Name'Length) := Name;
727 C_Name (C_Name'Last) := ASCII.NUL;
728 return C_Create_File (C_Name (C_Name'First)'Address);
729 end Create_Output_Text_File;
731 ----------------------
732 -- Create_Temp_File --
733 ----------------------
735 procedure Create_Temp_File
736 (FD : out File_Descriptor;
737 Name : out Temp_File_Name)
739 function Open_New_Temp
740 (Name : System.Address;
741 Fmode : Mode) return File_Descriptor;
742 pragma Import (C, Open_New_Temp, "__gnat_open_new_temp");
744 begin
745 FD := Open_New_Temp (Name'Address, Binary);
746 end Create_Temp_File;
748 procedure Create_Temp_File
749 (FD : out File_Descriptor;
750 Name : out String_Access)
752 Pos : Positive;
753 Attempts : Natural := 0;
754 Current : String (Current_Temp_File_Name'Range);
756 begin
757 -- Loop until a new temp file can be created
759 File_Loop : loop
760 Locked : begin
761 -- We need to protect global variable Current_Temp_File_Name
762 -- against concurrent access by different tasks.
764 SSL.Lock_Task.all;
766 -- Start at the last digit
768 Pos := Temp_File_Name_Last_Digit;
770 Digit_Loop :
771 loop
772 -- Increment the digit by one
774 case Current_Temp_File_Name (Pos) is
775 when '0' .. '8' =>
776 Current_Temp_File_Name (Pos) :=
777 Character'Succ (Current_Temp_File_Name (Pos));
778 exit Digit_Loop;
780 when '9' =>
782 -- For 9, set the digit to 0 and go to the previous digit
784 Current_Temp_File_Name (Pos) := '0';
785 Pos := Pos - 1;
787 when others =>
789 -- If it is not a digit, then there are no available
790 -- temp file names. Return Invalid_FD. There is almost
791 -- no chance that this code will be ever be executed,
792 -- since it would mean that there are one million temp
793 -- files in the same directory!
795 SSL.Unlock_Task.all;
796 FD := Invalid_FD;
797 Name := null;
798 exit File_Loop;
799 end case;
800 end loop Digit_Loop;
802 Current := Current_Temp_File_Name;
804 -- We can now release the lock, because we are no longer
805 -- accessing Current_Temp_File_Name.
807 SSL.Unlock_Task.all;
809 exception
810 when others =>
811 SSL.Unlock_Task.all;
812 raise;
813 end Locked;
815 -- Attempt to create the file
817 FD := Create_New_File (Current, Binary);
819 if FD /= Invalid_FD then
820 Name := new String'(Current);
821 exit File_Loop;
822 end if;
824 if not Is_Regular_File (Current) then
826 -- If the file does not already exist and we are unable to create
827 -- it, we give up after Max_Attempts. Otherwise, we try again with
828 -- the next available file name.
830 Attempts := Attempts + 1;
832 if Attempts >= Max_Attempts then
833 FD := Invalid_FD;
834 Name := null;
835 exit File_Loop;
836 end if;
837 end if;
838 end loop File_Loop;
839 end Create_Temp_File;
841 -----------------
842 -- Delete_File --
843 -----------------
845 procedure Delete_File (Name : Address; Success : out Boolean) is
846 R : Integer;
847 begin
848 R := System.CRTL.unlink (Name);
849 Success := (R = 0);
850 end Delete_File;
852 procedure Delete_File (Name : String; Success : out Boolean) is
853 C_Name : String (1 .. Name'Length + 1);
855 begin
856 C_Name (1 .. Name'Length) := Name;
857 C_Name (C_Name'Last) := ASCII.NUL;
859 Delete_File (C_Name'Address, Success);
860 end Delete_File;
862 ---------------------
863 -- File_Time_Stamp --
864 ---------------------
866 function File_Time_Stamp (FD : File_Descriptor) return OS_Time is
867 function File_Time (FD : File_Descriptor) return OS_Time;
868 pragma Import (C, File_Time, "__gnat_file_time_fd");
869 begin
870 return File_Time (FD);
871 end File_Time_Stamp;
873 function File_Time_Stamp (Name : C_File_Name) return OS_Time is
874 function File_Time (Name : Address) return OS_Time;
875 pragma Import (C, File_Time, "__gnat_file_time_name");
876 begin
877 return File_Time (Name);
878 end File_Time_Stamp;
880 function File_Time_Stamp (Name : String) return OS_Time is
881 F_Name : String (1 .. Name'Length + 1);
882 begin
883 F_Name (1 .. Name'Length) := Name;
884 F_Name (F_Name'Last) := ASCII.NUL;
885 return File_Time_Stamp (F_Name'Address);
886 end File_Time_Stamp;
888 ---------------------------
889 -- Get_Debuggable_Suffix --
890 ---------------------------
892 function Get_Debuggable_Suffix return String_Access is
893 procedure Get_Suffix_Ptr (Length, Ptr : Address);
894 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_debuggable_suffix_ptr");
896 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
897 pragma Import (C, Strncpy, "strncpy");
899 Suffix_Ptr : Address;
900 Suffix_Length : Integer;
901 Result : String_Access;
903 begin
904 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
906 Result := new String (1 .. Suffix_Length);
908 if Suffix_Length > 0 then
909 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
910 end if;
912 return Result;
913 end Get_Debuggable_Suffix;
915 ---------------------------
916 -- Get_Executable_Suffix --
917 ---------------------------
919 function Get_Executable_Suffix return String_Access is
920 procedure Get_Suffix_Ptr (Length, Ptr : Address);
921 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_executable_suffix_ptr");
923 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
924 pragma Import (C, Strncpy, "strncpy");
926 Suffix_Ptr : Address;
927 Suffix_Length : Integer;
928 Result : String_Access;
930 begin
931 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
933 Result := new String (1 .. Suffix_Length);
935 if Suffix_Length > 0 then
936 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
937 end if;
939 return Result;
940 end Get_Executable_Suffix;
942 -----------------------
943 -- Get_Object_Suffix --
944 -----------------------
946 function Get_Object_Suffix return String_Access is
947 procedure Get_Suffix_Ptr (Length, Ptr : Address);
948 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_object_suffix_ptr");
950 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
951 pragma Import (C, Strncpy, "strncpy");
953 Suffix_Ptr : Address;
954 Suffix_Length : Integer;
955 Result : String_Access;
957 begin
958 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
960 Result := new String (1 .. Suffix_Length);
962 if Suffix_Length > 0 then
963 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
964 end if;
966 return Result;
967 end Get_Object_Suffix;
969 ----------------------------------
970 -- Get_Target_Debuggable_Suffix --
971 ----------------------------------
973 function Get_Target_Debuggable_Suffix return String_Access is
974 Target_Exec_Ext_Ptr : Address;
975 pragma Import
976 (C, Target_Exec_Ext_Ptr, "__gnat_target_debuggable_extension");
978 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
979 pragma Import (C, Strncpy, "strncpy");
981 function Strlen (Cstring : Address) return Integer;
982 pragma Import (C, Strlen, "strlen");
984 Suffix_Length : Integer;
985 Result : String_Access;
987 begin
988 Suffix_Length := Strlen (Target_Exec_Ext_Ptr);
990 Result := new String (1 .. Suffix_Length);
992 if Suffix_Length > 0 then
993 Strncpy (Result.all'Address, Target_Exec_Ext_Ptr, Suffix_Length);
994 end if;
996 return Result;
997 end Get_Target_Debuggable_Suffix;
999 ----------------------------------
1000 -- Get_Target_Executable_Suffix --
1001 ----------------------------------
1003 function Get_Target_Executable_Suffix return String_Access is
1004 Target_Exec_Ext_Ptr : Address;
1005 pragma Import
1006 (C, Target_Exec_Ext_Ptr, "__gnat_target_executable_extension");
1008 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1009 pragma Import (C, Strncpy, "strncpy");
1011 function Strlen (Cstring : Address) return Integer;
1012 pragma Import (C, Strlen, "strlen");
1014 Suffix_Length : Integer;
1015 Result : String_Access;
1017 begin
1018 Suffix_Length := Strlen (Target_Exec_Ext_Ptr);
1020 Result := new String (1 .. Suffix_Length);
1022 if Suffix_Length > 0 then
1023 Strncpy (Result.all'Address, Target_Exec_Ext_Ptr, Suffix_Length);
1024 end if;
1026 return Result;
1027 end Get_Target_Executable_Suffix;
1029 ------------------------------
1030 -- Get_Target_Object_Suffix --
1031 ------------------------------
1033 function Get_Target_Object_Suffix return String_Access is
1034 Target_Object_Ext_Ptr : Address;
1035 pragma Import
1036 (C, Target_Object_Ext_Ptr, "__gnat_target_object_extension");
1038 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1039 pragma Import (C, Strncpy, "strncpy");
1041 function Strlen (Cstring : Address) return Integer;
1042 pragma Import (C, Strlen, "strlen");
1044 Suffix_Length : Integer;
1045 Result : String_Access;
1047 begin
1048 Suffix_Length := Strlen (Target_Object_Ext_Ptr);
1050 Result := new String (1 .. Suffix_Length);
1052 if Suffix_Length > 0 then
1053 Strncpy (Result.all'Address, Target_Object_Ext_Ptr, Suffix_Length);
1054 end if;
1056 return Result;
1057 end Get_Target_Object_Suffix;
1059 ------------
1060 -- Getenv --
1061 ------------
1063 function Getenv (Name : String) return String_Access is
1064 procedure Get_Env_Value_Ptr (Name, Length, Ptr : Address);
1065 pragma Import (C, Get_Env_Value_Ptr, "__gnat_getenv");
1067 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1068 pragma Import (C, Strncpy, "strncpy");
1070 Env_Value_Ptr : aliased Address;
1071 Env_Value_Length : aliased Integer;
1072 F_Name : aliased String (1 .. Name'Length + 1);
1073 Result : String_Access;
1075 begin
1076 F_Name (1 .. Name'Length) := Name;
1077 F_Name (F_Name'Last) := ASCII.NUL;
1079 Get_Env_Value_Ptr
1080 (F_Name'Address, Env_Value_Length'Address, Env_Value_Ptr'Address);
1082 Result := new String (1 .. Env_Value_Length);
1084 if Env_Value_Length > 0 then
1085 Strncpy (Result.all'Address, Env_Value_Ptr, Env_Value_Length);
1086 end if;
1088 return Result;
1089 end Getenv;
1091 ------------
1092 -- GM_Day --
1093 ------------
1095 function GM_Day (Date : OS_Time) return Day_Type is
1096 D : Day_Type;
1098 pragma Warnings (Off);
1099 Y : Year_Type;
1100 Mo : Month_Type;
1101 H : Hour_Type;
1102 Mn : Minute_Type;
1103 S : Second_Type;
1104 pragma Warnings (On);
1106 begin
1107 GM_Split (Date, Y, Mo, D, H, Mn, S);
1108 return D;
1109 end GM_Day;
1111 -------------
1112 -- GM_Hour --
1113 -------------
1115 function GM_Hour (Date : OS_Time) return Hour_Type is
1116 H : Hour_Type;
1118 pragma Warnings (Off);
1119 Y : Year_Type;
1120 Mo : Month_Type;
1121 D : Day_Type;
1122 Mn : Minute_Type;
1123 S : Second_Type;
1124 pragma Warnings (On);
1126 begin
1127 GM_Split (Date, Y, Mo, D, H, Mn, S);
1128 return H;
1129 end GM_Hour;
1131 ---------------
1132 -- GM_Minute --
1133 ---------------
1135 function GM_Minute (Date : OS_Time) return Minute_Type is
1136 Mn : Minute_Type;
1138 pragma Warnings (Off);
1139 Y : Year_Type;
1140 Mo : Month_Type;
1141 D : Day_Type;
1142 H : Hour_Type;
1143 S : Second_Type;
1144 pragma Warnings (On);
1146 begin
1147 GM_Split (Date, Y, Mo, D, H, Mn, S);
1148 return Mn;
1149 end GM_Minute;
1151 --------------
1152 -- GM_Month --
1153 --------------
1155 function GM_Month (Date : OS_Time) return Month_Type is
1156 Mo : Month_Type;
1158 pragma Warnings (Off);
1159 Y : Year_Type;
1160 D : Day_Type;
1161 H : Hour_Type;
1162 Mn : Minute_Type;
1163 S : Second_Type;
1164 pragma Warnings (On);
1166 begin
1167 GM_Split (Date, Y, Mo, D, H, Mn, S);
1168 return Mo;
1169 end GM_Month;
1171 ---------------
1172 -- GM_Second --
1173 ---------------
1175 function GM_Second (Date : OS_Time) return Second_Type is
1176 S : Second_Type;
1178 pragma Warnings (Off);
1179 Y : Year_Type;
1180 Mo : Month_Type;
1181 D : Day_Type;
1182 H : Hour_Type;
1183 Mn : Minute_Type;
1184 pragma Warnings (On);
1186 begin
1187 GM_Split (Date, Y, Mo, D, H, Mn, S);
1188 return S;
1189 end GM_Second;
1191 --------------
1192 -- GM_Split --
1193 --------------
1195 procedure GM_Split
1196 (Date : OS_Time;
1197 Year : out Year_Type;
1198 Month : out Month_Type;
1199 Day : out Day_Type;
1200 Hour : out Hour_Type;
1201 Minute : out Minute_Type;
1202 Second : out Second_Type)
1204 procedure To_GM_Time
1205 (P_Time_T, P_Year, P_Month, P_Day, P_Hours, P_Mins, P_Secs : Address);
1206 pragma Import (C, To_GM_Time, "__gnat_to_gm_time");
1208 T : OS_Time := Date;
1209 Y : Integer;
1210 Mo : Integer;
1211 D : Integer;
1212 H : Integer;
1213 Mn : Integer;
1214 S : Integer;
1216 begin
1217 -- Use the global lock because To_GM_Time is not thread safe
1219 Locked_Processing : begin
1220 SSL.Lock_Task.all;
1221 To_GM_Time
1222 (T'Address, Y'Address, Mo'Address, D'Address,
1223 H'Address, Mn'Address, S'Address);
1224 SSL.Unlock_Task.all;
1226 exception
1227 when others =>
1228 SSL.Unlock_Task.all;
1229 raise;
1230 end Locked_Processing;
1232 Year := Y + 1900;
1233 Month := Mo + 1;
1234 Day := D;
1235 Hour := H;
1236 Minute := Mn;
1237 Second := S;
1238 end GM_Split;
1240 -------------
1241 -- GM_Year --
1242 -------------
1244 function GM_Year (Date : OS_Time) return Year_Type is
1245 Y : Year_Type;
1247 pragma Warnings (Off);
1248 Mo : Month_Type;
1249 D : Day_Type;
1250 H : Hour_Type;
1251 Mn : Minute_Type;
1252 S : Second_Type;
1253 pragma Warnings (On);
1255 begin
1256 GM_Split (Date, Y, Mo, D, H, Mn, S);
1257 return Y;
1258 end GM_Year;
1260 ----------------------
1261 -- Is_Absolute_Path --
1262 ----------------------
1264 function Is_Absolute_Path (Name : String) return Boolean is
1265 function Is_Absolute_Path
1266 (Name : Address;
1267 Length : Integer) return Integer;
1268 pragma Import (C, Is_Absolute_Path, "__gnat_is_absolute_path");
1269 begin
1270 return Is_Absolute_Path (Name'Address, Name'Length) /= 0;
1271 end Is_Absolute_Path;
1273 ------------------
1274 -- Is_Directory --
1275 ------------------
1277 function Is_Directory (Name : C_File_Name) return Boolean is
1278 function Is_Directory (Name : Address) return Integer;
1279 pragma Import (C, Is_Directory, "__gnat_is_directory");
1280 begin
1281 return Is_Directory (Name) /= 0;
1282 end Is_Directory;
1284 function Is_Directory (Name : String) return Boolean is
1285 F_Name : String (1 .. Name'Length + 1);
1286 begin
1287 F_Name (1 .. Name'Length) := Name;
1288 F_Name (F_Name'Last) := ASCII.NUL;
1289 return Is_Directory (F_Name'Address);
1290 end Is_Directory;
1292 ----------------------
1293 -- Is_Readable_File --
1294 ----------------------
1296 function Is_Readable_File (Name : C_File_Name) return Boolean is
1297 function Is_Readable_File (Name : Address) return Integer;
1298 pragma Import (C, Is_Readable_File, "__gnat_is_readable_file");
1299 begin
1300 return Is_Readable_File (Name) /= 0;
1301 end Is_Readable_File;
1303 function Is_Readable_File (Name : String) return Boolean is
1304 F_Name : String (1 .. Name'Length + 1);
1305 begin
1306 F_Name (1 .. Name'Length) := Name;
1307 F_Name (F_Name'Last) := ASCII.NUL;
1308 return Is_Readable_File (F_Name'Address);
1309 end Is_Readable_File;
1311 ------------------------
1312 -- Is_Executable_File --
1313 ------------------------
1315 function Is_Executable_File (Name : C_File_Name) return Boolean is
1316 function Is_Executable_File (Name : Address) return Integer;
1317 pragma Import (C, Is_Executable_File, "__gnat_is_executable_file");
1318 begin
1319 return Is_Executable_File (Name) /= 0;
1320 end Is_Executable_File;
1322 function Is_Executable_File (Name : String) return Boolean is
1323 F_Name : String (1 .. Name'Length + 1);
1324 begin
1325 F_Name (1 .. Name'Length) := Name;
1326 F_Name (F_Name'Last) := ASCII.NUL;
1327 return Is_Executable_File (F_Name'Address);
1328 end Is_Executable_File;
1330 ---------------------
1331 -- Is_Regular_File --
1332 ---------------------
1334 function Is_Regular_File (Name : C_File_Name) return Boolean is
1335 function Is_Regular_File (Name : Address) return Integer;
1336 pragma Import (C, Is_Regular_File, "__gnat_is_regular_file");
1337 begin
1338 return Is_Regular_File (Name) /= 0;
1339 end Is_Regular_File;
1341 function Is_Regular_File (Name : String) return Boolean is
1342 F_Name : String (1 .. Name'Length + 1);
1343 begin
1344 F_Name (1 .. Name'Length) := Name;
1345 F_Name (F_Name'Last) := ASCII.NUL;
1346 return Is_Regular_File (F_Name'Address);
1347 end Is_Regular_File;
1349 ----------------------
1350 -- Is_Symbolic_Link --
1351 ----------------------
1353 function Is_Symbolic_Link (Name : C_File_Name) return Boolean is
1354 function Is_Symbolic_Link (Name : Address) return Integer;
1355 pragma Import (C, Is_Symbolic_Link, "__gnat_is_symbolic_link");
1356 begin
1357 return Is_Symbolic_Link (Name) /= 0;
1358 end Is_Symbolic_Link;
1360 function Is_Symbolic_Link (Name : String) return Boolean is
1361 F_Name : String (1 .. Name'Length + 1);
1362 begin
1363 F_Name (1 .. Name'Length) := Name;
1364 F_Name (F_Name'Last) := ASCII.NUL;
1365 return Is_Symbolic_Link (F_Name'Address);
1366 end Is_Symbolic_Link;
1368 ----------------------
1369 -- Is_Writable_File --
1370 ----------------------
1372 function Is_Writable_File (Name : C_File_Name) return Boolean is
1373 function Is_Writable_File (Name : Address) return Integer;
1374 pragma Import (C, Is_Writable_File, "__gnat_is_writable_file");
1375 begin
1376 return Is_Writable_File (Name) /= 0;
1377 end Is_Writable_File;
1379 function Is_Writable_File (Name : String) return Boolean is
1380 F_Name : String (1 .. Name'Length + 1);
1381 begin
1382 F_Name (1 .. Name'Length) := Name;
1383 F_Name (F_Name'Last) := ASCII.NUL;
1384 return Is_Writable_File (F_Name'Address);
1385 end Is_Writable_File;
1387 -------------------------
1388 -- Locate_Exec_On_Path --
1389 -------------------------
1391 function Locate_Exec_On_Path
1392 (Exec_Name : String) return String_Access
1394 function Locate_Exec_On_Path (C_Exec_Name : Address) return Address;
1395 pragma Import (C, Locate_Exec_On_Path, "__gnat_locate_exec_on_path");
1397 procedure Free (Ptr : System.Address);
1398 pragma Import (C, Free, "free");
1400 C_Exec_Name : String (1 .. Exec_Name'Length + 1);
1401 Path_Addr : Address;
1402 Path_Len : Integer;
1403 Result : String_Access;
1405 begin
1406 C_Exec_Name (1 .. Exec_Name'Length) := Exec_Name;
1407 C_Exec_Name (C_Exec_Name'Last) := ASCII.NUL;
1409 Path_Addr := Locate_Exec_On_Path (C_Exec_Name'Address);
1410 Path_Len := C_String_Length (Path_Addr);
1412 if Path_Len = 0 then
1413 return null;
1415 else
1416 Result := To_Path_String_Access (Path_Addr, Path_Len);
1417 Free (Path_Addr);
1419 -- Always return an absolute path name
1421 if not Is_Absolute_Path (Result.all) then
1422 declare
1423 Absolute_Path : constant String :=
1424 Normalize_Pathname (Result.all);
1425 begin
1426 Free (Result);
1427 Result := new String'(Absolute_Path);
1428 end;
1429 end if;
1431 return Result;
1432 end if;
1433 end Locate_Exec_On_Path;
1435 -------------------------
1436 -- Locate_Regular_File --
1437 -------------------------
1439 function Locate_Regular_File
1440 (File_Name : C_File_Name;
1441 Path : C_File_Name) return String_Access
1443 function Locate_Regular_File
1444 (C_File_Name, Path_Val : Address) return Address;
1445 pragma Import (C, Locate_Regular_File, "__gnat_locate_regular_file");
1447 procedure Free (Ptr : System.Address);
1448 pragma Import (C, Free, "free");
1450 Path_Addr : Address;
1451 Path_Len : Integer;
1452 Result : String_Access;
1454 begin
1455 Path_Addr := Locate_Regular_File (File_Name, Path);
1456 Path_Len := C_String_Length (Path_Addr);
1458 if Path_Len = 0 then
1459 return null;
1461 else
1462 Result := To_Path_String_Access (Path_Addr, Path_Len);
1463 Free (Path_Addr);
1464 return Result;
1465 end if;
1466 end Locate_Regular_File;
1468 function Locate_Regular_File
1469 (File_Name : String;
1470 Path : String) return String_Access
1472 C_File_Name : String (1 .. File_Name'Length + 1);
1473 C_Path : String (1 .. Path'Length + 1);
1474 Result : String_Access;
1476 begin
1477 C_File_Name (1 .. File_Name'Length) := File_Name;
1478 C_File_Name (C_File_Name'Last) := ASCII.NUL;
1480 C_Path (1 .. Path'Length) := Path;
1481 C_Path (C_Path'Last) := ASCII.NUL;
1483 Result := Locate_Regular_File (C_File_Name'Address, C_Path'Address);
1485 -- Always return an absolute path name
1487 if Result /= null and then not Is_Absolute_Path (Result.all) then
1488 declare
1489 Absolute_Path : constant String := Normalize_Pathname (Result.all);
1490 begin
1491 Free (Result);
1492 Result := new String'(Absolute_Path);
1493 end;
1494 end if;
1496 return Result;
1497 end Locate_Regular_File;
1499 ------------------------
1500 -- Non_Blocking_Spawn --
1501 ------------------------
1503 function Non_Blocking_Spawn
1504 (Program_Name : String;
1505 Args : Argument_List) return Process_Id
1507 Pid : Process_Id;
1508 Junk : Integer;
1509 pragma Warnings (Off, Junk);
1510 begin
1511 Spawn_Internal (Program_Name, Args, Junk, Pid, Blocking => False);
1512 return Pid;
1513 end Non_Blocking_Spawn;
1515 function Non_Blocking_Spawn
1516 (Program_Name : String;
1517 Args : Argument_List;
1518 Output_File_Descriptor : File_Descriptor;
1519 Err_To_Out : Boolean := True) return Process_Id
1521 Saved_Output : File_Descriptor;
1522 Saved_Error : File_Descriptor := Invalid_FD; -- prevent warning
1523 Pid : Process_Id;
1525 begin
1526 if Output_File_Descriptor = Invalid_FD then
1527 return Invalid_Pid;
1528 end if;
1530 -- Set standard output and, if specified, error to the temporary file
1532 Saved_Output := Dup (Standout);
1533 Dup2 (Output_File_Descriptor, Standout);
1535 if Err_To_Out then
1536 Saved_Error := Dup (Standerr);
1537 Dup2 (Output_File_Descriptor, Standerr);
1538 end if;
1540 -- Spawn the program
1542 Pid := Non_Blocking_Spawn (Program_Name, Args);
1544 -- Restore the standard output and error
1546 Dup2 (Saved_Output, Standout);
1548 if Err_To_Out then
1549 Dup2 (Saved_Error, Standerr);
1550 end if;
1552 -- And close the saved standard output and error file descriptors
1554 Close (Saved_Output);
1556 if Err_To_Out then
1557 Close (Saved_Error);
1558 end if;
1560 return Pid;
1561 end Non_Blocking_Spawn;
1563 function Non_Blocking_Spawn
1564 (Program_Name : String;
1565 Args : Argument_List;
1566 Output_File : String;
1567 Err_To_Out : Boolean := True) return Process_Id
1569 Output_File_Descriptor : constant File_Descriptor :=
1570 Create_Output_Text_File (Output_File);
1571 Result : Process_Id;
1573 begin
1574 -- Do not attempt to spawn if the output file could not be created
1576 if Output_File_Descriptor = Invalid_FD then
1577 return Invalid_Pid;
1579 else
1580 Result := Non_Blocking_Spawn
1581 (Program_Name, Args, Output_File_Descriptor, Err_To_Out);
1583 -- Close the file just created for the output, as the file descriptor
1584 -- cannot be used anywhere, being a local value. It is safe to do
1585 -- that, as the file descriptor has been duplicated to form
1586 -- standard output and error of the spawned process.
1588 Close (Output_File_Descriptor);
1590 return Result;
1591 end if;
1592 end Non_Blocking_Spawn;
1594 -------------------------
1595 -- Normalize_Arguments --
1596 -------------------------
1598 procedure Normalize_Arguments (Args : in out Argument_List) is
1600 procedure Quote_Argument (Arg : in out String_Access);
1601 -- Add quote around argument if it contains spaces
1603 C_Argument_Needs_Quote : Integer;
1604 pragma Import (C, C_Argument_Needs_Quote, "__gnat_argument_needs_quote");
1605 Argument_Needs_Quote : constant Boolean := C_Argument_Needs_Quote /= 0;
1607 --------------------
1608 -- Quote_Argument --
1609 --------------------
1611 procedure Quote_Argument (Arg : in out String_Access) is
1612 Res : String (1 .. Arg'Length * 2);
1613 J : Positive := 1;
1614 Quote_Needed : Boolean := False;
1616 begin
1617 if Arg (Arg'First) /= '"' or else Arg (Arg'Last) /= '"' then
1619 -- Starting quote
1621 Res (J) := '"';
1623 for K in Arg'Range loop
1625 J := J + 1;
1627 if Arg (K) = '"' then
1628 Res (J) := '\';
1629 J := J + 1;
1630 Res (J) := '"';
1631 Quote_Needed := True;
1633 elsif Arg (K) = ' ' then
1634 Res (J) := Arg (K);
1635 Quote_Needed := True;
1637 else
1638 Res (J) := Arg (K);
1639 end if;
1641 end loop;
1643 if Quote_Needed then
1645 -- If null terminated string, put the quote before
1647 if Res (J) = ASCII.NUL then
1648 Res (J) := '"';
1649 J := J + 1;
1650 Res (J) := ASCII.NUL;
1652 -- If argument is terminated by '\', then double it. Otherwise
1653 -- the ending quote will be taken as-is. This is quite strange
1654 -- spawn behavior from Windows, but this is what we see!
1656 else
1657 if Res (J) = '\' then
1658 J := J + 1;
1659 Res (J) := '\';
1660 end if;
1662 -- Ending quote
1664 J := J + 1;
1665 Res (J) := '"';
1666 end if;
1668 declare
1669 Old : String_Access := Arg;
1671 begin
1672 Arg := new String'(Res (1 .. J));
1673 Free (Old);
1674 end;
1675 end if;
1677 end if;
1678 end Quote_Argument;
1680 -- Start of processing for Normalize_Arguments
1682 begin
1683 if Argument_Needs_Quote then
1684 for K in Args'Range loop
1685 if Args (K) /= null and then Args (K)'Length /= 0 then
1686 Quote_Argument (Args (K));
1687 end if;
1688 end loop;
1689 end if;
1690 end Normalize_Arguments;
1692 ------------------------
1693 -- Normalize_Pathname --
1694 ------------------------
1696 function Normalize_Pathname
1697 (Name : String;
1698 Directory : String := "";
1699 Resolve_Links : Boolean := True;
1700 Case_Sensitive : Boolean := True) return String
1702 Max_Path : Integer;
1703 pragma Import (C, Max_Path, "__gnat_max_path_len");
1704 -- Maximum length of a path name
1706 procedure Get_Current_Dir
1707 (Dir : System.Address;
1708 Length : System.Address);
1709 pragma Import (C, Get_Current_Dir, "__gnat_get_current_dir");
1711 Path_Buffer : String (1 .. Max_Path + Max_Path + 2);
1712 End_Path : Natural := 0;
1713 Link_Buffer : String (1 .. Max_Path + 2);
1714 Status : Integer;
1715 Last : Positive;
1716 Start : Natural;
1717 Finish : Positive;
1719 Max_Iterations : constant := 500;
1721 function Get_File_Names_Case_Sensitive return Integer;
1722 pragma Import
1723 (C, Get_File_Names_Case_Sensitive,
1724 "__gnat_get_file_names_case_sensitive");
1726 Fold_To_Lower_Case : constant Boolean :=
1727 not Case_Sensitive
1728 and then Get_File_Names_Case_Sensitive = 0;
1730 function Readlink
1731 (Path : System.Address;
1732 Buf : System.Address;
1733 Bufsiz : Integer) return Integer;
1734 pragma Import (C, Readlink, "__gnat_readlink");
1736 function To_Canonical_File_Spec
1737 (Host_File : System.Address) return System.Address;
1738 pragma Import
1739 (C, To_Canonical_File_Spec, "__gnat_to_canonical_file_spec");
1741 The_Name : String (1 .. Name'Length + 1);
1742 Canonical_File_Addr : System.Address;
1743 Canonical_File_Len : Integer;
1745 function Strlen (S : System.Address) return Integer;
1746 pragma Import (C, Strlen, "strlen");
1748 function Final_Value (S : String) return String;
1749 -- Make final adjustment to the returned string. This function strips
1750 -- trailing directory separators, and folds returned string to lower
1751 -- case if required.
1753 function Get_Directory (Dir : String) return String;
1754 -- If Dir is not empty, return it, adding a directory separator
1755 -- if not already present, otherwise return current working directory
1756 -- with terminating directory separator.
1758 -----------------
1759 -- Final_Value --
1760 -----------------
1762 function Final_Value (S : String) return String is
1763 S1 : String := S;
1764 -- We may need to fold S to lower case, so we need a variable
1766 Last : Natural;
1768 begin
1769 if Fold_To_Lower_Case then
1770 System.Case_Util.To_Lower (S1);
1771 end if;
1773 -- Remove trailing directory separator, if any
1775 Last := S1'Last;
1777 if Last > 1
1778 and then (S1 (Last) = '/'
1779 or else
1780 S1 (Last) = Directory_Separator)
1781 then
1782 -- Special case for Windows: C:\
1784 if Last = 3
1785 and then S1 (1) /= Directory_Separator
1786 and then S1 (2) = ':'
1787 then
1788 null;
1790 else
1791 Last := Last - 1;
1792 end if;
1793 end if;
1795 return S1 (1 .. Last);
1796 end Final_Value;
1798 -------------------
1799 -- Get_Directory --
1800 -------------------
1802 function Get_Directory (Dir : String) return String is
1803 begin
1804 -- Directory given, add directory separator if needed
1806 if Dir'Length > 0 then
1807 if Dir (Dir'Last) = Directory_Separator then
1808 return Dir;
1809 else
1810 declare
1811 Result : String (1 .. Dir'Length + 1);
1812 begin
1813 Result (1 .. Dir'Length) := Dir;
1814 Result (Result'Length) := Directory_Separator;
1815 return Result;
1816 end;
1817 end if;
1819 -- Directory name not given, get current directory
1821 else
1822 declare
1823 Buffer : String (1 .. Max_Path + 2);
1824 Path_Len : Natural := Max_Path;
1826 begin
1827 Get_Current_Dir (Buffer'Address, Path_Len'Address);
1829 if Buffer (Path_Len) /= Directory_Separator then
1830 Path_Len := Path_Len + 1;
1831 Buffer (Path_Len) := Directory_Separator;
1832 end if;
1834 -- By default, the drive letter on Windows is in upper case
1836 if On_Windows
1837 and then Path_Len >= 2
1838 and then Buffer (2) = ':'
1839 then
1840 System.Case_Util.To_Upper (Buffer (1 .. 1));
1841 end if;
1843 return Buffer (1 .. Path_Len);
1844 end;
1845 end if;
1846 end Get_Directory;
1848 -- Start of processing for Normalize_Pathname
1850 begin
1851 -- Special case, if name is null, then return null
1853 if Name'Length = 0 then
1854 return "";
1855 end if;
1857 -- First, convert VMS file spec to Unix file spec.
1858 -- If Name is not in VMS syntax, then this is equivalent
1859 -- to put Name at the beginning of Path_Buffer.
1861 VMS_Conversion : begin
1862 The_Name (1 .. Name'Length) := Name;
1863 The_Name (The_Name'Last) := ASCII.NUL;
1865 Canonical_File_Addr := To_Canonical_File_Spec (The_Name'Address);
1866 Canonical_File_Len := Strlen (Canonical_File_Addr);
1868 -- If VMS syntax conversion has failed, return an empty string
1869 -- to indicate the failure.
1871 if Canonical_File_Len = 0 then
1872 return "";
1873 end if;
1875 declare
1876 subtype Path_String is String (1 .. Canonical_File_Len);
1877 type Path_String_Access is access Path_String;
1879 function Address_To_Access is new
1880 Ada.Unchecked_Conversion (Source => Address,
1881 Target => Path_String_Access);
1883 Path_Access : constant Path_String_Access :=
1884 Address_To_Access (Canonical_File_Addr);
1886 begin
1887 Path_Buffer (1 .. Canonical_File_Len) := Path_Access.all;
1888 End_Path := Canonical_File_Len;
1889 Last := 1;
1890 end;
1891 end VMS_Conversion;
1893 -- Replace all '/' by Directory Separators (this is for Windows)
1895 if Directory_Separator /= '/' then
1896 for Index in 1 .. End_Path loop
1897 if Path_Buffer (Index) = '/' then
1898 Path_Buffer (Index) := Directory_Separator;
1899 end if;
1900 end loop;
1901 end if;
1903 -- Resolve directory names for Windows (formerly also VMS)
1905 -- On VMS, if we have a Unix path such as /temp/..., and TEMP is a
1906 -- logical name, we must not try to resolve this logical name, because
1907 -- it may have multiple equivalences and if resolved we will only
1908 -- get the first one.
1910 if On_Windows then
1912 -- On Windows, if we have an absolute path starting with a directory
1913 -- separator, we need to have the drive letter appended in front.
1915 -- On Windows, Get_Current_Dir will return a suitable directory name
1916 -- (path starting with a drive letter on Windows). So we take this
1917 -- drive letter and prepend it to the current path.
1919 if Path_Buffer (1) = Directory_Separator
1920 and then Path_Buffer (2) /= Directory_Separator
1921 then
1922 declare
1923 Cur_Dir : constant String := Get_Directory ("");
1924 -- Get the current directory to get the drive letter
1926 begin
1927 if Cur_Dir'Length > 2
1928 and then Cur_Dir (Cur_Dir'First + 1) = ':'
1929 then
1930 Path_Buffer (3 .. End_Path + 2) :=
1931 Path_Buffer (1 .. End_Path);
1932 Path_Buffer (1 .. 2) :=
1933 Cur_Dir (Cur_Dir'First .. Cur_Dir'First + 1);
1934 End_Path := End_Path + 2;
1935 end if;
1936 end;
1938 -- We have a drive letter, ensure it is upper-case
1940 elsif Path_Buffer (1) in 'a' .. 'z'
1941 and then Path_Buffer (2) = ':'
1942 then
1943 System.Case_Util.To_Upper (Path_Buffer (1 .. 1));
1944 end if;
1945 end if;
1947 -- On Windows, remove all double-quotes that are possibly part of the
1948 -- path but can cause problems with other methods.
1950 if On_Windows then
1951 declare
1952 Index : Natural;
1954 begin
1955 Index := Path_Buffer'First;
1956 for Current in Path_Buffer'First .. End_Path loop
1957 if Path_Buffer (Current) /= '"' then
1958 Path_Buffer (Index) := Path_Buffer (Current);
1959 Index := Index + 1;
1960 end if;
1961 end loop;
1963 End_Path := Index - 1;
1964 end;
1965 end if;
1967 -- Start the conversions
1969 -- If this is not finished after Max_Iterations, give up and return an
1970 -- empty string.
1972 for J in 1 .. Max_Iterations loop
1974 -- If we don't have an absolute pathname, prepend the directory
1975 -- Reference_Dir.
1977 if Last = 1
1978 and then not Is_Absolute_Path (Path_Buffer (1 .. End_Path))
1979 then
1980 declare
1981 Reference_Dir : constant String := Get_Directory (Directory);
1982 Ref_Dir_Len : constant Natural := Reference_Dir'Length;
1983 -- Current directory name specified and its length
1985 begin
1986 Path_Buffer (Ref_Dir_Len + 1 .. Ref_Dir_Len + End_Path) :=
1987 Path_Buffer (1 .. End_Path);
1988 End_Path := Ref_Dir_Len + End_Path;
1989 Path_Buffer (1 .. Ref_Dir_Len) := Reference_Dir;
1990 Last := Ref_Dir_Len;
1991 end;
1992 end if;
1994 Start := Last + 1;
1995 Finish := Last;
1997 -- Ensure that Windows network drives are kept, e.g: \\server\drive-c
1999 if Start = 2
2000 and then Directory_Separator = '\'
2001 and then Path_Buffer (1 .. 2) = "\\"
2002 then
2003 Start := 3;
2004 end if;
2006 -- If we have traversed the full pathname, return it
2008 if Start > End_Path then
2009 return Final_Value (Path_Buffer (1 .. End_Path));
2010 end if;
2012 -- Remove duplicate directory separators
2014 while Path_Buffer (Start) = Directory_Separator loop
2015 if Start = End_Path then
2016 return Final_Value (Path_Buffer (1 .. End_Path - 1));
2018 else
2019 Path_Buffer (Start .. End_Path - 1) :=
2020 Path_Buffer (Start + 1 .. End_Path);
2021 End_Path := End_Path - 1;
2022 end if;
2023 end loop;
2025 -- Find the end of the current field: last character or the one
2026 -- preceding the next directory separator.
2028 while Finish < End_Path
2029 and then Path_Buffer (Finish + 1) /= Directory_Separator
2030 loop
2031 Finish := Finish + 1;
2032 end loop;
2034 -- Remove "." field
2036 if Start = Finish and then Path_Buffer (Start) = '.' then
2037 if Start = End_Path then
2038 if Last = 1 then
2039 return (1 => Directory_Separator);
2040 else
2042 if Fold_To_Lower_Case then
2043 System.Case_Util.To_Lower (Path_Buffer (1 .. Last - 1));
2044 end if;
2046 return Path_Buffer (1 .. Last - 1);
2048 end if;
2050 else
2051 Path_Buffer (Last + 1 .. End_Path - 2) :=
2052 Path_Buffer (Last + 3 .. End_Path);
2053 End_Path := End_Path - 2;
2054 end if;
2056 -- Remove ".." fields
2058 elsif Finish = Start + 1
2059 and then Path_Buffer (Start .. Finish) = ".."
2060 then
2061 Start := Last;
2062 loop
2063 Start := Start - 1;
2064 exit when Start < 1 or else
2065 Path_Buffer (Start) = Directory_Separator;
2066 end loop;
2068 if Start <= 1 then
2069 if Finish = End_Path then
2070 return (1 => Directory_Separator);
2072 else
2073 Path_Buffer (1 .. End_Path - Finish) :=
2074 Path_Buffer (Finish + 1 .. End_Path);
2075 End_Path := End_Path - Finish;
2076 Last := 1;
2077 end if;
2079 else
2080 if Finish = End_Path then
2081 return Final_Value (Path_Buffer (1 .. Start - 1));
2083 else
2084 Path_Buffer (Start + 1 .. Start + End_Path - Finish - 1) :=
2085 Path_Buffer (Finish + 2 .. End_Path);
2086 End_Path := Start + End_Path - Finish - 1;
2087 Last := Start;
2088 end if;
2089 end if;
2091 -- Check if current field is a symbolic link
2093 elsif Resolve_Links then
2094 declare
2095 Saved : constant Character := Path_Buffer (Finish + 1);
2097 begin
2098 Path_Buffer (Finish + 1) := ASCII.NUL;
2099 Status := Readlink (Path_Buffer'Address,
2100 Link_Buffer'Address,
2101 Link_Buffer'Length);
2102 Path_Buffer (Finish + 1) := Saved;
2103 end;
2105 -- Not a symbolic link, move to the next field, if any
2107 if Status <= 0 then
2108 Last := Finish + 1;
2110 -- Replace symbolic link with its value
2112 else
2113 if Is_Absolute_Path (Link_Buffer (1 .. Status)) then
2114 Path_Buffer (Status + 1 .. End_Path - (Finish - Status)) :=
2115 Path_Buffer (Finish + 1 .. End_Path);
2116 End_Path := End_Path - (Finish - Status);
2117 Path_Buffer (1 .. Status) := Link_Buffer (1 .. Status);
2118 Last := 1;
2120 else
2121 Path_Buffer
2122 (Last + Status + 1 .. End_Path - Finish + Last + Status) :=
2123 Path_Buffer (Finish + 1 .. End_Path);
2124 End_Path := End_Path - Finish + Last + Status;
2125 Path_Buffer (Last + 1 .. Last + Status) :=
2126 Link_Buffer (1 .. Status);
2127 end if;
2128 end if;
2130 else
2131 Last := Finish + 1;
2132 end if;
2133 end loop;
2135 -- Too many iterations: give up
2137 -- This can happen when there is a circularity in the symbolic links: A
2138 -- is a symbolic link for B, which itself is a symbolic link, and the
2139 -- target of B or of another symbolic link target of B is A. In this
2140 -- case, we return an empty string to indicate failure to resolve.
2142 return "";
2143 end Normalize_Pathname;
2145 ---------------
2146 -- Open_Read --
2147 ---------------
2149 function Open_Read
2150 (Name : C_File_Name;
2151 Fmode : Mode) return File_Descriptor
2153 function C_Open_Read
2154 (Name : C_File_Name;
2155 Fmode : Mode) return File_Descriptor;
2156 pragma Import (C, C_Open_Read, "__gnat_open_read");
2157 begin
2158 return C_Open_Read (Name, Fmode);
2159 end Open_Read;
2161 function Open_Read
2162 (Name : String;
2163 Fmode : Mode) return File_Descriptor
2165 C_Name : String (1 .. Name'Length + 1);
2166 begin
2167 C_Name (1 .. Name'Length) := Name;
2168 C_Name (C_Name'Last) := ASCII.NUL;
2169 return Open_Read (C_Name (C_Name'First)'Address, Fmode);
2170 end Open_Read;
2172 ---------------------
2173 -- Open_Read_Write --
2174 ---------------------
2176 function Open_Read_Write
2177 (Name : C_File_Name;
2178 Fmode : Mode) return File_Descriptor
2180 function C_Open_Read_Write
2181 (Name : C_File_Name;
2182 Fmode : Mode) return File_Descriptor;
2183 pragma Import (C, C_Open_Read_Write, "__gnat_open_rw");
2184 begin
2185 return C_Open_Read_Write (Name, Fmode);
2186 end Open_Read_Write;
2188 function Open_Read_Write
2189 (Name : String;
2190 Fmode : Mode) return File_Descriptor
2192 C_Name : String (1 .. Name'Length + 1);
2193 begin
2194 C_Name (1 .. Name'Length) := Name;
2195 C_Name (C_Name'Last) := ASCII.NUL;
2196 return Open_Read_Write (C_Name (C_Name'First)'Address, Fmode);
2197 end Open_Read_Write;
2199 -------------
2200 -- OS_Exit --
2201 -------------
2203 procedure OS_Exit (Status : Integer) is
2204 begin
2205 OS_Exit_Ptr (Status);
2206 raise Program_Error;
2207 end OS_Exit;
2209 ---------------------
2210 -- OS_Exit_Default --
2211 ---------------------
2213 procedure OS_Exit_Default (Status : Integer) is
2214 procedure GNAT_OS_Exit (Status : Integer);
2215 pragma Import (C, GNAT_OS_Exit, "__gnat_os_exit");
2216 pragma No_Return (GNAT_OS_Exit);
2217 begin
2218 GNAT_OS_Exit (Status);
2219 end OS_Exit_Default;
2221 --------------------
2222 -- Pid_To_Integer --
2223 --------------------
2225 function Pid_To_Integer (Pid : Process_Id) return Integer is
2226 begin
2227 return Integer (Pid);
2228 end Pid_To_Integer;
2230 ----------
2231 -- Read --
2232 ----------
2234 function Read
2235 (FD : File_Descriptor;
2236 A : System.Address;
2237 N : Integer) return Integer
2239 begin
2240 return Integer (System.CRTL.read
2241 (System.CRTL.int (FD), System.CRTL.chars (A), System.CRTL.int (N)));
2242 end Read;
2244 -----------------
2245 -- Rename_File --
2246 -----------------
2248 procedure Rename_File
2249 (Old_Name : C_File_Name;
2250 New_Name : C_File_Name;
2251 Success : out Boolean)
2253 function rename (From, To : Address) return Integer;
2254 pragma Import (C, rename, "__gnat_rename");
2255 R : Integer;
2256 begin
2257 R := rename (Old_Name, New_Name);
2258 Success := (R = 0);
2259 end Rename_File;
2261 procedure Rename_File
2262 (Old_Name : String;
2263 New_Name : String;
2264 Success : out Boolean)
2266 C_Old_Name : String (1 .. Old_Name'Length + 1);
2267 C_New_Name : String (1 .. New_Name'Length + 1);
2268 begin
2269 C_Old_Name (1 .. Old_Name'Length) := Old_Name;
2270 C_Old_Name (C_Old_Name'Last) := ASCII.NUL;
2271 C_New_Name (1 .. New_Name'Length) := New_Name;
2272 C_New_Name (C_New_Name'Last) := ASCII.NUL;
2273 Rename_File (C_Old_Name'Address, C_New_Name'Address, Success);
2274 end Rename_File;
2276 -----------------------
2277 -- Set_Close_On_Exec --
2278 -----------------------
2280 procedure Set_Close_On_Exec
2281 (FD : File_Descriptor;
2282 Close_On_Exec : Boolean;
2283 Status : out Boolean)
2285 function C_Set_Close_On_Exec
2286 (FD : File_Descriptor; Close_On_Exec : System.CRTL.int)
2287 return System.CRTL.int;
2288 pragma Import (C, C_Set_Close_On_Exec, "__gnat_set_close_on_exec");
2289 begin
2290 Status := C_Set_Close_On_Exec (FD, Boolean'Pos (Close_On_Exec)) = 0;
2291 end Set_Close_On_Exec;
2293 --------------------
2294 -- Set_Executable --
2295 --------------------
2297 procedure Set_Executable (Name : String) is
2298 procedure C_Set_Executable (Name : C_File_Name);
2299 pragma Import (C, C_Set_Executable, "__gnat_set_executable");
2300 C_Name : aliased String (Name'First .. Name'Last + 1);
2301 begin
2302 C_Name (Name'Range) := Name;
2303 C_Name (C_Name'Last) := ASCII.NUL;
2304 C_Set_Executable (C_Name (C_Name'First)'Address);
2305 end Set_Executable;
2307 ----------------------
2308 -- Set_Non_Readable --
2309 ----------------------
2311 procedure Set_Non_Readable (Name : String) is
2312 procedure C_Set_Non_Readable (Name : C_File_Name);
2313 pragma Import (C, C_Set_Non_Readable, "__gnat_set_non_readable");
2314 C_Name : aliased String (Name'First .. Name'Last + 1);
2315 begin
2316 C_Name (Name'Range) := Name;
2317 C_Name (C_Name'Last) := ASCII.NUL;
2318 C_Set_Non_Readable (C_Name (C_Name'First)'Address);
2319 end Set_Non_Readable;
2321 ----------------------
2322 -- Set_Non_Writable --
2323 ----------------------
2325 procedure Set_Non_Writable (Name : String) is
2326 procedure C_Set_Non_Writable (Name : C_File_Name);
2327 pragma Import (C, C_Set_Non_Writable, "__gnat_set_non_writable");
2328 C_Name : aliased String (Name'First .. Name'Last + 1);
2329 begin
2330 C_Name (Name'Range) := Name;
2331 C_Name (C_Name'Last) := ASCII.NUL;
2332 C_Set_Non_Writable (C_Name (C_Name'First)'Address);
2333 end Set_Non_Writable;
2335 ------------------
2336 -- Set_Readable --
2337 ------------------
2339 procedure Set_Readable (Name : String) is
2340 procedure C_Set_Readable (Name : C_File_Name);
2341 pragma Import (C, C_Set_Readable, "__gnat_set_readable");
2342 C_Name : aliased String (Name'First .. Name'Last + 1);
2343 begin
2344 C_Name (Name'Range) := Name;
2345 C_Name (C_Name'Last) := ASCII.NUL;
2346 C_Set_Readable (C_Name (C_Name'First)'Address);
2347 end Set_Readable;
2349 --------------------
2350 -- Set_Writable --
2351 --------------------
2353 procedure Set_Writable (Name : String) is
2354 procedure C_Set_Writable (Name : C_File_Name);
2355 pragma Import (C, C_Set_Writable, "__gnat_set_writable");
2356 C_Name : aliased String (Name'First .. Name'Last + 1);
2357 begin
2358 C_Name (Name'Range) := Name;
2359 C_Name (C_Name'Last) := ASCII.NUL;
2360 C_Set_Writable (C_Name (C_Name'First)'Address);
2361 end Set_Writable;
2363 ------------
2364 -- Setenv --
2365 ------------
2367 procedure Setenv (Name : String; Value : String) is
2368 F_Name : String (1 .. Name'Length + 1);
2369 F_Value : String (1 .. Value'Length + 1);
2371 procedure Set_Env_Value (Name, Value : System.Address);
2372 pragma Import (C, Set_Env_Value, "__gnat_setenv");
2374 begin
2375 F_Name (1 .. Name'Length) := Name;
2376 F_Name (F_Name'Last) := ASCII.NUL;
2378 F_Value (1 .. Value'Length) := Value;
2379 F_Value (F_Value'Last) := ASCII.NUL;
2381 Set_Env_Value (F_Name'Address, F_Value'Address);
2382 end Setenv;
2384 -----------
2385 -- Spawn --
2386 -----------
2388 function Spawn
2389 (Program_Name : String;
2390 Args : Argument_List) return Integer
2392 Result : Integer;
2393 Junk : Process_Id;
2394 pragma Warnings (Off, Junk);
2395 begin
2396 Spawn_Internal (Program_Name, Args, Result, Junk, Blocking => True);
2397 return Result;
2398 end Spawn;
2400 procedure Spawn
2401 (Program_Name : String;
2402 Args : Argument_List;
2403 Success : out Boolean)
2405 begin
2406 Success := (Spawn (Program_Name, Args) = 0);
2407 end Spawn;
2409 procedure Spawn
2410 (Program_Name : String;
2411 Args : Argument_List;
2412 Output_File_Descriptor : File_Descriptor;
2413 Return_Code : out Integer;
2414 Err_To_Out : Boolean := True)
2416 Saved_Output : File_Descriptor;
2417 Saved_Error : File_Descriptor := Invalid_FD; -- prevent compiler warning
2419 begin
2420 -- Set standard output and error to the temporary file
2422 Saved_Output := Dup (Standout);
2423 Dup2 (Output_File_Descriptor, Standout);
2425 if Err_To_Out then
2426 Saved_Error := Dup (Standerr);
2427 Dup2 (Output_File_Descriptor, Standerr);
2428 end if;
2430 -- Spawn the program
2432 Return_Code := Spawn (Program_Name, Args);
2434 -- Restore the standard output and error
2436 Dup2 (Saved_Output, Standout);
2438 if Err_To_Out then
2439 Dup2 (Saved_Error, Standerr);
2440 end if;
2442 -- And close the saved standard output and error file descriptors
2444 Close (Saved_Output);
2446 if Err_To_Out then
2447 Close (Saved_Error);
2448 end if;
2449 end Spawn;
2451 procedure Spawn
2452 (Program_Name : String;
2453 Args : Argument_List;
2454 Output_File : String;
2455 Success : out Boolean;
2456 Return_Code : out Integer;
2457 Err_To_Out : Boolean := True)
2459 FD : File_Descriptor;
2461 begin
2462 Success := True;
2463 Return_Code := 0;
2465 FD := Create_Output_Text_File (Output_File);
2467 if FD = Invalid_FD then
2468 Success := False;
2469 return;
2470 end if;
2472 Spawn (Program_Name, Args, FD, Return_Code, Err_To_Out);
2474 Close (FD, Success);
2475 end Spawn;
2477 --------------------
2478 -- Spawn_Internal --
2479 --------------------
2481 procedure Spawn_Internal
2482 (Program_Name : String;
2483 Args : Argument_List;
2484 Result : out Integer;
2485 Pid : out Process_Id;
2486 Blocking : Boolean)
2489 procedure Spawn (Args : Argument_List);
2490 -- Call Spawn with given argument list
2492 N_Args : Argument_List (Args'Range);
2493 -- Normalized arguments
2495 -----------
2496 -- Spawn --
2497 -----------
2499 procedure Spawn (Args : Argument_List) is
2500 type Chars is array (Positive range <>) of aliased Character;
2501 type Char_Ptr is access constant Character;
2503 Command_Len : constant Positive := Program_Name'Length + 1
2504 + Args_Length (Args);
2505 Command_Last : Natural := 0;
2506 Command : aliased Chars (1 .. Command_Len);
2507 -- Command contains all characters of the Program_Name and Args, all
2508 -- terminated by ASCII.NUL characters.
2510 Arg_List_Len : constant Positive := Args'Length + 2;
2511 Arg_List_Last : Natural := 0;
2512 Arg_List : aliased array (1 .. Arg_List_Len) of Char_Ptr;
2513 -- List with pointers to NUL-terminated strings of the Program_Name
2514 -- and the Args and terminated with a null pointer. We rely on the
2515 -- default initialization for the last null pointer.
2517 procedure Add_To_Command (S : String);
2518 -- Add S and a NUL character to Command, updating Last
2520 function Portable_Spawn (Args : Address) return Integer;
2521 pragma Import (C, Portable_Spawn, "__gnat_portable_spawn");
2523 function Portable_No_Block_Spawn (Args : Address) return Process_Id;
2524 pragma Import
2525 (C, Portable_No_Block_Spawn, "__gnat_portable_no_block_spawn");
2527 --------------------
2528 -- Add_To_Command --
2529 --------------------
2531 procedure Add_To_Command (S : String) is
2532 First : constant Natural := Command_Last + 1;
2534 begin
2535 Command_Last := Command_Last + S'Length;
2537 -- Move characters one at a time, because Command has aliased
2538 -- components.
2540 -- But not volatile, so why is this necessary ???
2542 for J in S'Range loop
2543 Command (First + J - S'First) := S (J);
2544 end loop;
2546 Command_Last := Command_Last + 1;
2547 Command (Command_Last) := ASCII.NUL;
2549 Arg_List_Last := Arg_List_Last + 1;
2550 Arg_List (Arg_List_Last) := Command (First)'Access;
2551 end Add_To_Command;
2553 -- Start of processing for Spawn
2555 begin
2556 Add_To_Command (Program_Name);
2558 for J in Args'Range loop
2559 Add_To_Command (Args (J).all);
2560 end loop;
2562 if Blocking then
2563 Pid := Invalid_Pid;
2564 Result := Portable_Spawn (Arg_List'Address);
2565 else
2566 Pid := Portable_No_Block_Spawn (Arg_List'Address);
2567 Result := Boolean'Pos (Pid /= Invalid_Pid);
2568 end if;
2569 end Spawn;
2571 -- Start of processing for Spawn_Internal
2573 begin
2574 -- Copy arguments into a local structure
2576 for K in N_Args'Range loop
2577 N_Args (K) := new String'(Args (K).all);
2578 end loop;
2580 -- Normalize those arguments
2582 Normalize_Arguments (N_Args);
2584 -- Call spawn using the normalized arguments
2586 Spawn (N_Args);
2588 -- Free arguments list
2590 for K in N_Args'Range loop
2591 Free (N_Args (K));
2592 end loop;
2593 end Spawn_Internal;
2595 ---------------------------
2596 -- To_Path_String_Access --
2597 ---------------------------
2599 function To_Path_String_Access
2600 (Path_Addr : Address;
2601 Path_Len : Integer) return String_Access
2603 subtype Path_String is String (1 .. Path_Len);
2604 type Path_String_Access is access Path_String;
2606 function Address_To_Access is new Ada.Unchecked_Conversion
2607 (Source => Address, Target => Path_String_Access);
2609 Path_Access : constant Path_String_Access :=
2610 Address_To_Access (Path_Addr);
2612 Return_Val : String_Access;
2614 begin
2615 Return_Val := new String (1 .. Path_Len);
2617 for J in 1 .. Path_Len loop
2618 Return_Val (J) := Path_Access (J);
2619 end loop;
2621 return Return_Val;
2622 end To_Path_String_Access;
2624 ------------------
2625 -- Wait_Process --
2626 ------------------
2628 procedure Wait_Process (Pid : out Process_Id; Success : out Boolean) is
2629 Status : Integer;
2631 function Portable_Wait (S : Address) return Process_Id;
2632 pragma Import (C, Portable_Wait, "__gnat_portable_wait");
2634 begin
2635 Pid := Portable_Wait (Status'Address);
2636 Success := (Status = 0);
2637 end Wait_Process;
2639 -----------
2640 -- Write --
2641 -----------
2643 function Write
2644 (FD : File_Descriptor;
2645 A : System.Address;
2646 N : Integer) return Integer
2648 begin
2649 return Integer (System.CRTL.write
2650 (System.CRTL.int (FD), System.CRTL.chars (A), System.CRTL.int (N)));
2651 end Write;
2653 end System.OS_Lib;