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-2012, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Ada
.Characters
.Handling
;
33 with Ada
.Strings
.Fixed
;
35 with Ada
.Unchecked_Deallocation
;
36 with Ada
.Unchecked_Conversion
;
38 with System
; use System
;
39 with System
.CRTL
; use System
.CRTL
;
43 package body GNAT
.Directory_Operations
is
47 Filename_Max
: constant Integer := 1024;
48 -- 1024 is the value of FILENAME_MAX in stdio.h
51 Ada
.Unchecked_Deallocation
(Dir_Type_Value
, Dir_Type
);
53 On_Windows
: constant Boolean := GNAT
.OS_Lib
.Directory_Separator
= '\';
54 -- An indication that we are on Windows. Used in Get_Current_Dir, to
55 -- deal with drive letters in the beginning of absolute paths.
63 Suffix
: String := "") return String
65 function Get_File_Names_Case_Sensitive
return Integer;
67 (C
, Get_File_Names_Case_Sensitive
,
68 "__gnat_get_file_names_case_sensitive");
70 Case_Sensitive_File_Name
: constant Boolean :=
71 Get_File_Names_Case_Sensitive
= 1;
75 Suffix
: String := "") return String;
76 -- This function does the job. The only difference between Basename
77 -- and Base_Name (the parent function) is that the former is case
78 -- sensitive, while the latter is not. Path and Suffix are adjusted
79 -- appropriately before calling Basename under platforms where the
80 -- file system is not case sensitive.
88 Suffix
: String := "") return String
90 Cut_Start
: Natural :=
92 (Path
, Dir_Seps
, Going
=> Strings
.Backward
);
96 -- Cut_Start point to the first basename character
98 Cut_Start
:= (if Cut_Start
= 0 then Path
'First else Cut_Start
+ 1);
100 -- Cut_End point to the last basename character
102 Cut_End
:= Path
'Last;
104 -- If basename ends with Suffix, adjust Cut_End
107 and then Path
(Path
'Last - Suffix
'Length + 1 .. Cut_End
) = Suffix
109 Cut_End
:= Path
'Last - Suffix
'Length;
112 Check_For_Standard_Dirs
: declare
113 Offset
: constant Integer := Path
'First - Base_Name
.Path
'First;
114 BN
: constant String :=
115 Base_Name
.Path
(Cut_Start
- Offset
.. Cut_End
- Offset
);
116 -- Here we use Base_Name.Path to keep the original casing
118 Has_Drive_Letter
: constant Boolean :=
119 OS_Lib
.Path_Separator
/= ':';
120 -- If Path separator is not ':' then we are on a DOS based OS
121 -- where this character is used as a drive letter separator.
124 if BN
= "." or else BN
= ".." then
127 elsif Has_Drive_Letter
128 and then BN
'Length > 2
129 and then Characters
.Handling
.Is_Letter
(BN
(BN
'First))
130 and then BN
(BN
'First + 1) = ':'
132 -- We have a DOS drive letter prefix, remove it
134 return BN
(BN
'First + 2 .. BN
'Last);
139 end Check_For_Standard_Dirs
;
142 -- Start of processing for Base_Name
145 if Path
'Length <= Suffix
'Length then
149 if Case_Sensitive_File_Name
then
150 return Basename
(Path
, Suffix
);
153 (Characters
.Handling
.To_Lower
(Path
),
154 Characters
.Handling
.To_Lower
(Suffix
));
162 procedure Change_Dir
(Dir_Name
: Dir_Name_Str
) is
163 C_Dir_Name
: constant String := Dir_Name
& ASCII
.NUL
;
165 if chdir
(C_Dir_Name
) /= 0 then
166 raise Directory_Error
;
174 procedure Close
(Dir
: in out Dir_Type
) is
176 pragma Warnings
(Off
, Discard
);
178 function closedir
(directory
: DIRs
) return Integer;
179 pragma Import
(C
, closedir
, "__gnat_closedir");
182 if not Is_Open
(Dir
) then
183 raise Directory_Error
;
186 Discard
:= closedir
(DIRs
(Dir
.all));
194 function Dir_Name
(Path
: Path_Name
) return Dir_Name_Str
is
195 Last_DS
: constant Natural :=
197 (Path
, Dir_Seps
, Going
=> Strings
.Backward
);
202 -- There is no directory separator, returns current working directory
204 return "." & Dir_Separator
;
207 return Path
(Path
'First .. Last_DS
);
217 Mode
: Environment_Style
:= System_Default
) return Path_Name
219 Environment_Variable_Char
: Character;
220 pragma Import
(C
, Environment_Variable_Char
, "__gnat_environment_char");
222 Result
: OS_Lib
.String_Access
:= new String (1 .. 200);
223 Result_Last
: Natural := 0;
225 procedure Append
(C
: Character);
226 procedure Append
(S
: String);
229 procedure Double_Result_Size
;
230 -- Reallocate Result, doubling its size
232 function Is_Var_Prefix
(C
: Character) return Boolean;
233 pragma Inline
(Is_Var_Prefix
);
235 procedure Read
(K
: in out Positive);
236 -- Update Result while reading current Path starting at position K. If
237 -- a variable is found, call Var below.
239 procedure Var
(K
: in out Positive);
240 -- Translate variable name starting at position K with the associated
241 -- environment value.
247 procedure Append
(C
: Character) is
249 if Result_Last
= Result
'Last then
253 Result_Last
:= Result_Last
+ 1;
254 Result
(Result_Last
) := C
;
257 procedure Append
(S
: String) is
259 while Result_Last
+ S
'Length - 1 > Result
'Last loop
263 Result
(Result_Last
+ 1 .. Result_Last
+ S
'Length) := S
;
264 Result_Last
:= Result_Last
+ S
'Length;
267 ------------------------
268 -- Double_Result_Size --
269 ------------------------
271 procedure Double_Result_Size
is
272 New_Result
: constant OS_Lib
.String_Access
:=
273 new String (1 .. 2 * Result
'Last);
275 New_Result
(1 .. Result_Last
) := Result
(1 .. Result_Last
);
276 OS_Lib
.Free
(Result
);
277 Result
:= New_Result
;
278 end Double_Result_Size
;
284 function Is_Var_Prefix
(C
: Character) return Boolean is
286 return (C
= Environment_Variable_Char
and then Mode
= System_Default
)
288 (C
= '$' and then (Mode
= UNIX
or else Mode
= Both
))
290 (C
= '%' and then (Mode
= DOS
or else Mode
= Both
));
297 procedure Read
(K
: in out Positive) is
301 For_All_Characters
: loop
302 if Is_Var_Prefix
(Path
(K
)) then
305 -- Could be a variable
307 if K
< Path
'Last then
308 if Path
(K
+ 1) = P
then
310 -- Not a variable after all, this is a double $ or %,
311 -- just insert one in the result string.
317 -- Let's parse the variable
323 -- We have an ending $ or % sign
329 -- This is a standard character, just add it to the result
334 -- Skip to next character
338 exit For_All_Characters
when K
> Path
'Last;
339 end loop For_All_Characters
;
346 procedure Var
(K
: in out Positive) is
347 P
: constant Character := Path
(K
);
354 if P
= '%' or else Path
(K
) = '{' then
356 -- Set terminator character
365 -- Look for terminator character, k point to the first character
366 -- for the variable name.
372 exit when Path
(E
) = T
or else E
= Path
'Last;
377 -- OK found, translate with environment value
380 Env
: OS_Lib
.String_Access
:=
381 OS_Lib
.Getenv
(Path
(K
.. E
- 1));
389 -- No terminator character, not a variable after all or a
390 -- syntax error, ignore it, insert string as-is.
392 Append
(P
); -- Add prefix character
394 if T
= '}' then -- If we were looking for curly bracket
395 Append
('{'); -- terminator, add the curly bracket
398 Append
(Path
(K
.. E
));
402 -- The variable name is everything from current position to first
403 -- non letter/digit character.
407 -- Check that first character is a letter
409 if Characters
.Handling
.Is_Letter
(Path
(E
)) then
413 exit Var_Name
when E
> Path
'Last;
415 if Characters
.Handling
.Is_Letter
(Path
(E
))
416 or else Characters
.Handling
.Is_Digit
(Path
(E
))
427 Env
: OS_Lib
.String_Access
:= OS_Lib
.Getenv
(Path
(K
.. E
));
435 -- This is not a variable after all
446 -- Start of processing for Expand_Path
450 K
: Positive := Path
'First;
456 Returned_Value
: constant String := Result
(1 .. Result_Last
);
459 OS_Lib
.Free
(Result
);
460 return Returned_Value
;
469 function File_Extension
(Path
: Path_Name
) return String is
472 (Path
, Dir_Seps
, Going
=> Strings
.Backward
);
481 Dot
:= Strings
.Fixed
.Index
(Path
(First
.. Path
'Last),
483 Going
=> Strings
.Backward
);
485 if Dot
= 0 or else Dot
= Path
'Last then
488 return Path
(Dot
.. Path
'Last);
496 function File_Name
(Path
: Path_Name
) return String is
498 return Base_Name
(Path
);
501 ---------------------
502 -- Format_Pathname --
503 ---------------------
505 function Format_Pathname
507 Style
: Path_Style
:= System_Default
) return String
509 N_Path
: String := Path
;
510 K
: Positive := N_Path
'First;
511 Prev_Dirsep
: Boolean := False;
514 if Dir_Separator
= '\'
515 and then Path
'Length > 1
516 and then Path
(K
.. K
+ 1) = "\\"
519 N_Path
(K
.. K
+ 1) := "//";
525 for J
in K
.. Path
'Last loop
526 if Strings
.Maps
.Is_In
(Path
(J
), Dir_Seps
) then
527 if not Prev_Dirsep
then
529 when UNIX
=> N_Path
(K
) := '/';
530 when DOS
=> N_Path
(K
) := '\';
531 when System_Default
=> N_Path
(K
) := Dir_Separator
;
540 N_Path
(K
) := Path
(J
);
542 Prev_Dirsep
:= False;
546 return N_Path
(N_Path
'First .. K
- 1);
549 ---------------------
550 -- Get_Current_Dir --
551 ---------------------
554 pragma Import
(C
, Max_Path
, "__gnat_max_path_len");
556 function Get_Current_Dir
return Dir_Name_Str
is
557 Current_Dir
: String (1 .. Max_Path
+ 1);
560 Get_Current_Dir
(Current_Dir
, Last
);
561 return Current_Dir
(1 .. Last
);
564 procedure Get_Current_Dir
(Dir
: out Dir_Name_Str
; Last
: out Natural) is
565 Path_Len
: Natural := Max_Path
;
566 Buffer
: String (Dir
'First .. Dir
'First + Max_Path
+ 1);
568 procedure Local_Get_Current_Dir
569 (Dir
: System
.Address
;
570 Length
: System
.Address
);
571 pragma Import
(C
, Local_Get_Current_Dir
, "__gnat_get_current_dir");
574 Local_Get_Current_Dir
(Buffer
'Address, Path_Len
'Address);
577 (if Dir
'Length > Path_Len
then Dir
'First + Path_Len
- 1 else Dir
'Last);
579 Dir
(Buffer
'First .. Last
) := Buffer
(Buffer
'First .. Last
);
581 -- By default, the drive letter on Windows is in upper case
583 if On_Windows
and then Last
> Dir
'First and then
584 Dir
(Dir
'First + 1) = ':'
587 Ada
.Characters
.Handling
.To_Upper
(Dir
(Dir
'First));
595 function Is_Open
(Dir
: Dir_Type
) return Boolean is
597 return Dir
/= Null_Dir
598 and then System
.Address
(Dir
.all) /= System
.Null_Address
;
605 procedure Make_Dir
(Dir_Name
: Dir_Name_Str
) is
606 C_Dir_Name
: constant String := Dir_Name
& ASCII
.NUL
;
608 if CRTL
.mkdir
(C_Dir_Name
, Unspecified
) /= 0 then
609 raise Directory_Error
;
619 Dir_Name
: Dir_Name_Str
)
621 function opendir
(file_name
: String) return DIRs
;
622 pragma Import
(C
, opendir
, "__gnat_opendir");
624 C_File_Name
: constant String := Dir_Name
& ASCII
.NUL
;
627 Dir
:= new Dir_Type_Value
'(Dir_Type_Value (opendir (C_File_Name)));
629 if not Is_Open (Dir) then
632 raise Directory_Error;
645 Filename_Addr : Address;
646 Filename_Len : aliased Integer;
648 Buffer : array (0 .. Filename_Max + 12) of Character;
649 -- 12 is the size of the dirent structure (see dirent.h), without the
650 -- field for the filename.
652 function readdir_gnat
653 (Directory : System.Address;
654 Buffer : System.Address;
655 Last : not null access Integer) return System.Address;
656 pragma Import (C, readdir_gnat, "__gnat_readdir");
659 if not Is_Open (Dir) then
660 raise Directory_Error;
665 (System.Address (Dir.all), Buffer'Address, Filename_Len'Access);
667 if Filename_Addr = System.Null_Address then
673 (if Str'Length > Filename_Len then Str'First + Filename_Len - 1
677 subtype Path_String is String (1 .. Filename_Len);
678 type Path_String_Access is access Path_String;
680 function Address_To_Access is new
681 Ada.Unchecked_Conversion
683 Target => Path_String_Access);
685 Path_Access : constant Path_String_Access :=
686 Address_To_Access (Filename_Addr);
689 for J in Str'First .. Last loop
690 Str (J) := Path_Access (J - Str'First + 1);
695 -------------------------
696 -- Read_Is_Thread_Sage --
697 -------------------------
699 function Read_Is_Thread_Safe return Boolean is
700 function readdir_is_thread_safe return Integer;
702 (C, readdir_is_thread_safe, "__gnat_readdir_is_thread_safe");
704 return (readdir_is_thread_safe /= 0);
705 end Read_Is_Thread_Safe;
712 (Dir_Name : Dir_Name_Str;
713 Recursive : Boolean := False)
715 C_Dir_Name : constant String := Dir_Name & ASCII.NUL;
717 Str : String (1 .. Filename_Max);
719 Current_Dir : Dir_Type;
722 -- Remove the directory only if it is empty
724 if not Recursive then
725 if rmdir (C_Dir_Name) /= 0 then
726 raise Directory_Error;
729 -- Remove directory and all files and directories that it may contain
732 Open (Current_Dir, Dir_Name);
735 Read (Current_Dir, Str, Last);
738 if GNAT.OS_Lib.Is_Directory
739 (Dir_Name & Dir_Separator & Str (1 .. Last))
741 if Str (1 .. Last) /= "."
743 Str (1 .. Last) /= ".."
745 -- Recursive call to remove a subdirectory and all its
749 (Dir_Name & Dir_Separator & Str (1 .. Last),
754 GNAT.OS_Lib.Delete_File
755 (Dir_Name & Dir_Separator & Str (1 .. Last),
759 raise Directory_Error;
765 Remove_Dir (Dir_Name);
769 end GNAT.Directory_Operations;