Update concepts branch to revision 131834
[official-gcc.git] / gcc / ada / s-os_lib.adb
blob5655b3c0d7c2b0a9810d9f379d12194f8ee896d6
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 Warnings (Off);
35 pragma Compiler_Unit;
36 pragma Warnings (On);
38 with System.Case_Util;
39 with System.CRTL;
40 with System.Soft_Links;
41 with Ada.Unchecked_Conversion;
42 with Ada.Unchecked_Deallocation;
43 with System; use System;
45 package body System.OS_Lib is
47 -- Imported procedures Dup and Dup2 are used in procedures Spawn and
48 -- Non_Blocking_Spawn.
50 function Dup (Fd : File_Descriptor) return File_Descriptor;
51 pragma Import (C, Dup, "__gnat_dup");
53 procedure Dup2 (Old_Fd, New_Fd : File_Descriptor);
54 pragma Import (C, Dup2, "__gnat_dup2");
56 On_Windows : constant Boolean := Directory_Separator = '\';
57 -- An indication that we are on Windows. Used in Normalize_Pathname, to
58 -- deal with drive letters in the beginning of absolute paths.
60 package SSL renames System.Soft_Links;
62 -- The following are used by Create_Temp_File
64 First_Temp_File_Name : constant String := "GNAT-TEMP-000000.TMP";
65 -- Used to initialize Current_Temp_File_Name and Temp_File_Name_Last_Digit
67 Current_Temp_File_Name : String := First_Temp_File_Name;
68 -- Name of the temp file last created
70 Temp_File_Name_Last_Digit : constant Positive :=
71 First_Temp_File_Name'Last - 4;
72 -- Position of the last digit in Current_Temp_File_Name
74 Max_Attempts : constant := 100;
75 -- The maximum number of attempts to create a new temp file
77 -----------------------
78 -- Local Subprograms --
79 -----------------------
81 function Args_Length (Args : Argument_List) return Natural;
82 -- Returns total number of characters needed to create a string
83 -- of all Args terminated by ASCII.NUL characters
85 function C_String_Length (S : Address) return Integer;
86 -- Returns the length of a C string. Does check for null address
87 -- (returns 0).
89 procedure Spawn_Internal
90 (Program_Name : String;
91 Args : Argument_List;
92 Result : out Integer;
93 Pid : out Process_Id;
94 Blocking : Boolean);
95 -- Internal routine to implement the two Spawn (blocking/non blocking)
96 -- routines. If Blocking is set to True then the spawn is blocking
97 -- otherwise it is non blocking. In this latter case the Pid contains the
98 -- process id number. The first three parameters are as in Spawn. Note that
99 -- Spawn_Internal normalizes the argument list before calling the low level
100 -- system spawn routines (see Normalize_Arguments).
102 -- Note: Normalize_Arguments is designed to do nothing if it is called more
103 -- than once, so calling Normalize_Arguments before calling one of the
104 -- spawn routines is fine.
106 function To_Path_String_Access
107 (Path_Addr : Address;
108 Path_Len : Integer) return String_Access;
109 -- Converts a C String to an Ada String. We could do this making use of
110 -- Interfaces.C.Strings but we prefer not to import that entire package
112 ---------
113 -- "<" --
114 ---------
116 function "<" (X, Y : OS_Time) return Boolean is
117 begin
118 return Long_Integer (X) < Long_Integer (Y);
119 end "<";
121 ----------
122 -- "<=" --
123 ----------
125 function "<=" (X, Y : OS_Time) return Boolean is
126 begin
127 return Long_Integer (X) <= Long_Integer (Y);
128 end "<=";
130 ---------
131 -- ">" --
132 ---------
134 function ">" (X, Y : OS_Time) return Boolean is
135 begin
136 return Long_Integer (X) > Long_Integer (Y);
137 end ">";
139 ----------
140 -- ">=" --
141 ----------
143 function ">=" (X, Y : OS_Time) return Boolean is
144 begin
145 return Long_Integer (X) >= Long_Integer (Y);
146 end ">=";
148 -----------------
149 -- Args_Length --
150 -----------------
152 function Args_Length (Args : Argument_List) return Natural is
153 Len : Natural := 0;
155 begin
156 for J in Args'Range loop
157 Len := Len + Args (J)'Length + 1; -- One extra for ASCII.NUL
158 end loop;
160 return Len;
161 end Args_Length;
163 -----------------------------
164 -- Argument_String_To_List --
165 -----------------------------
167 function Argument_String_To_List
168 (Arg_String : String) return Argument_List_Access
170 Max_Args : constant Integer := Arg_String'Length;
171 New_Argv : Argument_List (1 .. Max_Args);
172 New_Argc : Natural := 0;
173 Idx : Integer;
175 begin
176 Idx := Arg_String'First;
178 loop
179 exit when Idx > Arg_String'Last;
181 declare
182 Quoted : Boolean := False;
183 Backqd : Boolean := False;
184 Old_Idx : Integer;
186 begin
187 Old_Idx := Idx;
189 loop
190 -- An unquoted space is the end of an argument
192 if not (Backqd or Quoted)
193 and then Arg_String (Idx) = ' '
194 then
195 exit;
197 -- Start of a quoted string
199 elsif not (Backqd or Quoted)
200 and then Arg_String (Idx) = '"'
201 then
202 Quoted := True;
204 -- End of a quoted string and end of an argument
206 elsif (Quoted and not Backqd)
207 and then Arg_String (Idx) = '"'
208 then
209 Idx := Idx + 1;
210 exit;
212 -- Following character is backquoted
214 elsif Arg_String (Idx) = '\' then
215 Backqd := True;
217 -- Turn off backquoting after advancing one character
219 elsif Backqd then
220 Backqd := False;
222 end if;
224 Idx := Idx + 1;
225 exit when Idx > Arg_String'Last;
226 end loop;
228 -- Found an argument
230 New_Argc := New_Argc + 1;
231 New_Argv (New_Argc) :=
232 new String'(Arg_String (Old_Idx .. Idx - 1));
234 -- Skip extraneous spaces
236 while Idx <= Arg_String'Last and then Arg_String (Idx) = ' ' loop
237 Idx := Idx + 1;
238 end loop;
239 end;
240 end loop;
242 return new Argument_List'(New_Argv (1 .. New_Argc));
243 end Argument_String_To_List;
245 ---------------------
246 -- C_String_Length --
247 ---------------------
249 function C_String_Length (S : Address) return Integer is
250 function Strlen (S : Address) return Integer;
251 pragma Import (C, Strlen, "strlen");
252 begin
253 if S = Null_Address then
254 return 0;
255 else
256 return Strlen (S);
257 end if;
258 end C_String_Length;
260 -----------
261 -- Close --
262 -----------
264 procedure Close (FD : File_Descriptor) is
265 procedure C_Close (FD : File_Descriptor);
266 pragma Import (C, C_Close, "close");
267 begin
268 C_Close (FD);
269 end Close;
271 procedure Close (FD : File_Descriptor; Status : out Boolean) is
272 function C_Close (FD : File_Descriptor) return Integer;
273 pragma Import (C, C_Close, "close");
274 begin
275 Status := (C_Close (FD) = 0);
276 end Close;
278 ---------------
279 -- Copy_File --
280 ---------------
282 procedure Copy_File
283 (Name : String;
284 Pathname : String;
285 Success : out Boolean;
286 Mode : Copy_Mode := Copy;
287 Preserve : Attribute := Time_Stamps)
289 From : File_Descriptor;
290 To : File_Descriptor;
292 Copy_Error : exception;
293 -- Internal exception raised to signal error in copy
295 function Build_Path (Dir : String; File : String) return String;
296 -- Returns pathname Dir concatenated with File adding the directory
297 -- separator only if needed.
299 procedure Copy (From, To : File_Descriptor);
300 -- Read data from From and place them into To. In both cases the
301 -- operations uses the current file position. Raises Constraint_Error
302 -- if a problem occurs during the copy.
304 procedure Copy_To (To_Name : String);
305 -- Does a straight copy from source to designated destination file
307 ----------------
308 -- Build_Path --
309 ----------------
311 function Build_Path (Dir : String; File : String) return String is
312 Res : String (1 .. Dir'Length + File'Length + 1);
314 Base_File_Ptr : Integer;
315 -- The base file name is File (Base_File_Ptr + 1 .. File'Last)
317 function Is_Dirsep (C : Character) return Boolean;
318 pragma Inline (Is_Dirsep);
319 -- Returns True if C is a directory separator. On Windows we
320 -- handle both styles of directory separator.
322 ---------------
323 -- Is_Dirsep --
324 ---------------
326 function Is_Dirsep (C : Character) return Boolean is
327 begin
328 return C = Directory_Separator or else C = '/';
329 end Is_Dirsep;
331 -- Start of processing for Build_Path
333 begin
334 -- Find base file name
336 Base_File_Ptr := File'Last;
337 while Base_File_Ptr >= File'First loop
338 exit when Is_Dirsep (File (Base_File_Ptr));
339 Base_File_Ptr := Base_File_Ptr - 1;
340 end loop;
342 declare
343 Base_File : String renames
344 File (Base_File_Ptr + 1 .. File'Last);
346 begin
347 Res (1 .. Dir'Length) := Dir;
349 if Is_Dirsep (Dir (Dir'Last)) then
350 Res (Dir'Length + 1 .. Dir'Length + Base_File'Length) :=
351 Base_File;
352 return Res (1 .. Dir'Length + Base_File'Length);
354 else
355 Res (Dir'Length + 1) := Directory_Separator;
356 Res (Dir'Length + 2 .. Dir'Length + 1 + Base_File'Length) :=
357 Base_File;
358 return Res (1 .. Dir'Length + 1 + Base_File'Length);
359 end if;
360 end;
361 end Build_Path;
363 ----------
364 -- Copy --
365 ----------
367 procedure Copy (From, To : File_Descriptor) is
368 Buf_Size : constant := 200_000;
369 type Buf is array (1 .. Buf_Size) of Character;
370 type Buf_Ptr is access Buf;
372 Buffer : Buf_Ptr;
373 R : Integer;
374 W : Integer;
376 Status_From : Boolean;
377 Status_To : Boolean;
378 -- Statuses for the calls to Close
380 procedure Free is new Ada.Unchecked_Deallocation (Buf, Buf_Ptr);
382 begin
383 -- Check for invalid descriptors, making sure that we do not
384 -- accidentally leave an open file descriptor around.
386 if From = Invalid_FD then
387 if To /= Invalid_FD then
388 Close (To, Status_To);
389 end if;
391 raise Copy_Error;
393 elsif To = Invalid_FD then
394 Close (From, Status_From);
395 raise Copy_Error;
396 end if;
398 -- Allocate the buffer on the heap
400 Buffer := new Buf;
402 loop
403 R := Read (From, Buffer (1)'Address, Buf_Size);
405 -- For VMS, the buffer may not be full. So, we need to try again
406 -- until there is nothing to read.
408 exit when R = 0;
410 W := Write (To, Buffer (1)'Address, R);
412 if W < R then
414 -- Problem writing data, could be a disk full. Close files
415 -- without worrying about status, since we are raising a
416 -- Copy_Error exception in any case.
418 Close (From, Status_From);
419 Close (To, Status_To);
421 Free (Buffer);
423 raise Copy_Error;
424 end if;
425 end loop;
427 Close (From, Status_From);
428 Close (To, Status_To);
430 Free (Buffer);
432 if not (Status_From and Status_To) then
433 raise Copy_Error;
434 end if;
435 end Copy;
437 -------------
438 -- Copy_To --
439 -------------
441 procedure Copy_To (To_Name : String) is
443 function Copy_Attributes
444 (From, To : System.Address;
445 Mode : Integer) return Integer;
446 pragma Import (C, Copy_Attributes, "__gnat_copy_attribs");
447 -- Mode = 0 - copy only time stamps.
448 -- Mode = 1 - copy time stamps and read/write/execute attributes
450 C_From : String (1 .. Name'Length + 1);
451 C_To : String (1 .. To_Name'Length + 1);
453 begin
454 From := Open_Read (Name, Binary);
456 -- Do not clobber destination file if source file could not be opened
458 if From /= Invalid_FD then
459 To := Create_File (To_Name, Binary);
460 end if;
462 Copy (From, To);
464 -- Copy attributes
466 C_From (1 .. Name'Length) := Name;
467 C_From (C_From'Last) := ASCII.NUL;
469 C_To (1 .. To_Name'Length) := To_Name;
470 C_To (C_To'Last) := ASCII.NUL;
472 case Preserve is
474 when Time_Stamps =>
475 if Copy_Attributes (C_From'Address, C_To'Address, 0) = -1 then
476 raise Copy_Error;
477 end if;
479 when Full =>
480 if Copy_Attributes (C_From'Address, C_To'Address, 1) = -1 then
481 raise Copy_Error;
482 end if;
484 when None =>
485 null;
486 end case;
488 end Copy_To;
490 -- Start of processing for Copy_File
492 begin
493 Success := True;
495 -- The source file must exist
497 if not Is_Regular_File (Name) then
498 raise Copy_Error;
499 end if;
501 -- The source file exists
503 case Mode is
505 -- Copy case, target file must not exist
507 when Copy =>
509 -- If the target file exists, we have an error
511 if Is_Regular_File (Pathname) then
512 raise Copy_Error;
514 -- Case of target is a directory
516 elsif Is_Directory (Pathname) then
517 declare
518 Dest : constant String := Build_Path (Pathname, Name);
520 begin
521 -- If target file exists, we have an error, else do copy
523 if Is_Regular_File (Dest) then
524 raise Copy_Error;
525 else
526 Copy_To (Dest);
527 end if;
528 end;
530 -- Case of normal copy to file (destination does not exist)
532 else
533 Copy_To (Pathname);
534 end if;
536 -- Overwrite case (destination file may or may not exist)
538 when Overwrite =>
539 if Is_Directory (Pathname) then
540 Copy_To (Build_Path (Pathname, Name));
541 else
542 Copy_To (Pathname);
543 end if;
545 -- Append case (destination file may or may not exist)
547 when Append =>
549 -- Appending to existing file
551 if Is_Regular_File (Pathname) then
553 -- Append mode and destination file exists, append data at the
554 -- end of Pathname. But if we fail to open source file, do not
555 -- touch destination file at all.
557 From := Open_Read (Name, Binary);
558 if From /= Invalid_FD then
559 To := Open_Read_Write (Pathname, Binary);
560 end if;
562 Lseek (To, 0, Seek_End);
564 Copy (From, To);
566 -- Appending to directory, not allowed
568 elsif Is_Directory (Pathname) then
569 raise Copy_Error;
571 -- Appending when target file does not exist
573 else
574 Copy_To (Pathname);
575 end if;
576 end case;
578 -- All error cases are caught here
580 exception
581 when Copy_Error =>
582 Success := False;
583 end Copy_File;
585 procedure Copy_File
586 (Name : C_File_Name;
587 Pathname : C_File_Name;
588 Success : out Boolean;
589 Mode : Copy_Mode := Copy;
590 Preserve : Attribute := Time_Stamps)
592 Ada_Name : String_Access :=
593 To_Path_String_Access
594 (Name, C_String_Length (Name));
596 Ada_Pathname : String_Access :=
597 To_Path_String_Access
598 (Pathname, C_String_Length (Pathname));
600 begin
601 Copy_File (Ada_Name.all, Ada_Pathname.all, Success, Mode, Preserve);
602 Free (Ada_Name);
603 Free (Ada_Pathname);
604 end Copy_File;
606 ----------------------
607 -- Copy_Time_Stamps --
608 ----------------------
610 procedure Copy_Time_Stamps (Source, Dest : String; Success : out Boolean) is
612 function Copy_Attributes
613 (From, To : System.Address;
614 Mode : Integer) return Integer;
615 pragma Import (C, Copy_Attributes, "__gnat_copy_attribs");
616 -- Mode = 0 - copy only time stamps.
617 -- Mode = 1 - copy time stamps and read/write/execute attributes
619 begin
620 if Is_Regular_File (Source) and then Is_Writable_File (Dest) then
621 declare
622 C_Source : String (1 .. Source'Length + 1);
623 C_Dest : String (1 .. Dest'Length + 1);
624 begin
625 C_Source (1 .. Source'Length) := Source;
626 C_Source (C_Source'Last) := ASCII.NUL;
628 C_Dest (1 .. Dest'Length) := Dest;
629 C_Dest (C_Dest'Last) := ASCII.NUL;
631 if Copy_Attributes (C_Source'Address, C_Dest'Address, 0) = -1 then
632 Success := False;
633 else
634 Success := True;
635 end if;
636 end;
638 else
639 Success := False;
640 end if;
641 end Copy_Time_Stamps;
643 procedure Copy_Time_Stamps
644 (Source, Dest : C_File_Name;
645 Success : out Boolean)
647 Ada_Source : String_Access :=
648 To_Path_String_Access
649 (Source, C_String_Length (Source));
651 Ada_Dest : String_Access :=
652 To_Path_String_Access
653 (Dest, C_String_Length (Dest));
654 begin
655 Copy_Time_Stamps (Ada_Source.all, Ada_Dest.all, Success);
656 Free (Ada_Source);
657 Free (Ada_Dest);
658 end Copy_Time_Stamps;
660 -----------------
661 -- Create_File --
662 -----------------
664 function Create_File
665 (Name : C_File_Name;
666 Fmode : Mode) return File_Descriptor
668 function C_Create_File
669 (Name : C_File_Name;
670 Fmode : Mode) return File_Descriptor;
671 pragma Import (C, C_Create_File, "__gnat_open_create");
673 begin
674 return C_Create_File (Name, Fmode);
675 end Create_File;
677 function Create_File
678 (Name : String;
679 Fmode : Mode) return File_Descriptor
681 C_Name : String (1 .. Name'Length + 1);
683 begin
684 C_Name (1 .. Name'Length) := Name;
685 C_Name (C_Name'Last) := ASCII.NUL;
686 return Create_File (C_Name (C_Name'First)'Address, Fmode);
687 end Create_File;
689 ---------------------
690 -- Create_New_File --
691 ---------------------
693 function Create_New_File
694 (Name : C_File_Name;
695 Fmode : Mode) return File_Descriptor
697 function C_Create_New_File
698 (Name : C_File_Name;
699 Fmode : Mode) return File_Descriptor;
700 pragma Import (C, C_Create_New_File, "__gnat_open_new");
702 begin
703 return C_Create_New_File (Name, Fmode);
704 end Create_New_File;
706 function Create_New_File
707 (Name : String;
708 Fmode : Mode) return File_Descriptor
710 C_Name : String (1 .. Name'Length + 1);
712 begin
713 C_Name (1 .. Name'Length) := Name;
714 C_Name (C_Name'Last) := ASCII.NUL;
715 return Create_New_File (C_Name (C_Name'First)'Address, Fmode);
716 end Create_New_File;
718 -----------------------------
719 -- Create_Output_Text_File --
720 -----------------------------
722 function Create_Output_Text_File (Name : String) return File_Descriptor is
723 function C_Create_File
724 (Name : C_File_Name) return File_Descriptor;
725 pragma Import (C, C_Create_File, "__gnat_create_output_file");
727 C_Name : String (1 .. Name'Length + 1);
729 begin
730 C_Name (1 .. Name'Length) := Name;
731 C_Name (C_Name'Last) := ASCII.NUL;
732 return C_Create_File (C_Name (C_Name'First)'Address);
733 end Create_Output_Text_File;
735 ----------------------
736 -- Create_Temp_File --
737 ----------------------
739 procedure Create_Temp_File
740 (FD : out File_Descriptor;
741 Name : out Temp_File_Name)
743 function Open_New_Temp
744 (Name : System.Address;
745 Fmode : Mode) return File_Descriptor;
746 pragma Import (C, Open_New_Temp, "__gnat_open_new_temp");
748 begin
749 FD := Open_New_Temp (Name'Address, Binary);
750 end Create_Temp_File;
752 procedure Create_Temp_File
753 (FD : out File_Descriptor;
754 Name : out String_Access)
756 Pos : Positive;
757 Attempts : Natural := 0;
758 Current : String (Current_Temp_File_Name'Range);
760 begin
761 -- Loop until a new temp file can be created
763 File_Loop : loop
764 Locked : begin
765 -- We need to protect global variable Current_Temp_File_Name
766 -- against concurrent access by different tasks.
768 SSL.Lock_Task.all;
770 -- Start at the last digit
772 Pos := Temp_File_Name_Last_Digit;
774 Digit_Loop :
775 loop
776 -- Increment the digit by one
778 case Current_Temp_File_Name (Pos) is
779 when '0' .. '8' =>
780 Current_Temp_File_Name (Pos) :=
781 Character'Succ (Current_Temp_File_Name (Pos));
782 exit Digit_Loop;
784 when '9' =>
786 -- For 9, set the digit to 0 and go to the previous digit
788 Current_Temp_File_Name (Pos) := '0';
789 Pos := Pos - 1;
791 when others =>
793 -- If it is not a digit, then there are no available
794 -- temp file names. Return Invalid_FD. There is almost
795 -- no that this code will be ever be executed, since
796 -- it would mean that there are one million temp files
797 -- in the same directory!
799 SSL.Unlock_Task.all;
800 FD := Invalid_FD;
801 Name := null;
802 exit File_Loop;
803 end case;
804 end loop Digit_Loop;
806 Current := Current_Temp_File_Name;
808 -- We can now release the lock, because we are no longer
809 -- accessing Current_Temp_File_Name.
811 SSL.Unlock_Task.all;
813 exception
814 when others =>
815 SSL.Unlock_Task.all;
816 raise;
817 end Locked;
819 -- Attempt to create the file
821 FD := Create_New_File (Current, Binary);
823 if FD /= Invalid_FD then
824 Name := new String'(Current);
825 exit File_Loop;
826 end if;
828 if not Is_Regular_File (Current) then
830 -- If the file does not already exist and we are unable to create
831 -- it, we give up after Max_Attempts. Otherwise, we try again with
832 -- the next available file name.
834 Attempts := Attempts + 1;
836 if Attempts >= Max_Attempts then
837 FD := Invalid_FD;
838 Name := null;
839 exit File_Loop;
840 end if;
841 end if;
842 end loop File_Loop;
843 end Create_Temp_File;
845 -----------------
846 -- Delete_File --
847 -----------------
849 procedure Delete_File (Name : Address; Success : out Boolean) is
850 R : Integer;
852 function unlink (A : Address) return Integer;
853 pragma Import (C, unlink, "unlink");
855 begin
856 R := unlink (Name);
857 Success := (R = 0);
858 end Delete_File;
860 procedure Delete_File (Name : String; Success : out Boolean) is
861 C_Name : String (1 .. Name'Length + 1);
863 begin
864 C_Name (1 .. Name'Length) := Name;
865 C_Name (C_Name'Last) := ASCII.NUL;
867 Delete_File (C_Name'Address, Success);
868 end Delete_File;
870 ---------------------
871 -- File_Time_Stamp --
872 ---------------------
874 function File_Time_Stamp (FD : File_Descriptor) return OS_Time is
875 function File_Time (FD : File_Descriptor) return OS_Time;
876 pragma Import (C, File_Time, "__gnat_file_time_fd");
877 begin
878 return File_Time (FD);
879 end File_Time_Stamp;
881 function File_Time_Stamp (Name : C_File_Name) return OS_Time is
882 function File_Time (Name : Address) return OS_Time;
883 pragma Import (C, File_Time, "__gnat_file_time_name");
884 begin
885 return File_Time (Name);
886 end File_Time_Stamp;
888 function File_Time_Stamp (Name : String) return OS_Time is
889 F_Name : String (1 .. Name'Length + 1);
890 begin
891 F_Name (1 .. Name'Length) := Name;
892 F_Name (F_Name'Last) := ASCII.NUL;
893 return File_Time_Stamp (F_Name'Address);
894 end File_Time_Stamp;
896 ---------------------------
897 -- Get_Debuggable_Suffix --
898 ---------------------------
900 function Get_Debuggable_Suffix return String_Access is
901 procedure Get_Suffix_Ptr (Length, Ptr : Address);
902 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_debuggable_suffix_ptr");
904 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
905 pragma Import (C, Strncpy, "strncpy");
907 Suffix_Ptr : Address;
908 Suffix_Length : Integer;
909 Result : String_Access;
911 begin
912 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
914 Result := new String (1 .. Suffix_Length);
916 if Suffix_Length > 0 then
917 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
918 end if;
920 return Result;
921 end Get_Debuggable_Suffix;
923 ---------------------------
924 -- Get_Executable_Suffix --
925 ---------------------------
927 function Get_Executable_Suffix return String_Access is
928 procedure Get_Suffix_Ptr (Length, Ptr : Address);
929 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_executable_suffix_ptr");
931 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
932 pragma Import (C, Strncpy, "strncpy");
934 Suffix_Ptr : Address;
935 Suffix_Length : Integer;
936 Result : String_Access;
938 begin
939 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
941 Result := new String (1 .. Suffix_Length);
943 if Suffix_Length > 0 then
944 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
945 end if;
947 return Result;
948 end Get_Executable_Suffix;
950 -----------------------
951 -- Get_Object_Suffix --
952 -----------------------
954 function Get_Object_Suffix return String_Access is
955 procedure Get_Suffix_Ptr (Length, Ptr : Address);
956 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_object_suffix_ptr");
958 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
959 pragma Import (C, Strncpy, "strncpy");
961 Suffix_Ptr : Address;
962 Suffix_Length : Integer;
963 Result : String_Access;
965 begin
966 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
968 Result := new String (1 .. Suffix_Length);
970 if Suffix_Length > 0 then
971 Strncpy (Result.all'Address, Suffix_Ptr, Suffix_Length);
972 end if;
974 return Result;
975 end Get_Object_Suffix;
977 ----------------------------------
978 -- Get_Target_Debuggable_Suffix --
979 ----------------------------------
981 function Get_Target_Debuggable_Suffix return String_Access is
982 Target_Exec_Ext_Ptr : Address;
983 pragma Import
984 (C, Target_Exec_Ext_Ptr, "__gnat_target_debuggable_extension");
986 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
987 pragma Import (C, Strncpy, "strncpy");
989 function Strlen (Cstring : Address) return Integer;
990 pragma Import (C, Strlen, "strlen");
992 Suffix_Length : Integer;
993 Result : String_Access;
995 begin
996 Suffix_Length := Strlen (Target_Exec_Ext_Ptr);
998 Result := new String (1 .. Suffix_Length);
1000 if Suffix_Length > 0 then
1001 Strncpy (Result.all'Address, Target_Exec_Ext_Ptr, Suffix_Length);
1002 end if;
1004 return Result;
1005 end Get_Target_Debuggable_Suffix;
1007 ----------------------------------
1008 -- Get_Target_Executable_Suffix --
1009 ----------------------------------
1011 function Get_Target_Executable_Suffix return String_Access is
1012 Target_Exec_Ext_Ptr : Address;
1013 pragma Import
1014 (C, Target_Exec_Ext_Ptr, "__gnat_target_executable_extension");
1016 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1017 pragma Import (C, Strncpy, "strncpy");
1019 function Strlen (Cstring : Address) return Integer;
1020 pragma Import (C, Strlen, "strlen");
1022 Suffix_Length : Integer;
1023 Result : String_Access;
1025 begin
1026 Suffix_Length := Strlen (Target_Exec_Ext_Ptr);
1028 Result := new String (1 .. Suffix_Length);
1030 if Suffix_Length > 0 then
1031 Strncpy (Result.all'Address, Target_Exec_Ext_Ptr, Suffix_Length);
1032 end if;
1034 return Result;
1035 end Get_Target_Executable_Suffix;
1037 ------------------------------
1038 -- Get_Target_Object_Suffix --
1039 ------------------------------
1041 function Get_Target_Object_Suffix return String_Access is
1042 Target_Object_Ext_Ptr : Address;
1043 pragma Import
1044 (C, Target_Object_Ext_Ptr, "__gnat_target_object_extension");
1046 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1047 pragma Import (C, Strncpy, "strncpy");
1049 function Strlen (Cstring : Address) return Integer;
1050 pragma Import (C, Strlen, "strlen");
1052 Suffix_Length : Integer;
1053 Result : String_Access;
1055 begin
1056 Suffix_Length := Strlen (Target_Object_Ext_Ptr);
1058 Result := new String (1 .. Suffix_Length);
1060 if Suffix_Length > 0 then
1061 Strncpy (Result.all'Address, Target_Object_Ext_Ptr, Suffix_Length);
1062 end if;
1064 return Result;
1065 end Get_Target_Object_Suffix;
1067 ------------
1068 -- Getenv --
1069 ------------
1071 function Getenv (Name : String) return String_Access is
1072 procedure Get_Env_Value_Ptr (Name, Length, Ptr : Address);
1073 pragma Import (C, Get_Env_Value_Ptr, "__gnat_getenv");
1075 procedure Strncpy (Astring_Addr, Cstring : Address; N : Integer);
1076 pragma Import (C, Strncpy, "strncpy");
1078 Env_Value_Ptr : aliased Address;
1079 Env_Value_Length : aliased Integer;
1080 F_Name : aliased String (1 .. Name'Length + 1);
1081 Result : String_Access;
1083 begin
1084 F_Name (1 .. Name'Length) := Name;
1085 F_Name (F_Name'Last) := ASCII.NUL;
1087 Get_Env_Value_Ptr
1088 (F_Name'Address, Env_Value_Length'Address, Env_Value_Ptr'Address);
1090 Result := new String (1 .. Env_Value_Length);
1092 if Env_Value_Length > 0 then
1093 Strncpy (Result.all'Address, Env_Value_Ptr, Env_Value_Length);
1094 end if;
1096 return Result;
1097 end Getenv;
1099 ------------
1100 -- GM_Day --
1101 ------------
1103 function GM_Day (Date : OS_Time) return Day_Type is
1104 D : Day_Type;
1106 pragma Warnings (Off);
1107 Y : Year_Type;
1108 Mo : Month_Type;
1109 H : Hour_Type;
1110 Mn : Minute_Type;
1111 S : Second_Type;
1112 pragma Warnings (On);
1114 begin
1115 GM_Split (Date, Y, Mo, D, H, Mn, S);
1116 return D;
1117 end GM_Day;
1119 -------------
1120 -- GM_Hour --
1121 -------------
1123 function GM_Hour (Date : OS_Time) return Hour_Type is
1124 H : Hour_Type;
1126 pragma Warnings (Off);
1127 Y : Year_Type;
1128 Mo : Month_Type;
1129 D : Day_Type;
1130 Mn : Minute_Type;
1131 S : Second_Type;
1132 pragma Warnings (On);
1134 begin
1135 GM_Split (Date, Y, Mo, D, H, Mn, S);
1136 return H;
1137 end GM_Hour;
1139 ---------------
1140 -- GM_Minute --
1141 ---------------
1143 function GM_Minute (Date : OS_Time) return Minute_Type is
1144 Mn : Minute_Type;
1146 pragma Warnings (Off);
1147 Y : Year_Type;
1148 Mo : Month_Type;
1149 D : Day_Type;
1150 H : Hour_Type;
1151 S : Second_Type;
1152 pragma Warnings (On);
1154 begin
1155 GM_Split (Date, Y, Mo, D, H, Mn, S);
1156 return Mn;
1157 end GM_Minute;
1159 --------------
1160 -- GM_Month --
1161 --------------
1163 function GM_Month (Date : OS_Time) return Month_Type is
1164 Mo : Month_Type;
1166 pragma Warnings (Off);
1167 Y : Year_Type;
1168 D : Day_Type;
1169 H : Hour_Type;
1170 Mn : Minute_Type;
1171 S : Second_Type;
1172 pragma Warnings (On);
1174 begin
1175 GM_Split (Date, Y, Mo, D, H, Mn, S);
1176 return Mo;
1177 end GM_Month;
1179 ---------------
1180 -- GM_Second --
1181 ---------------
1183 function GM_Second (Date : OS_Time) return Second_Type is
1184 S : Second_Type;
1186 pragma Warnings (Off);
1187 Y : Year_Type;
1188 Mo : Month_Type;
1189 D : Day_Type;
1190 H : Hour_Type;
1191 Mn : Minute_Type;
1192 pragma Warnings (On);
1194 begin
1195 GM_Split (Date, Y, Mo, D, H, Mn, S);
1196 return S;
1197 end GM_Second;
1199 --------------
1200 -- GM_Split --
1201 --------------
1203 procedure GM_Split
1204 (Date : OS_Time;
1205 Year : out Year_Type;
1206 Month : out Month_Type;
1207 Day : out Day_Type;
1208 Hour : out Hour_Type;
1209 Minute : out Minute_Type;
1210 Second : out Second_Type)
1212 procedure To_GM_Time
1213 (P_Time_T, P_Year, P_Month, P_Day, P_Hours, P_Mins, P_Secs : Address);
1214 pragma Import (C, To_GM_Time, "__gnat_to_gm_time");
1216 T : OS_Time := Date;
1217 Y : Integer;
1218 Mo : Integer;
1219 D : Integer;
1220 H : Integer;
1221 Mn : Integer;
1222 S : Integer;
1224 begin
1225 -- Use the global lock because To_GM_Time is not thread safe
1227 Locked_Processing : begin
1228 SSL.Lock_Task.all;
1229 To_GM_Time
1230 (T'Address, Y'Address, Mo'Address, D'Address,
1231 H'Address, Mn'Address, S'Address);
1232 SSL.Unlock_Task.all;
1234 exception
1235 when others =>
1236 SSL.Unlock_Task.all;
1237 raise;
1238 end Locked_Processing;
1240 Year := Y + 1900;
1241 Month := Mo + 1;
1242 Day := D;
1243 Hour := H;
1244 Minute := Mn;
1245 Second := S;
1246 end GM_Split;
1248 -------------
1249 -- GM_Year --
1250 -------------
1252 function GM_Year (Date : OS_Time) return Year_Type is
1253 Y : Year_Type;
1255 pragma Warnings (Off);
1256 Mo : Month_Type;
1257 D : Day_Type;
1258 H : Hour_Type;
1259 Mn : Minute_Type;
1260 S : Second_Type;
1261 pragma Warnings (On);
1263 begin
1264 GM_Split (Date, Y, Mo, D, H, Mn, S);
1265 return Y;
1266 end GM_Year;
1268 ----------------------
1269 -- Is_Absolute_Path --
1270 ----------------------
1272 function Is_Absolute_Path (Name : String) return Boolean is
1273 function Is_Absolute_Path
1274 (Name : Address;
1275 Length : Integer) return Integer;
1276 pragma Import (C, Is_Absolute_Path, "__gnat_is_absolute_path");
1277 begin
1278 return Is_Absolute_Path (Name'Address, Name'Length) /= 0;
1279 end Is_Absolute_Path;
1281 ------------------
1282 -- Is_Directory --
1283 ------------------
1285 function Is_Directory (Name : C_File_Name) return Boolean is
1286 function Is_Directory (Name : Address) return Integer;
1287 pragma Import (C, Is_Directory, "__gnat_is_directory");
1288 begin
1289 return Is_Directory (Name) /= 0;
1290 end Is_Directory;
1292 function Is_Directory (Name : String) return Boolean is
1293 F_Name : String (1 .. Name'Length + 1);
1294 begin
1295 F_Name (1 .. Name'Length) := Name;
1296 F_Name (F_Name'Last) := ASCII.NUL;
1297 return Is_Directory (F_Name'Address);
1298 end Is_Directory;
1300 ----------------------
1301 -- Is_Readable_File --
1302 ----------------------
1304 function Is_Readable_File (Name : C_File_Name) return Boolean is
1305 function Is_Readable_File (Name : Address) return Integer;
1306 pragma Import (C, Is_Readable_File, "__gnat_is_readable_file");
1307 begin
1308 return Is_Readable_File (Name) /= 0;
1309 end Is_Readable_File;
1311 function Is_Readable_File (Name : String) return Boolean is
1312 F_Name : String (1 .. Name'Length + 1);
1313 begin
1314 F_Name (1 .. Name'Length) := Name;
1315 F_Name (F_Name'Last) := ASCII.NUL;
1316 return Is_Readable_File (F_Name'Address);
1317 end Is_Readable_File;
1319 ---------------------
1320 -- Is_Regular_File --
1321 ---------------------
1323 function Is_Regular_File (Name : C_File_Name) return Boolean is
1324 function Is_Regular_File (Name : Address) return Integer;
1325 pragma Import (C, Is_Regular_File, "__gnat_is_regular_file");
1326 begin
1327 return Is_Regular_File (Name) /= 0;
1328 end Is_Regular_File;
1330 function Is_Regular_File (Name : String) return Boolean is
1331 F_Name : String (1 .. Name'Length + 1);
1332 begin
1333 F_Name (1 .. Name'Length) := Name;
1334 F_Name (F_Name'Last) := ASCII.NUL;
1335 return Is_Regular_File (F_Name'Address);
1336 end Is_Regular_File;
1338 ----------------------
1339 -- Is_Symbolic_Link --
1340 ----------------------
1342 function Is_Symbolic_Link (Name : C_File_Name) return Boolean is
1343 function Is_Symbolic_Link (Name : Address) return Integer;
1344 pragma Import (C, Is_Symbolic_Link, "__gnat_is_symbolic_link");
1345 begin
1346 return Is_Symbolic_Link (Name) /= 0;
1347 end Is_Symbolic_Link;
1349 function Is_Symbolic_Link (Name : String) return Boolean is
1350 F_Name : String (1 .. Name'Length + 1);
1351 begin
1352 F_Name (1 .. Name'Length) := Name;
1353 F_Name (F_Name'Last) := ASCII.NUL;
1354 return Is_Symbolic_Link (F_Name'Address);
1355 end Is_Symbolic_Link;
1357 ----------------------
1358 -- Is_Writable_File --
1359 ----------------------
1361 function Is_Writable_File (Name : C_File_Name) return Boolean is
1362 function Is_Writable_File (Name : Address) return Integer;
1363 pragma Import (C, Is_Writable_File, "__gnat_is_writable_file");
1364 begin
1365 return Is_Writable_File (Name) /= 0;
1366 end Is_Writable_File;
1368 function Is_Writable_File (Name : String) return Boolean is
1369 F_Name : String (1 .. Name'Length + 1);
1370 begin
1371 F_Name (1 .. Name'Length) := Name;
1372 F_Name (F_Name'Last) := ASCII.NUL;
1373 return Is_Writable_File (F_Name'Address);
1374 end Is_Writable_File;
1376 -------------------------
1377 -- Locate_Exec_On_Path --
1378 -------------------------
1380 function Locate_Exec_On_Path
1381 (Exec_Name : String) return String_Access
1383 function Locate_Exec_On_Path (C_Exec_Name : Address) return Address;
1384 pragma Import (C, Locate_Exec_On_Path, "__gnat_locate_exec_on_path");
1386 procedure Free (Ptr : System.Address);
1387 pragma Import (C, Free, "free");
1389 C_Exec_Name : String (1 .. Exec_Name'Length + 1);
1390 Path_Addr : Address;
1391 Path_Len : Integer;
1392 Result : String_Access;
1394 begin
1395 C_Exec_Name (1 .. Exec_Name'Length) := Exec_Name;
1396 C_Exec_Name (C_Exec_Name'Last) := ASCII.NUL;
1398 Path_Addr := Locate_Exec_On_Path (C_Exec_Name'Address);
1399 Path_Len := C_String_Length (Path_Addr);
1401 if Path_Len = 0 then
1402 return null;
1404 else
1405 Result := To_Path_String_Access (Path_Addr, Path_Len);
1406 Free (Path_Addr);
1408 -- Always return an absolute path name
1410 if not Is_Absolute_Path (Result.all) then
1411 declare
1412 Absolute_Path : constant String :=
1413 Normalize_Pathname (Result.all);
1414 begin
1415 Free (Result);
1416 Result := new String'(Absolute_Path);
1417 end;
1418 end if;
1420 return Result;
1421 end if;
1422 end Locate_Exec_On_Path;
1424 -------------------------
1425 -- Locate_Regular_File --
1426 -------------------------
1428 function Locate_Regular_File
1429 (File_Name : C_File_Name;
1430 Path : C_File_Name) return String_Access
1432 function Locate_Regular_File
1433 (C_File_Name, Path_Val : Address) return Address;
1434 pragma Import (C, Locate_Regular_File, "__gnat_locate_regular_file");
1436 procedure Free (Ptr : System.Address);
1437 pragma Import (C, Free, "free");
1439 Path_Addr : Address;
1440 Path_Len : Integer;
1441 Result : String_Access;
1443 begin
1444 Path_Addr := Locate_Regular_File (File_Name, Path);
1445 Path_Len := C_String_Length (Path_Addr);
1447 if Path_Len = 0 then
1448 return null;
1449 else
1450 Result := To_Path_String_Access (Path_Addr, Path_Len);
1451 Free (Path_Addr);
1452 return Result;
1453 end if;
1454 end Locate_Regular_File;
1456 function Locate_Regular_File
1457 (File_Name : String;
1458 Path : String) return String_Access
1460 C_File_Name : String (1 .. File_Name'Length + 1);
1461 C_Path : String (1 .. Path'Length + 1);
1462 Result : String_Access;
1464 begin
1465 C_File_Name (1 .. File_Name'Length) := File_Name;
1466 C_File_Name (C_File_Name'Last) := ASCII.NUL;
1468 C_Path (1 .. Path'Length) := Path;
1469 C_Path (C_Path'Last) := ASCII.NUL;
1471 Result := Locate_Regular_File (C_File_Name'Address, C_Path'Address);
1473 -- Always return an absolute path name
1475 if Result /= null and then not Is_Absolute_Path (Result.all) then
1476 declare
1477 Absolute_Path : constant String := Normalize_Pathname (Result.all);
1478 begin
1479 Free (Result);
1480 Result := new String'(Absolute_Path);
1481 end;
1482 end if;
1484 return Result;
1485 end Locate_Regular_File;
1487 ------------------------
1488 -- Non_Blocking_Spawn --
1489 ------------------------
1491 function Non_Blocking_Spawn
1492 (Program_Name : String;
1493 Args : Argument_List) return Process_Id
1495 Pid : Process_Id;
1496 Junk : Integer;
1497 pragma Warnings (Off, Junk);
1498 begin
1499 Spawn_Internal (Program_Name, Args, Junk, Pid, Blocking => False);
1500 return Pid;
1501 end Non_Blocking_Spawn;
1503 function Non_Blocking_Spawn
1504 (Program_Name : String;
1505 Args : Argument_List;
1506 Output_File_Descriptor : File_Descriptor;
1507 Err_To_Out : Boolean := True) return Process_Id
1509 Saved_Output : File_Descriptor;
1510 Saved_Error : File_Descriptor := Invalid_FD; -- prevent warning
1511 Pid : Process_Id;
1513 begin
1514 if Output_File_Descriptor = Invalid_FD then
1515 return Invalid_Pid;
1516 end if;
1518 -- Set standard output and, if specified, error to the temporary file
1520 Saved_Output := Dup (Standout);
1521 Dup2 (Output_File_Descriptor, Standout);
1523 if Err_To_Out then
1524 Saved_Error := Dup (Standerr);
1525 Dup2 (Output_File_Descriptor, Standerr);
1526 end if;
1528 -- Spawn the program
1530 Pid := Non_Blocking_Spawn (Program_Name, Args);
1532 -- Restore the standard output and error
1534 Dup2 (Saved_Output, Standout);
1536 if Err_To_Out then
1537 Dup2 (Saved_Error, Standerr);
1538 end if;
1540 -- And close the saved standard output and error file descriptors
1542 Close (Saved_Output);
1544 if Err_To_Out then
1545 Close (Saved_Error);
1546 end if;
1548 return Pid;
1549 end Non_Blocking_Spawn;
1551 function Non_Blocking_Spawn
1552 (Program_Name : String;
1553 Args : Argument_List;
1554 Output_File : String;
1555 Err_To_Out : Boolean := True) return Process_Id
1557 Output_File_Descriptor : constant File_Descriptor :=
1558 Create_Output_Text_File (Output_File);
1559 Result : Process_Id;
1561 begin
1562 -- Do not attempt to spawn if the output file could not be created
1564 if Output_File_Descriptor = Invalid_FD then
1565 return Invalid_Pid;
1567 else
1568 Result := Non_Blocking_Spawn
1569 (Program_Name, Args, Output_File_Descriptor, Err_To_Out);
1571 -- Close the file just created for the output, as the file descriptor
1572 -- cannot be used anywhere, being a local value. It is safe to do
1573 -- that, as the file descriptor has been duplicated to form
1574 -- standard output and error of the spawned process.
1576 Close (Output_File_Descriptor);
1578 return Result;
1579 end if;
1580 end Non_Blocking_Spawn;
1582 -------------------------
1583 -- Normalize_Arguments --
1584 -------------------------
1586 procedure Normalize_Arguments (Args : in out Argument_List) is
1588 procedure Quote_Argument (Arg : in out String_Access);
1589 -- Add quote around argument if it contains spaces
1591 C_Argument_Needs_Quote : Integer;
1592 pragma Import (C, C_Argument_Needs_Quote, "__gnat_argument_needs_quote");
1593 Argument_Needs_Quote : constant Boolean := C_Argument_Needs_Quote /= 0;
1595 --------------------
1596 -- Quote_Argument --
1597 --------------------
1599 procedure Quote_Argument (Arg : in out String_Access) is
1600 Res : String (1 .. Arg'Length * 2);
1601 J : Positive := 1;
1602 Quote_Needed : Boolean := False;
1604 begin
1605 if Arg (Arg'First) /= '"' or else Arg (Arg'Last) /= '"' then
1607 -- Starting quote
1609 Res (J) := '"';
1611 for K in Arg'Range loop
1613 J := J + 1;
1615 if Arg (K) = '"' then
1616 Res (J) := '\';
1617 J := J + 1;
1618 Res (J) := '"';
1619 Quote_Needed := True;
1621 elsif Arg (K) = ' ' then
1622 Res (J) := Arg (K);
1623 Quote_Needed := True;
1625 else
1626 Res (J) := Arg (K);
1627 end if;
1629 end loop;
1631 if Quote_Needed then
1633 -- If null terminated string, put the quote before
1635 if Res (J) = ASCII.NUL then
1636 Res (J) := '"';
1637 J := J + 1;
1638 Res (J) := ASCII.NUL;
1640 -- If argument is terminated by '\', then double it. Otherwise
1641 -- the ending quote will be taken as-is. This is quite strange
1642 -- spawn behavior from Windows, but this is what we see!
1644 else
1645 if Res (J) = '\' then
1646 J := J + 1;
1647 Res (J) := '\';
1648 end if;
1650 -- Ending quote
1652 J := J + 1;
1653 Res (J) := '"';
1654 end if;
1656 declare
1657 Old : String_Access := Arg;
1659 begin
1660 Arg := new String'(Res (1 .. J));
1661 Free (Old);
1662 end;
1663 end if;
1665 end if;
1666 end Quote_Argument;
1668 -- Start of processing for Normalize_Arguments
1670 begin
1671 if Argument_Needs_Quote then
1672 for K in Args'Range loop
1673 if Args (K) /= null and then Args (K)'Length /= 0 then
1674 Quote_Argument (Args (K));
1675 end if;
1676 end loop;
1677 end if;
1678 end Normalize_Arguments;
1680 ------------------------
1681 -- Normalize_Pathname --
1682 ------------------------
1684 function Normalize_Pathname
1685 (Name : String;
1686 Directory : String := "";
1687 Resolve_Links : Boolean := True;
1688 Case_Sensitive : Boolean := True) return String
1690 Max_Path : Integer;
1691 pragma Import (C, Max_Path, "__gnat_max_path_len");
1692 -- Maximum length of a path name
1694 procedure Get_Current_Dir
1695 (Dir : System.Address;
1696 Length : System.Address);
1697 pragma Import (C, Get_Current_Dir, "__gnat_get_current_dir");
1699 Path_Buffer : String (1 .. Max_Path + Max_Path + 2);
1700 End_Path : Natural := 0;
1701 Link_Buffer : String (1 .. Max_Path + 2);
1702 Status : Integer;
1703 Last : Positive;
1704 Start : Natural;
1705 Finish : Positive;
1707 Max_Iterations : constant := 500;
1709 function Get_File_Names_Case_Sensitive return Integer;
1710 pragma Import
1711 (C, Get_File_Names_Case_Sensitive,
1712 "__gnat_get_file_names_case_sensitive");
1714 Fold_To_Lower_Case : constant Boolean :=
1715 not Case_Sensitive
1716 and then Get_File_Names_Case_Sensitive = 0;
1718 function Readlink
1719 (Path : System.Address;
1720 Buf : System.Address;
1721 Bufsiz : Integer) return Integer;
1722 pragma Import (C, Readlink, "__gnat_readlink");
1724 function To_Canonical_File_Spec
1725 (Host_File : System.Address) return System.Address;
1726 pragma Import
1727 (C, To_Canonical_File_Spec, "__gnat_to_canonical_file_spec");
1729 The_Name : String (1 .. Name'Length + 1);
1730 Canonical_File_Addr : System.Address;
1731 Canonical_File_Len : Integer;
1733 function Strlen (S : System.Address) return Integer;
1734 pragma Import (C, Strlen, "strlen");
1736 function Final_Value (S : String) return String;
1737 -- Make final adjustment to the returned string. This function strips
1738 -- trailing directory separators, and folds returned string to lower
1739 -- case if required.
1741 function Get_Directory (Dir : String) return String;
1742 -- If Dir is not empty, return it, adding a directory separator
1743 -- if not already present, otherwise return current working directory
1744 -- with terminating directory separator.
1746 -----------------
1747 -- Final_Value --
1748 -----------------
1750 function Final_Value (S : String) return String is
1751 S1 : String := S;
1752 -- We may need to fold S to lower case, so we need a variable
1754 Last : Natural;
1756 begin
1757 if Fold_To_Lower_Case then
1758 System.Case_Util.To_Lower (S1);
1759 end if;
1761 -- Remove trailing directory separator, if any
1763 Last := S1'Last;
1765 if Last > 1
1766 and then (S1 (Last) = '/'
1767 or else
1768 S1 (Last) = Directory_Separator)
1769 then
1770 -- Special case for Windows: C:\
1772 if Last = 3
1773 and then S1 (1) /= Directory_Separator
1774 and then S1 (2) = ':'
1775 then
1776 null;
1778 else
1779 Last := Last - 1;
1780 end if;
1781 end if;
1783 return S1 (1 .. Last);
1784 end Final_Value;
1786 -------------------
1787 -- Get_Directory --
1788 -------------------
1790 function Get_Directory (Dir : String) return String is
1791 begin
1792 -- Directory given, add directory separator if needed
1794 if Dir'Length > 0 then
1795 if Dir (Dir'Last) = Directory_Separator then
1796 return Dir;
1797 else
1798 declare
1799 Result : String (1 .. Dir'Length + 1);
1800 begin
1801 Result (1 .. Dir'Length) := Dir;
1802 Result (Result'Length) := Directory_Separator;
1803 return Result;
1804 end;
1805 end if;
1807 -- Directory name not given, get current directory
1809 else
1810 declare
1811 Buffer : String (1 .. Max_Path + 2);
1812 Path_Len : Natural := Max_Path;
1814 begin
1815 Get_Current_Dir (Buffer'Address, Path_Len'Address);
1817 if Buffer (Path_Len) /= Directory_Separator then
1818 Path_Len := Path_Len + 1;
1819 Buffer (Path_Len) := Directory_Separator;
1820 end if;
1822 -- By default, the drive letter on Windows is in upper case
1824 if On_Windows and then Path_Len >= 2 and then
1825 Buffer (2) = ':'
1826 then
1827 System.Case_Util.To_Upper (Buffer (1 .. 1));
1828 end if;
1830 return Buffer (1 .. Path_Len);
1831 end;
1832 end if;
1833 end Get_Directory;
1835 -- Start of processing for Normalize_Pathname
1837 begin
1838 -- Special case, if name is null, then return null
1840 if Name'Length = 0 then
1841 return "";
1842 end if;
1844 -- First, convert VMS file spec to Unix file spec.
1845 -- If Name is not in VMS syntax, then this is equivalent
1846 -- to put Name at the beginning of Path_Buffer.
1848 VMS_Conversion : begin
1849 The_Name (1 .. Name'Length) := Name;
1850 The_Name (The_Name'Last) := ASCII.NUL;
1852 Canonical_File_Addr := To_Canonical_File_Spec (The_Name'Address);
1853 Canonical_File_Len := Strlen (Canonical_File_Addr);
1855 -- If VMS syntax conversion has failed, return an empty string
1856 -- to indicate the failure.
1858 if Canonical_File_Len = 0 then
1859 return "";
1860 end if;
1862 declare
1863 subtype Path_String is String (1 .. Canonical_File_Len);
1864 type Path_String_Access is access Path_String;
1866 function Address_To_Access is new
1867 Ada.Unchecked_Conversion (Source => Address,
1868 Target => Path_String_Access);
1870 Path_Access : constant Path_String_Access :=
1871 Address_To_Access (Canonical_File_Addr);
1873 begin
1874 Path_Buffer (1 .. Canonical_File_Len) := Path_Access.all;
1875 End_Path := Canonical_File_Len;
1876 Last := 1;
1877 end;
1878 end VMS_Conversion;
1880 -- Replace all '/' by Directory Separators (this is for Windows)
1882 if Directory_Separator /= '/' then
1883 for Index in 1 .. End_Path loop
1884 if Path_Buffer (Index) = '/' then
1885 Path_Buffer (Index) := Directory_Separator;
1886 end if;
1887 end loop;
1888 end if;
1890 -- Resolve directory names for Windows (formerly also VMS)
1892 -- On VMS, if we have a Unix path such as /temp/..., and TEMP is a
1893 -- logical name, we must not try to resolve this logical name, because
1894 -- it may have multiple equivalences and if resolved we will only
1895 -- get the first one.
1897 -- On Windows, if we have an absolute path starting with a directory
1898 -- separator, we need to have the drive letter appended in front.
1900 -- On Windows, Get_Current_Dir will return a suitable directory
1901 -- name (path starting with a drive letter on Windows). So we take this
1902 -- drive letter and prepend it to the current path.
1904 if On_Windows
1905 and then Path_Buffer (1) = Directory_Separator
1906 and then Path_Buffer (2) /= Directory_Separator
1907 then
1908 declare
1909 Cur_Dir : constant String := Get_Directory ("");
1910 -- Get the current directory to get the drive letter
1912 begin
1913 if Cur_Dir'Length > 2
1914 and then Cur_Dir (Cur_Dir'First + 1) = ':'
1915 then
1916 Path_Buffer (3 .. End_Path + 2) := Path_Buffer (1 .. End_Path);
1917 Path_Buffer (1 .. 2) :=
1918 Cur_Dir (Cur_Dir'First .. Cur_Dir'First + 1);
1919 End_Path := End_Path + 2;
1920 end if;
1921 end;
1922 end if;
1924 -- Start the conversions
1926 -- If this is not finished after Max_Iterations, give up and return an
1927 -- empty string.
1929 for J in 1 .. Max_Iterations loop
1931 -- If we don't have an absolute pathname, prepend the directory
1932 -- Reference_Dir.
1934 if Last = 1
1935 and then not Is_Absolute_Path (Path_Buffer (1 .. End_Path))
1936 then
1937 declare
1938 Reference_Dir : constant String := Get_Directory (Directory);
1939 Ref_Dir_Len : constant Natural := Reference_Dir'Length;
1940 -- Current directory name specified and its length
1942 begin
1943 Path_Buffer (Ref_Dir_Len + 1 .. Ref_Dir_Len + End_Path) :=
1944 Path_Buffer (1 .. End_Path);
1945 End_Path := Ref_Dir_Len + End_Path;
1946 Path_Buffer (1 .. Ref_Dir_Len) := Reference_Dir;
1947 Last := Ref_Dir_Len;
1948 end;
1949 end if;
1951 Start := Last + 1;
1952 Finish := Last;
1954 -- Ensure that Windows network drives are kept, e.g: \\server\drive-c
1956 if Start = 2
1957 and then Directory_Separator = '\'
1958 and then Path_Buffer (1 .. 2) = "\\"
1959 then
1960 Start := 3;
1961 end if;
1963 -- If we have traversed the full pathname, return it
1965 if Start > End_Path then
1966 return Final_Value (Path_Buffer (1 .. End_Path));
1967 end if;
1969 -- Remove duplicate directory separators
1971 while Path_Buffer (Start) = Directory_Separator loop
1972 if Start = End_Path then
1973 return Final_Value (Path_Buffer (1 .. End_Path - 1));
1975 else
1976 Path_Buffer (Start .. End_Path - 1) :=
1977 Path_Buffer (Start + 1 .. End_Path);
1978 End_Path := End_Path - 1;
1979 end if;
1980 end loop;
1982 -- Find the end of the current field: last character or the one
1983 -- preceding the next directory separator.
1985 while Finish < End_Path
1986 and then Path_Buffer (Finish + 1) /= Directory_Separator
1987 loop
1988 Finish := Finish + 1;
1989 end loop;
1991 -- Remove "." field
1993 if Start = Finish and then Path_Buffer (Start) = '.' then
1994 if Start = End_Path then
1995 if Last = 1 then
1996 return (1 => Directory_Separator);
1997 else
1999 if Fold_To_Lower_Case then
2000 System.Case_Util.To_Lower (Path_Buffer (1 .. Last - 1));
2001 end if;
2003 return Path_Buffer (1 .. Last - 1);
2005 end if;
2007 else
2008 Path_Buffer (Last + 1 .. End_Path - 2) :=
2009 Path_Buffer (Last + 3 .. End_Path);
2010 End_Path := End_Path - 2;
2011 end if;
2013 -- Remove ".." fields
2015 elsif Finish = Start + 1
2016 and then Path_Buffer (Start .. Finish) = ".."
2017 then
2018 Start := Last;
2019 loop
2020 Start := Start - 1;
2021 exit when Start < 1 or else
2022 Path_Buffer (Start) = Directory_Separator;
2023 end loop;
2025 if Start <= 1 then
2026 if Finish = End_Path then
2027 return (1 => Directory_Separator);
2029 else
2030 Path_Buffer (1 .. End_Path - Finish) :=
2031 Path_Buffer (Finish + 1 .. End_Path);
2032 End_Path := End_Path - Finish;
2033 Last := 1;
2034 end if;
2036 else
2037 if Finish = End_Path then
2038 return Final_Value (Path_Buffer (1 .. Start - 1));
2040 else
2041 Path_Buffer (Start + 1 .. Start + End_Path - Finish - 1) :=
2042 Path_Buffer (Finish + 2 .. End_Path);
2043 End_Path := Start + End_Path - Finish - 1;
2044 Last := Start;
2045 end if;
2046 end if;
2048 -- Check if current field is a symbolic link
2050 elsif Resolve_Links then
2051 declare
2052 Saved : constant Character := Path_Buffer (Finish + 1);
2054 begin
2055 Path_Buffer (Finish + 1) := ASCII.NUL;
2056 Status := Readlink (Path_Buffer'Address,
2057 Link_Buffer'Address,
2058 Link_Buffer'Length);
2059 Path_Buffer (Finish + 1) := Saved;
2060 end;
2062 -- Not a symbolic link, move to the next field, if any
2064 if Status <= 0 then
2065 Last := Finish + 1;
2067 -- Replace symbolic link with its value
2069 else
2070 if Is_Absolute_Path (Link_Buffer (1 .. Status)) then
2071 Path_Buffer (Status + 1 .. End_Path - (Finish - Status)) :=
2072 Path_Buffer (Finish + 1 .. End_Path);
2073 End_Path := End_Path - (Finish - Status);
2074 Path_Buffer (1 .. Status) := Link_Buffer (1 .. Status);
2075 Last := 1;
2077 else
2078 Path_Buffer
2079 (Last + Status + 1 .. End_Path - Finish + Last + Status) :=
2080 Path_Buffer (Finish + 1 .. End_Path);
2081 End_Path := End_Path - Finish + Last + Status;
2082 Path_Buffer (Last + 1 .. Last + Status) :=
2083 Link_Buffer (1 .. Status);
2084 end if;
2085 end if;
2087 else
2088 Last := Finish + 1;
2089 end if;
2090 end loop;
2092 -- Too many iterations: give up
2094 -- This can happen when there is a circularity in the symbolic links: A
2095 -- is a symbolic link for B, which itself is a symbolic link, and the
2096 -- target of B or of another symbolic link target of B is A. In this
2097 -- case, we return an empty string to indicate failure to resolve.
2099 return "";
2100 end Normalize_Pathname;
2102 ---------------
2103 -- Open_Read --
2104 ---------------
2106 function Open_Read
2107 (Name : C_File_Name;
2108 Fmode : Mode) return File_Descriptor
2110 function C_Open_Read
2111 (Name : C_File_Name;
2112 Fmode : Mode) return File_Descriptor;
2113 pragma Import (C, C_Open_Read, "__gnat_open_read");
2114 begin
2115 return C_Open_Read (Name, Fmode);
2116 end Open_Read;
2118 function Open_Read
2119 (Name : String;
2120 Fmode : Mode) return File_Descriptor
2122 C_Name : String (1 .. Name'Length + 1);
2123 begin
2124 C_Name (1 .. Name'Length) := Name;
2125 C_Name (C_Name'Last) := ASCII.NUL;
2126 return Open_Read (C_Name (C_Name'First)'Address, Fmode);
2127 end Open_Read;
2129 ---------------------
2130 -- Open_Read_Write --
2131 ---------------------
2133 function Open_Read_Write
2134 (Name : C_File_Name;
2135 Fmode : Mode) return File_Descriptor
2137 function C_Open_Read_Write
2138 (Name : C_File_Name;
2139 Fmode : Mode) return File_Descriptor;
2140 pragma Import (C, C_Open_Read_Write, "__gnat_open_rw");
2141 begin
2142 return C_Open_Read_Write (Name, Fmode);
2143 end Open_Read_Write;
2145 function Open_Read_Write
2146 (Name : String;
2147 Fmode : Mode) return File_Descriptor
2149 C_Name : String (1 .. Name'Length + 1);
2150 begin
2151 C_Name (1 .. Name'Length) := Name;
2152 C_Name (C_Name'Last) := ASCII.NUL;
2153 return Open_Read_Write (C_Name (C_Name'First)'Address, Fmode);
2154 end Open_Read_Write;
2156 -------------
2157 -- OS_Exit --
2158 -------------
2160 procedure OS_Exit (Status : Integer) is
2161 begin
2162 OS_Exit_Ptr (Status);
2163 raise Program_Error;
2164 end OS_Exit;
2166 ---------------------
2167 -- OS_Exit_Default --
2168 ---------------------
2170 procedure OS_Exit_Default (Status : Integer) is
2171 procedure GNAT_OS_Exit (Status : Integer);
2172 pragma Import (C, GNAT_OS_Exit, "__gnat_os_exit");
2173 pragma No_Return (GNAT_OS_Exit);
2174 begin
2175 GNAT_OS_Exit (Status);
2176 end OS_Exit_Default;
2178 --------------------
2179 -- Pid_To_Integer --
2180 --------------------
2182 function Pid_To_Integer (Pid : Process_Id) return Integer is
2183 begin
2184 return Integer (Pid);
2185 end Pid_To_Integer;
2187 ----------
2188 -- Read --
2189 ----------
2191 function Read
2192 (FD : File_Descriptor;
2193 A : System.Address;
2194 N : Integer) return Integer
2196 begin
2197 return Integer (System.CRTL.read
2198 (System.CRTL.int (FD), System.CRTL.chars (A), System.CRTL.int (N)));
2199 end Read;
2201 -----------------
2202 -- Rename_File --
2203 -----------------
2205 procedure Rename_File
2206 (Old_Name : C_File_Name;
2207 New_Name : C_File_Name;
2208 Success : out Boolean)
2210 function rename (From, To : Address) return Integer;
2211 pragma Import (C, rename, "rename");
2212 R : Integer;
2213 begin
2214 R := rename (Old_Name, New_Name);
2215 Success := (R = 0);
2216 end Rename_File;
2218 procedure Rename_File
2219 (Old_Name : String;
2220 New_Name : String;
2221 Success : out Boolean)
2223 C_Old_Name : String (1 .. Old_Name'Length + 1);
2224 C_New_Name : String (1 .. New_Name'Length + 1);
2225 begin
2226 C_Old_Name (1 .. Old_Name'Length) := Old_Name;
2227 C_Old_Name (C_Old_Name'Last) := ASCII.NUL;
2228 C_New_Name (1 .. New_Name'Length) := New_Name;
2229 C_New_Name (C_New_Name'Last) := ASCII.NUL;
2230 Rename_File (C_Old_Name'Address, C_New_Name'Address, Success);
2231 end Rename_File;
2233 -----------------------
2234 -- Set_Close_On_Exec --
2235 -----------------------
2237 procedure Set_Close_On_Exec
2238 (FD : File_Descriptor;
2239 Close_On_Exec : Boolean;
2240 Status : out Boolean)
2242 function C_Set_Close_On_Exec
2243 (FD : File_Descriptor; Close_On_Exec : System.CRTL.int)
2244 return System.CRTL.int;
2245 pragma Import (C, C_Set_Close_On_Exec, "__gnat_set_close_on_exec");
2246 begin
2247 Status := C_Set_Close_On_Exec (FD, Boolean'Pos (Close_On_Exec)) = 0;
2248 end Set_Close_On_Exec;
2250 --------------------
2251 -- Set_Executable --
2252 --------------------
2254 procedure Set_Executable (Name : String) is
2255 procedure C_Set_Executable (Name : C_File_Name);
2256 pragma Import (C, C_Set_Executable, "__gnat_set_executable");
2257 C_Name : aliased String (Name'First .. Name'Last + 1);
2258 begin
2259 C_Name (Name'Range) := Name;
2260 C_Name (C_Name'Last) := ASCII.NUL;
2261 C_Set_Executable (C_Name (C_Name'First)'Address);
2262 end Set_Executable;
2264 --------------------
2265 -- Set_Read_Only --
2266 --------------------
2268 procedure Set_Read_Only (Name : String) is
2269 procedure C_Set_Read_Only (Name : C_File_Name);
2270 pragma Import (C, C_Set_Read_Only, "__gnat_set_readonly");
2271 C_Name : aliased String (Name'First .. Name'Last + 1);
2272 begin
2273 C_Name (Name'Range) := Name;
2274 C_Name (C_Name'Last) := ASCII.NUL;
2275 C_Set_Read_Only (C_Name (C_Name'First)'Address);
2276 end Set_Read_Only;
2278 --------------------
2279 -- Set_Writable --
2280 --------------------
2282 procedure Set_Writable (Name : String) is
2283 procedure C_Set_Writable (Name : C_File_Name);
2284 pragma Import (C, C_Set_Writable, "__gnat_set_writable");
2285 C_Name : aliased String (Name'First .. Name'Last + 1);
2286 begin
2287 C_Name (Name'Range) := Name;
2288 C_Name (C_Name'Last) := ASCII.NUL;
2289 C_Set_Writable (C_Name (C_Name'First)'Address);
2290 end Set_Writable;
2292 ------------
2293 -- Setenv --
2294 ------------
2296 procedure Setenv (Name : String; Value : String) is
2297 F_Name : String (1 .. Name'Length + 1);
2298 F_Value : String (1 .. Value'Length + 1);
2300 procedure Set_Env_Value (Name, Value : System.Address);
2301 pragma Import (C, Set_Env_Value, "__gnat_setenv");
2303 begin
2304 F_Name (1 .. Name'Length) := Name;
2305 F_Name (F_Name'Last) := ASCII.NUL;
2307 F_Value (1 .. Value'Length) := Value;
2308 F_Value (F_Value'Last) := ASCII.NUL;
2310 Set_Env_Value (F_Name'Address, F_Value'Address);
2311 end Setenv;
2313 -----------
2314 -- Spawn --
2315 -----------
2317 function Spawn
2318 (Program_Name : String;
2319 Args : Argument_List) return Integer
2321 Result : Integer;
2322 Junk : Process_Id;
2323 pragma Warnings (Off, Junk);
2324 begin
2325 Spawn_Internal (Program_Name, Args, Result, Junk, Blocking => True);
2326 return Result;
2327 end Spawn;
2329 procedure Spawn
2330 (Program_Name : String;
2331 Args : Argument_List;
2332 Success : out Boolean)
2334 begin
2335 Success := (Spawn (Program_Name, Args) = 0);
2336 end Spawn;
2338 procedure Spawn
2339 (Program_Name : String;
2340 Args : Argument_List;
2341 Output_File_Descriptor : File_Descriptor;
2342 Return_Code : out Integer;
2343 Err_To_Out : Boolean := True)
2345 Saved_Output : File_Descriptor;
2346 Saved_Error : File_Descriptor := Invalid_FD; -- prevent compiler warning
2348 begin
2349 -- Set standard output and error to the temporary file
2351 Saved_Output := Dup (Standout);
2352 Dup2 (Output_File_Descriptor, Standout);
2354 if Err_To_Out then
2355 Saved_Error := Dup (Standerr);
2356 Dup2 (Output_File_Descriptor, Standerr);
2357 end if;
2359 -- Spawn the program
2361 Return_Code := Spawn (Program_Name, Args);
2363 -- Restore the standard output and error
2365 Dup2 (Saved_Output, Standout);
2367 if Err_To_Out then
2368 Dup2 (Saved_Error, Standerr);
2369 end if;
2371 -- And close the saved standard output and error file descriptors
2373 Close (Saved_Output);
2375 if Err_To_Out then
2376 Close (Saved_Error);
2377 end if;
2378 end Spawn;
2380 procedure Spawn
2381 (Program_Name : String;
2382 Args : Argument_List;
2383 Output_File : String;
2384 Success : out Boolean;
2385 Return_Code : out Integer;
2386 Err_To_Out : Boolean := True)
2388 FD : File_Descriptor;
2390 begin
2391 Success := True;
2392 Return_Code := 0;
2394 FD := Create_Output_Text_File (Output_File);
2396 if FD = Invalid_FD then
2397 Success := False;
2398 return;
2399 end if;
2401 Spawn (Program_Name, Args, FD, Return_Code, Err_To_Out);
2403 Close (FD, Success);
2404 end Spawn;
2406 --------------------
2407 -- Spawn_Internal --
2408 --------------------
2410 procedure Spawn_Internal
2411 (Program_Name : String;
2412 Args : Argument_List;
2413 Result : out Integer;
2414 Pid : out Process_Id;
2415 Blocking : Boolean)
2418 procedure Spawn (Args : Argument_List);
2419 -- Call Spawn with given argument list
2421 N_Args : Argument_List (Args'Range);
2422 -- Normalized arguments
2424 -----------
2425 -- Spawn --
2426 -----------
2428 procedure Spawn (Args : Argument_List) is
2429 type Chars is array (Positive range <>) of aliased Character;
2430 type Char_Ptr is access constant Character;
2432 Command_Len : constant Positive := Program_Name'Length + 1
2433 + Args_Length (Args);
2434 Command_Last : Natural := 0;
2435 Command : aliased Chars (1 .. Command_Len);
2436 -- Command contains all characters of the Program_Name and Args, all
2437 -- terminated by ASCII.NUL characters
2439 Arg_List_Len : constant Positive := Args'Length + 2;
2440 Arg_List_Last : Natural := 0;
2441 Arg_List : aliased array (1 .. Arg_List_Len) of Char_Ptr;
2442 -- List with pointers to NUL-terminated strings of the Program_Name
2443 -- and the Args and terminated with a null pointer. We rely on the
2444 -- default initialization for the last null pointer.
2446 procedure Add_To_Command (S : String);
2447 -- Add S and a NUL character to Command, updating Last
2449 function Portable_Spawn (Args : Address) return Integer;
2450 pragma Import (C, Portable_Spawn, "__gnat_portable_spawn");
2452 function Portable_No_Block_Spawn (Args : Address) return Process_Id;
2453 pragma Import
2454 (C, Portable_No_Block_Spawn, "__gnat_portable_no_block_spawn");
2456 --------------------
2457 -- Add_To_Command --
2458 --------------------
2460 procedure Add_To_Command (S : String) is
2461 First : constant Natural := Command_Last + 1;
2463 begin
2464 Command_Last := Command_Last + S'Length;
2466 -- Move characters one at a time, because Command has aliased
2467 -- components.
2469 -- But not volatile, so why is this necessary ???
2471 for J in S'Range loop
2472 Command (First + J - S'First) := S (J);
2473 end loop;
2475 Command_Last := Command_Last + 1;
2476 Command (Command_Last) := ASCII.NUL;
2478 Arg_List_Last := Arg_List_Last + 1;
2479 Arg_List (Arg_List_Last) := Command (First)'Access;
2480 end Add_To_Command;
2482 -- Start of processing for Spawn
2484 begin
2485 Add_To_Command (Program_Name);
2487 for J in Args'Range loop
2488 Add_To_Command (Args (J).all);
2489 end loop;
2491 if Blocking then
2492 Pid := Invalid_Pid;
2493 Result := Portable_Spawn (Arg_List'Address);
2494 else
2495 Pid := Portable_No_Block_Spawn (Arg_List'Address);
2496 Result := Boolean'Pos (Pid /= Invalid_Pid);
2497 end if;
2498 end Spawn;
2500 -- Start of processing for Spawn_Internal
2502 begin
2503 -- Copy arguments into a local structure
2505 for K in N_Args'Range loop
2506 N_Args (K) := new String'(Args (K).all);
2507 end loop;
2509 -- Normalize those arguments
2511 Normalize_Arguments (N_Args);
2513 -- Call spawn using the normalized arguments
2515 Spawn (N_Args);
2517 -- Free arguments list
2519 for K in N_Args'Range loop
2520 Free (N_Args (K));
2521 end loop;
2522 end Spawn_Internal;
2524 ---------------------------
2525 -- To_Path_String_Access --
2526 ---------------------------
2528 function To_Path_String_Access
2529 (Path_Addr : Address;
2530 Path_Len : Integer) return String_Access
2532 subtype Path_String is String (1 .. Path_Len);
2533 type Path_String_Access is access Path_String;
2535 function Address_To_Access is new
2536 Ada.Unchecked_Conversion (Source => Address,
2537 Target => Path_String_Access);
2539 Path_Access : constant Path_String_Access :=
2540 Address_To_Access (Path_Addr);
2542 Return_Val : String_Access;
2544 begin
2545 Return_Val := new String (1 .. Path_Len);
2547 for J in 1 .. Path_Len loop
2548 Return_Val (J) := Path_Access (J);
2549 end loop;
2551 return Return_Val;
2552 end To_Path_String_Access;
2554 ------------------
2555 -- Wait_Process --
2556 ------------------
2558 procedure Wait_Process (Pid : out Process_Id; Success : out Boolean) is
2559 Status : Integer;
2561 function Portable_Wait (S : Address) return Process_Id;
2562 pragma Import (C, Portable_Wait, "__gnat_portable_wait");
2564 begin
2565 Pid := Portable_Wait (Status'Address);
2566 Success := (Status = 0);
2567 end Wait_Process;
2569 -----------
2570 -- Write --
2571 -----------
2573 function Write
2574 (FD : File_Descriptor;
2575 A : System.Address;
2576 N : Integer) return Integer
2578 begin
2579 return Integer (System.CRTL.write
2580 (System.CRTL.int (FD), System.CRTL.chars (A), System.CRTL.int (N)));
2581 end Write;
2583 end System.OS_Lib;