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 --
11 -- Copyright (C) 1998-2001 Ada Core Technologies, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 with Ada.Characters.Handling;
36 with Ada.Strings.Fixed;
37 with Ada.Strings.Maps;
38 with Unchecked_Deallocation;
39 with Unchecked_Conversion;
40 with System; use System;
44 package body GNAT.Directory_Operations is
48 type Dir_Type_Value is new System.Address;
49 -- This is the low-level address directory structure as returned by the C
53 Unchecked_Deallocation (Dir_Type_Value, Dir_Type);
61 Suffix : String := "")
64 function Get_File_Names_Case_Sensitive return Integer;
66 (C, Get_File_Names_Case_Sensitive,
67 "__gnat_get_file_names_case_sensitive");
69 Case_Sensitive_File_Name : constant Boolean :=
70 Get_File_Names_Case_Sensitive = 1;
74 Suffix : 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 := "")
91 Cut_Start : Natural :=
93 (Path, Dir_Seps, Going => Strings.Backward);
97 -- Cut_Start point to the first basename character
100 Cut_Start := Path'First;
103 Cut_Start := Cut_Start + 1;
106 -- Cut_End point to the last basename character.
108 Cut_End := Path'Last;
110 -- If basename ends with Suffix, adjust Cut_End.
113 and then Path (Path'Last - Suffix'Length + 1 .. Cut_End) = Suffix
115 Cut_End := Path'Last - Suffix'Length;
118 Check_For_Standard_Dirs : declare
119 Offset : constant Integer := Path'First - Base_Name.Path'First;
120 BN : constant String :=
121 Base_Name.Path (Cut_Start - Offset .. Cut_End - Offset);
122 -- Here we use Base_Name.Path to keep the original casing
125 if BN = "." or else BN = ".." then
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 processing for Base_Name
145 if Case_Sensitive_File_Name then
146 return Basename (Path, Suffix);
150 (Characters.Handling.To_Lower (Path),
151 Characters.Handling.To_Lower (Suffix));
159 procedure Change_Dir (Dir_Name : Dir_Name_Str) is
160 C_Dir_Name : String := Dir_Name & ASCII.NUL;
162 function chdir (Dir_Name : String) return Integer;
163 pragma Import (C, chdir, "chdir");
166 if chdir (C_Dir_Name) /= 0 then
167 raise Directory_Error;
175 procedure Close (Dir : in out Dir_Type) is
177 function closedir (Directory : System.Address) return Integer;
178 pragma Import (C, closedir, "closedir");
183 if not Is_Open (Dir) then
184 raise Directory_Error;
187 Discard := closedir (System.Address (Dir.all));
195 function Dir_Name (Path : Path_Name) return Dir_Name_Str is
196 Last_DS : constant Natural :=
198 (Path, Dir_Seps, Going => Strings.Backward);
203 -- There is no directory separator, returns current working directory
205 return "." & Dir_Separator;
208 return Path (Path'First .. Last_DS);
216 function Expand_Path (Path : Path_Name) return String is
218 Result : OS_Lib.String_Access := new String (1 .. 200);
219 Result_Last : Natural := 0;
221 procedure Append (C : Character);
222 procedure Append (S : String);
225 procedure Double_Result_Size;
226 -- Reallocate Result, doubling its size
228 procedure Read (K : in out Positive);
229 -- Update Result while reading current Path starting at position K. If
230 -- a variable is found, call Var below.
232 procedure Var (K : in out Positive);
233 -- Translate variable name starting at position K with the associated
234 -- environment value.
240 procedure Append (C : Character) is
242 if Result_Last = Result'Last then
246 Result_Last := Result_Last + 1;
247 Result (Result_Last) := C;
250 procedure Append (S : String) is
252 while Result_Last + S'Length - 1 > Result'Last loop
256 Result (Result_Last + 1 .. Result_Last + S'Length - 1) := S;
257 Result_Last := Result_Last + S'Length - 1;
260 ------------------------
261 -- Double_Result_Size --
262 ------------------------
264 procedure Double_Result_Size is
265 New_Result : constant OS_Lib.String_Access :=
266 new String (1 .. 2 * Result'Last);
269 New_Result (1 .. Result_Last) := Result (1 .. Result_Last);
270 OS_Lib.Free (Result);
271 Result := New_Result;
272 end Double_Result_Size;
278 procedure Read (K : in out Positive) is
280 For_All_Characters : loop
281 if Path (K) = '$' then
283 -- Could be a variable
285 if K < Path'Last then
287 if Path (K + 1) = '$' then
289 -- Not a variable after all, this is a double $, just
290 -- insert one in the result string.
296 -- Let's parse the variable
303 -- We have an ending $ sign
309 -- This is a standard character, just add it to the result
314 -- Skip to next character
318 exit For_All_Characters when K > Path'Last;
319 end loop For_All_Characters;
326 procedure Var (K : in out Positive) is
330 if Path (K) = '{' then
332 -- Look for closing } (curly bracket).
338 exit when Path (E) = '}' or else E = Path'Last;
341 if Path (E) = '}' then
343 -- OK found, translate with environment value
346 Env : OS_Lib.String_Access :=
347 OS_Lib.Getenv (Path (K + 1 .. E - 1));
355 -- No closing curly bracket, not a variable after all or a
356 -- syntax error, ignore it, insert string as-is.
359 Append (Path (K .. E));
363 -- The variable name is everything from current position to first
364 -- non letter/digit character.
368 -- Check that first chartacter is a letter
370 if Characters.Handling.Is_Letter (Path (E)) then
374 exit Var_Name when E = Path'Last;
376 if Characters.Handling.Is_Letter (Path (E))
377 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 -- Get_Current_Dir --
463 ---------------------
466 pragma Import (C, Max_Path, "max_path_len");
468 function Get_Current_Dir return Dir_Name_Str is
469 Current_Dir : String (1 .. Max_Path + 1);
473 Get_Current_Dir (Current_Dir, Last);
474 return Current_Dir (1 .. Last);
477 procedure Get_Current_Dir (Dir : out Dir_Name_Str; Last : out Natural) is
478 Path_Len : Natural := Max_Path;
479 Buffer : String (Dir'First .. Dir'First + Max_Path + 1);
481 procedure Local_Get_Current_Dir
482 (Dir : System.Address;
483 Length : System.Address);
484 pragma Import (C, Local_Get_Current_Dir, "__gnat_get_current_dir");
487 Local_Get_Current_Dir (Buffer'Address, Path_Len'Address);
489 if Dir'Length > Path_Len then
490 Last := Dir'First + Path_Len - 1;
495 Dir (Buffer'First .. Last) := Buffer (Buffer'First .. Last);
502 function Is_Open (Dir : Dir_Type) return Boolean is
504 return Dir /= Null_Dir
505 and then System.Address (Dir.all) /= System.Null_Address;
512 procedure Make_Dir (Dir_Name : Dir_Name_Str) is
513 C_Dir_Name : String := Dir_Name & ASCII.NUL;
515 function mkdir (Dir_Name : String) return Integer;
516 pragma Import (C, mkdir, "__gnat_mkdir");
519 if mkdir (C_Dir_Name) /= 0 then
520 raise Directory_Error;
524 ------------------------
525 -- Normalize_Pathname --
526 ------------------------
528 function Normalize_Pathname
530 Style : Path_Style := System_Default)
533 N_Path : String := Path;
534 K : Positive := N_Path'First;
535 Prev_Dirsep : Boolean := False;
538 for J in Path'Range loop
540 if Strings.Maps.Is_In (Path (J), Dir_Seps) then
541 if not Prev_Dirsep then
544 when UNIX => N_Path (K) := '/';
545 when DOS => N_Path (K) := '\';
546 when System_Default => N_Path (K) := Dir_Separator;
555 N_Path (K) := Path (J);
557 Prev_Dirsep := False;
561 return N_Path (N_Path'First .. K - 1);
562 end Normalize_Pathname;
570 Dir_Name : Dir_Name_Str)
572 C_File_Name : String := Dir_Name & ASCII.NUL;
576 return Dir_Type_Value;
577 pragma Import (C, opendir, "opendir");
580 Dir := new Dir_Type_Value'(opendir (C_File_Name));
582 if not Is_Open (Dir) then
585 raise Directory_Error;
594 (Dir : in out Dir_Type;
598 Filename_Addr : Address;
599 Filename_Len : Integer;
601 Buffer : array (0 .. 1024) of Character;
602 -- 1024 is the value of FILENAME_MAX in stdio.h
604 function readdir_gnat
605 (Directory : System.Address;
606 Buffer : System.Address)
607 return System.Address;
608 pragma Import (C, readdir_gnat, "__gnat_readdir");
610 function strlen (S : Address) return Integer;
611 pragma Import (C, strlen, "strlen");
614 if not Is_Open (Dir) then
615 raise Directory_Error;
619 readdir_gnat (System.Address (Dir.all), Buffer'Address);
621 if Filename_Addr = System.Null_Address then
626 Filename_Len := strlen (Filename_Addr);
628 if Str'Length > Filename_Len then
629 Last := Str'First + Filename_Len - 1;
635 subtype Path_String is String (1 .. Filename_Len);
636 type Path_String_Access is access Path_String;
638 function Address_To_Access is new
641 Target => Path_String_Access);
643 Path_Access : Path_String_Access := Address_To_Access (Filename_Addr);
646 for J in Str'First .. Last loop
647 Str (J) := Path_Access (J - Str'First + 1);
652 -------------------------
653 -- Read_Is_Thread_Sage --
654 -------------------------
656 function Read_Is_Thread_Safe return Boolean is
658 function readdir_is_thread_safe return Integer;
660 (C, readdir_is_thread_safe, "__gnat_readdir_is_thread_safe");
663 return (readdir_is_thread_safe /= 0);
664 end Read_Is_Thread_Safe;
670 procedure Remove_Dir (Dir_Name : Dir_Name_Str) is
671 C_Dir_Name : String := Dir_Name & ASCII.NUL;
673 procedure rmdir (Dir_Name : String);
674 pragma Import (C, rmdir, "rmdir");
680 end GNAT.Directory_Operations;