Debian Codegraph @

gcc/ada/libgnat/s-os_lib.adb

raw
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-2025, AdaCore --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 with Ada.Unchecked_Conversion;
33 with Ada.Unchecked_Deallocation;
34 with System.Case_Util;
35 with System.CRTL;
36 with System.Soft_Links;
37 with Interfaces.C;
38
39 package body System.OS_Lib is
40
41 subtype size_t is CRTL.size_t;
42
43 procedure Strncpy (dest, src : System.Address; n : size_t)
44 renames CRTL.strncpy;
45
46 -- Imported procedures Dup and Dup2 are used in procedures Spawn and
47 -- Non_Blocking_Spawn.
48
49 function Dup (Fd : File_Descriptor) return File_Descriptor;
50 pragma Import (C, Dup, "__gnat_dup");
51
52 procedure Dup2 (Old_Fd, New_Fd : File_Descriptor);
53 pragma Import (C, Dup2, "__gnat_dup2");
54
55 function Copy_Attributes
56 (From : System.Address;
57 To : System.Address;
58 Mode : Integer) return Integer;
59 pragma Import (C, Copy_Attributes, "__gnat_copy_attribs");
60 -- Mode = 0 - copy only time stamps.
61 -- Mode = 1 - copy time stamps and read/write/execute attributes
62 -- Mode = 2 - copy read/write/execute attributes
63
64 function Is_Dirsep (C : Character) return Boolean;
65 pragma Inline (Is_Dirsep);
66 -- Returns True if C is a directory separator. On Windows we
67 -- accept both \ and / as a directory separator.
68
69 On_Windows : constant Boolean := Directory_Separator = '\';
70 -- An indication that we are on Windows. Used in Normalize_Pathname, to
71 -- deal with drive letters in the beginning of absolute paths.
72
73 package SSL renames System.Soft_Links;
74
75 -- The following are used by Create_Temp_File
76
77 First_Temp_File_Name : constant String := "GNAT-TEMP-000000.TMP";
78 -- Used to initialize Current_Temp_File_Name and Temp_File_Name_Last_Digit
79
80 Current_Temp_File_Name : String := First_Temp_File_Name;
81 -- Name of the temp file last created
82
83 Temp_File_Name_Last_Digit : constant Positive :=
84 First_Temp_File_Name'Last - 4;
85 -- Position of the last digit in Current_Temp_File_Name
86
87 Max_Attempts : constant := 100;
88 -- The maximum number of attempts to create a new temp file
89
90 -----------------------
91 -- Local Subprograms --
92 -----------------------
93
94 function Args_Length (Args : Argument_List) return Natural;
95 -- Returns total number of characters needed to create a string of all Args
96 -- terminated by ASCII.NUL characters.
97
98 procedure Create_Temp_File_Internal
99 (FD : out File_Descriptor;
100 Name : out String_Access;
101 Stdout : Boolean);
102 -- Internal routine to implement two Create_Temp_File routines. If Stdout
103 -- is set to True the created descriptor is stdout-compatible, otherwise
104 -- it might not be depending on the OS. The first two parameters are as
105 -- in Create_Temp_File.
106
107 function C_String_Length (S : Address) return Integer;
108 -- Returns the length of C (null-terminated) string at S, or 0 for
109 -- Null_Address.
110
111 procedure Spawn_Internal
112 (Program_Name : String;
113 Args : Argument_List;
114 Result : out Integer;
115 Pid : out Process_Id;
116 Blocking : Boolean);
117 -- Internal routine to implement the two Spawn (blocking/non blocking)
118 -- routines. If Blocking is set to True then the spawn is blocking
119 -- otherwise it is non blocking. In this latter case the Pid contains the
120 -- process id number. The first three parameters are as in Spawn. Note that
121 -- Spawn_Internal normalizes the argument list before calling the low level
122 -- system spawn routines (see Normalize_Arguments).
123 --
124 -- Note: Normalize_Arguments is designed to do nothing if it is called more
125 -- than once, so calling Normalize_Arguments before calling one of the
126 -- spawn routines is fine.
127
128 function To_Path_String_Access
129 (Path_Addr : Address;
130 Path_Len : Integer) return String_Access;
131 -- Converts a C String to an Ada String. We could do this making use of
132 -- Interfaces.C.Strings but we prefer not to import that entire package
133
134 -----------------
135 -- Args_Length --
136 -----------------
137
138 function Args_Length (Args : Argument_List) return Natural is
139 Len : Natural := 0;
140
141 begin
142 for J in Args'Range loop
143 Len := Len + Args (J)'Length + 1; -- One extra for ASCII.NUL
144 end loop;
145
146 return Len;
147 end Args_Length;
148
149 -----------------------------
150 -- Argument_String_To_List --
151 -----------------------------
152
153 function Argument_String_To_List
154 (Arg_String : String) return Argument_List_Access
155 is
156 Max_Args : constant Integer := Arg_String'Length;
157
158 Backslash_Is_Sep : constant Boolean := Directory_Separator = '\';
159 -- Whether '\' is a directory separator (as on Windows), or a way to
160 -- quote special characters.
161
162 Backqd : Boolean := False;
163 Idx : Integer;
164 New_Argc : Natural := 0;
165 New_Argv : Argument_List (1 .. Max_Args);
166 Quoted : Boolean := False;
167
168 Cleaned : String (1 .. Arg_String'Length);
169 Cleaned_Idx : Natural;
170 -- A cleaned up version of the argument. This function is taking
171 -- backslash escapes when computing the bounds for arguments. It
172 -- is then removing the extra backslashes from the argument.
173
174 begin
175 Idx := Arg_String'First;
176
177 loop
178 -- Skip extraneous spaces
179
180 while Idx <= Arg_String'Last and then Arg_String (Idx) = ' ' loop
181 Idx := Idx + 1;
182 end loop;
183
184 exit when Idx > Arg_String'Last;
185
186 Cleaned_Idx := Cleaned'First;
187 Backqd := False;
188 Quoted := False;
189
190 loop
191 -- An unquoted space is the end of an argument
192
193 if not (Backqd or Quoted) and then Arg_String (Idx) = ' ' then
194 exit;
195
196 -- Start of a quoted string
197
198 elsif not (Backqd or Quoted) and then Arg_String (Idx) = '"' then
199 Quoted := True;
200 Cleaned (Cleaned_Idx) := Arg_String (Idx);
201 Cleaned_Idx := Cleaned_Idx + 1;
202
203 -- End of a quoted string and end of an argument
204
205 elsif (Quoted and not Backqd) and then Arg_String (Idx) = '"' then
206 Cleaned (Cleaned_Idx) := Arg_String (Idx);
207 Cleaned_Idx := Cleaned_Idx + 1;
208 Idx := Idx + 1;
209 exit;
210
211 -- Turn off backquoting after advancing one character
212
213 elsif Backqd then
214 Backqd := False;
215 Cleaned (Cleaned_Idx) := Arg_String (Idx);
216 Cleaned_Idx := Cleaned_Idx + 1;
217
218 -- Following character is backquoted
219
220 elsif not Backslash_Is_Sep and then Arg_String (Idx) = '\' then
221 Backqd := True;
222
223 else
224 Cleaned (Cleaned_Idx) := Arg_String (Idx);
225 Cleaned_Idx := Cleaned_Idx + 1;
226 end if;
227
228 Idx := Idx + 1;
229 exit when Idx > Arg_String'Last;
230 end loop;
231
232 -- Found an argument
233
234 New_Argc := New_Argc + 1;
235 New_Argv (New_Argc) :=
236 new String'(Cleaned (Cleaned'First .. Cleaned_Idx - 1));
237 end loop;
238
239 return new Argument_List'(New_Argv (1 .. New_Argc));
240 end Argument_String_To_List;
241
242 ---------------------
243 -- C_String_Length --
244 ---------------------
245
246 function C_String_Length (S : Address) return Integer is
247 begin
248 if S = Null_Address then
249 return 0;
250 else
251 return Integer (CRTL.strlen (S));
252 end if;
253 end C_String_Length;
254
255 -----------
256 -- Close --
257 -----------
258
259 procedure Close (FD : File_Descriptor) is
260 use CRTL;
261 Discard : constant int := close (int (FD));
262 begin
263 null;
264 end Close;
265
266 procedure Close (FD : File_Descriptor; Status : out Boolean) is
267 use CRTL;
268 begin
269 Status := (close (int (FD)) = 0);
270 end Close;
271
272 ---------------
273 -- Copy_File --
274 ---------------
275
276 procedure Copy_File
277 (Name : String;
278 Pathname : String;
279 Success : out Boolean;
280 Mode : Copy_Mode := Copy;
281 Preserve : Attribute := Time_Stamps)
282 is
283 From : File_Descriptor;
284 To : File_Descriptor;
285
286 Copy_Error : exception;
287 -- Internal exception raised to signal error in copy
288
289 function Build_Path (Dir : String; File : String) return String;
290 -- Returns pathname Dir concatenated with File adding the directory
291 -- separator only if needed.
292
293 procedure Copy (From : File_Descriptor; To : File_Descriptor);
294 -- Read data from From and place them into To. In both cases the
295 -- operations uses the current file position. Raises Constraint_Error
296 -- if a problem occurs during the copy.
297
298 procedure Copy_To (To_Name : String);
299 -- Does a straight copy from source to designated destination file
300
301 ----------------
302 -- Build_Path --
303 ----------------
304
305 function Build_Path (Dir : String; File : String) return String is
306 Base_File_Ptr : Integer;
307 -- The base file name is File (Base_File_Ptr + 1 .. File'Last)
308
309 Res : String (1 .. Dir'Length + File'Length + 1);
310
311 -- Start of processing for Build_Path
312
313 begin
314 -- Find base file name
315
316 Base_File_Ptr := File'Last;
317 while Base_File_Ptr >= File'First loop
318 exit when Is_Dirsep (File (Base_File_Ptr));
319 Base_File_Ptr := Base_File_Ptr - 1;
320 end loop;
321
322 declare
323 Base_File : String renames
324 File (Base_File_Ptr + 1 .. File'Last);
325
326 begin
327 Res (1 .. Dir'Length) := Dir;
328
329 if Is_Dirsep (Dir (Dir'Last)) then
330 Res (Dir'Length + 1 .. Dir'Length + Base_File'Length) :=
331 Base_File;
332 return Res (1 .. Dir'Length + Base_File'Length);
333
334 else
335 Res (Dir'Length + 1) := Directory_Separator;
336 Res (Dir'Length + 2 .. Dir'Length + 1 + Base_File'Length) :=
337 Base_File;
338 return Res (1 .. Dir'Length + 1 + Base_File'Length);
339 end if;
340 end;
341 end Build_Path;
342
343 ----------
344 -- Copy --
345 ----------
346
347 procedure Copy (From : File_Descriptor; To : File_Descriptor) is
348 Buf_Size : constant := 200_000;
349 type Buf is array (1 .. Buf_Size) of Character;
350 type Buf_Ptr is access Buf;
351
352 Buffer : Buf_Ptr;
353 R : Integer;
354 W : Integer;
355
356 Status_From : Boolean;
357 Status_To : Boolean;
358 -- Statuses for the calls to Close
359
360 procedure Free is new Ada.Unchecked_Deallocation (Buf, Buf_Ptr);
361
362 begin
363 -- Check for invalid descriptors, making sure that we do not
364 -- accidentally leave an open file descriptor around.
365
366 if From = Invalid_FD then
367 if To /= Invalid_FD then
368 Close (To, Status_To);
369 end if;
370
371 raise Copy_Error;
372
373 elsif To = Invalid_FD then
374 Close (From, Status_From);
375 raise Copy_Error;
376 end if;
377
378 -- Allocate the buffer on the heap
379
380 Buffer := new Buf;
381
382 loop
383 R := Read (From, Buffer (1)'Address, Buf_Size);
384
385 -- On some systems, the buffer may not be full. So, we need to try
386 -- again until there is nothing to read.
387
388 exit when R = 0;
389
390 W := Write (To, Buffer (1)'Address, R);
391
392 if W < R then
393
394 -- Problem writing data, could be a disk full. Close files
395 -- without worrying about status, since we are raising a
396 -- Copy_Error exception in any case.
397
398 Close (From, Status_From);
399 Close (To, Status_To);
400
401 Free (Buffer);
402
403 raise Copy_Error;
404 end if;
405 end loop;
406
407 Close (From, Status_From);
408 Close (To, Status_To);
409
410 Free (Buffer);
411
412 if not (Status_From and Status_To) then
413 raise Copy_Error;
414 end if;
415 end Copy;
416
417 -------------
418 -- Copy_To --
419 -------------
420
421 procedure Copy_To (To_Name : String) is
422 C_From : String (1 .. Name'Length + 1);
423 C_To : String (1 .. To_Name'Length + 1);
424
425 begin
426 From := Open_Read (Name, Binary);
427
428 -- Do not clobber destination file if source file could not be opened
429
430 if From /= Invalid_FD then
431 To := Create_File (To_Name, Binary);
432 end if;
433
434 Copy (From, To);
435
436 -- Copy attributes
437
438 C_From (1 .. Name'Length) := Name;
439 C_From (C_From'Last) := ASCII.NUL;
440
441 C_To (1 .. To_Name'Length) := To_Name;
442 C_To (C_To'Last) := ASCII.NUL;
443
444 case Preserve is
445 when Time_Stamps =>
446 if Copy_Attributes (C_From'Address, C_To'Address, 0) = -1 then
447 raise Copy_Error;
448 end if;
449
450 when Full =>
451 if Copy_Attributes (C_From'Address, C_To'Address, 1) = -1 then
452 raise Copy_Error;
453 end if;
454
455 when None =>
456 null;
457 end case;
458 end Copy_To;
459
460 -- Start of processing for Copy_File
461
462 begin
463 Success := True;
464
465 -- The source file must exist
466
467 if not Is_Regular_File (Name) then
468 raise Copy_Error;
469 end if;
470
471 -- The source file exists
472
473 case Mode is
474
475 -- Copy case, target file must not exist
476
477 when Copy =>
478
479 -- If the target file exists, we have an error
480
481 if Is_Regular_File (Pathname) then
482 raise Copy_Error;
483
484 -- Case of target is a directory
485
486 elsif Is_Directory (Pathname) then
487 declare
488 Dest : constant String := Build_Path (Pathname, Name);
489
490 begin
491 -- If target file exists, we have an error, else do copy
492
493 if Is_Regular_File (Dest) then
494 raise Copy_Error;
495 else
496 Copy_To (Dest);
497 end if;
498 end;
499
500 -- Case of normal copy to file (destination does not exist)
501
502 else
503 Copy_To (Pathname);
504 end if;
505
506 -- Overwrite case (destination file may or may not exist)
507
508 when Overwrite =>
509 if Is_Directory (Pathname) then
510 Copy_To (Build_Path (Pathname, Name));
511 else
512 Copy_To (Pathname);
513 end if;
514
515 -- Append case (destination file may or may not exist)
516
517 when Append =>
518
519 -- Appending to existing file
520
521 if Is_Regular_File (Pathname) then
522
523 -- Append mode and destination file exists, append data at the
524 -- end of Pathname. But if we fail to open source file, do not
525 -- touch destination file at all.
526
527 From := Open_Read (Name, Binary);
528
529 if From = Invalid_FD then
530 Success := False;
531 else
532 To := Open_Read_Write (Pathname, Binary);
533 Lseek (To, 0, Seek_End);
534 Copy (From, To);
535 end if;
536
537 -- Appending to directory, not allowed
538
539 elsif Is_Directory (Pathname) then
540 raise Copy_Error;
541
542 -- Appending when target file does not exist
543
544 else
545 Copy_To (Pathname);
546 end if;
547 end case;
548
549 -- All error cases are caught here
550
551 exception
552 when Copy_Error =>
553 Success := False;
554 end Copy_File;
555
556 procedure Copy_File
557 (Name : C_File_Name;
558 Pathname : C_File_Name;
559 Success : out Boolean;
560 Mode : Copy_Mode := Copy;
561 Preserve : Attribute := Time_Stamps)
562 is
563 Ada_Name : String_Access :=
564 To_Path_String_Access
565 (Name, C_String_Length (Name));
566 Ada_Pathname : String_Access :=
567 To_Path_String_Access
568 (Pathname, C_String_Length (Pathname));
569
570 begin
571 Copy_File (Ada_Name.all, Ada_Pathname.all, Success, Mode, Preserve);
572 Free (Ada_Name);
573 Free (Ada_Pathname);
574 end Copy_File;
575
576 --------------------------
577 -- Copy_File_Attributes --
578 --------------------------
579
580 procedure Copy_File_Attributes
581 (From : String;
582 To : String;
583 Success : out Boolean;
584 Copy_Timestamp : Boolean := True;
585 Copy_Permissions : Boolean := True)
586 is
587 F : aliased String (1 .. From'Length + 1);
588 T : aliased String (1 .. To'Length + 1);
589
590 Mode : Integer;
591
592 begin
593 if Copy_Timestamp then
594 if Copy_Permissions then
595 Mode := 1;
596 else
597 Mode := 0;
598 end if;
599 else
600 if Copy_Permissions then
601 Mode := 2;
602 else
603 Success := True;
604 return; -- nothing to do
605 end if;
606 end if;
607
608 F (1 .. From'Length) := From;
609 F (F'Last) := ASCII.NUL;
610
611 T (1 .. To'Length) := To;
612 T (T'Last) := ASCII.NUL;
613
614 Success := Copy_Attributes (F'Address, T'Address, Mode) /= -1;
615 end Copy_File_Attributes;
616
617 ----------------------
618 -- Copy_Time_Stamps --
619 ----------------------
620
621 procedure Copy_Time_Stamps
622 (Source : String;
623 Dest : String;
624 Success : out Boolean)
625 is
626 begin
627 if Is_Regular_File (Source) and then Is_Writable_File (Dest) then
628 declare
629 C_Source : String (1 .. Source'Length + 1);
630 C_Dest : String (1 .. Dest'Length + 1);
631
632 begin
633 C_Source (1 .. Source'Length) := Source;
634 C_Source (C_Source'Last) := ASCII.NUL;
635
636 C_Dest (1 .. Dest'Length) := Dest;
637 C_Dest (C_Dest'Last) := ASCII.NUL;
638
639 if Copy_Attributes (C_Source'Address, C_Dest'Address, 0) = -1 then
640 Success := False;
641 else
642 Success := True;
643 end if;
644 end;
645
646 else
647 Success := False;
648 end if;
649 end Copy_Time_Stamps;
650
651 procedure Copy_Time_Stamps
652 (Source : C_File_Name;
653 Dest : C_File_Name;
654 Success : out Boolean)
655 is
656 Ada_Source : String_Access :=
657 To_Path_String_Access
658 (Source, C_String_Length (Source));
659 Ada_Dest : String_Access :=
660 To_Path_String_Access
661 (Dest, C_String_Length (Dest));
662
663 begin
664 Copy_Time_Stamps (Ada_Source.all, Ada_Dest.all, Success);
665 Free (Ada_Source);
666 Free (Ada_Dest);
667 end Copy_Time_Stamps;
668
669 -----------------
670 -- Create_File --
671 -----------------
672
673 function Create_File
674 (Name : C_File_Name;
675 Fmode : Mode) return File_Descriptor
676 is
677 function C_Create_File
678 (Name : C_File_Name;
679 Fmode : Mode) return File_Descriptor;
680 pragma Import (C, C_Create_File, "__gnat_open_create");
681 begin
682 return C_Create_File (Name, Fmode);
683 end Create_File;
684
685 function Create_File
686 (Name : String;
687 Fmode : Mode) return File_Descriptor
688 is
689 C_Name : String (1 .. Name'Length + 1);
690 begin
691 C_Name (1 .. Name'Length) := Name;
692 C_Name (C_Name'Last) := ASCII.NUL;
693 return Create_File (C_Name (C_Name'First)'Address, Fmode);
694 end Create_File;
695
696 ---------------------
697 -- Create_New_File --
698 ---------------------
699
700 function Create_New_File
701 (Name : C_File_Name;
702 Fmode : Mode) return File_Descriptor
703 is
704 function C_Create_New_File
705 (Name : C_File_Name;
706 Fmode : Mode) return File_Descriptor;
707 pragma Import (C, C_Create_New_File, "__gnat_open_new");
708 begin
709 return C_Create_New_File (Name, Fmode);
710 end Create_New_File;
711
712 function Create_New_File
713 (Name : String;
714 Fmode : Mode) return File_Descriptor
715 is
716 C_Name : String (1 .. Name'Length + 1);
717 begin
718 C_Name (1 .. Name'Length) := Name;
719 C_Name (C_Name'Last) := ASCII.NUL;
720 return Create_New_File (C_Name (C_Name'First)'Address, Fmode);
721 end Create_New_File;
722
723 -----------------------------
724 -- Create_Output_Text_File --
725 -----------------------------
726
727 function Create_Output_Text_File (Name : String) return File_Descriptor is
728 function C_Create_File (Name : C_File_Name) return File_Descriptor;
729 pragma Import (C, C_Create_File, "__gnat_create_output_file");
730
731 C_Name : String (1 .. Name'Length + 1);
732
733 begin
734 C_Name (1 .. Name'Length) := Name;
735 C_Name (C_Name'Last) := ASCII.NUL;
736 return C_Create_File (C_Name (C_Name'First)'Address);
737 end Create_Output_Text_File;
738
739 ----------------------
740 -- Create_Temp_File --
741 ----------------------
742
743 procedure Create_Temp_File
744 (FD : out File_Descriptor;
745 Name : out Temp_File_Name)
746 is
747 function Open_New_Temp
748 (Name : System.Address;
749 Fmode : Mode) return File_Descriptor;
750 pragma Import (C, Open_New_Temp, "__gnat_open_new_temp");
751
752 begin
753 FD := Open_New_Temp (Name'Address, Binary);
754 end Create_Temp_File;
755
756 procedure Create_Temp_File
757 (FD : out File_Descriptor;
758 Name : out String_Access)
759 is
760 begin
761 Create_Temp_File_Internal (FD, Name, Stdout => False);
762 end Create_Temp_File;
763
764 -----------------------------
765 -- Create_Temp_Output_File --
766 -----------------------------
767
768 procedure Create_Temp_Output_File
769 (FD : out File_Descriptor;
770 Name : out String_Access)
771 is
772 begin
773 Create_Temp_File_Internal (FD, Name, Stdout => True);
774 end Create_Temp_Output_File;
775
776 -------------------------------
777 -- Create_Temp_File_Internal --
778 -------------------------------
779
780 procedure Create_Temp_File_Internal
781 (FD : out File_Descriptor;
782 Name : out String_Access;
783 Stdout : Boolean)
784 is
785 Pos : Positive;
786 Attempts : Natural := 0;
787 Current : String (Current_Temp_File_Name'Range);
788
789 function Create_New_Output_Text_File
790 (Name : String) return File_Descriptor;
791 -- Similar to Create_Output_Text_File, except it fails if the file
792 -- already exists. We need this behavior to ensure we don't accidentally
793 -- open a temp file that has just been created by a concurrently running
794 -- process. There is no point exposing this function, as it's generally
795 -- not particularly useful.
796
797 ---------------------------------
798 -- Create_New_Output_Text_File --
799 ---------------------------------
800
801 function Create_New_Output_Text_File
802 (Name : String) return File_Descriptor
803 is
804 function C_Create_File (Name : C_File_Name) return File_Descriptor;
805 pragma Import (C, C_Create_File, "__gnat_create_output_file_new");
806
807 C_Name : String (1 .. Name'Length + 1);
808
809 begin
810 C_Name (1 .. Name'Length) := Name;
811 C_Name (C_Name'Last) := ASCII.NUL;
812 return C_Create_File (C_Name (C_Name'First)'Address);
813 end Create_New_Output_Text_File;
814
815 -- Start of processing for Create_Temp_File_Internal
816
817 begin
818 -- Loop until a new temp file can be created
819
820 File_Loop : loop
821 Locked : begin
822
823 -- We need to protect global variable Current_Temp_File_Name
824 -- against concurrent access by different tasks.
825
826 SSL.Lock_Task.all;
827
828 -- Start at the last digit
829
830 Pos := Temp_File_Name_Last_Digit;
831
832 Digit_Loop :
833 loop
834 -- Increment the digit by one
835
836 case Current_Temp_File_Name (Pos) is
837 when '0' .. '8' =>
838 Current_Temp_File_Name (Pos) :=
839 Character'Succ (Current_Temp_File_Name (Pos));
840 exit Digit_Loop;
841
842 when '9' =>
843
844 -- For 9, set the digit to 0 and go to the previous digit
845
846 Current_Temp_File_Name (Pos) := '0';
847 Pos := Pos - 1;
848
849 when others =>
850
851 -- If it is not a digit, then there are no available
852 -- temp file names. Return Invalid_FD. There is almost no
853 -- chance that this code will be ever be executed, since
854 -- it would mean that there are one million temp files in
855 -- the same directory.
856
857 SSL.Unlock_Task.all;
858 FD := Invalid_FD;
859 Name := null;
860 exit File_Loop;
861 end case;
862 end loop Digit_Loop;
863
864 Current := Current_Temp_File_Name;
865
866 -- We can now release the lock, because we are no longer accessing
867 -- Current_Temp_File_Name.
868
869 SSL.Unlock_Task.all;
870
871 exception
872 when others =>
873 SSL.Unlock_Task.all;
874 raise;
875 end Locked;
876
877 -- Attempt to create the file
878
879 if Stdout then
880 FD := Create_New_Output_Text_File (Current);
881 else
882 FD := Create_New_File (Current, Binary);
883 end if;
884
885 if FD /= Invalid_FD then
886 Name := new String'(Current);
887 exit File_Loop;
888 end if;
889
890 if not Is_Regular_File (Current) then
891
892 -- If the file does not already exist and we are unable to create
893 -- it, we give up after Max_Attempts. Otherwise, we try again with
894 -- the next available file name.
895
896 Attempts := Attempts + 1;
897
898 if Attempts >= Max_Attempts then
899 FD := Invalid_FD;
900 Name := null;
901 exit File_Loop;
902 end if;
903 end if;
904 end loop File_Loop;
905 end Create_Temp_File_Internal;
906
907 -------------------------
908 -- Current_Time_String --
909 -------------------------
910
911 function Current_Time_String return String is
912 subtype S23 is String (1 .. 23);
913 -- Holds current time in ISO 8601 format YYYY-MM-DD HH:MM:SS.SS + NUL
914
915 procedure Current_Time_String (Time : System.Address);
916 pragma Import (C, Current_Time_String, "__gnat_current_time_string");
917 -- Puts current time into Time in above ISO 8601 format
918
919 Result23 : aliased S23;
920 -- Current time in ISO 8601 format
921
922 begin
923 Current_Time_String (Result23'Address);
924 return Result23 (1 .. 19);
925 end Current_Time_String;
926
927 -----------------
928 -- Delete_File --
929 -----------------
930
931 procedure Delete_File (Name : Address; Success : out Boolean) is
932 R : Integer;
933 begin
934 R := System.CRTL.unlink (Name);
935 Success := (R = 0);
936 end Delete_File;
937
938 procedure Delete_File (Name : String; Success : out Boolean) is
939 C_Name : String (1 .. Name'Length + 1);
940 begin
941 C_Name (1 .. Name'Length) := Name;
942 C_Name (C_Name'Last) := ASCII.NUL;
943 Delete_File (C_Name'Address, Success);
944 end Delete_File;
945
946 -------------------
947 -- Errno_Message --
948 -------------------
949
950 function Errno_Message
951 (Err : Integer := Errno;
952 Default : String := "") return String
953 is
954 function strerror (errnum : Integer) return System.Address;
955 pragma Import (C, strerror, "strerror");
956
957 C_Msg : constant System.Address := strerror (Err);
958
959 begin
960 if C_Msg = Null_Address then
961 if Default /= "" then
962 return Default;
963
964 else
965 -- Note: for bootstrap reasons, it is impractical
966 -- to use Integer'Image here.
967
968 declare
969 Val : Integer;
970 First : Integer;
971
972 Buf : String (1 .. 20);
973 -- Buffer large enough to hold image of largest Integer values
974
975 begin
976 Val := abs Err;
977 First := Buf'Last;
978 loop
979 Buf (First) :=
980 Character'Val (Character'Pos ('0') + Val mod 10);
981 Val := Val / 10;
982 exit when Val = 0;
983 First := First - 1;
984 end loop;
985
986 if Err < 0 then
987 First := First - 1;
988 Buf (First) := '-';
989 end if;
990
991 return "errno = " & Buf (First .. Buf'Last);
992 end;
993 end if;
994
995 else
996 declare
997 Msg : String (1 .. Integer (CRTL.strlen (C_Msg)));
998 for Msg'Address use C_Msg;
999 pragma Import (Ada, Msg);
1000 begin
1001 return Msg;
1002 end;
1003 end if;
1004 end Errno_Message;
1005
1006 ---------------------
1007 -- File_Time_Stamp --
1008 ---------------------
1009
1010 function File_Time_Stamp (FD : File_Descriptor) return OS_Time is
1011 function File_Time (FD : File_Descriptor) return OS_Time;
1012 pragma Import (C, File_Time, "__gnat_file_time_fd");
1013 begin
1014 return File_Time (FD);
1015 end File_Time_Stamp;
1016
1017 function File_Time_Stamp (Name : C_File_Name) return OS_Time is
1018 function File_Time (Name : Address) return OS_Time;
1019 pragma Import (C, File_Time, "__gnat_file_time_name");
1020 begin
1021 return File_Time (Name);
1022 end File_Time_Stamp;
1023
1024 function File_Time_Stamp (Name : String) return OS_Time is
1025 F_Name : String (1 .. Name'Length + 1);
1026 begin
1027 F_Name (1 .. Name'Length) := Name;
1028 F_Name (F_Name'Last) := ASCII.NUL;
1029 return File_Time_Stamp (F_Name'Address);
1030 end File_Time_Stamp;
1031
1032 ---------------------------
1033 -- Get_Debuggable_Suffix --
1034 ---------------------------
1035
1036 function Get_Debuggable_Suffix return String_Access is
1037 procedure Get_Suffix_Ptr (Length, Ptr : Address);
1038 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_debuggable_suffix_ptr");
1039
1040 Result : String_Access;
1041 Suffix_Length : Integer;
1042 Suffix_Ptr : Address;
1043
1044 begin
1045 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
1046 Result := new String (1 .. Suffix_Length);
1047
1048 if Suffix_Length > 0 then
1049 Strncpy (Result.all'Address, Suffix_Ptr, size_t (Suffix_Length));
1050 end if;
1051
1052 return Result;
1053 end Get_Debuggable_Suffix;
1054
1055 ---------------------------
1056 -- Get_Executable_Suffix --
1057 ---------------------------
1058
1059 function Get_Executable_Suffix return String_Access is
1060 procedure Get_Suffix_Ptr (Length, Ptr : Address);
1061 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_executable_suffix_ptr");
1062
1063 Result : String_Access;
1064 Suffix_Length : Integer;
1065 Suffix_Ptr : Address;
1066
1067 begin
1068 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
1069 Result := new String (1 .. Suffix_Length);
1070
1071 if Suffix_Length > 0 then
1072 Strncpy (Result.all'Address, Suffix_Ptr, size_t (Suffix_Length));
1073 end if;
1074
1075 return Result;
1076 end Get_Executable_Suffix;
1077
1078 -----------------------
1079 -- Get_Object_Suffix --
1080 -----------------------
1081
1082 function Get_Object_Suffix return String_Access is
1083 procedure Get_Suffix_Ptr (Length, Ptr : Address);
1084 pragma Import (C, Get_Suffix_Ptr, "__gnat_get_object_suffix_ptr");
1085
1086 Result : String_Access;
1087 Suffix_Length : Integer;
1088 Suffix_Ptr : Address;
1089
1090 begin
1091 Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
1092 Result := new String (1 .. Suffix_Length);
1093
1094 if Suffix_Length > 0 then
1095 Strncpy (Result.all'Address, Suffix_Ptr, size_t (Suffix_Length));
1096 end if;
1097
1098 return Result;
1099 end Get_Object_Suffix;
1100
1101 ----------------------------------
1102 -- Get_Target_Debuggable_Suffix --
1103 ----------------------------------
1104
1105 function Get_Target_Debuggable_Suffix return String_Access is
1106 Target_Exec_Ext_Ptr : Address;
1107 pragma Import
1108 (C, Target_Exec_Ext_Ptr, "__gnat_target_debuggable_extension");
1109
1110 Result : String_Access;
1111 Suffix_Length : Integer;
1112
1113 begin
1114 Suffix_Length := Integer (CRTL.strlen (Target_Exec_Ext_Ptr));
1115 Result := new String (1 .. Suffix_Length);
1116
1117 if Suffix_Length > 0 then
1118 Strncpy
1119 (Result.all'Address, Target_Exec_Ext_Ptr, size_t (Suffix_Length));
1120 end if;
1121
1122 return Result;
1123 end Get_Target_Debuggable_Suffix;
1124
1125 ----------------------------------
1126 -- Get_Target_Executable_Suffix --
1127 ----------------------------------
1128
1129 function Get_Target_Executable_Suffix return String_Access is
1130 Target_Exec_Ext_Ptr : Address;
1131 pragma Import
1132 (C, Target_Exec_Ext_Ptr, "__gnat_target_executable_extension");
1133
1134 Result : String_Access;
1135 Suffix_Length : Integer;
1136
1137 begin
1138 Suffix_Length := Integer (CRTL.strlen (Target_Exec_Ext_Ptr));
1139 Result := new String (1 .. Suffix_Length);
1140
1141 if Suffix_Length > 0 then
1142 Strncpy
1143 (Result.all'Address, Target_Exec_Ext_Ptr, size_t (Suffix_Length));
1144 end if;
1145
1146 return Result;
1147 end Get_Target_Executable_Suffix;
1148
1149 ------------------------------
1150 -- Get_Target_Object_Suffix --
1151 ------------------------------
1152
1153 function Get_Target_Object_Suffix return String_Access is
1154 Target_Object_Ext_Ptr : Address;
1155 pragma Import
1156 (C, Target_Object_Ext_Ptr, "__gnat_target_object_extension");
1157
1158 Result : String_Access;
1159 Suffix_Length : Integer;
1160
1161 begin
1162 Suffix_Length := Integer (CRTL.strlen (Target_Object_Ext_Ptr));
1163 Result := new String (1 .. Suffix_Length);
1164
1165 if Suffix_Length > 0 then
1166 Strncpy
1167 (Result.all'Address, Target_Object_Ext_Ptr, size_t (Suffix_Length));
1168 end if;
1169
1170 return Result;
1171 end Get_Target_Object_Suffix;
1172
1173 ------------
1174 -- Getenv --
1175 ------------
1176
1177 function Getenv (Name : String) return String_Access is
1178 procedure Get_Env_Value_Ptr (Name, Length, Ptr : Address);
1179 pragma Import (C, Get_Env_Value_Ptr, "__gnat_getenv");
1180
1181 Env_Value_Ptr : aliased Address;
1182 Env_Value_Length : aliased Integer;
1183 F_Name : aliased String (1 .. Name'Length + 1);
1184 Result : String_Access;
1185
1186 begin
1187 F_Name (1 .. Name'Length) := Name;
1188 F_Name (F_Name'Last) := ASCII.NUL;
1189
1190 Get_Env_Value_Ptr
1191 (F_Name'Address, Env_Value_Length'Address, Env_Value_Ptr'Address);
1192
1193 Result := new String (1 .. Env_Value_Length);
1194
1195 if Env_Value_Length > 0 then
1196 Strncpy
1197 (Result.all'Address, Env_Value_Ptr, size_t (Env_Value_Length));
1198 end if;
1199
1200 return Result;
1201 end Getenv;
1202
1203 ------------
1204 -- GM_Day --
1205 ------------
1206
1207 function GM_Day (Date : OS_Time) return Day_Type is
1208 D : Day_Type;
1209
1210 Y : Year_Type;
1211 Mo : Month_Type;
1212 H : Hour_Type;
1213 Mn : Minute_Type;
1214 S : Second_Type;
1215
1216 begin
1217 GM_Split (Date, Y, Mo, D, H, Mn, S);
1218 return D;
1219 end GM_Day;
1220
1221 -------------
1222 -- GM_Hour --
1223 -------------
1224
1225 function GM_Hour (Date : OS_Time) return Hour_Type is
1226 H : Hour_Type;
1227
1228 Y : Year_Type;
1229 Mo : Month_Type;
1230 D : Day_Type;
1231 Mn : Minute_Type;
1232 S : Second_Type;
1233
1234 begin
1235 GM_Split (Date, Y, Mo, D, H, Mn, S);
1236 return H;
1237 end GM_Hour;
1238
1239 ---------------
1240 -- GM_Minute --
1241 ---------------
1242
1243 function GM_Minute (Date : OS_Time) return Minute_Type is
1244 Mn : Minute_Type;
1245
1246 Y : Year_Type;
1247 Mo : Month_Type;
1248 D : Day_Type;
1249 H : Hour_Type;
1250 S : Second_Type;
1251
1252 begin
1253 GM_Split (Date, Y, Mo, D, H, Mn, S);
1254 return Mn;
1255 end GM_Minute;
1256
1257 --------------
1258 -- GM_Month --
1259 --------------
1260
1261 function GM_Month (Date : OS_Time) return Month_Type is
1262 Mo : Month_Type;
1263
1264 Y : Year_Type;
1265 D : Day_Type;
1266 H : Hour_Type;
1267 Mn : Minute_Type;
1268 S : Second_Type;
1269
1270 begin
1271 GM_Split (Date, Y, Mo, D, H, Mn, S);
1272 return Mo;
1273 end GM_Month;
1274
1275 ---------------
1276 -- GM_Second --
1277 ---------------
1278
1279 function GM_Second (Date : OS_Time) return Second_Type is
1280 S : Second_Type;
1281
1282 Y : Year_Type;
1283 Mo : Month_Type;
1284 D : Day_Type;
1285 H : Hour_Type;
1286 Mn : Minute_Type;
1287
1288 begin
1289 GM_Split (Date, Y, Mo, D, H, Mn, S);
1290 return S;
1291 end GM_Second;
1292
1293 --------------
1294 -- GM_Split --
1295 --------------
1296
1297 procedure GM_Split
1298 (Date : OS_Time;
1299 Year : out Year_Type;
1300 Month : out Month_Type;
1301 Day : out Day_Type;
1302 Hour : out Hour_Type;
1303 Minute : out Minute_Type;
1304 Second : out Second_Type)
1305 is
1306 procedure To_GM_Time
1307 (P_OS_Time : Address;
1308 P_Year : Address;
1309 P_Month : Address;
1310 P_Day : Address;
1311 P_Hours : Address;
1312 P_Mins : Address;
1313 P_Secs : Address);
1314 pragma Import (C, To_GM_Time, "__gnat_to_gm_time");
1315
1316 T : OS_Time := Date;
1317 Y : Integer;
1318 Mo : Integer;
1319 D : Integer;
1320 H : Integer;
1321 Mn : Integer;
1322 S : Integer;
1323
1324 begin
1325 -- Special case Invalid_Time which is handled differently between
1326 -- Windows and Linux: Linux will set to 1 second before 1970-01-01
1327 -- while Windows will set the time to 1970-01-01 with Second set to -1,
1328 -- which is not a valid value.
1329
1330 if Date = Invalid_Time then
1331 Year := 1969;
1332 Month := 12;
1333 Day := 31;
1334 Hour := 23;
1335 Minute := 59;
1336 Second := 59;
1337 return;
1338 end if;
1339
1340 -- Use the global lock because To_GM_Time is not thread safe
1341
1342 Locked_Processing : begin
1343 SSL.Lock_Task.all;
1344 To_GM_Time
1345 (P_OS_Time => T'Address,
1346 P_Year => Y'Address,
1347 P_Month => Mo'Address,
1348 P_Day => D'Address,
1349 P_Hours => H'Address,
1350 P_Mins => Mn'Address,
1351 P_Secs => S'Address);
1352 SSL.Unlock_Task.all;
1353
1354 exception
1355 when others =>
1356 SSL.Unlock_Task.all;
1357 raise;
1358 end Locked_Processing;
1359
1360 Year := Y + 1900;
1361 Month := Mo + 1;
1362
1363 -- May happen if To_GM_Time fails
1364
1365 if D = 0 then
1366 Day := 1;
1367 else
1368 Day := D;
1369 end if;
1370
1371 Hour := H;
1372 Minute := Mn;
1373 Second := S;
1374 end GM_Split;
1375
1376 ----------------
1377 -- GM_Time_Of --
1378 ----------------
1379
1380 function GM_Time_Of
1381 (Year : Year_Type;
1382 Month : Month_Type;
1383 Day : Day_Type;
1384 Hour : Hour_Type;
1385 Minute : Minute_Type;
1386 Second : Second_Type) return OS_Time
1387 is
1388 procedure To_OS_Time
1389 (P_OS_Time : Address;
1390 P_Year : Integer;
1391 P_Month : Integer;
1392 P_Day : Integer;
1393 P_Hours : Integer;
1394 P_Mins : Integer;
1395 P_Secs : Integer);
1396 pragma Import (C, To_OS_Time, "__gnat_to_os_time");
1397
1398 Result : OS_Time;
1399
1400 begin
1401 To_OS_Time
1402 (P_OS_Time => Result'Address,
1403 P_Year => Year - 1900,
1404 P_Month => Month - 1,
1405 P_Day => Day,
1406 P_Hours => Hour,
1407 P_Mins => Minute,
1408 P_Secs => Second);
1409 return Result;
1410 end GM_Time_Of;
1411
1412 -------------
1413 -- GM_Year --
1414 -------------
1415
1416 function GM_Year (Date : OS_Time) return Year_Type is
1417 Y : Year_Type;
1418
1419 Mo : Month_Type;
1420 D : Day_Type;
1421 H : Hour_Type;
1422 Mn : Minute_Type;
1423 S : Second_Type;
1424
1425 begin
1426 GM_Split (Date, Y, Mo, D, H, Mn, S);
1427 return Y;
1428 end GM_Year;
1429
1430 ----------------------
1431 -- Is_Absolute_Path --
1432 ----------------------
1433
1434 function Is_Absolute_Path (Name : String) return Boolean is
1435 function Is_Absolute_Path
1436 (Name : Address;
1437 Length : Integer) return Integer;
1438 pragma Import (C, Is_Absolute_Path, "__gnat_is_absolute_path");
1439 begin
1440 return Is_Absolute_Path (Name'Address, Name'Length) /= 0;
1441 end Is_Absolute_Path;
1442
1443 ---------------
1444 -- Is_Dirsep --
1445 ---------------
1446
1447 function Is_Dirsep (C : Character) return Boolean is
1448 begin
1449 return C = Directory_Separator or else C = '/';
1450 end Is_Dirsep;
1451
1452 ------------------
1453 -- Is_Directory --
1454 ------------------
1455
1456 function Is_Directory (Name : C_File_Name) return Boolean is
1457 function Is_Directory (Name : Address) return Integer;
1458 pragma Import (C, Is_Directory, "__gnat_is_directory");
1459 begin
1460 return Is_Directory (Name) /= 0;
1461 end Is_Directory;
1462
1463 function Is_Directory (Name : String) return Boolean is
1464 F_Name : String (1 .. Name'Length + 1);
1465 begin
1466 F_Name (1 .. Name'Length) := Name;
1467 F_Name (F_Name'Last) := ASCII.NUL;
1468 return Is_Directory (F_Name'Address);
1469 end Is_Directory;
1470
1471 -----------------------------
1472 -- Is_Read_Accessible_File --
1473 -----------------------------
1474
1475 function Is_Read_Accessible_File (Name : String) return Boolean is
1476 function Is_Read_Accessible_File (Name : Address) return Integer;
1477 pragma Import
1478 (C, Is_Read_Accessible_File, "__gnat_is_read_accessible_file");
1479 F_Name : String (1 .. Name'Length + 1);
1480
1481 begin
1482 F_Name (1 .. Name'Length) := Name;
1483 F_Name (F_Name'Last) := ASCII.NUL;
1484 return Is_Read_Accessible_File (F_Name'Address) /= 0;
1485 end Is_Read_Accessible_File;
1486
1487 ----------------------------
1488 -- Is_Owner_Readable_File --
1489 ----------------------------
1490
1491 function Is_Owner_Readable_File (Name : C_File_Name) return Boolean is
1492 function Is_Readable_File (Name : Address) return Integer;
1493 pragma Import (C, Is_Readable_File, "__gnat_is_readable_file");
1494 begin
1495 return Is_Readable_File (Name) /= 0;
1496 end Is_Owner_Readable_File;
1497
1498 function Is_Owner_Readable_File (Name : String) return Boolean is
1499 F_Name : String (1 .. Name'Length + 1);
1500 begin
1501 F_Name (1 .. Name'Length) := Name;
1502 F_Name (F_Name'Last) := ASCII.NUL;
1503 return Is_Owner_Readable_File (F_Name'Address);
1504 end Is_Owner_Readable_File;
1505
1506 ------------------------
1507 -- Is_Executable_File --
1508 ------------------------
1509
1510 function Is_Executable_File (Name : C_File_Name) return Boolean is
1511 function Is_Executable_File (Name : Address) return Integer;
1512 pragma Import (C, Is_Executable_File, "__gnat_is_executable_file");
1513 begin
1514 return Is_Executable_File (Name) /= 0;
1515 end Is_Executable_File;
1516
1517 function Is_Executable_File (Name : String) return Boolean is
1518 F_Name : String (1 .. Name'Length + 1);
1519 begin
1520 F_Name (1 .. Name'Length) := Name;
1521 F_Name (F_Name'Last) := ASCII.NUL;
1522 return Is_Executable_File (F_Name'Address);
1523 end Is_Executable_File;
1524
1525 ---------------------
1526 -- Is_Regular_File --
1527 ---------------------
1528
1529 function Is_Regular_File (Name : C_File_Name) return Boolean is
1530 function Is_Regular_File (Name : Address) return Integer;
1531 pragma Import (C, Is_Regular_File, "__gnat_is_regular_file");
1532 begin
1533 return Is_Regular_File (Name) /= 0;
1534 end Is_Regular_File;
1535
1536 function Is_Regular_File (Name : String) return Boolean is
1537 F_Name : String (1 .. Name'Length + 1);
1538 begin
1539 F_Name (1 .. Name'Length) := Name;
1540 F_Name (F_Name'Last) := ASCII.NUL;
1541 return Is_Regular_File (F_Name'Address);
1542 end Is_Regular_File;
1543
1544 ----------------------
1545 -- Is_Symbolic_Link --
1546 ----------------------
1547
1548 function Is_Symbolic_Link (Name : C_File_Name) return Boolean is
1549 function Is_Symbolic_Link (Name : Address) return Integer;
1550 pragma Import (C, Is_Symbolic_Link, "__gnat_is_symbolic_link");
1551 begin
1552 return Is_Symbolic_Link (Name) /= 0;
1553 end Is_Symbolic_Link;
1554
1555 function Is_Symbolic_Link (Name : String) return Boolean is
1556 F_Name : String (1 .. Name'Length + 1);
1557 begin
1558 F_Name (1 .. Name'Length) := Name;
1559 F_Name (F_Name'Last) := ASCII.NUL;
1560 return Is_Symbolic_Link (F_Name'Address);
1561 end Is_Symbolic_Link;
1562
1563 ------------------------------
1564 -- Is_Write_Accessible_File --
1565 ------------------------------
1566
1567 function Is_Write_Accessible_File (Name : String) return Boolean is
1568 function Is_Write_Accessible_File (Name : Address) return Integer;
1569 pragma Import
1570 (C, Is_Write_Accessible_File, "__gnat_is_write_accessible_file");
1571 F_Name : String (1 .. Name'Length + 1);
1572
1573 begin
1574 F_Name (1 .. Name'Length) := Name;
1575 F_Name (F_Name'Last) := ASCII.NUL;
1576 return Is_Write_Accessible_File (F_Name'Address) /= 0;
1577 end Is_Write_Accessible_File;
1578
1579 ----------------------------
1580 -- Is_Owner_Writable_File --
1581 ----------------------------
1582
1583 function Is_Owner_Writable_File (Name : C_File_Name) return Boolean is
1584 function Is_Writable_File (Name : Address) return Integer;
1585 pragma Import (C, Is_Writable_File, "__gnat_is_writable_file");
1586 begin
1587 return Is_Writable_File (Name) /= 0;
1588 end Is_Owner_Writable_File;
1589
1590 function Is_Owner_Writable_File (Name : String) return Boolean is
1591 F_Name : String (1 .. Name'Length + 1);
1592 begin
1593 F_Name (1 .. Name'Length) := Name;
1594 F_Name (F_Name'Last) := ASCII.NUL;
1595 return Is_Owner_Writable_File (F_Name'Address);
1596 end Is_Owner_Writable_File;
1597
1598 ----------
1599 -- Kill --
1600 ----------
1601
1602 procedure Kill (Pid : Process_Id; Hard_Kill : Boolean := True) is
1603 SIGKILL : constant := 9;
1604 SIGINT : constant := 2;
1605
1606 procedure C_Kill (Pid : Process_Id; Sig_Num : Integer);
1607 pragma Import (C, C_Kill, "__gnat_kill");
1608
1609 begin
1610 if Pid /= Invalid_Pid then
1611 if Hard_Kill then
1612 C_Kill (Pid, SIGKILL);
1613 else
1614 C_Kill (Pid, SIGINT);
1615 end if;
1616 end if;
1617 end Kill;
1618
1619 -----------------------
1620 -- Kill_Process_Tree --
1621 -----------------------
1622
1623 procedure Kill_Process_Tree
1624 (Pid : Process_Id; Hard_Kill : Boolean := True)
1625 is
1626 SIGKILL : constant := 9;
1627 SIGINT : constant := 2;
1628
1629 procedure C_Kill_PT (Pid : Process_Id; Sig_Num : Integer);
1630 pragma Import (C, C_Kill_PT, "__gnat_killprocesstree");
1631
1632 begin
1633 if Hard_Kill then
1634 C_Kill_PT (Pid, SIGKILL);
1635 else
1636 C_Kill_PT (Pid, SIGINT);
1637 end if;
1638 end Kill_Process_Tree;
1639
1640 -------------------------
1641 -- Locate_Exec_On_Path --
1642 -------------------------
1643
1644 function Locate_Exec_On_Path
1645 (Exec_Name : String;
1646 Current_Dir_On_Win : Boolean := False) return String_Access
1647 is
1648 function Locate_Exec_On_Path
1649 (C_Exec_Name : Address;
1650 Current_Dir_On_Win : Interfaces.C.int) return Address;
1651 pragma Import (C, Locate_Exec_On_Path, "__gnat_locate_exec_on_path");
1652
1653 C_Exec_Name : String (1 .. Exec_Name'Length + 1);
1654 Path_Addr : Address;
1655 Path_Len : Integer;
1656 Result : String_Access;
1657
1658 begin
1659 C_Exec_Name (1 .. Exec_Name'Length) := Exec_Name;
1660 C_Exec_Name (C_Exec_Name'Last) := ASCII.NUL;
1661
1662 Path_Addr := Locate_Exec_On_Path
1663 (C_Exec_Name'Address, (if Current_Dir_On_Win then 1 else 0));
1664 Path_Len := C_String_Length (Path_Addr);
1665
1666 if Path_Len = 0 then
1667 return null;
1668
1669 else
1670 Result := To_Path_String_Access (Path_Addr, Path_Len);
1671 CRTL.free (Path_Addr);
1672
1673 -- Always return an absolute path name
1674
1675 if not Is_Absolute_Path (Result.all) then
1676 declare
1677 Absolute_Path : constant String :=
1678 Normalize_Pathname (Result.all, Resolve_Links => False);
1679 begin
1680 Free (Result);
1681 Result := new String'(Absolute_Path);
1682 end;
1683 end if;
1684
1685 return Result;
1686 end if;
1687 end Locate_Exec_On_Path;
1688
1689 -------------------------
1690 -- Locate_Regular_File --
1691 -------------------------
1692
1693 function Locate_Regular_File
1694 (File_Name : C_File_Name;
1695 Path : C_File_Name) return String_Access
1696 is
1697 function Locate_Regular_File
1698 (C_File_Name, Path_Val : Address) return Address;
1699 pragma Import (C, Locate_Regular_File, "__gnat_locate_regular_file");
1700
1701 Path_Addr : Address;
1702 Path_Len : Integer;
1703 Result : String_Access;
1704
1705 begin
1706 Path_Addr := Locate_Regular_File (File_Name, Path);
1707 Path_Len := C_String_Length (Path_Addr);
1708
1709 if Path_Len = 0 then
1710 return null;
1711
1712 else
1713 Result := To_Path_String_Access (Path_Addr, Path_Len);
1714 CRTL.free (Path_Addr);
1715 return Result;
1716 end if;
1717 end Locate_Regular_File;
1718
1719 function Locate_Regular_File
1720 (File_Name : String;
1721 Path : String) return String_Access
1722 is
1723 C_File_Name : String (1 .. File_Name'Length + 1);
1724 C_Path : String (1 .. Path'Length + 1);
1725 Result : String_Access;
1726
1727 begin
1728 C_File_Name (1 .. File_Name'Length) := File_Name;
1729 C_File_Name (C_File_Name'Last) := ASCII.NUL;
1730
1731 C_Path (1 .. Path'Length) := Path;
1732 C_Path (C_Path'Last) := ASCII.NUL;
1733
1734 Result := Locate_Regular_File (C_File_Name'Address, C_Path'Address);
1735
1736 -- Always return an absolute path name
1737
1738 if Result /= null and then not Is_Absolute_Path (Result.all) then
1739 declare
1740 Absolute_Path : constant String := Normalize_Pathname (Result.all);
1741 begin
1742 Free (Result);
1743 Result := new String'(Absolute_Path);
1744 end;
1745 end if;
1746
1747 return Result;
1748 end Locate_Regular_File;
1749
1750 ------------------------
1751 -- Non_Blocking_Spawn --
1752 ------------------------
1753
1754 function Non_Blocking_Spawn
1755 (Program_Name : String;
1756 Args : Argument_List) return Process_Id
1757 is
1758 Junk : Integer;
1759 pragma Warnings (Off, Junk);
1760 Pid : Process_Id;
1761
1762 begin
1763 Spawn_Internal (Program_Name, Args, Junk, Pid, Blocking => False);
1764 return Pid;
1765 end Non_Blocking_Spawn;
1766
1767 function Non_Blocking_Spawn
1768 (Program_Name : String;
1769 Args : Argument_List;
1770 Output_File_Descriptor : File_Descriptor;
1771 Err_To_Out : Boolean := True) return Process_Id
1772 is
1773 Pid : Process_Id;
1774 Saved_Error : File_Descriptor := Invalid_FD; -- prevent warning
1775 Saved_Output : File_Descriptor;
1776
1777 begin
1778 if Output_File_Descriptor = Invalid_FD then
1779 return Invalid_Pid;
1780 end if;
1781
1782 -- Set standard output and, if specified, error to the temporary file
1783
1784 Saved_Output := Dup (Standout);
1785 Dup2 (Output_File_Descriptor, Standout);
1786
1787 if Err_To_Out then
1788 Saved_Error := Dup (Standerr);
1789 Dup2 (Output_File_Descriptor, Standerr);
1790 end if;
1791
1792 -- Spawn the program
1793
1794 Pid := Non_Blocking_Spawn (Program_Name, Args);
1795
1796 -- Restore the standard output and error
1797
1798 Dup2 (Saved_Output, Standout);
1799
1800 if Err_To_Out then
1801 Dup2 (Saved_Error, Standerr);
1802 end if;
1803
1804 -- And close the saved standard output and error file descriptors
1805
1806 Close (Saved_Output);
1807
1808 if Err_To_Out then
1809 Close (Saved_Error);
1810 end if;
1811
1812 return Pid;
1813 end Non_Blocking_Spawn;
1814
1815 function Non_Blocking_Spawn
1816 (Program_Name : String;
1817 Args : Argument_List;
1818 Output_File : String;
1819 Err_To_Out : Boolean := True) return Process_Id
1820 is
1821 Output_File_Descriptor : constant File_Descriptor :=
1822 Create_Output_Text_File (Output_File);
1823 Result : Process_Id;
1824
1825 begin
1826 -- Do not attempt to spawn if the output file could not be created
1827
1828 if Output_File_Descriptor = Invalid_FD then
1829 return Invalid_Pid;
1830
1831 else
1832 Result :=
1833 Non_Blocking_Spawn
1834 (Program_Name, Args, Output_File_Descriptor, Err_To_Out);
1835
1836 -- Close the file just created for the output, as the file descriptor
1837 -- cannot be used anywhere, being a local value. It is safe to do
1838 -- that, as the file descriptor has been duplicated to form
1839 -- standard output and error of the spawned process.
1840
1841 Close (Output_File_Descriptor);
1842
1843 return Result;
1844 end if;
1845 end Non_Blocking_Spawn;
1846
1847 function Non_Blocking_Spawn
1848 (Program_Name : String;
1849 Args : Argument_List;
1850 Stdout_File : String;
1851 Stderr_File : String) return Process_Id
1852 is
1853 Stderr_FD : constant File_Descriptor :=
1854 Create_Output_Text_File (Stderr_File);
1855 Stdout_FD : constant File_Descriptor :=
1856 Create_Output_Text_File (Stdout_File);
1857
1858 Result : Process_Id;
1859 Saved_Error : File_Descriptor;
1860 Saved_Output : File_Descriptor;
1861
1862 Dummy_Status : Boolean;
1863
1864 begin
1865 -- Do not attempt to spawn if the output files could not be created
1866
1867 if Stdout_FD = Invalid_FD or else Stderr_FD = Invalid_FD then
1868 return Invalid_Pid;
1869 end if;
1870
1871 -- Set standard output and error to the specified files
1872
1873 Saved_Output := Dup (Standout);
1874 Dup2 (Stdout_FD, Standout);
1875
1876 Saved_Error := Dup (Standerr);
1877 Dup2 (Stderr_FD, Standerr);
1878
1879 Set_Close_On_Exec (Saved_Output, True, Dummy_Status);
1880 Set_Close_On_Exec (Saved_Error, True, Dummy_Status);
1881
1882 -- Close the files just created for the output, as the file descriptors
1883 -- cannot be used anywhere, being local values. It is safe to do that,
1884 -- as the file descriptors have been duplicated to form standard output
1885 -- and standard error of the spawned process.
1886
1887 Close (Stdout_FD);
1888 Close (Stderr_FD);
1889
1890 -- Spawn the program
1891
1892 Result := Non_Blocking_Spawn (Program_Name, Args);
1893
1894 -- Restore the standard output and error
1895
1896 Dup2 (Saved_Output, Standout);
1897 Dup2 (Saved_Error, Standerr);
1898
1899 -- And close the saved standard output and error file descriptors
1900
1901 Close (Saved_Output);
1902 Close (Saved_Error);
1903
1904 return Result;
1905 end Non_Blocking_Spawn;
1906
1907 -------------------------------
1908 -- Non_Blocking_Wait_Process --
1909 -------------------------------
1910
1911 procedure Non_Blocking_Wait_Process
1912 (Pid : out Process_Id; Success : out Boolean)
1913 is
1914 Status : Integer;
1915
1916 function Portable_No_Block_Wait (S : Address) return Process_Id;
1917 pragma Import
1918 (C, Portable_No_Block_Wait, "__gnat_portable_no_block_wait");
1919
1920 begin
1921 Pid := Portable_No_Block_Wait (Status'Address);
1922 Success := (Status = 0);
1923
1924 if Pid = 0 then
1925 Pid := Invalid_Pid;
1926 end if;
1927 end Non_Blocking_Wait_Process;
1928
1929 -------------------------
1930 -- Normalize_Arguments --
1931 -------------------------
1932
1933 procedure Normalize_Arguments (Args : in out Argument_List) is
1934 procedure Quote_Argument (Arg : in out String_Access);
1935 -- Add quote around argument if it contains spaces (or HT characters)
1936
1937 C_Argument_Needs_Quote : Integer;
1938 pragma Import (C, C_Argument_Needs_Quote, "__gnat_argument_needs_quote");
1939 Argument_Needs_Quote : constant Boolean := C_Argument_Needs_Quote /= 0;
1940
1941 --------------------
1942 -- Quote_Argument --
1943 --------------------
1944
1945 procedure Quote_Argument (Arg : in out String_Access) is
1946 J : Positive := 1;
1947 Quote_Needed : Boolean := False;
1948 Res : String (1 .. Arg'Length * 2 + 2);
1949
1950 begin
1951 if Arg (Arg'First) /= '"' or else Arg (Arg'Last) /= '"' then
1952
1953 -- Starting quote
1954
1955 Res (J) := '"';
1956
1957 for K in Arg'Range loop
1958
1959 J := J + 1;
1960
1961 if Arg (K) = '"' then
1962 Res (J) := '\';
1963 J := J + 1;
1964 Res (J) := '"';
1965 Quote_Needed := True;
1966
1967 elsif Arg (K) = ' ' or else Arg (K) = ASCII.HT then
1968 Res (J) := Arg (K);
1969 Quote_Needed := True;
1970
1971 else
1972 Res (J) := Arg (K);
1973 end if;
1974 end loop;
1975
1976 if Quote_Needed then
1977
1978 -- Case of null terminated string
1979
1980 if Res (J) = ASCII.NUL then
1981
1982 -- If the string ends with \, double it
1983
1984 pragma Annotate (CodePeer, Modified, Res (J - 1));
1985
1986 if Res (J - 1) = '\' then
1987 Res (J) := '\';
1988 J := J + 1;
1989 end if;
1990
1991 -- Put a quote just before the null at the end
1992
1993 Res (J) := '"';
1994 J := J + 1;
1995 Res (J) := ASCII.NUL;
1996
1997 -- If argument is terminated by '\', then double it. Otherwise
1998 -- the ending quote will be taken as-is. This is quite strange
1999 -- spawn behavior from Windows, but this is what we see.
2000
2001 else
2002 if Res (J) = '\' then
2003 J := J + 1;
2004 Res (J) := '\';
2005 end if;
2006
2007 -- Ending quote
2008
2009 J := J + 1;
2010 Res (J) := '"';
2011 end if;
2012
2013 declare
2014 Old : String_Access := Arg;
2015
2016 begin
2017 Arg := new String'(Res (1 .. J));
2018 Free (Old);
2019 end;
2020 end if;
2021
2022 end if;
2023 end Quote_Argument;
2024
2025 -- Start of processing for Normalize_Arguments
2026
2027 begin
2028 if Argument_Needs_Quote then
2029 for K in Args'Range loop
2030 if Args (K) /= null and then Args (K)'Length /= 0 then
2031 Quote_Argument (Args (K));
2032 end if;
2033 end loop;
2034 end if;
2035 end Normalize_Arguments;
2036
2037 ------------------------
2038 -- Normalize_Pathname --
2039 ------------------------
2040
2041 function Normalize_Pathname
2042 (Name : String;
2043 Directory : String := "";
2044 Resolve_Links : Boolean := True;
2045 Case_Sensitive : Boolean := True) return String
2046 is
2047 procedure Get_Current_Dir
2048 (Dir : System.Address;
2049 Length : System.Address);
2050 pragma Import (C, Get_Current_Dir, "__gnat_get_current_dir");
2051
2052 function Get_File_Names_Case_Sensitive return Integer;
2053 pragma Import
2054 (C, Get_File_Names_Case_Sensitive,
2055 "__gnat_get_file_names_case_sensitive");
2056
2057 Max_Path : Integer;
2058 pragma Import (C, Max_Path, "__gnat_max_path_len");
2059 -- Maximum length of a path name
2060
2061 function Readlink
2062 (Path : System.Address;
2063 Buf : System.Address;
2064 Bufsiz : size_t) return Integer;
2065 pragma Import (C, Readlink, "__gnat_readlink");
2066
2067 Fold_To_Lower_Case : constant Boolean :=
2068 not Case_Sensitive
2069 and then Get_File_Names_Case_Sensitive = 0;
2070
2071 Cur_Dir_Len : Natural := 0;
2072 End_Path : Natural := Name'Length;
2073 Last : Positive := 1;
2074 Path_Buffer : String (1 .. End_Path + 2 * Max_Path + 4);
2075 -- We need to potentially store in this buffer the following elements:
2076 -- the path itself, the current directory if the path is relative,
2077 -- and additional fragments up to Max_Path in length in case
2078 -- there are any symlinks.
2079
2080 function Final_Value (S : String) return String;
2081 -- Make final adjustment to the returned string. This function strips
2082 -- trailing directory separators, and folds returned string to lower
2083 -- case if required.
2084
2085 procedure Fill_Directory (Drive_Only : Boolean := False);
2086 -- Fill Cur_Dir and Cur_Dir_Len with Directory and ending directory
2087 -- separator or with current directory if Directory is not defined.
2088 -- If Drive_Only is True takes only Drive letter with colon and
2089 -- directory separator from Directory parameter or from current
2090 -- directory if Directory parameter is empty.
2091
2092 function Is_With_Drive (Name : String) return Boolean;
2093 pragma Inline (Is_With_Drive);
2094 -- Returns True only if the Name is including a drive
2095 -- letter at start.
2096
2097 function Drive_Letter_Omitted (Name : String) return Boolean;
2098 -- Name must be an absolute path. Returns True if and only if
2099 -- Name doesn't start with a drive letter and Name is not a
2100 -- UNC path.
2101
2102 -------------------
2103 -- Is_With_Drive --
2104 -------------------
2105
2106 function Is_With_Drive (Name : String) return Boolean is
2107 begin
2108 return Name'Length > 1
2109 and then Name (Name'First + 1) = ':'
2110 and then (Name (Name'First) in 'a' .. 'z'
2111 or else Name (Name'First) in 'A' .. 'Z');
2112 end Is_With_Drive;
2113
2114 --------------------------
2115 -- Drive_Letter_Omitted --
2116 --------------------------
2117
2118 function Drive_Letter_Omitted (Name : String) return Boolean is
2119 begin
2120 return On_Windows
2121 and then not Is_With_Drive (Name)
2122 and then (Name'Length < 2 -- not \\name case
2123 or else Name (Name'First)
2124 /= Directory_Separator
2125 or else Name (Name'First + 1)
2126 /= Directory_Separator);
2127 end Drive_Letter_Omitted;
2128
2129 -----------------
2130 -- Final_Value --
2131 -----------------
2132
2133 function Final_Value (S : String) return String is
2134 S1 : String := S;
2135 -- We may need to fold S to lower case, so we need a variable
2136
2137 Last : Natural;
2138
2139 begin
2140 if Fold_To_Lower_Case then
2141 System.Case_Util.To_Lower (S1);
2142 end if;
2143
2144 -- Remove trailing directory separator, if any
2145
2146 Last := S1'Last;
2147
2148 if Last > 1 and then Is_Dirsep (S1 (Last))
2149 and then not
2150 (On_Windows -- Special case for Windows: C:\
2151 and then Last = 3
2152 and then S1 (1) /= Directory_Separator
2153 and then S1 (2) = ':')
2154 then
2155 Last := Last - 1;
2156 end if;
2157
2158 -- And ensure that there is a trailing directory separator if the
2159 -- path contains only a drive letter.
2160
2161 if On_Windows
2162 and then Last = 2
2163 and then S1 (1) /= Directory_Separator
2164 and then S1 (2) = ':'
2165 then
2166 return S1 (1 .. Last) & Directory_Separator;
2167 else
2168 return S1 (1 .. Last);
2169 end if;
2170 end Final_Value;
2171
2172 --------------------
2173 -- Fill_Directory --
2174 --------------------
2175
2176 procedure Fill_Directory (Drive_Only : Boolean := False) is
2177 begin
2178 if Drive_Only and then Is_With_Drive (Directory) then
2179 Path_Buffer (1 .. 3) :=
2180 Directory (Directory'First .. Directory'First + 2);
2181
2182 elsif Directory = ""
2183 or else not Is_Absolute_Path (Directory)
2184 or else Drive_Letter_Omitted (Directory)
2185 then
2186 -- Directory name not given or it is not absolute or without drive
2187 -- letter on Windows, get current directory.
2188
2189 Cur_Dir_Len := Max_Path;
2190
2191 Get_Current_Dir (Path_Buffer'Address, Cur_Dir_Len'Address);
2192
2193 if Cur_Dir_Len = 0 then
2194 raise Program_Error;
2195 end if;
2196
2197 if not Resolve_Links then
2198 Last := Cur_Dir_Len;
2199 end if;
2200
2201 if not Drive_Only and then Directory /= "" then
2202 if On_Windows and then Is_Absolute_Path (Directory) then
2203 -- Drive letter taken from current directory but directory
2204 -- itself taken from Directory parameter.
2205
2206 Path_Buffer (3 .. Directory'Length + 2) := Directory;
2207 Cur_Dir_Len := Directory'Length + 2;
2208 Last := 3;
2209
2210 else
2211 -- Append relative Directory to current directory
2212
2213 Path_Buffer
2214 (Cur_Dir_Len + 1 .. Cur_Dir_Len + Directory'Length) :=
2215 Directory;
2216 Cur_Dir_Len := Cur_Dir_Len + Directory'Length;
2217 end if;
2218 end if;
2219
2220 elsif Directory'Length >= Path_Buffer'Length then
2221 raise Constraint_Error with "Directory name to big";
2222
2223 else
2224 Path_Buffer (1 .. Directory'Length) := Directory;
2225 Cur_Dir_Len := Directory'Length;
2226 end if;
2227
2228 if Drive_Only then
2229 -- When we need only drive letter from current directory on
2230 -- Windows
2231
2232 Cur_Dir_Len := 3;
2233 Last := Cur_Dir_Len;
2234
2235 elsif not Is_Dirsep (Path_Buffer (Cur_Dir_Len)) then
2236 Cur_Dir_Len := Cur_Dir_Len + 1;
2237 Path_Buffer (Cur_Dir_Len) := Directory_Separator;
2238 end if;
2239 end Fill_Directory;
2240
2241 -- Local variables
2242
2243 Max_Iterations : constant := 500;
2244
2245 Link_Buffer : String (1 .. Max_Path + 2);
2246
2247 Finish : Positive;
2248 Start : Positive;
2249 Status : Integer;
2250
2251 -- Start of processing for Normalize_Pathname
2252
2253 begin
2254 -- Special case, return null if name is null
2255
2256 if End_Path = 0 then
2257 return "";
2258 end if;
2259
2260 if Is_Absolute_Path (Name) then
2261 if Drive_Letter_Omitted (Name) then
2262 Fill_Directory (Drive_Only => True);
2263
2264 -- Take only drive letter part with colon
2265
2266 End_Path := End_Path + 2;
2267 Path_Buffer (3 .. End_Path) := Name;
2268
2269 else
2270 Path_Buffer (1 .. End_Path) := Name;
2271 end if;
2272
2273 else
2274 -- If this is a relative pathname, prepend current directory
2275 Fill_Directory;
2276 Path_Buffer (Cur_Dir_Len + 1 .. Cur_Dir_Len + End_Path) := Name;
2277 End_Path := Cur_Dir_Len + End_Path;
2278 end if;
2279
2280 -- Special handling for Windows:
2281 -- * Replace all '/' by '\'
2282 -- * Check the drive letter
2283 -- * Remove all double-quotes
2284
2285 if On_Windows then
2286 -- Replace all '/' by '\'
2287
2288 for Index in 1 .. End_Path loop
2289 if Path_Buffer (Index) = '/' then
2290 Path_Buffer (Index) := Directory_Separator;
2291 end if;
2292 end loop;
2293
2294 -- Ensure drive letter is upper-case
2295
2296 if Path_Buffer (1) in 'a' .. 'z' then
2297 System.Case_Util.To_Upper (Path_Buffer (1 .. 1));
2298 end if;
2299
2300 -- Remove all double-quotes that are possibly part of the
2301 -- path but can cause problems with other methods.
2302
2303 declare
2304 Index : Natural;
2305
2306 begin
2307 Index := Path_Buffer'First;
2308 for Current in Path_Buffer'First .. End_Path loop
2309 if Path_Buffer (Current) /= '"' then
2310 Path_Buffer (Index) := Path_Buffer (Current);
2311 Index := Index + 1;
2312 end if;
2313 end loop;
2314
2315 End_Path := Index - 1;
2316 end;
2317 end if;
2318
2319 -- Start the conversions
2320
2321 -- If this is not finished after Max_Iterations, give up and return an
2322 -- empty string.
2323
2324 for J in 1 .. Max_Iterations loop
2325 Start := Last + 1;
2326 Finish := Last;
2327
2328 -- Ensure that Windows UNC path is preserved, e.g: \\server\drive-c
2329
2330 if Start = 2
2331 and then Directory_Separator = '\'
2332 and then Path_Buffer (1 .. 2) = "\\"
2333 then
2334 Start := 3;
2335 end if;
2336
2337 -- If we have traversed the full pathname, return it
2338
2339 if Start > End_Path then
2340 return Final_Value (Path_Buffer (1 .. End_Path));
2341 end if;
2342
2343 -- Remove duplicate directory separators
2344
2345 while Path_Buffer (Start) = Directory_Separator loop
2346 if Start = End_Path then
2347 return Final_Value (Path_Buffer (1 .. End_Path - 1));
2348
2349 else
2350 Path_Buffer (Start .. End_Path - 1) :=
2351 Path_Buffer (Start + 1 .. End_Path);
2352 End_Path := End_Path - 1;
2353 end if;
2354 end loop;
2355
2356 -- Find the end of the current field: last character or the one
2357 -- preceding the next directory separator.
2358
2359 while Finish < End_Path
2360 and then Path_Buffer (Finish + 1) /= Directory_Separator
2361 loop
2362 Finish := Finish + 1;
2363 end loop;
2364
2365 -- Remove "." field
2366
2367 if Start = Finish and then Path_Buffer (Start) = '.' then
2368 if Start = End_Path then
2369 if Last = 1 then
2370 return (1 => Directory_Separator);
2371 else
2372 if Fold_To_Lower_Case then
2373 System.Case_Util.To_Lower (Path_Buffer (1 .. Last - 1));
2374 end if;
2375
2376 return Path_Buffer (1 .. Last - 1);
2377 end if;
2378
2379 else
2380 Path_Buffer (Last + 1 .. End_Path - 2) :=
2381 Path_Buffer (Last + 3 .. End_Path);
2382 End_Path := End_Path - 2;
2383 end if;
2384
2385 -- Remove ".." fields
2386
2387 elsif Finish = Start + 1
2388 and then Path_Buffer (Start .. Finish) = ".."
2389 then
2390 if Last > 1 then
2391 Start := Last - 1;
2392
2393 while Start > 1
2394 and then Path_Buffer (Start) /= Directory_Separator
2395 loop
2396 Start := Start - 1;
2397 end loop;
2398
2399 else
2400 Start := Last;
2401 end if;
2402
2403 if Start = 1 then
2404 if Finish = End_Path then
2405 return (1 => Directory_Separator);
2406
2407 else
2408 Path_Buffer (1 .. End_Path - Finish) :=
2409 Path_Buffer (Finish + 1 .. End_Path);
2410 End_Path := End_Path - Finish;
2411 Last := 1;
2412 end if;
2413
2414 else
2415 if Finish = End_Path then
2416 return Final_Value (Path_Buffer (1 .. Start - 1));
2417
2418 else
2419 Path_Buffer (Start + 1 .. Start + End_Path - Finish - 1) :=
2420 Path_Buffer (Finish + 2 .. End_Path);
2421 End_Path := Start + End_Path - Finish - 1;
2422 Last := Start;
2423 end if;
2424 end if;
2425
2426 -- Check if current field is a symbolic link
2427
2428 elsif Resolve_Links then
2429 declare
2430 Saved : constant Character := Path_Buffer (Finish + 1);
2431
2432 begin
2433 Path_Buffer (Finish + 1) := ASCII.NUL;
2434 Status :=
2435 Readlink
2436 (Path => Path_Buffer'Address,
2437 Buf => Link_Buffer'Address,
2438 Bufsiz => Link_Buffer'Length);
2439 Path_Buffer (Finish + 1) := Saved;
2440 end;
2441
2442 -- Not a symbolic link, move to the next field, if any
2443
2444 if Status <= 0 then
2445 Last := Finish + 1;
2446
2447 -- Replace symbolic link with its value
2448
2449 else
2450 if Is_Absolute_Path (Link_Buffer (1 .. Status)) then
2451 Path_Buffer (Status + 1 .. End_Path - (Finish - Status)) :=
2452 Path_Buffer (Finish + 1 .. End_Path);
2453 End_Path := End_Path - (Finish - Status);
2454 Path_Buffer (1 .. Status) := Link_Buffer (1 .. Status);
2455 Last := 1;
2456
2457 else
2458 Path_Buffer
2459 (Last + Status + 1 .. End_Path - Finish + Last + Status) :=
2460 Path_Buffer (Finish + 1 .. End_Path);
2461 End_Path := End_Path - Finish + Last + Status;
2462 Path_Buffer (Last + 1 .. Last + Status) :=
2463 Link_Buffer (1 .. Status);
2464 end if;
2465 end if;
2466
2467 else
2468 Last := Finish + 1;
2469 end if;
2470 end loop;
2471
2472 -- Too many iterations: give up
2473
2474 -- This can happen when there is a circularity in the symbolic links: A
2475 -- is a symbolic link for B, which itself is a symbolic link, and the
2476 -- target of B or of another symbolic link target of B is A. In this
2477 -- case, we return an empty string to indicate failure to resolve.
2478
2479 return "";
2480 end Normalize_Pathname;
2481
2482 -----------------
2483 -- Open_Append --
2484 -----------------
2485
2486 function Open_Append
2487 (Name : C_File_Name;
2488 Fmode : Mode) return File_Descriptor
2489 is
2490 function C_Open_Append
2491 (Name : C_File_Name;
2492 Fmode : Mode) return File_Descriptor;
2493 pragma Import (C, C_Open_Append, "__gnat_open_append");
2494 begin
2495 return C_Open_Append (Name, Fmode);
2496 end Open_Append;
2497
2498 function Open_Append
2499 (Name : String;
2500 Fmode : Mode) return File_Descriptor
2501 is
2502 C_Name : String (1 .. Name'Length + 1);
2503 begin
2504 C_Name (1 .. Name'Length) := Name;
2505 C_Name (C_Name'Last) := ASCII.NUL;
2506 return Open_Append (C_Name (C_Name'First)'Address, Fmode);
2507 end Open_Append;
2508
2509 ---------------
2510 -- Open_Read --
2511 ---------------
2512
2513 function Open_Read
2514 (Name : C_File_Name;
2515 Fmode : Mode) return File_Descriptor
2516 is
2517 function C_Open_Read
2518 (Name : C_File_Name;
2519 Fmode : Mode) return File_Descriptor;
2520 pragma Import (C, C_Open_Read, "__gnat_open_read");
2521 begin
2522 return C_Open_Read (Name, Fmode);
2523 end Open_Read;
2524
2525 function Open_Read
2526 (Name : String;
2527 Fmode : Mode) return File_Descriptor
2528 is
2529 C_Name : String (1 .. Name'Length + 1);
2530 begin
2531 C_Name (1 .. Name'Length) := Name;
2532 C_Name (C_Name'Last) := ASCII.NUL;
2533 return Open_Read (C_Name (C_Name'First)'Address, Fmode);
2534 end Open_Read;
2535
2536 ---------------------
2537 -- Open_Read_Write --
2538 ---------------------
2539
2540 function Open_Read_Write
2541 (Name : C_File_Name;
2542 Fmode : Mode) return File_Descriptor
2543 is
2544 function C_Open_Read_Write
2545 (Name : C_File_Name;
2546 Fmode : Mode) return File_Descriptor;
2547 pragma Import (C, C_Open_Read_Write, "__gnat_open_rw");
2548 begin
2549 return C_Open_Read_Write (Name, Fmode);
2550 end Open_Read_Write;
2551
2552 function Open_Read_Write
2553 (Name : String;
2554 Fmode : Mode) return File_Descriptor
2555 is
2556 C_Name : String (1 .. Name'Length + 1);
2557 begin
2558 C_Name (1 .. Name'Length) := Name;
2559 C_Name (C_Name'Last) := ASCII.NUL;
2560 return Open_Read_Write (C_Name (C_Name'First)'Address, Fmode);
2561 end Open_Read_Write;
2562
2563 -------------
2564 -- OS_Exit --
2565 -------------
2566
2567 procedure OS_Exit (Status : Integer) is
2568 begin
2569 OS_Exit_Ptr (Status);
2570 raise Program_Error;
2571 end OS_Exit;
2572
2573 ---------------------
2574 -- OS_Exit_Default --
2575 ---------------------
2576
2577 procedure OS_Exit_Default (Status : Integer) is
2578 procedure GNAT_OS_Exit (Status : Integer);
2579 pragma Import (C, GNAT_OS_Exit, "__gnat_os_exit");
2580 pragma No_Return (GNAT_OS_Exit);
2581 begin
2582 GNAT_OS_Exit (Status);
2583 end OS_Exit_Default;
2584
2585 --------------------
2586 -- Pid_To_Integer --
2587 --------------------
2588
2589 function Pid_To_Integer (Pid : Process_Id) return Integer is
2590 begin
2591 return Integer (Pid);
2592 end Pid_To_Integer;
2593
2594 ----------
2595 -- Read --
2596 ----------
2597
2598 function Read
2599 (FD : File_Descriptor;
2600 A : System.Address;
2601 N : Integer) return Integer
2602 is
2603 begin
2604 return
2605 Integer (System.CRTL.read
2606 (System.CRTL.int (FD),
2607 System.CRTL.chars (A),
2608 System.CRTL.size_t (N)));
2609 end Read;
2610
2611 -----------------
2612 -- Rename_File --
2613 -----------------
2614
2615 procedure Rename_File
2616 (Old_Name : C_File_Name;
2617 New_Name : C_File_Name;
2618 Success : out Boolean)
2619 is
2620 function rename (From, To : Address) return Integer;
2621 pragma Import (C, rename, "__gnat_rename");
2622 R : Integer;
2623
2624 begin
2625 R := rename (Old_Name, New_Name);
2626 Success := (R = 0);
2627 end Rename_File;
2628
2629 procedure Rename_File
2630 (Old_Name : String;
2631 New_Name : String;
2632 Success : out Boolean)
2633 is
2634 C_Old_Name : String (1 .. Old_Name'Length + 1);
2635 C_New_Name : String (1 .. New_Name'Length + 1);
2636
2637 begin
2638 C_Old_Name (1 .. Old_Name'Length) := Old_Name;
2639 C_Old_Name (C_Old_Name'Last) := ASCII.NUL;
2640 C_New_Name (1 .. New_Name'Length) := New_Name;
2641 C_New_Name (C_New_Name'Last) := ASCII.NUL;
2642 Rename_File (C_Old_Name'Address, C_New_Name'Address, Success);
2643 end Rename_File;
2644
2645 -----------------------
2646 -- Set_Close_On_Exec --
2647 -----------------------
2648
2649 procedure Set_Close_On_Exec
2650 (FD : File_Descriptor;
2651 Close_On_Exec : Boolean;
2652 Status : out Boolean)
2653 is
2654 function C_Set_Close_On_Exec
2655 (FD : File_Descriptor; Close_On_Exec : System.CRTL.int)
2656 return System.CRTL.int;
2657 pragma Import (C, C_Set_Close_On_Exec, "__gnat_set_close_on_exec");
2658 begin
2659 Status := C_Set_Close_On_Exec (FD, Boolean'Pos (Close_On_Exec)) = 0;
2660 end Set_Close_On_Exec;
2661
2662 --------------------
2663 -- Set_Executable --
2664 --------------------
2665
2666 procedure Set_Executable (Name : String; Mode : Positive := S_Owner) is
2667 procedure C_Set_Executable (Name : C_File_Name; Mode : Integer);
2668 pragma Import (C, C_Set_Executable, "__gnat_set_executable");
2669 C_Name : aliased String (Name'First .. Name'Last + 1);
2670
2671 begin
2672 C_Name (Name'Range) := Name;
2673 C_Name (C_Name'Last) := ASCII.NUL;
2674 C_Set_Executable (C_Name (C_Name'First)'Address, Mode);
2675 end Set_Executable;
2676
2677 -------------------------------------
2678 -- Set_File_Last_Modify_Time_Stamp --
2679 -------------------------------------
2680
2681 procedure Set_File_Last_Modify_Time_Stamp (Name : String; Time : OS_Time) is
2682 procedure C_Set_File_Time (Name : C_File_Name; Time : OS_Time);
2683 pragma Import (C, C_Set_File_Time, "__gnat_set_file_time_name");
2684 C_Name : aliased String (Name'First .. Name'Last + 1);
2685
2686 begin
2687 C_Name (Name'Range) := Name;
2688 C_Name (C_Name'Last) := ASCII.NUL;
2689 C_Set_File_Time (C_Name'Address, Time);
2690 end Set_File_Last_Modify_Time_Stamp;
2691
2692 ----------------------
2693 -- Set_Non_Readable --
2694 ----------------------
2695
2696 procedure Set_Non_Readable (Name : String) is
2697 procedure C_Set_Non_Readable (Name : C_File_Name);
2698 pragma Import (C, C_Set_Non_Readable, "__gnat_set_non_readable");
2699 C_Name : aliased String (Name'First .. Name'Last + 1);
2700
2701 begin
2702 C_Name (Name'Range) := Name;
2703 C_Name (C_Name'Last) := ASCII.NUL;
2704 C_Set_Non_Readable (C_Name (C_Name'First)'Address);
2705 end Set_Non_Readable;
2706
2707 ----------------------
2708 -- Set_Non_Writable --
2709 ----------------------
2710
2711 procedure Set_Non_Writable (Name : String) is
2712 procedure C_Set_Non_Writable (Name : C_File_Name);
2713 pragma Import (C, C_Set_Non_Writable, "__gnat_set_non_writable");
2714 C_Name : aliased String (Name'First .. Name'Last + 1);
2715
2716 begin
2717 C_Name (Name'Range) := Name;
2718 C_Name (C_Name'Last) := ASCII.NUL;
2719 C_Set_Non_Writable (C_Name (C_Name'First)'Address);
2720 end Set_Non_Writable;
2721
2722 ------------------
2723 -- Set_Readable --
2724 ------------------
2725
2726 procedure Set_Readable (Name : String) is
2727 procedure C_Set_Readable (Name : C_File_Name);
2728 pragma Import (C, C_Set_Readable, "__gnat_set_readable");
2729 C_Name : aliased String (Name'First .. Name'Last + 1);
2730
2731 begin
2732 C_Name (Name'Range) := Name;
2733 C_Name (C_Name'Last) := ASCII.NUL;
2734 C_Set_Readable (C_Name (C_Name'First)'Address);
2735 end Set_Readable;
2736
2737 --------------------
2738 -- Set_Writable --
2739 --------------------
2740
2741 procedure Set_Writable (Name : String) is
2742 procedure C_Set_Writable (Name : C_File_Name);
2743 pragma Import (C, C_Set_Writable, "__gnat_set_writable");
2744 C_Name : aliased String (Name'First .. Name'Last + 1);
2745
2746 begin
2747 C_Name (Name'Range) := Name;
2748 C_Name (C_Name'Last) := ASCII.NUL;
2749 C_Set_Writable (C_Name (C_Name'First)'Address);
2750 end Set_Writable;
2751
2752 ------------
2753 -- Setenv --
2754 ------------
2755
2756 procedure Setenv (Name : String; Value : String) is
2757 F_Name : String (1 .. Name'Length + 1);
2758 F_Value : String (1 .. Value'Length + 1);
2759
2760 procedure Set_Env_Value (Name, Value : System.Address);
2761 pragma Import (C, Set_Env_Value, "__gnat_setenv");
2762
2763 begin
2764 F_Name (1 .. Name'Length) := Name;
2765 F_Name (F_Name'Last) := ASCII.NUL;
2766
2767 F_Value (1 .. Value'Length) := Value;
2768 F_Value (F_Value'Last) := ASCII.NUL;
2769
2770 Set_Env_Value (F_Name'Address, F_Value'Address);
2771 end Setenv;
2772
2773 -----------
2774 -- Spawn --
2775 -----------
2776
2777 function Spawn
2778 (Program_Name : String;
2779 Args : Argument_List) return Integer
2780 is
2781 Junk : Process_Id;
2782 pragma Warnings (Off, Junk);
2783 Result : Integer;
2784
2785 begin
2786 Spawn_Internal (Program_Name, Args, Result, Junk, Blocking => True);
2787 return Result;
2788 end Spawn;
2789
2790 procedure Spawn
2791 (Program_Name : String;
2792 Args : Argument_List;
2793 Success : out Boolean)
2794 is
2795 begin
2796 Success := (Spawn (Program_Name, Args) = 0);
2797 end Spawn;
2798
2799 procedure Spawn
2800 (Program_Name : String;
2801 Args : Argument_List;
2802 Output_File_Descriptor : File_Descriptor;
2803 Return_Code : out Integer;
2804 Err_To_Out : Boolean := True)
2805 is
2806 Saved_Error : File_Descriptor := Invalid_FD; -- prevent compiler warning
2807 Saved_Output : File_Descriptor;
2808
2809 begin
2810 -- Set standard output and error to the temporary file
2811
2812 Saved_Output := Dup (Standout);
2813 Dup2 (Output_File_Descriptor, Standout);
2814
2815 if Err_To_Out then
2816 Saved_Error := Dup (Standerr);
2817 Dup2 (Output_File_Descriptor, Standerr);
2818 end if;
2819
2820 -- Spawn the program
2821
2822 Return_Code := Spawn (Program_Name, Args);
2823
2824 -- Restore the standard output and error
2825
2826 Dup2 (Saved_Output, Standout);
2827
2828 if Err_To_Out then
2829 Dup2 (Saved_Error, Standerr);
2830 end if;
2831
2832 -- And close the saved standard output and error file descriptors
2833
2834 Close (Saved_Output);
2835
2836 if Err_To_Out then
2837 Close (Saved_Error);
2838 end if;
2839 end Spawn;
2840
2841 procedure Spawn
2842 (Program_Name : String;
2843 Args : Argument_List;
2844 Output_File : String;
2845 Success : out Boolean;
2846 Return_Code : out Integer;
2847 Err_To_Out : Boolean := True)
2848 is
2849 FD : File_Descriptor;
2850
2851 begin
2852 Success := True;
2853 Return_Code := 0;
2854
2855 FD := Create_Output_Text_File (Output_File);
2856
2857 if FD = Invalid_FD then
2858 Success := False;
2859 return;
2860 end if;
2861
2862 Spawn (Program_Name, Args, FD, Return_Code, Err_To_Out);
2863
2864 Close (FD, Success);
2865 end Spawn;
2866
2867 --------------------
2868 -- Spawn_Internal --
2869 --------------------
2870
2871 procedure Spawn_Internal
2872 (Program_Name : String;
2873 Args : Argument_List;
2874 Result : out Integer;
2875 Pid : out Process_Id;
2876 Blocking : Boolean)
2877 is
2878 procedure Spawn (Args : Argument_List);
2879 -- Call Spawn with given argument list
2880
2881 N_Args : Argument_List (Args'Range);
2882 -- Normalized arguments
2883
2884 -----------
2885 -- Spawn --
2886 -----------
2887
2888 procedure Spawn (Args : Argument_List) is
2889 type Chars is array (Positive range <>) of aliased Character;
2890 type Char_Ptr is access constant Character;
2891
2892 Command_Len : constant Positive :=
2893 Program_Name'Length + 1 + Args_Length (Args);
2894 Command_Last : Natural := 0;
2895 Command : aliased Chars (1 .. Command_Len);
2896 -- Command contains all characters of the Program_Name and Args, all
2897 -- terminated by ASCII.NUL characters.
2898
2899 Arg_List_Len : constant Positive := Args'Length + 2;
2900 Arg_List_Last : Natural := 0;
2901 Arg_List : aliased array (1 .. Arg_List_Len) of Char_Ptr;
2902 -- List with pointers to NUL-terminated strings of the Program_Name
2903 -- and the Args and terminated with a null pointer. We rely on the
2904 -- default initialization for the last null pointer.
2905
2906 procedure Add_To_Command (S : String);
2907 -- Add S and a NUL character to Command, updating Last
2908
2909 function Portable_Spawn (Args : Address) return Integer;
2910 pragma Import (C, Portable_Spawn, "__gnat_portable_spawn");
2911
2912 function Portable_No_Block_Spawn (Args : Address) return Process_Id;
2913 pragma Import
2914 (C, Portable_No_Block_Spawn, "__gnat_portable_no_block_spawn");
2915
2916 --------------------
2917 -- Add_To_Command --
2918 --------------------
2919
2920 procedure Add_To_Command (S : String) is
2921 First : constant Natural := Command_Last + 1;
2922
2923 begin
2924 Command_Last := Command_Last + S'Length;
2925
2926 -- Move characters one at a time, because Command has aliased
2927 -- components.
2928
2929 -- But not volatile, so why is this necessary ???
2930
2931 for J in S'Range loop
2932 Command (First + J - S'First) := S (J);
2933 end loop;
2934
2935 Command_Last := Command_Last + 1;
2936 Command (Command_Last) := ASCII.NUL;
2937
2938 Arg_List_Last := Arg_List_Last + 1;
2939 Arg_List (Arg_List_Last) := Command (First)'Access;
2940 end Add_To_Command;
2941
2942 -- Start of processing for Spawn
2943
2944 begin
2945 Add_To_Command (Program_Name);
2946
2947 for J in Args'Range loop
2948 Add_To_Command (Args (J).all);
2949 end loop;
2950
2951 if Blocking then
2952 Pid := Invalid_Pid;
2953 Result := Portable_Spawn (Arg_List'Address);
2954 else
2955 Pid := Portable_No_Block_Spawn (Arg_List'Address);
2956 Result := Boolean'Pos (Pid /= Invalid_Pid);
2957 end if;
2958 end Spawn;
2959
2960 -- Start of processing for Spawn_Internal
2961
2962 begin
2963 -- Copy arguments into a local structure
2964
2965 for K in N_Args'Range loop
2966 N_Args (K) := new String'(Args (K).all);
2967 end loop;
2968
2969 -- Normalize those arguments
2970
2971 Normalize_Arguments (N_Args);
2972
2973 -- Call spawn using the normalized arguments
2974
2975 Spawn (N_Args);
2976
2977 -- Free arguments list
2978
2979 for K in N_Args'Range loop
2980 Free (N_Args (K));
2981 end loop;
2982 end Spawn_Internal;
2983
2984 ------------
2985 -- To_Ada --
2986 ------------
2987
2988 function To_Ada (Time : time_t) return OS_Time is
2989 begin
2990 return OS_Time (Time);
2991 end To_Ada;
2992
2993 ---------------------------
2994 -- To_Path_String_Access --
2995 ---------------------------
2996
2997 function To_Path_String_Access
2998 (Path_Addr : Address;
2999 Path_Len : Integer) return String_Access
3000 is
3001 subtype Path_String is String (1 .. Path_Len);
3002 type Path_String_Access is access Path_String;
3003
3004 function Address_To_Access is new Ada.Unchecked_Conversion
3005 (Source => Address, Target => Path_String_Access);
3006
3007 Path_Access : constant Path_String_Access :=
3008 Address_To_Access (Path_Addr);
3009
3010 Return_Val : String_Access;
3011
3012 begin
3013 Return_Val := new String (1 .. Path_Len);
3014
3015 for J in 1 .. Path_Len loop
3016 Return_Val (J) := Path_Access (J);
3017 end loop;
3018
3019 return Return_Val;
3020 end To_Path_String_Access;
3021
3022 ----------
3023 -- To_C --
3024 ----------
3025
3026 function To_C (Time : OS_Time) return time_t is
3027 begin
3028 return time_t (Time);
3029 end To_C;
3030
3031 ------------------
3032 -- Wait_Process --
3033 ------------------
3034
3035 procedure Wait_Process (Pid : out Process_Id; Success : out Boolean) is
3036 Status : Integer;
3037
3038 function Portable_Wait (S : Address) return Process_Id;
3039 pragma Import (C, Portable_Wait, "__gnat_portable_wait");
3040
3041 begin
3042 Pid := Portable_Wait (Status'Address);
3043 Success := (Status = 0);
3044 end Wait_Process;
3045
3046 -----------
3047 -- Write --
3048 -----------
3049
3050 function Write
3051 (FD : File_Descriptor;
3052 A : System.Address;
3053 N : Integer) return Integer
3054 is
3055 begin
3056 return
3057 Integer (System.CRTL.write
3058 (System.CRTL.int (FD),
3059 System.CRTL.chars (A),
3060 System.CRTL.size_t (N)));
3061 end Write;
3062
3063 end System.OS_Lib;