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-2003 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
;
37 with Unchecked_Deallocation
;
38 with Unchecked_Conversion
;
39 with System
; use System
;
43 package body GNAT
.Directory_Operations
is
47 type Dir_Type_Value
is new System
.Address
;
48 -- This is the low-level address directory structure as returned by the C
51 Filename_Max
: constant Integer := 1024;
52 -- 1024 is the value of FILENAME_MAX in stdio.h
55 Unchecked_Deallocation
(Dir_Type_Value
, Dir_Type
);
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
99 Cut_Start
:= Path
'First;
102 Cut_Start
:= Cut_Start
+ 1;
105 -- Cut_End point to the last basename character.
107 Cut_End
:= Path
'Last;
109 -- If basename ends with Suffix, adjust Cut_End.
112 and then Path
(Path
'Last - Suffix
'Length + 1 .. Cut_End
) = Suffix
114 Cut_End
:= Path
'Last - Suffix
'Length;
117 Check_For_Standard_Dirs
: declare
118 Offset
: constant Integer := Path
'First - Base_Name
.Path
'First;
119 BN
: constant String :=
120 Base_Name
.Path
(Cut_Start
- Offset
.. Cut_End
- Offset
);
121 -- Here we use Base_Name.Path to keep the original casing
123 Has_Drive_Letter
: constant Boolean :=
124 OS_Lib
.Path_Separator
/= ':';
125 -- If Path separator is not ':' then we are on a DOS based OS
126 -- where this character is used as a drive letter separator.
129 if BN
= "." or else BN
= ".." then
132 elsif Has_Drive_Letter
133 and then BN
'Length > 2
134 and then Characters
.Handling
.Is_Letter
(BN
(BN
'First))
135 and then BN
(BN
'First + 1) = ':'
137 -- We have a DOS drive letter prefix, remove it
139 return BN
(BN
'First + 2 .. BN
'Last);
144 end Check_For_Standard_Dirs
;
147 -- Start processing for Base_Name
150 if Path
'Length <= Suffix
'Length then
154 if Case_Sensitive_File_Name
then
155 return Basename
(Path
, Suffix
);
158 (Characters
.Handling
.To_Lower
(Path
),
159 Characters
.Handling
.To_Lower
(Suffix
));
167 procedure Change_Dir
(Dir_Name
: Dir_Name_Str
) is
168 C_Dir_Name
: constant String := Dir_Name
& ASCII
.NUL
;
170 function chdir
(Dir_Name
: String) return Integer;
171 pragma Import
(C
, chdir
, "chdir");
174 if chdir
(C_Dir_Name
) /= 0 then
175 raise Directory_Error
;
183 procedure Close
(Dir
: in out Dir_Type
) is
185 function closedir
(Directory
: System
.Address
) return Integer;
186 pragma Import
(C
, closedir
, "closedir");
189 pragma Warnings
(Off
, Discard
);
192 if not Is_Open
(Dir
) then
193 raise Directory_Error
;
196 Discard
:= closedir
(System
.Address
(Dir
.all));
204 function Dir_Name
(Path
: Path_Name
) return Dir_Name_Str
is
205 Last_DS
: constant Natural :=
207 (Path
, Dir_Seps
, Going
=> Strings
.Backward
);
212 -- There is no directory separator, returns current working directory
214 return "." & Dir_Separator
;
217 return Path
(Path
'First .. Last_DS
);
227 Mode
: Environment_Style
:= System_Default
) return Path_Name
229 Environment_Variable_Char
: Character;
230 pragma Import
(C
, Environment_Variable_Char
, "__gnat_environment_char");
232 Result
: OS_Lib
.String_Access
:= new String (1 .. 200);
233 Result_Last
: Natural := 0;
235 procedure Append
(C
: Character);
236 procedure Append
(S
: String);
239 procedure Double_Result_Size
;
240 -- Reallocate Result, doubling its size
242 function Is_Var_Prefix
(C
: Character) return Boolean;
243 pragma Inline
(Is_Var_Prefix
);
245 procedure Read
(K
: in out Positive);
246 -- Update Result while reading current Path starting at position K. If
247 -- a variable is found, call Var below.
249 procedure Var
(K
: in out Positive);
250 -- Translate variable name starting at position K with the associated
251 -- environment value.
257 procedure Append
(C
: Character) is
259 if Result_Last
= Result
'Last then
263 Result_Last
:= Result_Last
+ 1;
264 Result
(Result_Last
) := C
;
267 procedure Append
(S
: String) is
269 while Result_Last
+ S
'Length - 1 > Result
'Last loop
273 Result
(Result_Last
+ 1 .. Result_Last
+ S
'Length) := S
;
274 Result_Last
:= Result_Last
+ S
'Length;
277 ------------------------
278 -- Double_Result_Size --
279 ------------------------
281 procedure Double_Result_Size
is
282 New_Result
: constant OS_Lib
.String_Access
:=
283 new String (1 .. 2 * Result
'Last);
286 New_Result
(1 .. Result_Last
) := Result
(1 .. Result_Last
);
287 OS_Lib
.Free
(Result
);
288 Result
:= New_Result
;
289 end Double_Result_Size
;
295 function Is_Var_Prefix
(C
: Character) return Boolean is
297 return (C
= Environment_Variable_Char
and then Mode
= System_Default
)
299 (C
= '$' and then (Mode
= UNIX
or else Mode
= Both
))
301 (C
= '%' and then (Mode
= DOS
or else Mode
= Both
));
308 procedure Read
(K
: in out Positive) is
311 For_All_Characters
: loop
312 if Is_Var_Prefix
(Path
(K
)) then
315 -- Could be a variable
317 if K
< Path
'Last then
319 if Path
(K
+ 1) = P
then
321 -- Not a variable after all, this is a double $ or %,
322 -- just insert one in the result string.
328 -- Let's parse the variable
334 -- We have an ending $ or % sign
340 -- This is a standard character, just add it to the result
345 -- Skip to next character
349 exit For_All_Characters
when K
> Path
'Last;
350 end loop For_All_Characters
;
357 procedure Var
(K
: in out Positive) is
358 P
: constant Character := Path
(K
);
365 if P
= '%' or else Path
(K
) = '{' then
367 -- Set terminator character
376 -- Look for terminator character, k point to the first character
377 -- for the variable name.
383 exit when Path
(E
) = T
or else E
= Path
'Last;
388 -- OK found, translate with environment value
391 Env
: OS_Lib
.String_Access
:=
392 OS_Lib
.Getenv
(Path
(K
.. E
- 1));
400 -- No terminator character, not a variable after all or a
401 -- syntax error, ignore it, insert string as-is.
403 Append
(P
); -- Add prefix character
405 if T
= '}' then -- If we were looking for curly bracket
406 Append
('{'); -- terminator, add the curly bracket
409 Append
(Path
(K
.. E
));
413 -- The variable name is everything from current position to first
414 -- non letter/digit character.
418 -- Check that first chartacter is a letter
420 if Characters
.Handling
.Is_Letter
(Path
(E
)) then
424 exit Var_Name
when E
> Path
'Last;
426 if Characters
.Handling
.Is_Letter
(Path
(E
))
427 or else Characters
.Handling
.Is_Digit
(Path
(E
))
438 Env
: OS_Lib
.String_Access
:= OS_Lib
.Getenv
(Path
(K
.. E
));
446 -- This is not a variable after all
457 -- Start of processing for Expand_Path
461 K
: Positive := Path
'First;
467 Returned_Value
: constant String := Result
(1 .. Result_Last
);
470 OS_Lib
.Free
(Result
);
471 return Returned_Value
;
480 function File_Extension
(Path
: Path_Name
) return String is
483 (Path
, Dir_Seps
, Going
=> Strings
.Backward
);
492 Dot
:= Strings
.Fixed
.Index
(Path
(First
.. Path
'Last),
494 Going
=> Strings
.Backward
);
496 if Dot
= 0 or else Dot
= Path
'Last then
499 return Path
(Dot
.. Path
'Last);
507 function File_Name
(Path
: Path_Name
) return String is
509 return Base_Name
(Path
);
512 ---------------------
513 -- Format_Pathname --
514 ---------------------
516 function Format_Pathname
518 Style
: Path_Style
:= System_Default
) return String
520 N_Path
: String := Path
;
521 K
: Positive := N_Path
'First;
522 Prev_Dirsep
: Boolean := False;
525 if Dir_Separator
= '\'
526 and then Path
'Length > 1
527 and then Path
(K
.. K
+ 1) = "\\"
530 N_Path
(K
.. K
+ 1) := "//";
536 for J
in K
.. Path
'Last loop
537 if Strings
.Maps
.Is_In
(Path
(J
), Dir_Seps
) then
538 if not Prev_Dirsep
then
540 when UNIX
=> N_Path
(K
) := '/';
541 when DOS
=> N_Path
(K
) := '\';
542 when System_Default
=> N_Path
(K
) := Dir_Separator
;
551 N_Path
(K
) := Path
(J
);
553 Prev_Dirsep
:= False;
557 return N_Path
(N_Path
'First .. K
- 1);
560 ---------------------
561 -- Get_Current_Dir --
562 ---------------------
565 pragma Import
(C
, Max_Path
, "__gnat_max_path_len");
567 function Get_Current_Dir
return Dir_Name_Str
is
568 Current_Dir
: String (1 .. Max_Path
+ 1);
572 Get_Current_Dir
(Current_Dir
, Last
);
573 return Current_Dir
(1 .. Last
);
576 procedure Get_Current_Dir
(Dir
: out Dir_Name_Str
; Last
: out Natural) is
577 Path_Len
: Natural := Max_Path
;
578 Buffer
: String (Dir
'First .. Dir
'First + Max_Path
+ 1);
580 procedure Local_Get_Current_Dir
581 (Dir
: System
.Address
;
582 Length
: System
.Address
);
583 pragma Import
(C
, Local_Get_Current_Dir
, "__gnat_get_current_dir");
586 Local_Get_Current_Dir
(Buffer
'Address, Path_Len
'Address);
588 if Dir
'Length > Path_Len
then
589 Last
:= Dir
'First + Path_Len
- 1;
594 Dir
(Buffer
'First .. Last
) := Buffer
(Buffer
'First .. Last
);
601 function Is_Open
(Dir
: Dir_Type
) return Boolean is
603 return Dir
/= Null_Dir
604 and then System
.Address
(Dir
.all) /= System
.Null_Address
;
611 procedure Make_Dir
(Dir_Name
: Dir_Name_Str
) is
612 C_Dir_Name
: constant String := Dir_Name
& ASCII
.NUL
;
614 function mkdir
(Dir_Name
: String) return Integer;
615 pragma Import
(C
, mkdir
, "__gnat_mkdir");
618 if mkdir
(C_Dir_Name
) /= 0 then
619 raise Directory_Error
;
629 Dir_Name
: Dir_Name_Str
)
631 C_File_Name
: constant String := Dir_Name
& ASCII
.NUL
;
634 (File_Name
: String) return Dir_Type_Value
;
635 pragma Import
(C
, opendir
, "opendir");
638 Dir
:= new Dir_Type_Value
'(opendir (C_File_Name));
640 if not Is_Open (Dir) then
643 raise Directory_Error;
652 (Dir : in out Dir_Type;
656 Filename_Addr : Address;
657 Filename_Len : Integer;
659 Buffer : array (0 .. Filename_Max + 12) of Character;
660 -- 12 is the size of the dirent structure (see dirent.h), without the
661 -- field for the filename.
663 function readdir_gnat
664 (Directory : System.Address;
665 Buffer : System.Address) return System.Address;
666 pragma Import (C, readdir_gnat, "__gnat_readdir");
668 function strlen (S : Address) return Integer;
669 pragma Import (C, strlen, "strlen");
672 if not Is_Open (Dir) then
673 raise Directory_Error;
677 readdir_gnat (System.Address (Dir.all), Buffer'Address);
679 if Filename_Addr = System.Null_Address then
684 Filename_Len := strlen (Filename_Addr);
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
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
717 function readdir_is_thread_safe return Integer;
719 (C, readdir_is_thread_safe, "__gnat_readdir_is_thread_safe");
722 return (readdir_is_thread_safe /= 0);
723 end Read_Is_Thread_Safe;
730 (Dir_Name : Dir_Name_Str;
731 Recursive : Boolean := False)
733 C_Dir_Name : constant String := Dir_Name & ASCII.NUL;
734 Current_Dir : constant Dir_Name_Str := Get_Current_Dir;
736 Str : String (1 .. Filename_Max);
738 Working_Dir : Dir_Type;
740 procedure rmdir (Dir_Name : String);
741 pragma Import (C, rmdir, "rmdir");
744 -- Remove the directory only if it is empty
746 if not Recursive then
749 if GNAT.OS_Lib.Is_Directory (Dir_Name) then
750 raise Directory_Error;
753 -- Remove directory and all files and directories that it may contain
756 Change_Dir (Dir_Name);
757 Open (Working_Dir, ".");
760 Read (Working_Dir, Str, Last);
763 if GNAT.OS_Lib.Is_Directory (Str (1 .. Last)) then
764 if Str (1 .. Last) /= "." and then Str (1 .. Last) /= ".." then
765 Remove_Dir (Str (1 .. Last), True);
766 Remove_Dir (Str (1 .. Last));
770 GNAT.OS_Lib.Delete_File (Str (1 .. Last), Success);
773 Change_Dir (Current_Dir);
774 raise Directory_Error;
779 Change_Dir (Current_Dir);
781 Remove_Dir (Dir_Name);
785 end GNAT.Directory_Operations;