1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . D I R E C T O R Y _ O P E R A T I O N S --
9 -- Copyright (C) 1998-2008, AdaCore --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Ada.Characters.Handling;
35 with Ada.Strings.Fixed;
37 with Ada.Unchecked_Deallocation;
38 with Ada.Unchecked_Conversion;
40 with System; use System;
41 with System.CRTL; use System.CRTL;
45 package body GNAT.Directory_Operations is
49 Filename_Max : constant Integer := 1024;
50 -- 1024 is the value of FILENAME_MAX in stdio.h
53 Ada.Unchecked_Deallocation (Dir_Type_Value, Dir_Type);
55 On_Windows : constant Boolean := GNAT.OS_Lib.Directory_Separator = '\';
56 -- An indication that we are on Windows. Used in Get_Current_Dir, to
57 -- deal with drive letters in the beginning of absolute paths.
65 Suffix : String := "") return String
67 function Get_File_Names_Case_Sensitive return Integer;
69 (C, Get_File_Names_Case_Sensitive,
70 "__gnat_get_file_names_case_sensitive");
72 Case_Sensitive_File_Name : constant Boolean :=
73 Get_File_Names_Case_Sensitive = 1;
77 Suffix : String := "") 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.
90 Suffix : String := "") return String
92 Cut_Start : Natural :=
94 (Path, Dir_Seps, Going => Strings.Backward);
98 -- Cut_Start point to the first basename character
100 if Cut_Start = 0 then
101 Cut_Start := Path'First;
104 Cut_Start := Cut_Start + 1;
107 -- Cut_End point to the last basename character
109 Cut_End := Path'Last;
111 -- If basename ends with Suffix, adjust Cut_End
114 and then Path (Path'Last - Suffix'Length + 1 .. Cut_End) = Suffix
116 Cut_End := Path'Last - Suffix'Length;
119 Check_For_Standard_Dirs : declare
120 Offset : constant Integer := Path'First - Base_Name.Path'First;
121 BN : constant String :=
122 Base_Name.Path (Cut_Start - Offset .. Cut_End - Offset);
123 -- Here we use Base_Name.Path to keep the original casing
125 Has_Drive_Letter : constant Boolean :=
126 OS_Lib.Path_Separator /= ':';
127 -- If Path separator is not ':' then we are on a DOS based OS
128 -- where this character is used as a drive letter separator.
131 if BN = "." or else BN = ".." then
134 elsif Has_Drive_Letter
135 and then BN'Length > 2
136 and then Characters.Handling.Is_Letter (BN (BN'First))
137 and then BN (BN'First + 1) = ':'
139 -- We have a DOS drive letter prefix, remove it
141 return BN (BN'First + 2 .. BN'Last);
146 end Check_For_Standard_Dirs;
149 -- Start of processing for Base_Name
152 if Path'Length <= Suffix'Length then
156 if Case_Sensitive_File_Name then
157 return Basename (Path, Suffix);
160 (Characters.Handling.To_Lower (Path),
161 Characters.Handling.To_Lower (Suffix));
169 procedure Change_Dir (Dir_Name : Dir_Name_Str) is
170 C_Dir_Name : constant String := Dir_Name & ASCII.NUL;
172 if chdir (C_Dir_Name) /= 0 then
173 raise Directory_Error;
181 procedure Close (Dir : in out Dir_Type) is
183 pragma Warnings (Off, Discard);
185 function closedir (directory : DIRs) return Integer;
186 pragma Import (C, closedir, "__gnat_closedir");
189 if not Is_Open (Dir) then
190 raise Directory_Error;
193 Discard := closedir (DIRs (Dir.all));
201 function Dir_Name (Path : Path_Name) return Dir_Name_Str is
202 Last_DS : constant Natural :=
204 (Path, Dir_Seps, Going => Strings.Backward);
209 -- There is no directory separator, returns current working directory
211 return "." & Dir_Separator;
214 return Path (Path'First .. Last_DS);
224 Mode : Environment_Style := System_Default) return Path_Name
226 Environment_Variable_Char : Character;
227 pragma Import (C, Environment_Variable_Char, "__gnat_environment_char");
229 Result : OS_Lib.String_Access := new String (1 .. 200);
230 Result_Last : Natural := 0;
232 procedure Append (C : Character);
233 procedure Append (S : String);
236 procedure Double_Result_Size;
237 -- Reallocate Result, doubling its size
239 function Is_Var_Prefix (C : Character) return Boolean;
240 pragma Inline (Is_Var_Prefix);
242 procedure Read (K : in out Positive);
243 -- Update Result while reading current Path starting at position K. If
244 -- a variable is found, call Var below.
246 procedure Var (K : in out Positive);
247 -- Translate variable name starting at position K with the associated
248 -- environment value.
254 procedure Append (C : Character) is
256 if Result_Last = Result'Last then
260 Result_Last := Result_Last + 1;
261 Result (Result_Last) := C;
264 procedure Append (S : String) is
266 while Result_Last + S'Length - 1 > Result'Last loop
270 Result (Result_Last + 1 .. Result_Last + S'Length) := S;
271 Result_Last := Result_Last + S'Length;
274 ------------------------
275 -- Double_Result_Size --
276 ------------------------
278 procedure Double_Result_Size is
279 New_Result : constant OS_Lib.String_Access :=
280 new String (1 .. 2 * Result'Last);
282 New_Result (1 .. Result_Last) := Result (1 .. Result_Last);
283 OS_Lib.Free (Result);
284 Result := New_Result;
285 end Double_Result_Size;
291 function Is_Var_Prefix (C : Character) return Boolean is
293 return (C = Environment_Variable_Char and then Mode = System_Default)
295 (C = '$' and then (Mode = UNIX or else Mode = Both))
297 (C = '%' and then (Mode = DOS or else Mode = Both));
304 procedure Read (K : in out Positive) is
308 For_All_Characters : loop
309 if Is_Var_Prefix (Path (K)) then
312 -- Could be a variable
314 if K < Path'Last then
315 if Path (K + 1) = P then
317 -- Not a variable after all, this is a double $ or %,
318 -- just insert one in the result string.
324 -- Let's parse the variable
330 -- We have an ending $ or % sign
336 -- This is a standard character, just add it to the result
341 -- Skip to next character
345 exit For_All_Characters when K > Path'Last;
346 end loop For_All_Characters;
353 procedure Var (K : in out Positive) is
354 P : constant Character := Path (K);
361 if P = '%' or else Path (K) = '{' then
363 -- Set terminator character
372 -- Look for terminator character, k point to the first character
373 -- for the variable name.
379 exit when Path (E) = T or else E = Path'Last;
384 -- OK found, translate with environment value
387 Env : OS_Lib.String_Access :=
388 OS_Lib.Getenv (Path (K .. E - 1));
396 -- No terminator character, not a variable after all or a
397 -- syntax error, ignore it, insert string as-is.
399 Append (P); -- Add prefix character
401 if T = '}' then -- If we were looking for curly bracket
402 Append ('{'); -- terminator, add the curly bracket
405 Append (Path (K .. E));
409 -- The variable name is everything from current position to first
410 -- non letter/digit character.
414 -- Check that first character is a letter
416 if Characters.Handling.Is_Letter (Path (E)) then
420 exit Var_Name when E > Path'Last;
422 if Characters.Handling.Is_Letter (Path (E))
423 or else Characters.Handling.Is_Digit (Path (E))
434 Env : OS_Lib.String_Access := OS_Lib.Getenv (Path (K .. E));
442 -- This is not a variable after all
453 -- Start of processing for Expand_Path
457 K : Positive := Path'First;
463 Returned_Value : constant String := Result (1 .. Result_Last);
466 OS_Lib.Free (Result);
467 return Returned_Value;
476 function File_Extension (Path : Path_Name) return String is
479 (Path, Dir_Seps, Going => Strings.Backward);
488 Dot := Strings.Fixed.Index (Path (First .. Path'Last),
490 Going => Strings.Backward);
492 if Dot = 0 or else Dot = Path'Last then
495 return Path (Dot .. Path'Last);
503 function File_Name (Path : Path_Name) return String is
505 return Base_Name (Path);
508 ---------------------
509 -- Format_Pathname --
510 ---------------------
512 function Format_Pathname
514 Style : Path_Style := System_Default) return String
516 N_Path : String := Path;
517 K : Positive := N_Path'First;
518 Prev_Dirsep : Boolean := False;
521 if Dir_Separator = '\'
522 and then Path'Length > 1
523 and then Path (K .. K + 1) = "\\"
526 N_Path (K .. K + 1) := "//";
532 for J in K .. Path'Last loop
533 if Strings.Maps.Is_In (Path (J), Dir_Seps) then
534 if not Prev_Dirsep then
536 when UNIX => N_Path (K) := '/';
537 when DOS => N_Path (K) := '\';
538 when System_Default => N_Path (K) := Dir_Separator;
547 N_Path (K) := Path (J);
549 Prev_Dirsep := False;
553 return N_Path (N_Path'First .. K - 1);
556 ---------------------
557 -- Get_Current_Dir --
558 ---------------------
561 pragma Import (C, Max_Path, "__gnat_max_path_len");
563 function Get_Current_Dir return Dir_Name_Str is
564 Current_Dir : String (1 .. Max_Path + 1);
567 Get_Current_Dir (Current_Dir, Last);
568 return Current_Dir (1 .. Last);
571 procedure Get_Current_Dir (Dir : out Dir_Name_Str; Last : out Natural) is
572 Path_Len : Natural := Max_Path;
573 Buffer : String (Dir'First .. Dir'First + Max_Path + 1);
575 procedure Local_Get_Current_Dir
576 (Dir : System.Address;
577 Length : System.Address);
578 pragma Import (C, Local_Get_Current_Dir, "__gnat_get_current_dir");
581 Local_Get_Current_Dir (Buffer'Address, Path_Len'Address);
583 if Dir'Length > Path_Len then
584 Last := Dir'First + Path_Len - 1;
589 Dir (Buffer'First .. Last) := Buffer (Buffer'First .. Last);
591 -- By default, the drive letter on Windows is in upper case
593 if On_Windows and then Last > Dir'First and then
594 Dir (Dir'First + 1) = ':'
597 Ada.Characters.Handling.To_Upper (Dir (Dir'First));
605 function Is_Open (Dir : Dir_Type) return Boolean is
607 return Dir /= Null_Dir
608 and then System.Address (Dir.all) /= System.Null_Address;
615 procedure Make_Dir (Dir_Name : Dir_Name_Str) is
616 C_Dir_Name : constant String := Dir_Name & ASCII.NUL;
618 function mkdir (Dir_Name : String) return Integer;
619 pragma Import (C, mkdir, "__gnat_mkdir");
622 if mkdir (C_Dir_Name) /= 0 then
623 raise Directory_Error;
633 Dir_Name : Dir_Name_Str)
635 function opendir (file_name : String) return DIRs;
636 pragma Import (C, opendir, "__gnat_opendir");
638 C_File_Name : constant String := Dir_Name & ASCII.NUL;
641 Dir := new Dir_Type_Value'(Dir_Type_Value (opendir (C_File_Name)));
643 if not Is_Open (Dir) then
646 raise Directory_Error;
659 Filename_Addr : Address;
660 Filename_Len : aliased Integer;
662 Buffer : array (0 .. Filename_Max + 12) of Character;
663 -- 12 is the size of the dirent structure (see dirent.h), without the
664 -- field for the filename.
666 function readdir_gnat
667 (Directory : System.Address;
668 Buffer : System.Address;
669 Last : not null access Integer) return System.Address;
670 pragma Import (C, readdir_gnat, "__gnat_readdir");
673 if not Is_Open (Dir) then
674 raise Directory_Error;
679 (System.Address (Dir.all), Buffer'Address, Filename_Len'Access);
681 if Filename_Addr = System.Null_Address then
686 if Str'Length > Filename_Len then
687 Last := Str'First + Filename_Len - 1;
693 subtype Path_String is String (1 .. Filename_Len);
694 type Path_String_Access is access Path_String;
696 function Address_To_Access is new
697 Ada.Unchecked_Conversion
699 Target => Path_String_Access);
701 Path_Access : constant Path_String_Access :=
702 Address_To_Access (Filename_Addr);
705 for J in Str'First .. Last loop
706 Str (J) := Path_Access (J - Str'First + 1);
711 -------------------------
712 -- Read_Is_Thread_Sage --
713 -------------------------
715 function Read_Is_Thread_Safe return Boolean is
716 function readdir_is_thread_safe return Integer;
718 (C, readdir_is_thread_safe, "__gnat_readdir_is_thread_safe");
720 return (readdir_is_thread_safe /= 0);
721 end Read_Is_Thread_Safe;
728 (Dir_Name : Dir_Name_Str;
729 Recursive : Boolean := False)
731 C_Dir_Name : constant String := Dir_Name & ASCII.NUL;
732 Current_Dir : constant Dir_Name_Str := Get_Current_Dir;
734 Str : String (1 .. Filename_Max);
736 Working_Dir : Dir_Type;
739 -- Remove the directory only if it is empty
741 if not Recursive then
742 if rmdir (C_Dir_Name) /= 0 then
743 raise Directory_Error;
746 -- Remove directory and all files and directories that it may contain
749 -- Substantial comments needed. See RH for revision 1.50 ???
752 Change_Dir (Dir_Name);
753 Open (Working_Dir, ".");
756 Read (Working_Dir, Str, Last);
759 if GNAT.OS_Lib.Is_Directory (Str (1 .. Last)) then
760 if Str (1 .. Last) /= "."
762 Str (1 .. Last) /= ".."
764 Remove_Dir (Str (1 .. Last), True);
768 GNAT.OS_Lib.Delete_File (Str (1 .. Last), Success);
771 Change_Dir (Current_Dir);
772 raise Directory_Error;
777 Change_Dir (Current_Dir);
779 Remove_Dir (Dir_Name);
784 -- An exception occurred. We must make sure the current working
785 -- directory is unchanged.
787 Change_Dir (Current_Dir);
789 -- What if the Change_Dir raises an exception itself, shouldn't
790 -- that be protected? ???
797 end GNAT.Directory_Operations;