1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . F I L E _ I O --
9 -- Copyright (C) 1992-2010, 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
.Finalization
; use Ada
.Finalization
;
33 with Ada
.IO_Exceptions
; use Ada
.IO_Exceptions
;
36 with Interfaces
.C
.Strings
; use Interfaces
.C
.Strings
;
37 with Interfaces
.C_Streams
; use Interfaces
.C_Streams
;
39 with System
.CRTL
.Runtime
;
40 with System
.Case_Util
; use System
.Case_Util
;
42 with System
.Soft_Links
;
44 with Ada
.Unchecked_Deallocation
;
46 package body System
.File_IO
is
48 use System
.File_Control_Block
;
50 package SSL
renames System
.Soft_Links
;
52 use type Interfaces
.C
.int
;
55 ----------------------
56 -- Global Variables --
57 ----------------------
59 Open_Files
: AFCB_Ptr
;
60 -- This points to a list of AFCB's for all open files. This is a doubly
61 -- linked list, with the Prev pointer of the first entry, and the Next
62 -- pointer of the last entry containing null. Note that this global
63 -- variable must be properly protected to provide thread safety.
65 type Temp_File_Record
;
66 type Temp_File_Record_Ptr
is access all Temp_File_Record
;
68 type Temp_File_Record
is record
69 Name
: String (1 .. max_path_len
+ 1);
70 Next
: Temp_File_Record_Ptr
;
72 -- One of these is allocated for each temporary file created
74 Temp_Files
: Temp_File_Record_Ptr
;
75 -- Points to list of names of temporary files. Note that this global
76 -- variable must be properly protected to provide thread safety.
78 type File_IO_Clean_Up_Type
is new Limited_Controlled
with null record;
79 -- The closing of all open files and deletion of temporary files is an
80 -- action that takes place at the end of execution of the main program.
81 -- This action is implemented using a library level object which gets
82 -- finalized at the end of program execution. Note that the type is
83 -- limited, in order to stop the compiler optimizing away the declaration
84 -- which would be allowed in the non-limited case.
86 procedure Finalize
(V
: in out File_IO_Clean_Up_Type
);
87 -- This is the finalize operation that is used to do the cleanup
89 File_IO_Clean_Up_Object
: File_IO_Clean_Up_Type
;
90 pragma Warnings
(Off
, File_IO_Clean_Up_Object
);
91 -- This is the single object of the type that triggers the finalization
92 -- call. Since it is at the library level, this happens just before the
93 -- environment task is finalized.
95 text_translation_required
: Boolean;
96 for text_translation_required
'Size use Character'Size;
98 (C
, text_translation_required
, "__gnat_text_translation_required");
99 -- If true, add appropriate suffix to control string for Open
101 function Get_Case_Sensitive
return Integer;
102 pragma Import
(C
, Get_Case_Sensitive
,
103 "__gnat_get_file_names_case_sensitive");
104 File_Names_Case_Sensitive
: constant Boolean := Get_Case_Sensitive
/= 0;
105 -- Set to indicate whether the operating system convention is for file
106 -- names to be case sensitive (e.g., in Unix, set True), or non case
107 -- sensitive (e.g., in Windows, set False).
109 -----------------------
110 -- Local Subprograms --
111 -----------------------
113 procedure Free_String
is new Ada
.Unchecked_Deallocation
(String, Pstring
);
115 subtype Fopen_String
is String (1 .. 4);
116 -- Holds open string (longest is "w+b" & nul)
123 Fopstr
: out Fopen_String
);
124 -- Determines proper open mode for a file to be opened in the given
125 -- Ada mode. Text is true for a text file and false otherwise, and
126 -- Creat is true for a create call, and False for an open call. The
127 -- value stored in Fopstr is a nul-terminated string suitable for a
128 -- call to fopen or freopen. Amethod is the character designating
129 -- the access method from the Access_Method field of the FCB.
131 function Errno_Message
132 (Errno
: Integer := OS_Lib
.Errno
) return String;
133 function Errno_Message
135 Errno
: Integer := OS_Lib
.Errno
) return String;
136 -- Return a message suitable for "raise ... with Errno_Message (...)".
137 -- Errno defaults to the current errno, but should be passed explicitly if
138 -- there is significant code in between the call that sets errno and the
139 -- call to Errno_Message, in case that code also sets errno. The version
140 -- with Name includes that file name in the message.
142 procedure Raise_Device_Error
143 (File
: AFCB_Ptr
; Errno
: Integer := OS_Lib
.Errno
);
144 pragma No_Return
(Raise_Device_Error
);
145 -- Clear error indication on File and raise Device_Error with an exception
146 -- message providing errno information.
152 procedure Append_Set
(File
: AFCB_Ptr
) is
154 if File
.Mode
= Append_File
then
155 if fseek
(File
.Stream
, 0, SEEK_END
) /= 0 then
156 Raise_Device_Error
(File
);
165 procedure Chain_File
(File
: AFCB_Ptr
) is
167 -- Take a task lock, to protect the global data value Open_Files
171 -- Do the chaining operation locked
173 File
.Next
:= Open_Files
;
177 if File
.Next
/= null then
178 File
.Next
.Prev
:= File
;
189 ---------------------
190 -- Check_File_Open --
191 ---------------------
193 procedure Check_File_Open
(File
: AFCB_Ptr
) is
196 raise Status_Error
with "file not open";
200 -----------------------
201 -- Check_Read_Status --
202 -----------------------
204 procedure Check_Read_Status
(File
: AFCB_Ptr
) is
207 raise Status_Error
with "file not open";
208 elsif File
.Mode
> Inout_File
then
209 raise Mode_Error
with "file not readable";
211 end Check_Read_Status
;
213 ------------------------
214 -- Check_Write_Status --
215 ------------------------
217 procedure Check_Write_Status
(File
: AFCB_Ptr
) is
220 raise Status_Error
with "file not open";
221 elsif File
.Mode
= In_File
then
222 raise Mode_Error
with "file not writable";
224 end Check_Write_Status
;
230 procedure Close
(File_Ptr
: access AFCB_Ptr
) is
231 Close_Status
: int
:= 0;
232 Dup_Strm
: Boolean := False;
233 File
: AFCB_Ptr
renames File_Ptr
.all;
237 -- Take a task lock, to protect the global data value Open_Files
241 Check_File_Open
(File
);
244 -- Sever the association between the given file and its associated
245 -- external file. The given file is left closed. Do not perform system
246 -- closes on the standard input, output and error files and also do not
247 -- attempt to close a stream that does not exist (signalled by a null
248 -- stream value -- happens in some error situations).
250 if not File
.Is_System_File
and then File
.Stream
/= NULL_Stream
then
252 -- Do not do an fclose if this is a shared file and there is at least
253 -- one other instance of the stream that is open.
255 if File
.Shared_Status
= Yes
then
262 if P
/= File
and then File
.Stream
= P
.Stream
then
272 -- Do the fclose unless this was a duplicate in the shared case
275 Close_Status
:= fclose
(File
.Stream
);
277 if Close_Status
/= 0 then
278 Errno
:= OS_Lib
.Errno
;
283 -- Dechain file from list of open files and then free the storage
285 if File
.Prev
= null then
286 Open_Files
:= File
.Next
;
288 File
.Prev
.Next
:= File
.Next
;
291 if File
.Next
/= null then
292 File
.Next
.Prev
:= File
.Prev
;
295 -- Deallocate some parts of the file structure that were kept in heap
296 -- storage with the exception of system files (standard input, output
297 -- and error) since they had some information allocated in the stack.
299 if not File
.Is_System_File
then
300 Free_String
(File
.Name
);
301 Free_String
(File
.Form
);
307 if Close_Status
/= 0 then
308 Raise_Device_Error
(null, Errno
);
323 procedure Delete
(File_Ptr
: access AFCB_Ptr
) is
324 File
: AFCB_Ptr
renames File_Ptr
.all;
327 Check_File_Open
(File
);
329 if not File
.Is_Regular_File
then
330 raise Use_Error
with "cannot delete non-regular file";
334 Filename
: aliased constant String := File
.Name
.all;
339 -- Now unlink the external file. Note that we use the full name in
340 -- this unlink, because the working directory may have changed since
341 -- we did the open, and we want to unlink the right file!
343 if unlink
(Filename
'Address) = -1 then
344 raise Use_Error
with Errno_Message
;
353 function End_Of_File
(File
: AFCB_Ptr
) return Boolean is
355 Check_File_Open
(File
);
357 if feof
(File
.Stream
) /= 0 then
361 Check_Read_Status
(File
);
363 if ungetc
(fgetc
(File
.Stream
), File
.Stream
) = EOF
then
364 clearerr
(File
.Stream
);
376 function Errno_Message
(Errno
: Integer := OS_Lib
.Errno
) return String is
377 Message
: constant chars_ptr
:= CRTL
.Runtime
.strerror
(Errno
);
380 if Message
= Null_Ptr
then
381 return "errno =" & Errno
'Img;
383 return Value
(Message
);
387 function Errno_Message
389 Errno
: Integer := OS_Lib
.Errno
) return String
392 return Name
& ": " & String'(Errno_Message (Errno));
399 -- Note: we do not need to worry about locking against multiple task access
400 -- in this routine, since it is called only from the environment task just
401 -- before terminating execution.
403 procedure Finalize (V : in out File_IO_Clean_Up_Type) is
404 pragma Warnings (Off, V);
406 Fptr1 : aliased AFCB_Ptr;
410 pragma Unreferenced (Discard);
413 -- Take a lock to protect global Open_Files data structure
417 -- First close all open files (the slightly complex form of this loop is
418 -- required because Close as a side effect nulls out its argument).
421 while Fptr1 /= null loop
423 Close (Fptr1'Access);
427 -- Now unlink all temporary files. We do not bother to free the blocks
428 -- because we are just about to terminate the program. We also ignore
429 -- any errors while attempting these unlink operations.
431 while Temp_Files /= null loop
432 Discard := unlink (Temp_Files.Name'Address);
433 Temp_Files := Temp_Files.Next;
448 procedure Flush (File : AFCB_Ptr) is
450 Check_Write_Status (File);
452 if fflush (File.Stream) /= 0 then
453 Raise_Device_Error (File);
461 -- The fopen mode to be used is shown by the following table:
464 -- Append_File "r+" "w+"
466 -- Out_File (Direct_IO) "r+" "w"
467 -- Out_File (all others) "w" "w"
468 -- Inout_File "r+" "w+"
470 -- Note: we do not use "a" or "a+" for Append_File, since this would not
471 -- work in the case of stream files, where even if in append file mode,
472 -- you can reset to earlier points in the file. The caller must use the
473 -- Append_Set routine to deal with the necessary positioning.
475 -- Note: in several cases, the fopen mode used allows reading and writing,
476 -- but the setting of the Ada mode is more restrictive. For instance,
477 -- Create in In_File mode uses "w+" which allows writing, but the Ada mode
478 -- In_File will cause any write operations to be rejected with Mode_Error
481 -- Note: for the Out_File/Open cases for other than the Direct_IO case, an
482 -- initial call will be made by the caller to first open the file in "r"
483 -- mode to be sure that it exists. The real open, in "w" mode, will then
484 -- destroy this file. This is peculiar, but that's what Ada semantics
485 -- require and the ACATS tests insist on!
487 -- If text file translation is required, then either "b" or "t" is appended
488 -- to the mode, depending on the setting of Text.
495 Fopstr : out Fopen_String)
512 if Amethod = 'D
' and then not Creat then
521 when Inout_File | Append_File =>
522 Fopstr (1) := (if Creat then 'w
' else 'r
');
528 -- If text_translation_required is true then we need to append either a
529 -- "t" or "b" to the string to get the right mode.
531 if text_translation_required then
532 Fopstr (Fptr) := (if Text then 't
' else 'b
');
536 Fopstr (Fptr) := ASCII.NUL;
543 function Form (File : AFCB_Ptr) return String is
546 raise Status_Error with "Form: file not open";
548 return File.Form.all (1 .. File.Form'Length - 1);
556 function Form_Boolean
559 Default : Boolean) return Boolean
562 pragma Unreferenced (V2);
565 Form_Parameter (Form, Keyword, V1, V2);
570 elsif Form (V1) = 'y
' then
573 elsif Form (V1) = 'n
' then
577 raise Use_Error with "invalid Form";
585 function Form_Integer
588 Default : Integer) return Integer
594 Form_Parameter (Form, Keyword, V1, V2);
602 for J in V1 .. V2 loop
603 if Form (J) not in '0' .. '9' then
604 raise Use_Error with "invalid Form";
606 V := V * 10 + Character'Pos (Form (J)) - Character'Pos ('0');
610 raise Use_Error with "invalid Form";
622 procedure Form_Parameter
628 Klen : constant Integer := Keyword'Length;
631 for J in Form'First + Klen .. Form'Last - 1 loop
633 and then Form (J - Klen .. J - 1) = Keyword
638 while Form (Stop + 1) /= ASCII.NUL
639 and then Form (Stop + 1) /= ','
656 function Is_Open (File : AFCB_Ptr) return Boolean is
658 -- We return True if the file is open, and the underlying file stream is
659 -- usable. In particular on Windows an application linked with -mwindows
660 -- option set does not have a console attached. In this case standard
661 -- files (Current_Output, Current_Error, Current_Input) are not created.
662 -- We want Is_Open (Current_Output) to return False in this case.
664 return File /= null and then fileno (File.Stream) /= -1;
671 procedure Make_Buffered
673 Buf_Siz : Interfaces.C_Streams.size_t)
676 pragma Unreferenced (status);
679 status := setvbuf (File.Stream, Null_Address, IOFBF, Buf_Siz);
682 ------------------------
683 -- Make_Line_Buffered --
684 ------------------------
686 procedure Make_Line_Buffered
688 Line_Siz : Interfaces.C_Streams.size_t)
691 pragma Unreferenced (status);
694 status := setvbuf (File.Stream, Null_Address, IOLBF, Line_Siz);
695 -- No error checking???
696 end Make_Line_Buffered;
698 ---------------------
699 -- Make_Unbuffered --
700 ---------------------
702 procedure Make_Unbuffered (File : AFCB_Ptr) is
704 pragma Unreferenced (status);
707 status := setvbuf (File.Stream, Null_Address, IONBF, 0);
708 -- No error checking???
715 function Mode (File : AFCB_Ptr) return File_Mode is
718 raise Status_Error with "Mode: file not open";
728 function Name (File : AFCB_Ptr) return String is
731 raise Status_Error with "Name: file not open";
733 return File.Name.all (1 .. File.Name'Length - 1);
742 (File_Ptr : in out AFCB_Ptr;
743 Dummy_FCB : AFCB'Class;
750 C_Stream : FILEs := NULL_Stream)
752 pragma Warnings (Off, Dummy_FCB);
753 -- Yes we know this is never assigned a value. That's intended, since
754 -- all we ever use of this value is the tag for dispatching purposes.
756 procedure Tmp_Name (Buffer : Address);
757 pragma Import (C, Tmp_Name, "__gnat_tmp_name");
758 -- Set buffer (a String address) with a temporary filename
760 Stream : FILEs := C_Stream;
761 -- Stream which we open in response to this request
763 Shared : Shared_Status_Type;
764 -- Setting of Shared_Status field for file
766 Fopstr : aliased Fopen_String;
767 -- Mode string used in fopen call
769 Formstr : aliased String (1 .. Form'Length + 1);
770 -- Form string with ASCII.NUL appended, folded to lower case
772 Is_Text_File : Boolean;
774 Tempfile : constant Boolean := (Name'Length = 0);
775 -- Indicates temporary file case
777 Namelen : constant Integer := max_path_len;
778 -- Length required for file name, not including final ASCII.NUL.
779 -- Note that we used to reference L_tmpnam here, which is not reliable
780 -- since __gnat_tmp_name does not always use tmpnam.
782 Namestr : aliased String (1 .. Namelen + 1);
783 -- Name as given or temporary file name with ASCII.NUL appended
785 Fullname : aliased String (1 .. max_path_len + 1);
786 -- Full name (as required for Name function, and as stored in the
787 -- control block in the Name field) with ASCII.NUL appended.
789 Full_Name_Len : Integer;
790 -- Length of name actually stored in Fullname
792 Encoding : CRTL.Filename_Encoding;
793 -- Filename encoding specified into the form parameter
796 if File_Ptr /= null then
797 raise Status_Error with "file already open";
800 -- Acquire form string, setting required NUL terminator
802 Formstr (1 .. Form'Length) := Form;
803 Formstr (Formstr'Last) := ASCII.NUL;
805 -- Convert form string to lower case
807 for J in Formstr'Range loop
808 if Formstr (J) in 'A
' .. 'Z
' then
809 Formstr (J) := Character'Val (Character'Pos (Formstr (J)) + 32);
813 -- Acquire setting of shared parameter
819 Form_Parameter (Formstr, "shared", V1, V2);
824 elsif Formstr (V1 .. V2) = "yes" then
827 elsif Formstr (V1 .. V2) = "no" then
831 raise Use_Error with "invalid Form";
835 -- Acquire setting of encoding parameter
841 Form_Parameter (Formstr, "encoding", V1, V2);
844 Encoding := CRTL.Unspecified;
846 elsif Formstr (V1 .. V2) = "utf8" then
847 Encoding := CRTL.UTF8;
849 elsif Formstr (V1 .. V2) = "8bits" then
850 Encoding := CRTL.ASCII_8bits;
853 raise Use_Error with "invalid Form";
857 -- Acquire setting of text_translation parameter. Only needed if this is
858 -- a [Wide_[Wide_]]Text_IO file, in which case we default to True, but
859 -- if the Form says Text_Translation=No, we use binary mode, so new-line
860 -- will be just LF, even on Windows.
862 Is_Text_File := Text;
866 Form_Boolean (Formstr, "text_translation", Default => True);
869 -- If we were given a stream (call from xxx.C_Streams.Open), then set
870 -- the full name to the given one, and skip to end of processing.
872 if Stream /= NULL_Stream then
873 Full_Name_Len := Name'Length + 1;
874 Fullname (1 .. Full_Name_Len - 1) := Name;
875 Fullname (Full_Name_Len) := ASCII.NUL;
877 -- Normal case of Open or Create
880 -- If temporary file case, get temporary file name and add to the
881 -- list of temporary files to be deleted on exit.
885 raise Name_Error with "opening temp file without creating it";
888 Tmp_Name (Namestr'Address);
890 if Namestr (1) = ASCII.NUL then
891 raise Use_Error with "invalid temp file name";
894 -- Chain to temp file list, ensuring thread safety with a lock
899 new Temp_File_Record'(Name
=> Namestr
, Next
=> Temp_Files
);
908 -- Normal case of non-null name given
911 if Name
'Length > Namelen
then
912 raise Name_Error
with "file name too long";
915 Namestr
(1 .. Name
'Length) := Name
;
916 Namestr
(Name
'Length + 1) := ASCII
.NUL
;
919 -- Get full name in accordance with the advice of RM A.8.2(22)
921 full_name
(Namestr
'Address, Fullname
'Address);
923 if Fullname
(1) = ASCII
.NUL
then
924 raise Use_Error
with Errno_Message
(Name
);
928 while Full_Name_Len
< Fullname
'Last
929 and then Fullname
(Full_Name_Len
) /= ASCII
.NUL
931 Full_Name_Len
:= Full_Name_Len
+ 1;
934 -- Fullname is generated by calling system's full_name. The problem
935 -- is, full_name does nothing about the casing, so a file name
936 -- comparison may generally speaking not be valid on non-case-
937 -- sensitive systems, and in particular we get unexpected failures
938 -- on Windows/Vista because of this. So we use s-casuti to force
939 -- the name to lower case.
941 if not File_Names_Case_Sensitive
then
942 To_Lower
(Fullname
(1 .. Full_Name_Len
));
945 -- If Shared=None or Shared=Yes, then check for the existence of
946 -- another file with exactly the same full name.
953 -- Take a task lock to protect Open_Files
957 -- Search list of open files
961 if Fullname
(1 .. Full_Name_Len
) = P
.Name
.all then
963 -- If we get a match, and either file has Shared=None,
964 -- then raise Use_Error, since we don't allow two files
965 -- of the same name to be opened unless they specify the
966 -- required sharing mode.
969 or else P
.Shared_Status
= None
971 raise Use_Error
with "reopening shared file";
973 -- If both files have Shared=Yes, then we acquire the
974 -- stream from the located file to use as our stream.
977 and then P
.Shared_Status
= Yes
982 -- Otherwise one of the files has Shared=Yes and one has
983 -- Shared=No. If the current file has Shared=No then all
984 -- is well but we don't want to share any other file's
985 -- stream. If the current file has Shared=Yes, we would
986 -- like to share a stream, but not from a file that has
987 -- Shared=No, so either way, we just continue the search.
1001 SSL
.Unlock_Task
.all;
1006 -- Open specified file if we did not find an existing stream
1008 if Stream
= NULL_Stream
then
1009 Fopen_Mode
(Mode
, Is_Text_File
, Creat
, Amethod
, Fopstr
);
1011 -- A special case, if we are opening (OPEN case) a file and the
1012 -- mode returned by Fopen_Mode is not "r" or "r+", then we first
1013 -- make sure that the file exists as required by Ada semantics.
1015 if not Creat
and then Fopstr
(1) /= 'r' then
1016 if file_exists
(Namestr
'Address) = 0 then
1017 raise Name_Error
with Errno_Message
(Name
);
1021 -- Now open the file. Note that we use the name as given in the
1022 -- original Open call for this purpose, since that seems the
1023 -- clearest implementation of the intent. It would presumably
1024 -- work to use the full name here, but if there is any difference,
1025 -- then we should use the name used in the call.
1027 -- Note: for a corresponding delete, we will use the full name,
1028 -- since by the time of the delete, the current working directory
1029 -- may have changed and we do not want to delete a different file!
1031 Stream
:= fopen
(Namestr
'Address, Fopstr
'Address, Encoding
);
1033 if Stream
= NULL_Stream
then
1035 -- Raise Name_Error if trying to open a non-existent file.
1036 -- Otherwise raise Use_Error.
1038 -- Should we raise Device_Error for ENOSPC???
1041 function Is_File_Not_Found_Error
1042 (Errno_Value
: Integer) return Integer;
1043 -- Non-zero when the given errno value indicates a non-
1047 (C
, Is_File_Not_Found_Error
,
1048 "__gnat_is_file_not_found_error");
1050 Errno
: constant Integer := OS_Lib
.Errno
;
1051 Message
: constant String := Errno_Message
(Name
, Errno
);
1053 if Is_File_Not_Found_Error
(Errno
) /= 0 then
1054 raise Name_Error
with Message
;
1056 raise Use_Error
with Message
;
1063 -- Stream has been successfully located or opened, so now we are
1064 -- committed to completing the opening of the file. Allocate block on
1065 -- heap and fill in its fields.
1067 File_Ptr
:= AFCB_Allocate
(Dummy_FCB
);
1069 File_Ptr
.Is_Regular_File
:= (is_regular_file
(fileno
(Stream
)) /= 0);
1070 File_Ptr
.Is_System_File
:= False;
1071 File_Ptr
.Is_Text_File
:= Is_Text_File
;
1072 File_Ptr
.Shared_Status
:= Shared
;
1073 File_Ptr
.Access_Method
:= Amethod
;
1074 File_Ptr
.Stream
:= Stream
;
1075 File_Ptr
.Form
:= new String'(Formstr);
1076 File_Ptr.Name := new String'(Fullname
(1 .. Full_Name_Len
));
1077 File_Ptr
.Mode
:= Mode
;
1078 File_Ptr
.Is_Temporary_File
:= Tempfile
;
1079 File_Ptr
.Encoding
:= Encoding
;
1081 Chain_File
(File_Ptr
);
1082 Append_Set
(File_Ptr
);
1085 ------------------------
1086 -- Raise_Device_Error --
1087 ------------------------
1089 procedure Raise_Device_Error
1090 (File
: AFCB_Ptr
; Errno
: Integer := OS_Lib
.Errno
)
1093 -- Clear error status so that the same error is not reported twice
1095 if File
/= null then
1096 clearerr
(File
.Stream
);
1099 raise Device_Error
with Errno_Message
(Errno
);
1100 end Raise_Device_Error
;
1106 procedure Read_Buf
(File
: AFCB_Ptr
; Buf
: Address
; Siz
: size_t
) is
1110 Nread
:= fread
(Buf
, 1, Siz
, File
.Stream
);
1115 elsif ferror
(File
.Stream
) /= 0 then
1116 Raise_Device_Error
(File
);
1118 elsif Nread
= 0 then
1121 else -- 0 < Nread < Siz
1122 raise Data_Error
with "not enough data read";
1130 Siz
: Interfaces
.C_Streams
.size_t
;
1131 Count
: out Interfaces
.C_Streams
.size_t
)
1134 Count
:= fread
(Buf
, 1, Siz
, File
.Stream
);
1136 if Count
= 0 and then ferror
(File
.Stream
) /= 0 then
1137 Raise_Device_Error
(File
);
1145 -- The reset which does not change the mode simply does a rewind
1147 procedure Reset
(File_Ptr
: access AFCB_Ptr
) is
1148 File
: AFCB_Ptr
renames File_Ptr
.all;
1150 Check_File_Open
(File
);
1151 Reset
(File_Ptr
, File
.Mode
);
1154 -- The reset with a change in mode is done using freopen, and is not
1155 -- permitted except for regular files (since otherwise there is no name for
1156 -- the freopen, and in any case it seems meaningless).
1158 procedure Reset
(File_Ptr
: access AFCB_Ptr
; Mode
: File_Mode
) is
1159 File
: AFCB_Ptr
renames File_Ptr
.all;
1160 Fopstr
: aliased Fopen_String
;
1163 Check_File_Open
(File
);
1165 -- Change of mode not allowed for shared file or file with no name or
1166 -- file that is not a regular file, or for a system file. Note that we
1167 -- allow the "change" of mode if it is not in fact doing a change.
1169 if Mode
/= File
.Mode
then
1170 if File
.Shared_Status
= Yes
then
1171 raise Use_Error
with "cannot change mode of shared file";
1172 elsif File
.Name
'Length <= 1 then
1173 raise Use_Error
with "cannot change mode of temp file";
1174 elsif File
.Is_System_File
then
1175 raise Use_Error
with "cannot change mode of system file";
1176 elsif not File
.Is_Regular_File
then
1177 raise Use_Error
with "cannot change mode of non-regular file";
1181 -- For In_File or Inout_File for a regular file, we can just do a rewind
1182 -- if the mode is unchanged, which is more efficient than doing a full
1186 and then Mode
<= Inout_File
1188 rewind
(File
.Stream
);
1190 -- Here the change of mode is permitted, we do it by reopening the file
1191 -- in the new mode and replacing the stream with a new stream.
1195 (Mode
, File
.Is_Text_File
, False, File
.Access_Method
, Fopstr
);
1197 File
.Stream
:= freopen
1198 (File
.Name
.all'Address, Fopstr
'Address, File
.Stream
, File
.Encoding
);
1200 if File
.Stream
= NULL_Stream
then
1215 procedure Write_Buf
(File
: AFCB_Ptr
; Buf
: Address
; Siz
: size_t
) is
1217 -- Note: for most purposes, the Siz and 1 parameters in the fwrite call
1218 -- could be reversed, but on VMS, this is a better choice, since for
1219 -- some file formats, reversing the parameters results in records of one
1222 SSL
.Abort_Defer
.all;
1224 if fwrite
(Buf
, Siz
, 1, File
.Stream
) /= 1 then
1226 SSL
.Abort_Undefer
.all;
1227 Raise_Device_Error
(File
);
1231 SSL
.Abort_Undefer
.all;