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