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 --
10 -- Copyright (C) 1998-2001 Ada Core Technologies, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
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
52 Unchecked_Deallocation
(Dir_Type_Value
, Dir_Type
);
60 Suffix
: String := "")
63 function Get_File_Names_Case_Sensitive
return Integer;
65 (C
, Get_File_Names_Case_Sensitive
,
66 "__gnat_get_file_names_case_sensitive");
68 Case_Sensitive_File_Name
: constant Boolean :=
69 Get_File_Names_Case_Sensitive
= 1;
73 Suffix
: String := "")
75 -- This function does the job. The only difference between Basename
76 -- and Base_Name (the parent function) is that the former is case
77 -- sensitive, while the latter is not. Path and Suffix are adjusted
78 -- appropriately before calling Basename under platforms where the
79 -- file system is not case sensitive.
87 Suffix
: 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
124 if BN
= "." or else BN
= ".." then
128 and then Characters
.Handling
.Is_Letter
(BN
(BN
'First))
129 and then BN
(BN
'First + 1) = ':'
131 -- We have a DOS drive letter prefix, remove it
133 return BN
(BN
'First + 2 .. BN
'Last);
138 end Check_For_Standard_Dirs
;
141 -- Start processing for Base_Name
144 if Case_Sensitive_File_Name
then
145 return Basename
(Path
, Suffix
);
149 (Characters
.Handling
.To_Lower
(Path
),
150 Characters
.Handling
.To_Lower
(Suffix
));
158 procedure Change_Dir
(Dir_Name
: Dir_Name_Str
) is
159 C_Dir_Name
: String := Dir_Name
& ASCII
.NUL
;
161 function chdir
(Dir_Name
: String) return Integer;
162 pragma Import
(C
, chdir
, "chdir");
165 if chdir
(C_Dir_Name
) /= 0 then
166 raise Directory_Error
;
174 procedure Close
(Dir
: in out Dir_Type
) is
176 function closedir
(Directory
: System
.Address
) return Integer;
177 pragma Import
(C
, closedir
, "closedir");
182 if not Is_Open
(Dir
) then
183 raise Directory_Error
;
186 Discard
:= closedir
(System
.Address
(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
);
215 function Expand_Path
(Path
: Path_Name
) return String is
217 Result
: OS_Lib
.String_Access
:= new String (1 .. 200);
218 Result_Last
: Natural := 0;
220 procedure Append
(C
: Character);
221 procedure Append
(S
: String);
224 procedure Double_Result_Size
;
225 -- Reallocate Result, doubling its size
227 procedure Read
(K
: in out Positive);
228 -- Update Result while reading current Path starting at position K. If
229 -- a variable is found, call Var below.
231 procedure Var
(K
: in out Positive);
232 -- Translate variable name starting at position K with the associated
233 -- environment value.
239 procedure Append
(C
: Character) is
241 if Result_Last
= Result
'Last then
245 Result_Last
:= Result_Last
+ 1;
246 Result
(Result_Last
) := C
;
249 procedure Append
(S
: String) is
251 while Result_Last
+ S
'Length - 1 > Result
'Last loop
255 Result
(Result_Last
+ 1 .. Result_Last
+ S
'Length) := S
;
256 Result_Last
:= Result_Last
+ S
'Length;
259 ------------------------
260 -- Double_Result_Size --
261 ------------------------
263 procedure Double_Result_Size
is
264 New_Result
: constant OS_Lib
.String_Access
:=
265 new String (1 .. 2 * Result
'Last);
268 New_Result
(1 .. Result_Last
) := Result
(1 .. Result_Last
);
269 OS_Lib
.Free
(Result
);
270 Result
:= New_Result
;
271 end Double_Result_Size
;
277 procedure Read
(K
: in out Positive) is
279 For_All_Characters
: loop
280 if Path
(K
) = '$' then
282 -- Could be a variable
284 if K
< Path
'Last then
286 if Path
(K
+ 1) = '$' then
288 -- Not a variable after all, this is a double $, just
289 -- insert one in the result string.
295 -- Let's parse the variable
302 -- We have an ending $ sign
308 -- This is a standard character, just add it to the result
313 -- Skip to next character
317 exit For_All_Characters
when K
> Path
'Last;
318 end loop For_All_Characters
;
325 procedure Var
(K
: in out Positive) is
329 if Path
(K
) = '{' then
331 -- Look for closing } (curly bracket).
337 exit when Path
(E
) = '}' or else E
= Path
'Last;
340 if Path
(E
) = '}' then
342 -- OK found, translate with environment value
345 Env
: OS_Lib
.String_Access
:=
346 OS_Lib
.Getenv
(Path
(K
+ 1 .. E
- 1));
354 -- No closing curly bracket, not a variable after all or a
355 -- syntax error, ignore it, insert string as-is.
358 Append
(Path
(K
.. E
));
362 -- The variable name is everything from current position to first
363 -- non letter/digit character.
367 -- Check that first chartacter is a letter
369 if Characters
.Handling
.Is_Letter
(Path
(E
)) then
373 exit Var_Name
when E
> Path
'Last;
375 if Characters
.Handling
.Is_Letter
(Path
(E
))
376 or else Characters
.Handling
.Is_Digit
(Path
(E
))
387 Env
: OS_Lib
.String_Access
:= OS_Lib
.Getenv
(Path
(K
.. E
));
395 -- This is not a variable after all
406 -- Start of processing for Expand_Path
410 K
: Positive := Path
'First;
416 Returned_Value
: constant String := Result
(1 .. Result_Last
);
419 OS_Lib
.Free
(Result
);
420 return Returned_Value
;
429 function File_Extension
(Path
: Path_Name
) return String is
432 (Path
, Dir_Seps
, Going
=> Strings
.Backward
);
441 Dot
:= Strings
.Fixed
.Index
(Path
(First
.. Path
'Last),
443 Going
=> Strings
.Backward
);
445 if Dot
= 0 or else Dot
= Path
'Last then
448 return Path
(Dot
.. Path
'Last);
456 function File_Name
(Path
: Path_Name
) return String is
458 return Base_Name
(Path
);
461 ---------------------
462 -- Format_Pathname --
463 ---------------------
465 function Format_Pathname
467 Style
: Path_Style
:= System_Default
)
470 N_Path
: String := Path
;
471 K
: Positive := N_Path
'First;
472 Prev_Dirsep
: Boolean := False;
475 for J
in Path
'Range loop
477 if Strings
.Maps
.Is_In
(Path
(J
), Dir_Seps
) then
478 if not Prev_Dirsep
then
480 when UNIX
=> N_Path
(K
) := '/';
481 when DOS
=> N_Path
(K
) := '\';
482 when System_Default
=> N_Path
(K
) := Dir_Separator
;
491 N_Path
(K
) := Path
(J
);
493 Prev_Dirsep
:= False;
497 return N_Path
(N_Path
'First .. K
- 1);
500 ---------------------
501 -- Get_Current_Dir --
502 ---------------------
505 pragma Import
(C
, Max_Path
, "__gnat_max_path_len");
507 function Get_Current_Dir
return Dir_Name_Str
is
508 Current_Dir
: String (1 .. Max_Path
+ 1);
512 Get_Current_Dir
(Current_Dir
, Last
);
513 return Current_Dir
(1 .. Last
);
516 procedure Get_Current_Dir
(Dir
: out Dir_Name_Str
; Last
: out Natural) is
517 Path_Len
: Natural := Max_Path
;
518 Buffer
: String (Dir
'First .. Dir
'First + Max_Path
+ 1);
520 procedure Local_Get_Current_Dir
521 (Dir
: System
.Address
;
522 Length
: System
.Address
);
523 pragma Import
(C
, Local_Get_Current_Dir
, "__gnat_get_current_dir");
526 Local_Get_Current_Dir
(Buffer
'Address, Path_Len
'Address);
528 if Dir
'Length > Path_Len
then
529 Last
:= Dir
'First + Path_Len
- 1;
534 Dir
(Buffer
'First .. Last
) := Buffer
(Buffer
'First .. Last
);
541 function Is_Open
(Dir
: Dir_Type
) return Boolean is
543 return Dir
/= Null_Dir
544 and then System
.Address
(Dir
.all) /= System
.Null_Address
;
551 procedure Make_Dir
(Dir_Name
: Dir_Name_Str
) is
552 C_Dir_Name
: String := Dir_Name
& ASCII
.NUL
;
554 function mkdir
(Dir_Name
: String) return Integer;
555 pragma Import
(C
, mkdir
, "__gnat_mkdir");
558 if mkdir
(C_Dir_Name
) /= 0 then
559 raise Directory_Error
;
569 Dir_Name
: Dir_Name_Str
)
571 C_File_Name
: String := Dir_Name
& ASCII
.NUL
;
575 return Dir_Type_Value
;
576 pragma Import
(C
, opendir
, "opendir");
579 Dir
:= new Dir_Type_Value
'(opendir (C_File_Name));
581 if not Is_Open (Dir) then
584 raise Directory_Error;
593 (Dir : in out Dir_Type;
597 Filename_Addr : Address;
598 Filename_Len : Integer;
600 Buffer : array (0 .. 1024) of Character;
601 -- 1024 is the value of FILENAME_MAX in stdio.h
603 function readdir_gnat
604 (Directory : System.Address;
605 Buffer : System.Address)
606 return System.Address;
607 pragma Import (C, readdir_gnat, "__gnat_readdir");
609 function strlen (S : Address) return Integer;
610 pragma Import (C, strlen, "strlen");
613 if not Is_Open (Dir) then
614 raise Directory_Error;
618 readdir_gnat (System.Address (Dir.all), Buffer'Address);
620 if Filename_Addr = System.Null_Address then
625 Filename_Len := strlen (Filename_Addr);
627 if Str'Length > Filename_Len then
628 Last := Str'First + Filename_Len - 1;
634 subtype Path_String is String (1 .. Filename_Len);
635 type Path_String_Access is access Path_String;
637 function Address_To_Access is new
640 Target => Path_String_Access);
642 Path_Access : Path_String_Access := Address_To_Access (Filename_Addr);
645 for J in Str'First .. Last loop
646 Str (J) := Path_Access (J - Str'First + 1);
651 -------------------------
652 -- Read_Is_Thread_Sage --
653 -------------------------
655 function Read_Is_Thread_Safe return Boolean is
657 function readdir_is_thread_safe return Integer;
659 (C, readdir_is_thread_safe, "__gnat_readdir_is_thread_safe");
662 return (readdir_is_thread_safe /= 0);
663 end Read_Is_Thread_Safe;
669 procedure Remove_Dir (Dir_Name : Dir_Name_Str) is
670 C_Dir_Name : String := Dir_Name & ASCII.NUL;
672 procedure rmdir (Dir_Name : String);
673 pragma Import (C, rmdir, "rmdir");
679 end GNAT.Directory_Operations;