1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . D I R E C T O R I E S --
9 -- Copyright (C) 2004-2009, Free Software Foundation, 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 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.Calendar; use Ada.Calendar;
33 with Ada.Calendar.Formatting; use Ada.Calendar.Formatting;
34 with Ada.Directories.Validity; use Ada.Directories.Validity;
35 with Ada.Strings.Maps;
36 with Ada.Strings.Fixed;
37 with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
38 with Ada.Unchecked_Conversion;
39 with Ada.Unchecked_Deallocation;
40 with Ada.Characters.Handling; use Ada.Characters.Handling;
42 with System.CRTL; use System.CRTL;
43 with System.OS_Lib; use System.OS_Lib;
44 with System.Regexp; use System.Regexp;
48 package body Ada.Directories is
50 Filename_Max : constant Integer := 1024;
51 -- 1024 is the value of FILENAME_MAX in stdio.h
53 type Dir_Type_Value is new System.Address;
54 -- This is the low-level address directory structure as returned by the C
57 No_Dir : constant Dir_Type_Value := Dir_Type_Value (System.Null_Address);
59 Dir_Separator : constant Character;
60 pragma Import (C, Dir_Separator, "__gnat_dir_separator");
61 -- Running system default directory separator
63 Dir_Seps : constant Ada.Strings.Maps.Character_Set :=
64 Ada.Strings.Maps.To_Set ("/\");
65 -- UNIX and DOS style directory separators
68 pragma Import (C, Max_Path, "__gnat_max_path_len");
69 -- The maximum length of a path
71 type Search_Data is record
72 Is_Valid : Boolean := False;
73 Name : Ada.Strings.Unbounded.Unbounded_String;
76 Dir : Dir_Type_Value := No_Dir;
77 Entry_Fetched : Boolean := False;
78 Dir_Entry : Directory_Entry_Type;
80 -- The current state of a search
82 Empty_String : constant String := (1 .. 0 => ASCII.NUL);
83 -- Empty string, returned by function Extension when there is no extension
85 procedure Free is new Ada.Unchecked_Deallocation (Search_Data, Search_Ptr);
87 procedure Close (Dir : Dir_Type_Value);
89 function File_Exists (Name : String) return Boolean;
90 -- Returns True if the named file exists
92 procedure Fetch_Next_Entry (Search : Search_Type);
93 -- Get the next entry in a directory, setting Entry_Fetched if successful
94 -- or resetting Is_Valid if not.
96 procedure To_Lower_If_Case_Insensitive (S : in out String);
97 -- Put S in lower case if file and path names are case-insensitive
103 function Base_Name (Name : String) return String is
104 Simple : String := Simple_Name (Name);
105 -- Simple'First is guaranteed to be 1
108 To_Lower_If_Case_Insensitive (Simple);
110 -- Look for the last dot in the file name and return the part of the
111 -- file name preceding this last dot. If the first dot is the first
112 -- character of the file name, the base name is the empty string.
114 for Pos in reverse Simple'Range loop
115 if Simple (Pos) = '.' then
116 return Simple (1 .. Pos - 1);
120 -- If there is no dot, return the complete file name
129 procedure Close (Dir : Dir_Type_Value) is
131 pragma Warnings (Off, Discard);
133 function closedir (directory : DIRs) return Integer;
134 pragma Import (C, closedir, "__gnat_closedir");
137 Discard := closedir (DIRs (Dir));
145 (Containing_Directory : String := "";
147 Extension : String := "") return String
149 Result : String (1 .. Containing_Directory'Length +
150 Name'Length + Extension'Length + 2);
154 -- First, deal with the invalid cases
156 if Containing_Directory /= ""
157 and then not Is_Valid_Path_Name (Containing_Directory)
159 raise Name_Error with
160 "invalid directory path name """ & Containing_Directory & '"';
163 Extension'Length = 0 and then (not Is_Valid_Simple_Name (Name))
165 raise Name_Error with
166 "invalid simple name """ & Name & '"';
168 elsif Extension'Length /= 0
169 and then not Is_Valid_Simple_Name (Name & '.' & Extension)
171 raise Name_Error with
172 "invalid file name """ & Name & '.' & Extension & '"';
174 -- This is not an invalid case so build the path name
177 Last := Containing_Directory'Length;
178 Result (1 .. Last) := Containing_Directory;
180 -- Add a directory separator if needed
182 if Last /= 0 and then Result (Last) /= Dir_Separator then
184 Result (Last) := Dir_Separator;
189 Result (Last + 1 .. Last + Name'Length) := Name;
190 Last := Last + Name'Length;
192 -- If extension was specified, add dot followed by this extension
194 if Extension'Length /= 0 then
196 Result (Last) := '.';
197 Result (Last + 1 .. Last + Extension'Length) := Extension;
198 Last := Last + Extension'Length;
201 To_Lower_If_Case_Insensitive (Result (1 .. Last));
202 return Result (1 .. Last);
206 --------------------------
207 -- Containing_Directory --
208 --------------------------
210 function Containing_Directory (Name : String) return String is
212 -- First, the invalid case
214 if not Is_Valid_Path_Name (Name) then
215 raise Name_Error with "invalid path name """ & Name & '"';
219 Norm : constant String := Normalize_Pathname (Name);
220 Last_DS : constant Natural :=
222 (Name, Dir_Seps, Going => Strings.Backward);
227 -- There is no directory separator, returns current working
230 return Current_Directory;
232 -- If Name indicates a root directory, raise Use_Error, because
233 -- it has no containing directory.
242 and then Norm (Norm'Last - 1 .. Norm'Last) = ":\"
243 and then (Norm (Norm'First) in 'a' .. 'z'
244 or else Norm (Norm'First) in 'A' .. 'Z'))))
247 "directory """ & Name & """ has no containing directory";
251 Last : Positive := Last_DS - Name'First + 1;
252 Result : String (1 .. Last);
255 Result := Name (Name'First .. Last_DS);
257 -- Remove any trailing directory separator, except as the
258 -- first character or the first character following a drive
259 -- number on Windows.
265 Result (Last) /= Directory_Separator;
269 and then Result (2) = ':'
271 (Result (1) in 'A' .. 'Z'
273 Result (1) in 'a' .. 'z');
278 -- Special case of current directory, identified by "."
280 if Last = 1 and then Result (1) = '.' then
281 return Current_Directory;
283 -- Special case of "..": the current directory may be a root
286 elsif Last = 2 and then Result (1 .. 2) = ".." then
287 return Containing_Directory (Current_Directory);
290 To_Lower_If_Case_Insensitive (Result (1 .. Last));
291 return Result (1 .. Last);
297 end Containing_Directory;
304 (Source_Name : String;
305 Target_Name : String;
308 pragma Unreferenced (Form);
312 -- First, the invalid cases
314 if not Is_Valid_Path_Name (Source_Name) then
315 raise Name_Error with
316 "invalid source path name """ & Source_Name & '"';
318 elsif not Is_Valid_Path_Name (Target_Name) then
319 raise Name_Error with
320 "invalid target path name """ & Target_Name & '"';
322 elsif not Is_Regular_File (Source_Name) then
323 raise Name_Error with '"' & Source_Name & """ is not a file";
325 elsif Is_Directory (Target_Name) then
326 raise Use_Error with "target """ & Target_Name & """ is a directory";
329 -- The implementation uses System.OS_Lib.Copy_File, with parameters
330 -- suitable for all platforms.
332 Copy_File (Source_Name, Target_Name, Success, Overwrite, None);
335 raise Use_Error with "copy of """ & Source_Name & """ failed";
340 ----------------------
341 -- Create_Directory --
342 ----------------------
344 procedure Create_Directory
345 (New_Directory : String;
348 pragma Unreferenced (Form);
350 C_Dir_Name : constant String := New_Directory & ASCII.NUL;
352 function mkdir (Dir_Name : String) return Integer;
353 pragma Import (C, mkdir, "__gnat_mkdir");
356 -- First, the invalid case
358 if not Is_Valid_Path_Name (New_Directory) then
359 raise Name_Error with
360 "invalid new directory path name """ & New_Directory & '"';
363 if mkdir (C_Dir_Name) /= 0 then
365 "creation of new directory """ & New_Directory & """ failed";
368 end Create_Directory;
374 procedure Create_Path
375 (New_Directory : String;
378 pragma Unreferenced (Form);
380 New_Dir : String (1 .. New_Directory'Length + 1);
381 Last : Positive := 1;
384 -- First, the invalid case
386 if not Is_Valid_Path_Name (New_Directory) then
387 raise Name_Error with
388 "invalid new directory path name """ & New_Directory & '"';
391 -- Build New_Dir with a directory separator at the end, so that the
392 -- complete path will be found in the loop below.
394 New_Dir (1 .. New_Directory'Length) := New_Directory;
395 New_Dir (New_Dir'Last) := Directory_Separator;
397 -- Create, if necessary, each directory in the path
399 for J in 2 .. New_Dir'Last loop
401 -- Look for the end of an intermediate directory
403 if New_Dir (J) /= Dir_Separator and then
408 -- We have found a new intermediate directory each time we find
409 -- a first directory separator.
411 elsif New_Dir (J - 1) /= Dir_Separator and then
412 New_Dir (J - 1) /= '/'
415 -- No need to create the directory if it already exists
417 if Is_Directory (New_Dir (1 .. Last)) then
420 -- It is an error if a file with such a name already exists
422 elsif Is_Regular_File (New_Dir (1 .. Last)) then
424 "file """ & New_Dir (1 .. Last) & """ already exists";
427 Create_Directory (New_Directory => New_Dir (1 .. Last));
434 -----------------------
435 -- Current_Directory --
436 -----------------------
438 function Current_Directory return String is
439 Path_Len : Natural := Max_Path;
440 Buffer : String (1 .. 1 + Max_Path + 1);
442 procedure Local_Get_Current_Dir
443 (Dir : System.Address;
444 Length : System.Address);
445 pragma Import (C, Local_Get_Current_Dir, "__gnat_get_current_dir");
448 Local_Get_Current_Dir (Buffer'Address, Path_Len'Address);
451 Cur : String := Normalize_Pathname (Buffer (1 .. Path_Len));
454 To_Lower_If_Case_Insensitive (Cur);
456 if Cur'Length > 1 and then Cur (Cur'Last) = Dir_Separator then
457 return Cur (1 .. Cur'Last - 1);
462 end Current_Directory;
464 ----------------------
465 -- Delete_Directory --
466 ----------------------
468 procedure Delete_Directory (Directory : String) is
470 -- First, the invalid cases
472 if not Is_Valid_Path_Name (Directory) then
473 raise Name_Error with
474 "invalid directory path name """ & Directory & '"';
476 elsif not Is_Directory (Directory) then
477 raise Name_Error with '"' & Directory & """ not a directory";
481 C_Dir_Name : constant String := Directory & ASCII.NUL;
486 if System.OS_Lib.Is_Directory (Directory) then
488 "deletion of directory """ & Directory & """ failed";
492 end Delete_Directory;
498 procedure Delete_File (Name : String) is
502 -- First, the invalid cases
504 if not Is_Valid_Path_Name (Name) then
505 raise Name_Error with "invalid path name """ & Name & '"';
507 elsif not Is_Regular_File (Name) then
508 raise Name_Error with "file """ & Name & """ does not exist";
511 -- The implementation uses System.OS_Lib.Delete_File
513 Delete_File (Name, Success);
516 raise Use_Error with "file """ & Name & """ could not be deleted";
525 procedure Delete_Tree (Directory : String) is
526 Current_Dir : constant String := Current_Directory;
527 Search : Search_Type;
528 Dir_Ent : Directory_Entry_Type;
530 -- First, the invalid cases
532 if not Is_Valid_Path_Name (Directory) then
533 raise Name_Error with
534 "invalid directory path name """ & Directory & '"';
536 elsif not Is_Directory (Directory) then
537 raise Name_Error with '"' & Directory & """ not a directory";
540 Set_Directory (Directory);
541 Start_Search (Search, Directory => ".", Pattern => "");
543 while More_Entries (Search) loop
544 Get_Next_Entry (Search, Dir_Ent);
547 File_Name : constant String := Simple_Name (Dir_Ent);
550 if System.OS_Lib.Is_Directory (File_Name) then
551 if File_Name /= "." and then File_Name /= ".." then
552 Delete_Tree (File_Name);
556 Delete_File (File_Name);
561 Set_Directory (Current_Dir);
565 C_Dir_Name : constant String := Directory & ASCII.NUL;
570 if System.OS_Lib.Is_Directory (Directory) then
572 "directory tree rooted at """ &
573 Directory & """ could not be deleted";
583 function Exists (Name : String) return Boolean is
585 -- First, the invalid case
587 if not Is_Valid_Path_Name (Name) then
588 raise Name_Error with "invalid path name """ & Name & '"';
591 -- The implementation is in File_Exists
593 return File_Exists (Name);
601 function Extension (Name : String) return String is
603 -- First, the invalid case
605 if not Is_Valid_Path_Name (Name) then
606 raise Name_Error with "invalid path name """ & Name & '"';
609 -- Look for first dot that is not followed by a directory separator
611 for Pos in reverse Name'Range loop
613 -- If a directory separator is found before a dot, there is no
616 if Name (Pos) = Dir_Separator then
619 elsif Name (Pos) = '.' then
621 -- We found a dot, build the return value with lower bound 1
624 subtype Result_Type is String (1 .. Name'Last - Pos);
626 return Result_Type (Name (Pos + 1 .. Name'Last));
631 -- No dot were found, there is no extension
637 ----------------------
638 -- Fetch_Next_Entry --
639 ----------------------
641 procedure Fetch_Next_Entry (Search : Search_Type) is
642 Name : String (1 .. 255);
645 Kind : File_Kind := Ordinary_File;
646 -- Initialized to avoid a compilation warning
648 Filename_Addr : System.Address;
649 Filename_Len : aliased Integer;
651 Buffer : array (0 .. Filename_Max + 12) of Character;
652 -- 12 is the size of the dirent structure (see dirent.h), without the
653 -- field for the filename.
655 function readdir_gnat
656 (Directory : System.Address;
657 Buffer : System.Address;
658 Last : not null access Integer) return System.Address;
659 pragma Import (C, readdir_gnat, "__gnat_readdir");
664 -- Search.Value.Is_Valid is always True when Fetch_Next_Entry is called
669 (System.Address (Search.Value.Dir),
671 Filename_Len'Access);
673 -- If no matching entry is found, set Is_Valid to False
675 if Filename_Addr = System.Null_Address then
676 Search.Value.Is_Valid := False;
681 subtype Path_String is String (1 .. Filename_Len);
682 type Path_String_Access is access Path_String;
684 function Address_To_Access is new
685 Ada.Unchecked_Conversion
687 Target => Path_String_Access);
689 Path_Access : constant Path_String_Access :=
690 Address_To_Access (Filename_Addr);
693 Last := Filename_Len;
694 Name (1 .. Last) := Path_Access.all;
697 -- Check if the entry matches the pattern
699 if Match (Name (1 .. Last), Search.Value.Pattern) then
701 Full_Name : constant String :=
704 (Search.Value.Name), Name (1 .. Last));
705 Found : Boolean := False;
708 if File_Exists (Full_Name) then
710 -- Now check if the file kind matches the filter
712 if Is_Regular_File (Full_Name) then
713 if Search.Value.Filter (Ordinary_File) then
714 Kind := Ordinary_File;
718 elsif Is_Directory (Full_Name) then
719 if Search.Value.Filter (Directory) then
724 elsif Search.Value.Filter (Special_File) then
725 Kind := Special_File;
729 -- If it does, update Search and return
732 Search.Value.Entry_Fetched := True;
733 Search.Value.Dir_Entry :=
735 Simple => To_Unbounded_String (Name (1 .. Last)),
736 Full => To_Unbounded_String (Full_Name),
744 end Fetch_Next_Entry;
750 function File_Exists (Name : String) return Boolean is
751 function C_File_Exists (A : System.Address) return Integer;
752 pragma Import (C, C_File_Exists, "__gnat_file_exists");
754 C_Name : String (1 .. Name'Length + 1);
757 C_Name (1 .. Name'Length) := Name;
758 C_Name (C_Name'Last) := ASCII.NUL;
759 return C_File_Exists (C_Name (1)'Address) = 1;
766 procedure Finalize (Search : in out Search_Type) is
768 if Search.Value /= null then
770 -- Close the directory, if one is open
772 if Search.Value.Dir /= No_Dir then
773 Close (Search.Value.Dir);
784 function Full_Name (Name : String) return String is
786 -- First, the invalid case
788 if not Is_Valid_Path_Name (Name) then
789 raise Name_Error with "invalid path name """ & Name & '"';
792 -- Build the return value with lower bound 1
794 -- Use System.OS_Lib.Normalize_Pathname
797 Value : String := Normalize_Pathname (Name);
798 subtype Result is String (1 .. Value'Length);
800 To_Lower_If_Case_Insensitive (Value);
801 return Result (Value);
806 function Full_Name (Directory_Entry : Directory_Entry_Type) return String is
808 -- First, the invalid case
810 if not Directory_Entry.Is_Valid then
811 raise Status_Error with "invalid directory entry";
814 -- The value to return has already been computed
816 return To_String (Directory_Entry.Full);
824 procedure Get_Next_Entry
825 (Search : in out Search_Type;
826 Directory_Entry : out Directory_Entry_Type)
829 -- First, the invalid case
831 if Search.Value = null or else not Search.Value.Is_Valid then
832 raise Status_Error with "invalid search";
835 -- Fetch the next entry, if needed
837 if not Search.Value.Entry_Fetched then
838 Fetch_Next_Entry (Search);
841 -- It is an error if no valid entry is found
843 if not Search.Value.Is_Valid then
844 raise Status_Error with "no next entry";
847 -- Reset Entry_Fetched and return the entry
849 Search.Value.Entry_Fetched := False;
850 Directory_Entry := Search.Value.Dir_Entry;
858 function Kind (Name : String) return File_Kind is
860 -- First, the invalid case
862 if not File_Exists (Name) then
863 raise Name_Error with "file """ & Name & """ does not exist";
865 elsif Is_Regular_File (Name) then
866 return Ordinary_File;
868 elsif Is_Directory (Name) then
876 function Kind (Directory_Entry : Directory_Entry_Type) return File_Kind is
878 -- First, the invalid case
880 if not Directory_Entry.Is_Valid then
881 raise Status_Error with "invalid directory entry";
884 -- The value to return has already be computed
886 return Directory_Entry.Kind;
890 -----------------------
891 -- Modification_Time --
892 -----------------------
894 function Modification_Time (Name : String) return Time is
900 Minute : Minute_Type;
901 Second : Second_Type;
905 -- First, the invalid cases
907 if not (Is_Regular_File (Name) or else Is_Directory (Name)) then
908 raise Name_Error with '"' & Name & """ not a file or directory";
911 Date := File_Time_Stamp (Name);
913 -- Break down the time stamp into its constituents relative to GMT.
914 -- This version of Split does not recognize leap seconds or buffer
915 -- space for time zone processing.
917 GM_Split (Date, Year, Month, Day, Hour, Minute, Second);
919 -- On OpenVMS, the resulting time value must be in the local time
920 -- zone. Ada.Calendar.Time_Of is exactly what we need. Note that
921 -- in both cases, the sub seconds are set to zero (0.0) because the
922 -- time stamp does not store them in its value.
927 (Year, Month, Day, Seconds_Of (Hour, Minute, Second, 0.0));
929 -- On Unix and Windows, the result must be in GMT. Ada.Calendar.
930 -- Formatting.Time_Of with default time zone of zero (0) is the
931 -- routine of choice.
934 Result := Time_Of (Year, Month, Day, Hour, Minute, Second, 0.0);
939 end Modification_Time;
941 function Modification_Time
942 (Directory_Entry : Directory_Entry_Type) return Ada.Calendar.Time
945 -- First, the invalid case
947 if not Directory_Entry.Is_Valid then
948 raise Status_Error with "invalid directory entry";
951 -- The value to return has already be computed
953 return Modification_Time (To_String (Directory_Entry.Full));
955 end Modification_Time;
961 function More_Entries (Search : Search_Type) return Boolean is
963 if Search.Value = null then
966 elsif Search.Value.Is_Valid then
968 -- Fetch the next entry, if needed
970 if not Search.Value.Entry_Fetched then
971 Fetch_Next_Entry (Search);
975 return Search.Value.Is_Valid;
982 procedure Rename (Old_Name, New_Name : String) is
986 -- First, the invalid cases
988 if not Is_Valid_Path_Name (Old_Name) then
989 raise Name_Error with "invalid old path name """ & Old_Name & '"';
991 elsif not Is_Valid_Path_Name (New_Name) then
992 raise Name_Error with "invalid new path name """ & New_Name & '"';
994 elsif not Is_Regular_File (Old_Name)
995 and then not Is_Directory (Old_Name)
997 raise Name_Error with "old file """ & Old_Name & """ does not exist";
999 elsif Is_Regular_File (New_Name) or Is_Directory (New_Name) then
1000 raise Use_Error with
1001 "new name """ & New_Name
1002 & """ designates a file that already exists";
1005 -- The implementation uses System.OS_Lib.Rename_File
1007 Rename_File (Old_Name, New_Name, Success);
1010 raise Use_Error with
1011 "file """ & Old_Name & """ could not be renamed";
1021 (Directory : String;
1023 Filter : Filter_Type := (others => True);
1024 Process : not null access procedure
1025 (Directory_Entry : Directory_Entry_Type))
1028 Directory_Entry : Directory_Entry_Type;
1031 Start_Search (Srch, Directory, Pattern, Filter);
1033 while More_Entries (Srch) loop
1034 Get_Next_Entry (Srch, Directory_Entry);
1035 Process (Directory_Entry);
1045 procedure Set_Directory (Directory : String) is
1046 C_Dir_Name : constant String := Directory & ASCII.NUL;
1048 function chdir (Dir_Name : String) return Integer;
1049 pragma Import (C, chdir, "chdir");
1052 if not Is_Valid_Path_Name (Directory) then
1053 raise Name_Error with
1054 "invalid directory path name & """ & Directory & '"';
1056 elsif not Is_Directory (Directory) then
1057 raise Name_Error with
1058 "directory """ & Directory & """ does not exist";
1060 elsif chdir (C_Dir_Name) /= 0 then
1061 raise Name_Error with
1062 "could not set to designated directory """ & Directory & '"';
1070 function Simple_Name (Name : String) return String is
1072 function Simple_Name_CI (Path : String) return String;
1073 -- This function does the job. The difference between Simple_Name_CI
1074 -- and Simple_Name (the parent function) is that the former is case
1075 -- sensitive, while the latter is not. Path and Suffix are adjusted
1076 -- appropriately before calling Simple_Name_CI under platforms where
1077 -- the file system is not case sensitive.
1079 --------------------
1080 -- Simple_Name_CI --
1081 --------------------
1083 function Simple_Name_CI (Path : String) return String is
1084 Cut_Start : Natural :=
1086 (Path, Dir_Seps, Going => Strings.Backward);
1090 -- Cut_Start point to the first simple name character
1092 if Cut_Start = 0 then
1093 Cut_Start := Path'First;
1096 Cut_Start := Cut_Start + 1;
1099 -- Cut_End point to the last simple name character
1101 Cut_End := Path'Last;
1103 Check_For_Standard_Dirs : declare
1104 Offset : constant Integer := Path'First - Name'First;
1105 BN : constant String :=
1106 Name (Cut_Start - Offset .. Cut_End - Offset);
1107 -- Here we use Simple_Name.Name to keep the original casing
1109 Has_Drive_Letter : constant Boolean :=
1110 System.OS_Lib.Path_Separator /= ':';
1111 -- If Path separator is not ':' then we are on a DOS based OS
1112 -- where this character is used as a drive letter separator.
1115 if BN = "." or else BN = ".." then
1118 elsif Has_Drive_Letter
1119 and then BN'Length > 2
1120 and then Characters.Handling.Is_Letter (BN (BN'First))
1121 and then BN (BN'First + 1) = ':'
1123 -- We have a DOS drive letter prefix, remove it
1125 return BN (BN'First + 2 .. BN'Last);
1130 end Check_For_Standard_Dirs;
1133 -- Start of processing for Simple_Name
1136 -- First, the invalid case
1138 if not Is_Valid_Path_Name (Name) then
1139 raise Name_Error with "invalid path name """ & Name & '"';
1142 -- Build the value to return with lower bound 1
1144 if Is_Path_Name_Case_Sensitive then
1146 Value : constant String := Simple_Name_CI (Name);
1147 subtype Result is String (1 .. Value'Length);
1149 return Result (Value);
1154 Value : constant String :=
1155 Simple_Name_CI (Characters.Handling.To_Lower (Name));
1156 subtype Result is String (1 .. Value'Length);
1158 return Result (Value);
1164 function Simple_Name
1165 (Directory_Entry : Directory_Entry_Type) return String
1168 -- First, the invalid case
1170 if not Directory_Entry.Is_Valid then
1171 raise Status_Error with "invalid directory entry";
1174 -- The value to return has already be computed
1176 return To_String (Directory_Entry.Simple);
1184 function Size (Name : String) return File_Size is
1185 C_Name : String (1 .. Name'Length + 1);
1187 function C_Size (Name : System.Address) return Long_Integer;
1188 pragma Import (C, C_Size, "__gnat_named_file_length");
1191 -- First, the invalid case
1193 if not Is_Regular_File (Name) then
1194 raise Name_Error with "file """ & Name & """ does not exist";
1197 C_Name (1 .. Name'Length) := Name;
1198 C_Name (C_Name'Last) := ASCII.NUL;
1199 return File_Size (C_Size (C_Name'Address));
1203 function Size (Directory_Entry : Directory_Entry_Type) return File_Size is
1205 -- First, the invalid case
1207 if not Directory_Entry.Is_Valid then
1208 raise Status_Error with "invalid directory entry";
1211 -- The value to return has already be computed
1213 return Size (To_String (Directory_Entry.Full));
1221 procedure Start_Search
1222 (Search : in out Search_Type;
1225 Filter : Filter_Type := (others => True))
1227 function opendir (file_name : String) return DIRs;
1228 pragma Import (C, opendir, "__gnat_opendir");
1230 C_File_Name : constant String := Directory & ASCII.NUL;
1232 Dir : Dir_Type_Value;
1235 -- First, the invalid case Name_Error
1237 if not Is_Directory (Directory) then
1238 raise Name_Error with
1239 "unknown directory """ & Simple_Name (Directory) & '"';
1242 -- Check the pattern
1245 Pat := Compile (Pattern, Glob => True);
1247 when Error_In_Regexp =>
1248 Free (Search.Value);
1249 raise Name_Error with "invalid pattern """ & Pattern & '"';
1252 Dir := Dir_Type_Value (opendir (C_File_Name));
1254 if Dir = No_Dir then
1255 raise Use_Error with
1256 "unreadable directory """ & Simple_Name (Directory) & '"';
1259 -- If needed, finalize Search
1263 -- Allocate the default data
1265 Search.Value := new Search_Data;
1267 -- Initialize some Search components
1269 Search.Value.Filter := Filter;
1270 Search.Value.Name := To_Unbounded_String (Full_Name (Directory));
1271 Search.Value.Pattern := Pat;
1272 Search.Value.Dir := Dir;
1273 Search.Value.Is_Valid := True;
1276 ----------------------------------
1277 -- To_Lower_If_Case_Insensitive --
1278 ----------------------------------
1280 procedure To_Lower_If_Case_Insensitive (S : in out String) is
1282 if not Is_Path_Name_Case_Sensitive then
1283 for J in S'Range loop
1284 S (J) := To_Lower (S (J));
1287 end To_Lower_If_Case_Insensitive;
1289 end Ada.Directories;