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-2005 Ada Core Technologies, Inc. --
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. --
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;
36 with Ada.Strings.Maps;
38 with Unchecked_Deallocation;
39 with Unchecked_Conversion;
41 with System; use System;
42 with System.CRTL; use System.CRTL;
46 package body GNAT.Directory_Operations is
50 type Dir_Type_Value is new System.Address;
51 -- This is the low-level address directory structure as returned by the C
54 Filename_Max : constant Integer := 1024;
55 -- 1024 is the value of FILENAME_MAX in stdio.h
58 Unchecked_Deallocation (Dir_Type_Value, Dir_Type);
66 Suffix : String := "") return String
68 function Get_File_Names_Case_Sensitive return Integer;
70 (C, Get_File_Names_Case_Sensitive,
71 "__gnat_get_file_names_case_sensitive");
73 Case_Sensitive_File_Name : constant Boolean :=
74 Get_File_Names_Case_Sensitive = 1;
78 Suffix : String := "") return String;
79 -- This function does the job. The only difference between Basename
80 -- and Base_Name (the parent function) is that the former is case
81 -- sensitive, while the latter is not. Path and Suffix are adjusted
82 -- appropriately before calling Basename under platforms where the
83 -- file system is not case sensitive.
91 Suffix : String := "") return String
93 Cut_Start : Natural :=
95 (Path, Dir_Seps, Going => Strings.Backward);
99 -- Cut_Start point to the first basename character
101 if Cut_Start = 0 then
102 Cut_Start := Path'First;
105 Cut_Start := Cut_Start + 1;
108 -- Cut_End point to the last basename character.
110 Cut_End := Path'Last;
112 -- If basename ends with Suffix, adjust Cut_End.
115 and then Path (Path'Last - Suffix'Length + 1 .. Cut_End) = Suffix
117 Cut_End := Path'Last - Suffix'Length;
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.
132 if BN = "." or else BN = ".." then
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) = ':'
140 -- We have a DOS drive letter prefix, remove it
142 return BN (BN'First + 2 .. BN'Last);
147 end Check_For_Standard_Dirs;
150 -- Start processing for Base_Name
153 if Path'Length <= Suffix'Length then
157 if Case_Sensitive_File_Name then
158 return Basename (Path, Suffix);
161 (Characters.Handling.To_Lower (Path),
162 Characters.Handling.To_Lower (Suffix));
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");
177 if chdir (C_Dir_Name) /= 0 then
178 raise Directory_Error;
186 procedure Close (Dir : in out Dir_Type) is
188 pragma Warnings (Off, Discard);
191 if not Is_Open (Dir) then
192 raise Directory_Error;
195 Discard := closedir (DIRs (Dir.all));
203 function Dir_Name (Path : Path_Name) return Dir_Name_Str is
204 Last_DS : constant Natural :=
206 (Path, Dir_Seps, Going => Strings.Backward);
211 -- There is no directory separator, returns current working directory
213 return "." & Dir_Separator;
216 return Path (Path'First .. Last_DS);
226 Mode : Environment_Style := System_Default) return Path_Name
228 Environment_Variable_Char : Character;
229 pragma Import (C, Environment_Variable_Char, "__gnat_environment_char");
231 Result : OS_Lib.String_Access := new String (1 .. 200);
232 Result_Last : Natural := 0;
234 procedure Append (C : Character);
235 procedure Append (S : String);
238 procedure Double_Result_Size;
239 -- Reallocate Result, doubling its size
241 function Is_Var_Prefix (C : Character) return Boolean;
242 pragma Inline (Is_Var_Prefix);
244 procedure Read (K : in out Positive);
245 -- Update Result while reading current Path starting at position K. If
246 -- a variable is found, call Var below.
248 procedure Var (K : in out Positive);
249 -- Translate variable name starting at position K with the associated
250 -- environment value.
256 procedure Append (C : Character) is
258 if Result_Last = Result'Last then
262 Result_Last := Result_Last + 1;
263 Result (Result_Last) := C;
266 procedure Append (S : String) is
268 while Result_Last + S'Length - 1 > Result'Last loop
272 Result (Result_Last + 1 .. Result_Last + S'Length) := S;
273 Result_Last := Result_Last + S'Length;
276 ------------------------
277 -- Double_Result_Size --
278 ------------------------
280 procedure Double_Result_Size is
281 New_Result : constant OS_Lib.String_Access :=
282 new String (1 .. 2 * Result'Last);
285 New_Result (1 .. Result_Last) := Result (1 .. Result_Last);
286 OS_Lib.Free (Result);
287 Result := New_Result;
288 end Double_Result_Size;
294 function Is_Var_Prefix (C : Character) return Boolean is
296 return (C = Environment_Variable_Char and then Mode = System_Default)
298 (C = '$' and then (Mode = UNIX or else Mode = Both))
300 (C = '%' and then (Mode = DOS or else Mode = Both));
307 procedure Read (K : in out Positive) is
310 For_All_Characters : loop
311 if Is_Var_Prefix (Path (K)) then
314 -- Could be a variable
316 if K < Path'Last then
318 if Path (K + 1) = P then
320 -- Not a variable after all, this is a double $ or %,
321 -- just insert one in the result string.
327 -- Let's parse the variable
333 -- We have an ending $ or % sign
339 -- This is a standard character, just add it to the result
344 -- Skip to next character
348 exit For_All_Characters when K > Path'Last;
349 end loop For_All_Characters;
356 procedure Var (K : in out Positive) is
357 P : constant Character := Path (K);
364 if P = '%' or else Path (K) = '{' then
366 -- Set terminator character
375 -- Look for terminator character, k point to the first character
376 -- for the variable name.
382 exit when Path (E) = T or else E = Path'Last;
387 -- OK found, translate with environment value
390 Env : OS_Lib.String_Access :=
391 OS_Lib.Getenv (Path (K .. E - 1));
399 -- No terminator character, not a variable after all or a
400 -- syntax error, ignore it, insert string as-is.
402 Append (P); -- Add prefix character
404 if T = '}' then -- If we were looking for curly bracket
405 Append ('{'); -- terminator, add the curly bracket
408 Append (Path (K .. E));
412 -- The variable name is everything from current position to first
413 -- non letter/digit character.
417 -- Check that first chartacter is a letter
419 if Characters.Handling.Is_Letter (Path (E)) then
423 exit Var_Name when E > Path'Last;
425 if Characters.Handling.Is_Letter (Path (E))
426 or else Characters.Handling.Is_Digit (Path (E))
437 Env : OS_Lib.String_Access := OS_Lib.Getenv (Path (K .. E));
445 -- This is not a variable after all
456 -- Start of processing for Expand_Path
460 K : Positive := Path'First;
466 Returned_Value : constant String := Result (1 .. Result_Last);
469 OS_Lib.Free (Result);
470 return Returned_Value;
479 function File_Extension (Path : Path_Name) return String is
482 (Path, Dir_Seps, Going => Strings.Backward);
491 Dot := Strings.Fixed.Index (Path (First .. Path'Last),
493 Going => Strings.Backward);
495 if Dot = 0 or else Dot = Path'Last then
498 return Path (Dot .. Path'Last);
506 function File_Name (Path : Path_Name) return String is
508 return Base_Name (Path);
511 ---------------------
512 -- Format_Pathname --
513 ---------------------
515 function Format_Pathname
517 Style : Path_Style := System_Default) return String
519 N_Path : String := Path;
520 K : Positive := N_Path'First;
521 Prev_Dirsep : Boolean := False;
524 if Dir_Separator = '\'
525 and then Path'Length > 1
526 and then Path (K .. K + 1) = "\\"
529 N_Path (K .. K + 1) := "//";
535 for J in K .. Path'Last loop
536 if Strings.Maps.Is_In (Path (J), Dir_Seps) then
537 if not Prev_Dirsep then
539 when UNIX => N_Path (K) := '/';
540 when DOS => N_Path (K) := '\';
541 when System_Default => N_Path (K) := Dir_Separator;
550 N_Path (K) := Path (J);
552 Prev_Dirsep := False;
556 return N_Path (N_Path'First .. K - 1);
559 ---------------------
560 -- Get_Current_Dir --
561 ---------------------
564 pragma Import (C, Max_Path, "__gnat_max_path_len");
566 function Get_Current_Dir return Dir_Name_Str is
567 Current_Dir : String (1 .. Max_Path + 1);
571 Get_Current_Dir (Current_Dir, Last);
572 return Current_Dir (1 .. Last);
575 procedure Get_Current_Dir (Dir : out Dir_Name_Str; Last : out Natural) is
576 Path_Len : Natural := Max_Path;
577 Buffer : String (Dir'First .. Dir'First + Max_Path + 1);
579 procedure Local_Get_Current_Dir
580 (Dir : System.Address;
581 Length : System.Address);
582 pragma Import (C, Local_Get_Current_Dir, "__gnat_get_current_dir");
585 Local_Get_Current_Dir (Buffer'Address, Path_Len'Address);
587 if Dir'Length > Path_Len then
588 Last := Dir'First + Path_Len - 1;
593 Dir (Buffer'First .. Last) := Buffer (Buffer'First .. Last);
600 function Is_Open (Dir : Dir_Type) return Boolean is
602 return Dir /= Null_Dir
603 and then System.Address (Dir.all) /= System.Null_Address;
610 procedure Make_Dir (Dir_Name : Dir_Name_Str) is
611 C_Dir_Name : constant String := Dir_Name & ASCII.NUL;
613 function mkdir (Dir_Name : String) return Integer;
614 pragma Import (C, mkdir, "__gnat_mkdir");
617 if mkdir (C_Dir_Name) /= 0 then
618 raise Directory_Error;
628 Dir_Name : Dir_Name_Str)
630 C_File_Name : constant String := Dir_Name & ASCII.NUL;
633 Dir := new Dir_Type_Value'(Dir_Type_Value (opendir (C_File_Name)));
635 if not Is_Open (Dir) then
638 raise Directory_Error;
647 (Dir : in out Dir_Type;
651 Filename_Addr : Address;
652 Filename_Len : Integer;
654 Buffer : array (0 .. Filename_Max + 12) of Character;
655 -- 12 is the size of the dirent structure (see dirent.h), without the
656 -- field for the filename.
658 function readdir_gnat
659 (Directory : System.Address;
660 Buffer : System.Address) return System.Address;
661 pragma Import (C, readdir_gnat, "__gnat_readdir");
663 function strlen (S : Address) return Integer;
664 pragma Import (C, strlen, "strlen");
667 if not Is_Open (Dir) then
668 raise Directory_Error;
672 readdir_gnat (System.Address (Dir.all), Buffer'Address);
674 if Filename_Addr = System.Null_Address then
679 Filename_Len := strlen (Filename_Addr);
681 if Str'Length > Filename_Len then
682 Last := Str'First + Filename_Len - 1;
688 subtype Path_String is String (1 .. Filename_Len);
689 type Path_String_Access is access Path_String;
691 function Address_To_Access is new
694 Target => Path_String_Access);
696 Path_Access : constant Path_String_Access :=
697 Address_To_Access (Filename_Addr);
700 for J in Str'First .. Last loop
701 Str (J) := Path_Access (J - Str'First + 1);
706 -------------------------
707 -- Read_Is_Thread_Sage --
708 -------------------------
710 function Read_Is_Thread_Safe return Boolean is
712 function readdir_is_thread_safe return Integer;
714 (C, readdir_is_thread_safe, "__gnat_readdir_is_thread_safe");
717 return (readdir_is_thread_safe /= 0);
718 end Read_Is_Thread_Safe;
725 (Dir_Name : Dir_Name_Str;
726 Recursive : Boolean := False)
728 C_Dir_Name : constant String := Dir_Name & ASCII.NUL;
729 Current_Dir : constant Dir_Name_Str := Get_Current_Dir;
731 Str : String (1 .. Filename_Max);
733 Working_Dir : Dir_Type;
736 -- Remove the directory only if it is empty
738 if not Recursive then
741 if GNAT.OS_Lib.Is_Directory (Dir_Name) then
742 raise Directory_Error;
745 -- Remove directory and all files and directories that it may contain
748 Change_Dir (Dir_Name);
749 Open (Working_Dir, ".");
752 Read (Working_Dir, Str, Last);
755 if GNAT.OS_Lib.Is_Directory (Str (1 .. Last)) then
756 if Str (1 .. Last) /= "." and then Str (1 .. Last) /= ".." then
757 Remove_Dir (Str (1 .. Last), True);
758 Remove_Dir (Str (1 .. Last));
762 GNAT.OS_Lib.Delete_File (Str (1 .. Last), Success);
765 Change_Dir (Current_Dir);
766 raise Directory_Error;
771 Change_Dir (Current_Dir);
773 Remove_Dir (Dir_Name);
777 end GNAT.Directory_Operations;