2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ada / g-dirope.adb
blob321b81244c4ee5fd5dd7e27794f2a732e890ae18
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T . D I R E C T O R Y _ O P E R A T I O N S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1998-2003 Ada Core Technologies, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 with Ada.Characters.Handling;
35 with Ada.Strings.Fixed;
36 with Ada.Strings.Maps;
37 with Unchecked_Deallocation;
38 with Unchecked_Conversion;
39 with System; use System;
41 with GNAT.OS_Lib;
43 package body GNAT.Directory_Operations is
45 use Ada;
47 type Dir_Type_Value is new System.Address;
48 -- This is the low-level address directory structure as returned by the C
49 -- opendir routine.
51 Filename_Max : constant Integer := 1024;
52 -- 1024 is the value of FILENAME_MAX in stdio.h
54 procedure Free is new
55 Unchecked_Deallocation (Dir_Type_Value, Dir_Type);
57 ---------------
58 -- Base_Name --
59 ---------------
61 function Base_Name
62 (Path : Path_Name;
63 Suffix : String := "")
64 return String
66 function Get_File_Names_Case_Sensitive return Integer;
67 pragma Import
68 (C, Get_File_Names_Case_Sensitive,
69 "__gnat_get_file_names_case_sensitive");
71 Case_Sensitive_File_Name : constant Boolean :=
72 Get_File_Names_Case_Sensitive = 1;
74 function Basename
75 (Path : Path_Name;
76 Suffix : String := "")
77 return String;
78 -- This function does the job. The only difference between Basename
79 -- and Base_Name (the parent function) is that the former is case
80 -- sensitive, while the latter is not. Path and Suffix are adjusted
81 -- appropriately before calling Basename under platforms where the
82 -- file system is not case sensitive.
84 --------------
85 -- Basename --
86 --------------
88 function Basename
89 (Path : Path_Name;
90 Suffix : String := "")
91 return String
93 Cut_Start : Natural :=
94 Strings.Fixed.Index
95 (Path, Dir_Seps, Going => Strings.Backward);
96 Cut_End : Natural;
98 begin
99 -- Cut_Start point to the first basename character
101 if Cut_Start = 0 then
102 Cut_Start := Path'First;
104 else
105 Cut_Start := Cut_Start + 1;
106 end if;
108 -- Cut_End point to the last basename character.
110 Cut_End := Path'Last;
112 -- If basename ends with Suffix, adjust Cut_End.
114 if Suffix /= ""
115 and then Path (Path'Last - Suffix'Length + 1 .. Cut_End) = Suffix
116 then
117 Cut_End := Path'Last - Suffix'Length;
118 end if;
120 Check_For_Standard_Dirs : declare
121 Offset : constant Integer := Path'First - Base_Name.Path'First;
122 BN : constant String :=
123 Base_Name.Path (Cut_Start - Offset .. Cut_End - Offset);
124 -- Here we use Base_Name.Path to keep the original casing
126 Has_Drive_Letter : constant Boolean :=
127 OS_Lib.Path_Separator /= ':';
128 -- If Path separator is not ':' then we are on a DOS based OS
129 -- where this character is used as a drive letter separator.
131 begin
132 if BN = "." or else BN = ".." then
133 return "";
135 elsif Has_Drive_Letter
136 and then BN'Length > 2
137 and then Characters.Handling.Is_Letter (BN (BN'First))
138 and then BN (BN'First + 1) = ':'
139 then
140 -- We have a DOS drive letter prefix, remove it
142 return BN (BN'First + 2 .. BN'Last);
144 else
145 return BN;
146 end if;
147 end Check_For_Standard_Dirs;
148 end Basename;
150 -- Start processing for Base_Name
152 begin
153 if Path'Length <= Suffix'Length then
154 return Path;
155 end if;
157 if Case_Sensitive_File_Name then
158 return Basename (Path, Suffix);
159 else
160 return Basename
161 (Characters.Handling.To_Lower (Path),
162 Characters.Handling.To_Lower (Suffix));
163 end if;
164 end Base_Name;
166 ----------------
167 -- Change_Dir --
168 ----------------
170 procedure Change_Dir (Dir_Name : Dir_Name_Str) is
171 C_Dir_Name : constant String := Dir_Name & ASCII.NUL;
173 function chdir (Dir_Name : String) return Integer;
174 pragma Import (C, chdir, "chdir");
176 begin
177 if chdir (C_Dir_Name) /= 0 then
178 raise Directory_Error;
179 end if;
180 end Change_Dir;
182 -----------
183 -- Close --
184 -----------
186 procedure Close (Dir : in out Dir_Type) is
188 function closedir (Directory : System.Address) return Integer;
189 pragma Import (C, closedir, "closedir");
191 Discard : Integer;
192 pragma Warnings (Off, Discard);
194 begin
195 if not Is_Open (Dir) then
196 raise Directory_Error;
197 end if;
199 Discard := closedir (System.Address (Dir.all));
200 Free (Dir);
201 end Close;
203 --------------
204 -- Dir_Name --
205 --------------
207 function Dir_Name (Path : Path_Name) return Dir_Name_Str is
208 Last_DS : constant Natural :=
209 Strings.Fixed.Index
210 (Path, Dir_Seps, Going => Strings.Backward);
212 begin
213 if Last_DS = 0 then
215 -- There is no directory separator, returns current working directory
217 return "." & Dir_Separator;
219 else
220 return Path (Path'First .. Last_DS);
221 end if;
222 end Dir_Name;
224 -----------------
225 -- Expand_Path --
226 -----------------
228 function Expand_Path
229 (Path : Path_Name;
230 Mode : Environment_Style := System_Default)
231 return Path_Name
233 Environment_Variable_Char : Character;
234 pragma Import (C, Environment_Variable_Char, "__gnat_environment_char");
236 Result : OS_Lib.String_Access := new String (1 .. 200);
237 Result_Last : Natural := 0;
239 procedure Append (C : Character);
240 procedure Append (S : String);
241 -- Append to Result
243 procedure Double_Result_Size;
244 -- Reallocate Result, doubling its size
246 function Is_Var_Prefix (C : Character) return Boolean;
247 pragma Inline (Is_Var_Prefix);
249 procedure Read (K : in out Positive);
250 -- Update Result while reading current Path starting at position K. If
251 -- a variable is found, call Var below.
253 procedure Var (K : in out Positive);
254 -- Translate variable name starting at position K with the associated
255 -- environment value.
257 ------------
258 -- Append --
259 ------------
261 procedure Append (C : Character) is
262 begin
263 if Result_Last = Result'Last then
264 Double_Result_Size;
265 end if;
267 Result_Last := Result_Last + 1;
268 Result (Result_Last) := C;
269 end Append;
271 procedure Append (S : String) is
272 begin
273 while Result_Last + S'Length - 1 > Result'Last loop
274 Double_Result_Size;
275 end loop;
277 Result (Result_Last + 1 .. Result_Last + S'Length) := S;
278 Result_Last := Result_Last + S'Length;
279 end Append;
281 ------------------------
282 -- Double_Result_Size --
283 ------------------------
285 procedure Double_Result_Size is
286 New_Result : constant OS_Lib.String_Access :=
287 new String (1 .. 2 * Result'Last);
289 begin
290 New_Result (1 .. Result_Last) := Result (1 .. Result_Last);
291 OS_Lib.Free (Result);
292 Result := New_Result;
293 end Double_Result_Size;
295 -------------------
296 -- Is_Var_Prefix --
297 -------------------
299 function Is_Var_Prefix (C : Character) return Boolean is
300 begin
301 return (C = Environment_Variable_Char and then Mode = System_Default)
302 or else
303 (C = '$' and then (Mode = UNIX or else Mode = Both))
304 or else
305 (C = '%' and then (Mode = DOS or else Mode = Both));
306 end Is_Var_Prefix;
308 ----------
309 -- Read --
310 ----------
312 procedure Read (K : in out Positive) is
313 P : Character;
314 begin
315 For_All_Characters : loop
316 if Is_Var_Prefix (Path (K)) then
317 P := Path (K);
319 -- Could be a variable
321 if K < Path'Last then
323 if Path (K + 1) = P then
325 -- Not a variable after all, this is a double $ or %,
326 -- just insert one in the result string.
328 Append (P);
329 K := K + 1;
331 else
332 -- Let's parse the variable
334 Var (K);
335 end if;
337 else
338 -- We have an ending $ or % sign
340 Append (P);
341 end if;
343 else
344 -- This is a standard character, just add it to the result
346 Append (Path (K));
347 end if;
349 -- Skip to next character
351 K := K + 1;
353 exit For_All_Characters when K > Path'Last;
354 end loop For_All_Characters;
355 end Read;
357 ---------
358 -- Var --
359 ---------
361 procedure Var (K : in out Positive) is
362 P : constant Character := Path (K);
363 T : Character;
364 E : Positive;
366 begin
367 K := K + 1;
369 if P = '%' or else Path (K) = '{' then
371 -- Set terminator character
373 if P = '%' then
374 T := '%';
375 else
376 T := '}';
377 K := K + 1;
378 end if;
380 -- Look for terminator character, k point to the first character
381 -- for the variable name.
383 E := K;
385 loop
386 E := E + 1;
387 exit when Path (E) = T or else E = Path'Last;
388 end loop;
390 if Path (E) = T then
392 -- OK found, translate with environment value
394 declare
395 Env : OS_Lib.String_Access :=
396 OS_Lib.Getenv (Path (K .. E - 1));
398 begin
399 Append (Env.all);
400 OS_Lib.Free (Env);
401 end;
403 else
404 -- No terminator character, not a variable after all or a
405 -- syntax error, ignore it, insert string as-is.
407 Append (P); -- Add prefix character
409 if T = '}' then -- If we were looking for curly bracket
410 Append ('{'); -- terminator, add the curly bracket
411 end if;
413 Append (Path (K .. E));
414 end if;
416 else
417 -- The variable name is everything from current position to first
418 -- non letter/digit character.
420 E := K;
422 -- Check that first chartacter is a letter
424 if Characters.Handling.Is_Letter (Path (E)) then
425 E := E + 1;
427 Var_Name : loop
428 exit Var_Name when E > Path'Last;
430 if Characters.Handling.Is_Letter (Path (E))
431 or else Characters.Handling.Is_Digit (Path (E))
432 then
433 E := E + 1;
434 else
435 exit Var_Name;
436 end if;
437 end loop Var_Name;
439 E := E - 1;
441 declare
442 Env : OS_Lib.String_Access := OS_Lib.Getenv (Path (K .. E));
444 begin
445 Append (Env.all);
446 OS_Lib.Free (Env);
447 end;
449 else
450 -- This is not a variable after all
452 Append ('$');
453 Append (Path (E));
454 end if;
456 end if;
458 K := E;
459 end Var;
461 -- Start of processing for Expand_Path
463 begin
464 declare
465 K : Positive := Path'First;
467 begin
468 Read (K);
470 declare
471 Returned_Value : constant String := Result (1 .. Result_Last);
473 begin
474 OS_Lib.Free (Result);
475 return Returned_Value;
476 end;
477 end;
478 end Expand_Path;
480 --------------------
481 -- File_Extension --
482 --------------------
484 function File_Extension (Path : Path_Name) return String is
485 First : Natural :=
486 Strings.Fixed.Index
487 (Path, Dir_Seps, Going => Strings.Backward);
489 Dot : Natural;
491 begin
492 if First = 0 then
493 First := Path'First;
494 end if;
496 Dot := Strings.Fixed.Index (Path (First .. Path'Last),
497 ".",
498 Going => Strings.Backward);
500 if Dot = 0 or else Dot = Path'Last then
501 return "";
502 else
503 return Path (Dot .. Path'Last);
504 end if;
505 end File_Extension;
507 ---------------
508 -- File_Name --
509 ---------------
511 function File_Name (Path : Path_Name) return String is
512 begin
513 return Base_Name (Path);
514 end File_Name;
516 ---------------------
517 -- Format_Pathname --
518 ---------------------
520 function Format_Pathname
521 (Path : Path_Name;
522 Style : Path_Style := System_Default)
523 return String
525 N_Path : String := Path;
526 K : Positive := N_Path'First;
527 Prev_Dirsep : Boolean := False;
529 begin
530 if Dir_Separator = '\'
531 and then Path'Length > 1
532 and then Path (K .. K + 1) = "\\"
533 then
534 if Style = UNIX then
535 N_Path (K .. K + 1) := "//";
536 end if;
538 K := K + 2;
539 end if;
541 for J in K .. Path'Last loop
542 if Strings.Maps.Is_In (Path (J), Dir_Seps) then
543 if not Prev_Dirsep then
544 case Style is
545 when UNIX => N_Path (K) := '/';
546 when DOS => N_Path (K) := '\';
547 when System_Default => N_Path (K) := Dir_Separator;
548 end case;
550 K := K + 1;
551 end if;
553 Prev_Dirsep := True;
555 else
556 N_Path (K) := Path (J);
557 K := K + 1;
558 Prev_Dirsep := False;
559 end if;
560 end loop;
562 return N_Path (N_Path'First .. K - 1);
563 end Format_Pathname;
565 ---------------------
566 -- Get_Current_Dir --
567 ---------------------
569 Max_Path : Integer;
570 pragma Import (C, Max_Path, "__gnat_max_path_len");
572 function Get_Current_Dir return Dir_Name_Str is
573 Current_Dir : String (1 .. Max_Path + 1);
574 Last : Natural;
576 begin
577 Get_Current_Dir (Current_Dir, Last);
578 return Current_Dir (1 .. Last);
579 end Get_Current_Dir;
581 procedure Get_Current_Dir (Dir : out Dir_Name_Str; Last : out Natural) is
582 Path_Len : Natural := Max_Path;
583 Buffer : String (Dir'First .. Dir'First + Max_Path + 1);
585 procedure Local_Get_Current_Dir
586 (Dir : System.Address;
587 Length : System.Address);
588 pragma Import (C, Local_Get_Current_Dir, "__gnat_get_current_dir");
590 begin
591 Local_Get_Current_Dir (Buffer'Address, Path_Len'Address);
593 if Dir'Length > Path_Len then
594 Last := Dir'First + Path_Len - 1;
595 else
596 Last := Dir'Last;
597 end if;
599 Dir (Buffer'First .. Last) := Buffer (Buffer'First .. Last);
600 end Get_Current_Dir;
602 -------------
603 -- Is_Open --
604 -------------
606 function Is_Open (Dir : Dir_Type) return Boolean is
607 begin
608 return Dir /= Null_Dir
609 and then System.Address (Dir.all) /= System.Null_Address;
610 end Is_Open;
612 --------------
613 -- Make_Dir --
614 --------------
616 procedure Make_Dir (Dir_Name : Dir_Name_Str) is
617 C_Dir_Name : constant String := Dir_Name & ASCII.NUL;
619 function mkdir (Dir_Name : String) return Integer;
620 pragma Import (C, mkdir, "__gnat_mkdir");
622 begin
623 if mkdir (C_Dir_Name) /= 0 then
624 raise Directory_Error;
625 end if;
626 end Make_Dir;
628 ----------
629 -- Open --
630 ----------
632 procedure Open
633 (Dir : out Dir_Type;
634 Dir_Name : Dir_Name_Str)
636 C_File_Name : constant String := Dir_Name & ASCII.NUL;
638 function opendir
639 (File_Name : String)
640 return Dir_Type_Value;
641 pragma Import (C, opendir, "opendir");
643 begin
644 Dir := new Dir_Type_Value'(opendir (C_File_Name));
646 if not Is_Open (Dir) then
647 Free (Dir);
648 Dir := Null_Dir;
649 raise Directory_Error;
650 end if;
651 end Open;
653 ----------
654 -- Read --
655 ----------
657 procedure Read
658 (Dir : in out Dir_Type;
659 Str : out String;
660 Last : out Natural)
662 Filename_Addr : Address;
663 Filename_Len : Integer;
665 Buffer : array (0 .. Filename_Max + 12) of Character;
666 -- 12 is the size of the dirent structure (see dirent.h), without the
667 -- field for the filename.
669 function readdir_gnat
670 (Directory : System.Address;
671 Buffer : System.Address)
672 return System.Address;
673 pragma Import (C, readdir_gnat, "__gnat_readdir");
675 function strlen (S : Address) return Integer;
676 pragma Import (C, strlen, "strlen");
678 begin
679 if not Is_Open (Dir) then
680 raise Directory_Error;
681 end if;
683 Filename_Addr :=
684 readdir_gnat (System.Address (Dir.all), Buffer'Address);
686 if Filename_Addr = System.Null_Address then
687 Last := 0;
688 return;
689 end if;
691 Filename_Len := strlen (Filename_Addr);
693 if Str'Length > Filename_Len then
694 Last := Str'First + Filename_Len - 1;
695 else
696 Last := Str'Last;
697 end if;
699 declare
700 subtype Path_String is String (1 .. Filename_Len);
701 type Path_String_Access is access Path_String;
703 function Address_To_Access is new
704 Unchecked_Conversion
705 (Source => Address,
706 Target => Path_String_Access);
708 Path_Access : constant Path_String_Access :=
709 Address_To_Access (Filename_Addr);
711 begin
712 for J in Str'First .. Last loop
713 Str (J) := Path_Access (J - Str'First + 1);
714 end loop;
715 end;
716 end Read;
718 -------------------------
719 -- Read_Is_Thread_Sage --
720 -------------------------
722 function Read_Is_Thread_Safe return Boolean is
724 function readdir_is_thread_safe return Integer;
725 pragma Import
726 (C, readdir_is_thread_safe, "__gnat_readdir_is_thread_safe");
728 begin
729 return (readdir_is_thread_safe /= 0);
730 end Read_Is_Thread_Safe;
732 ----------------
733 -- Remove_Dir --
734 ----------------
736 procedure Remove_Dir
737 (Dir_Name : Dir_Name_Str;
738 Recursive : Boolean := False)
740 C_Dir_Name : constant String := Dir_Name & ASCII.NUL;
741 Current_Dir : constant Dir_Name_Str := Get_Current_Dir;
742 Last : Integer;
743 Str : String (1 .. Filename_Max);
744 Success : Boolean;
745 Working_Dir : Dir_Type;
747 procedure rmdir (Dir_Name : String);
748 pragma Import (C, rmdir, "rmdir");
750 begin
751 -- Remove the directory only if it is empty
753 if not Recursive then
754 rmdir (C_Dir_Name);
756 if GNAT.OS_Lib.Is_Directory (Dir_Name) then
757 raise Directory_Error;
758 end if;
760 -- Remove directory and all files and directories that it may contain
762 else
763 Change_Dir (Dir_Name);
764 Open (Working_Dir, ".");
766 loop
767 Read (Working_Dir, Str, Last);
768 exit when Last = 0;
770 if GNAT.OS_Lib.Is_Directory (Str (1 .. Last)) then
771 if Str (1 .. Last) /= "." and then Str (1 .. Last) /= ".." then
772 Remove_Dir (Str (1 .. Last), True);
773 Remove_Dir (Str (1 .. Last));
774 end if;
776 else
777 GNAT.OS_Lib.Delete_File (Str (1 .. Last), Success);
779 if not Success then
780 Change_Dir (Current_Dir);
781 raise Directory_Error;
782 end if;
783 end if;
784 end loop;
786 Change_Dir (Current_Dir);
787 Close (Working_Dir);
788 Remove_Dir (Dir_Name);
789 end if;
790 end Remove_Dir;
792 end GNAT.Directory_Operations;