final.c: Use rtx_sequence
[official-gcc.git] / gcc / ada / s-fileio.adb
blob73838bf8e54b94113f911ffd05a6d61a2d0ae0af
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- S Y S T E M . F I L E _ I O --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
10 -- --
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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 with Ada.Finalization; use Ada.Finalization;
33 with Ada.IO_Exceptions; use Ada.IO_Exceptions;
34 with Ada.Unchecked_Deallocation;
36 with Interfaces.C;
37 with Interfaces.C_Streams; use Interfaces.C_Streams;
39 with System.Case_Util; use System.Case_Util;
40 with System.CRTL;
41 with System.OS_Lib;
42 with System.Soft_Links;
44 package body System.File_IO is
46 use System.File_Control_Block;
48 package SSL renames System.Soft_Links;
50 use type CRTL.size_t;
51 use type Interfaces.C.int;
53 ----------------------
54 -- Global Variables --
55 ----------------------
57 Open_Files : AFCB_Ptr;
58 -- This points to a list of AFCB's for all open files. This is a doubly
59 -- linked list, with the Prev pointer of the first entry, and the Next
60 -- pointer of the last entry containing null. Note that this global
61 -- variable must be properly protected to provide thread safety.
63 type Temp_File_Record;
64 type Temp_File_Record_Ptr is access all Temp_File_Record;
66 type Temp_File_Record is record
67 Name : String (1 .. max_path_len + 1);
68 Next : Temp_File_Record_Ptr;
69 end record;
70 -- One of these is allocated for each temporary file created
72 Temp_Files : Temp_File_Record_Ptr;
73 -- Points to list of names of temporary files. Note that this global
74 -- variable must be properly protected to provide thread safety.
76 type File_IO_Clean_Up_Type is new Limited_Controlled with null record;
77 -- The closing of all open files and deletion of temporary files is an
78 -- action that takes place at the end of execution of the main program.
79 -- This action is implemented using a library level object which gets
80 -- finalized at the end of program execution. Note that the type is
81 -- limited, in order to stop the compiler optimizing away the declaration
82 -- which would be allowed in the non-limited case.
84 procedure Finalize (V : in out File_IO_Clean_Up_Type);
85 -- This is the finalize operation that is used to do the cleanup
87 File_IO_Clean_Up_Object : File_IO_Clean_Up_Type;
88 pragma Warnings (Off, File_IO_Clean_Up_Object);
89 -- This is the single object of the type that triggers the finalization
90 -- call. Since it is at the library level, this happens just before the
91 -- environment task is finalized.
93 text_translation_required : Boolean;
94 for text_translation_required'Size use Character'Size;
95 pragma Import
96 (C, text_translation_required, "__gnat_text_translation_required");
97 -- If true, add appropriate suffix to control string for Open
99 -----------------------
100 -- Local Subprograms --
101 -----------------------
103 procedure Free_String is new Ada.Unchecked_Deallocation (String, Pstring);
105 subtype Fopen_String is String (1 .. 4);
106 -- Holds open string (longest is "w+b" & nul)
108 procedure Fopen_Mode
109 (Mode : File_Mode;
110 Text : Boolean;
111 Creat : Boolean;
112 Amethod : Character;
113 Fopstr : out Fopen_String);
114 -- Determines proper open mode for a file to be opened in the given Ada
115 -- mode. Text is true for a text file and false otherwise, and Creat is
116 -- true for a create call, and False for an open call. The value stored
117 -- in Fopstr is a nul-terminated string suitable for a call to fopen or
118 -- freopen. Amethod is the character designating the access method from
119 -- the Access_Method field of the FCB.
121 function Errno_Message
122 (Name : String;
123 Errno : Integer := OS_Lib.Errno) return String;
124 -- Return Errno_Message for Errno, with file name prepended
126 procedure Raise_Device_Error
127 (File : AFCB_Ptr;
128 Errno : Integer := OS_Lib.Errno);
129 pragma No_Return (Raise_Device_Error);
130 -- Clear error indication on File and raise Device_Error with an exception
131 -- message providing errno information.
133 ----------------
134 -- Append_Set --
135 ----------------
137 procedure Append_Set (File : AFCB_Ptr) is
138 begin
139 if File.Mode = Append_File then
140 if fseek (File.Stream, 0, SEEK_END) /= 0 then
141 Raise_Device_Error (File);
142 end if;
143 end if;
144 end Append_Set;
146 ----------------
147 -- Chain_File --
148 ----------------
150 procedure Chain_File (File : AFCB_Ptr) is
151 begin
152 -- Take a task lock, to protect the global data value Open_Files
154 SSL.Lock_Task.all;
156 -- Do the chaining operation locked
158 File.Next := Open_Files;
159 File.Prev := null;
160 Open_Files := File;
162 if File.Next /= null then
163 File.Next.Prev := File;
164 end if;
166 SSL.Unlock_Task.all;
168 exception
169 when others =>
170 SSL.Unlock_Task.all;
171 raise;
172 end Chain_File;
174 ---------------------
175 -- Check_File_Open --
176 ---------------------
178 procedure Check_File_Open (File : AFCB_Ptr) is
179 begin
180 if File = null then
181 raise Status_Error with "file not open";
182 end if;
183 end Check_File_Open;
185 -----------------------
186 -- Check_Read_Status --
187 -----------------------
189 procedure Check_Read_Status (File : AFCB_Ptr) is
190 begin
191 if File = null then
192 raise Status_Error with "file not open";
193 elsif File.Mode not in Read_File_Mode then
194 raise Mode_Error with "file not readable";
195 end if;
196 end Check_Read_Status;
198 ------------------------
199 -- Check_Write_Status --
200 ------------------------
202 procedure Check_Write_Status (File : AFCB_Ptr) is
203 begin
204 if File = null then
205 raise Status_Error with "file not open";
206 elsif File.Mode = In_File then
207 raise Mode_Error with "file not writable";
208 end if;
209 end Check_Write_Status;
211 -----------
212 -- Close --
213 -----------
215 procedure Close (File_Ptr : access AFCB_Ptr) is
216 Close_Status : int := 0;
217 Dup_Strm : Boolean := False;
218 File : AFCB_Ptr renames File_Ptr.all;
219 Errno : Integer := 0;
221 begin
222 -- Take a task lock, to protect the global data value Open_Files
224 SSL.Lock_Task.all;
226 Check_File_Open (File);
227 AFCB_Close (File);
229 -- Sever the association between the given file and its associated
230 -- external file. The given file is left closed. Do not perform system
231 -- closes on the standard input, output and error files and also do not
232 -- attempt to close a stream that does not exist (signalled by a null
233 -- stream value -- happens in some error situations).
235 if not File.Is_System_File and then File.Stream /= NULL_Stream then
237 -- Do not do an fclose if this is a shared file and there is at least
238 -- one other instance of the stream that is open.
240 if File.Shared_Status = Yes then
241 declare
242 P : AFCB_Ptr;
244 begin
245 P := Open_Files;
246 while P /= null loop
247 if P /= File and then File.Stream = P.Stream then
248 Dup_Strm := True;
249 exit;
250 end if;
252 P := P.Next;
253 end loop;
254 end;
255 end if;
257 -- Do the fclose unless this was a duplicate in the shared case
259 if not Dup_Strm then
260 Close_Status := fclose (File.Stream);
262 if Close_Status /= 0 then
263 Errno := OS_Lib.Errno;
264 end if;
265 end if;
266 end if;
268 -- Dechain file from list of open files and then free the storage
270 if File.Prev = null then
271 Open_Files := File.Next;
272 else
273 File.Prev.Next := File.Next;
274 end if;
276 if File.Next /= null then
277 File.Next.Prev := File.Prev;
278 end if;
280 -- Deallocate some parts of the file structure that were kept in heap
281 -- storage with the exception of system files (standard input, output
282 -- and error) since they had some information allocated in the stack.
284 if not File.Is_System_File then
285 Free_String (File.Name);
286 Free_String (File.Form);
287 AFCB_Free (File);
288 end if;
290 File := null;
292 if Close_Status /= 0 then
293 Raise_Device_Error (null, Errno);
294 end if;
296 SSL.Unlock_Task.all;
298 exception
299 when others =>
300 SSL.Unlock_Task.all;
301 raise;
302 end Close;
304 ------------
305 -- Delete --
306 ------------
308 procedure Delete (File_Ptr : access AFCB_Ptr) is
309 File : AFCB_Ptr renames File_Ptr.all;
311 begin
312 Check_File_Open (File);
314 if not File.Is_Regular_File then
315 raise Use_Error with "cannot delete non-regular file";
316 end if;
318 declare
319 Filename : aliased constant String := File.Name.all;
321 begin
322 Close (File_Ptr);
324 -- Now unlink the external file. Note that we use the full name in
325 -- this unlink, because the working directory may have changed since
326 -- we did the open, and we want to unlink the right file.
328 if unlink (Filename'Address) = -1 then
329 raise Use_Error with OS_Lib.Errno_Message;
330 end if;
331 end;
332 end Delete;
334 -----------------
335 -- End_Of_File --
336 -----------------
338 function End_Of_File (File : AFCB_Ptr) return Boolean is
339 begin
340 Check_File_Open (File);
342 if feof (File.Stream) /= 0 then
343 return True;
345 else
346 Check_Read_Status (File);
348 if ungetc (fgetc (File.Stream), File.Stream) = EOF then
349 clearerr (File.Stream);
350 return True;
351 else
352 return False;
353 end if;
354 end if;
355 end End_Of_File;
357 -------------------
358 -- Errno_Message --
359 -------------------
361 function Errno_Message
362 (Name : String;
363 Errno : Integer := OS_Lib.Errno) return String
365 begin
366 return Name & ": " & OS_Lib.Errno_Message (Err => Errno);
367 end Errno_Message;
369 --------------
370 -- Finalize --
371 --------------
373 procedure Finalize (V : in out File_IO_Clean_Up_Type) is
374 pragma Warnings (Off, V);
376 Fptr1 : aliased AFCB_Ptr;
377 Fptr2 : AFCB_Ptr;
379 Discard : int;
381 begin
382 -- Take a lock to protect global Open_Files data structure
384 SSL.Lock_Task.all;
386 -- First close all open files (the slightly complex form of this loop is
387 -- required because Close as a side effect nulls out its argument).
389 Fptr1 := Open_Files;
390 while Fptr1 /= null loop
391 Fptr2 := Fptr1.Next;
392 Close (Fptr1'Access);
393 Fptr1 := Fptr2;
394 end loop;
396 -- Now unlink all temporary files. We do not bother to free the blocks
397 -- because we are just about to terminate the program. We also ignore
398 -- any errors while attempting these unlink operations.
400 while Temp_Files /= null loop
401 Discard := unlink (Temp_Files.Name'Address);
402 Temp_Files := Temp_Files.Next;
403 end loop;
405 SSL.Unlock_Task.all;
407 exception
408 when others =>
409 SSL.Unlock_Task.all;
410 raise;
411 end Finalize;
413 -----------
414 -- Flush --
415 -----------
417 procedure Flush (File : AFCB_Ptr) is
418 begin
419 Check_Write_Status (File);
421 if fflush (File.Stream) /= 0 then
422 Raise_Device_Error (File);
423 end if;
424 end Flush;
426 ----------------
427 -- Fopen_Mode --
428 ----------------
430 -- The fopen mode to be used is shown by the following table:
432 -- OPEN CREATE
433 -- Append_File "r+" "w+"
434 -- In_File "r" "w+"
435 -- Out_File (Direct_IO) "r+" "w"
436 -- Out_File (all others) "w" "w"
437 -- Inout_File "r+" "w+"
439 -- Note: we do not use "a" or "a+" for Append_File, since this would not
440 -- work in the case of stream files, where even if in append file mode,
441 -- you can reset to earlier points in the file. The caller must use the
442 -- Append_Set routine to deal with the necessary positioning.
444 -- Note: in several cases, the fopen mode used allows reading and writing,
445 -- but the setting of the Ada mode is more restrictive. For instance,
446 -- Create in In_File mode uses "w+" which allows writing, but the Ada mode
447 -- In_File will cause any write operations to be rejected with Mode_Error
448 -- in any case.
450 -- Note: for the Out_File/Open cases for other than the Direct_IO case, an
451 -- initial call will be made by the caller to first open the file in "r"
452 -- mode to be sure that it exists. The real open, in "w" mode, will then
453 -- destroy this file. This is peculiar, but that's what Ada semantics
454 -- require and the ACATS tests insist on.
456 -- If text file translation is required, then either "b" or "t" is appended
457 -- to the mode, depending on the setting of Text.
459 procedure Fopen_Mode
460 (Mode : File_Mode;
461 Text : Boolean;
462 Creat : Boolean;
463 Amethod : Character;
464 Fopstr : out Fopen_String)
466 Fptr : Positive;
468 begin
469 case Mode is
470 when In_File =>
471 if Creat then
472 Fopstr (1) := 'w';
473 Fopstr (2) := '+';
474 Fptr := 3;
475 else
476 Fopstr (1) := 'r';
477 Fptr := 2;
478 end if;
480 when Out_File =>
481 if Amethod = 'D' and then not Creat then
482 Fopstr (1) := 'r';
483 Fopstr (2) := '+';
484 Fptr := 3;
485 else
486 Fopstr (1) := 'w';
487 Fptr := 2;
488 end if;
490 when Inout_File | Append_File =>
491 Fopstr (1) := (if Creat then 'w' else 'r');
492 Fopstr (2) := '+';
493 Fptr := 3;
494 end case;
496 -- If text_translation_required is true then we need to append either a
497 -- "t" or "b" to the string to get the right mode.
499 if text_translation_required then
500 Fopstr (Fptr) := (if Text then 't' else 'b');
501 Fptr := Fptr + 1;
502 end if;
504 Fopstr (Fptr) := ASCII.NUL;
505 end Fopen_Mode;
507 ----------
508 -- Form --
509 ----------
511 function Form (File : AFCB_Ptr) return String is
512 begin
513 if File = null then
514 raise Status_Error with "Form: file not open";
515 else
516 return File.Form.all (1 .. File.Form'Length - 1);
517 end if;
518 end Form;
520 ------------------
521 -- Form_Boolean --
522 ------------------
524 function Form_Boolean
525 (Form : String;
526 Keyword : String;
527 Default : Boolean) return Boolean
529 V1, V2 : Natural;
530 pragma Unreferenced (V2);
532 begin
533 Form_Parameter (Form, Keyword, V1, V2);
535 if V1 = 0 then
536 return Default;
537 elsif Form (V1) = 'y' then
538 return True;
539 elsif Form (V1) = 'n' then
540 return False;
541 else
542 raise Use_Error with "invalid Form";
543 end if;
544 end Form_Boolean;
546 ------------------
547 -- Form_Integer --
548 ------------------
550 function Form_Integer
551 (Form : String;
552 Keyword : String;
553 Default : Integer) return Integer
555 V1, V2 : Natural;
556 V : Integer;
558 begin
559 Form_Parameter (Form, Keyword, V1, V2);
561 if V1 = 0 then
562 return Default;
564 else
565 V := 0;
567 for J in V1 .. V2 loop
568 if Form (J) not in '0' .. '9' then
569 raise Use_Error with "invalid Form";
570 else
571 V := V * 10 + Character'Pos (Form (J)) - Character'Pos ('0');
572 end if;
574 if V > 999_999 then
575 raise Use_Error with "invalid Form";
576 end if;
577 end loop;
579 return V;
580 end if;
581 end Form_Integer;
583 --------------------
584 -- Form_Parameter --
585 --------------------
587 procedure Form_Parameter
588 (Form : String;
589 Keyword : String;
590 Start : out Natural;
591 Stop : out Natural)
593 Klen : constant Integer := Keyword'Length;
595 begin
596 for J in Form'First + Klen .. Form'Last - 1 loop
597 if Form (J) = '='
598 and then Form (J - Klen .. J - 1) = Keyword
599 then
600 Start := J + 1;
601 Stop := Start - 1;
602 while Form (Stop + 1) /= ASCII.NUL
603 and then Form (Stop + 1) /= ','
604 loop
605 Stop := Stop + 1;
606 end loop;
608 return;
609 end if;
610 end loop;
612 Start := 0;
613 Stop := 0;
614 end Form_Parameter;
616 -------------
617 -- Is_Open --
618 -------------
620 function Is_Open (File : AFCB_Ptr) return Boolean is
621 begin
622 -- We return True if the file is open, and the underlying file stream is
623 -- usable. In particular on Windows an application linked with -mwindows
624 -- option set does not have a console attached. In this case standard
625 -- files (Current_Output, Current_Error, Current_Input) are not created.
626 -- We want Is_Open (Current_Output) to return False in this case.
628 return File /= null and then fileno (File.Stream) /= -1;
629 end Is_Open;
631 -------------------
632 -- Make_Buffered --
633 -------------------
635 procedure Make_Buffered
636 (File : AFCB_Ptr;
637 Buf_Siz : Interfaces.C_Streams.size_t)
639 status : Integer;
640 pragma Unreferenced (status);
642 begin
643 status := setvbuf (File.Stream, Null_Address, IOFBF, Buf_Siz);
644 end Make_Buffered;
646 ------------------------
647 -- Make_Line_Buffered --
648 ------------------------
650 procedure Make_Line_Buffered
651 (File : AFCB_Ptr;
652 Line_Siz : Interfaces.C_Streams.size_t)
654 status : Integer;
655 pragma Unreferenced (status);
657 begin
658 status := setvbuf (File.Stream, Null_Address, IOLBF, Line_Siz);
659 -- No error checking???
660 end Make_Line_Buffered;
662 ---------------------
663 -- Make_Unbuffered --
664 ---------------------
666 procedure Make_Unbuffered (File : AFCB_Ptr) is
667 status : Integer;
668 pragma Unreferenced (status);
670 begin
671 status := setvbuf (File.Stream, Null_Address, IONBF, 0);
672 -- No error checking???
673 end Make_Unbuffered;
675 ----------
676 -- Mode --
677 ----------
679 function Mode (File : AFCB_Ptr) return File_Mode is
680 begin
681 if File = null then
682 raise Status_Error with "Mode: file not open";
683 else
684 return File.Mode;
685 end if;
686 end Mode;
688 ----------
689 -- Name --
690 ----------
692 function Name (File : AFCB_Ptr) return String is
693 begin
694 if File = null then
695 raise Status_Error with "Name: file not open";
696 else
697 return File.Name.all (1 .. File.Name'Length - 1);
698 end if;
699 end Name;
701 ----------
702 -- Open --
703 ----------
705 procedure Open
706 (File_Ptr : in out AFCB_Ptr;
707 Dummy_FCB : AFCB'Class;
708 Mode : File_Mode;
709 Name : String;
710 Form : String;
711 Amethod : Character;
712 Creat : Boolean;
713 Text : Boolean;
714 C_Stream : FILEs := NULL_Stream)
716 pragma Warnings (Off, Dummy_FCB);
717 -- Yes we know this is never assigned a value. That's intended, since
718 -- all we ever use of this value is the tag for dispatching purposes.
720 procedure Tmp_Name (Buffer : Address);
721 pragma Import (C, Tmp_Name, "__gnat_tmp_name");
722 -- Set buffer (a String address) with a temporary filename
724 function Get_Case_Sensitive return Integer;
725 pragma Import (C, Get_Case_Sensitive,
726 "__gnat_get_file_names_case_sensitive");
728 procedure Record_AFCB;
729 -- Create and record new AFCB into the runtime, note that the
730 -- implementation uses the variables below which corresponds to the
731 -- status of the opened file.
733 File_Names_Case_Sensitive : constant Boolean := Get_Case_Sensitive /= 0;
734 -- Set to indicate whether the operating system convention is for file
735 -- names to be case sensitive (e.g., in Unix, set True), or not case
736 -- sensitive (e.g., in Windows, set False). Declared locally to avoid
737 -- breaking the Preelaborate rule that disallows function calls at the
738 -- library level.
740 Stream : FILEs := C_Stream;
741 -- Stream which we open in response to this request
743 Shared : Shared_Status_Type;
744 -- Setting of Shared_Status field for file
746 Fopstr : aliased Fopen_String;
747 -- Mode string used in fopen call
749 Formstr : aliased String (1 .. Form'Length + 1);
750 -- Form string with ASCII.NUL appended, folded to lower case
752 Text_Encoding : Content_Encoding;
754 Tempfile : constant Boolean := (Name'Length = 0);
755 -- Indicates temporary file case
757 Namelen : constant Integer := max_path_len;
758 -- Length required for file name, not including final ASCII.NUL.
759 -- Note that we used to reference L_tmpnam here, which is not reliable
760 -- since __gnat_tmp_name does not always use tmpnam.
762 Namestr : aliased String (1 .. Namelen + 1);
763 -- Name as given or temporary file name with ASCII.NUL appended
765 Fullname : aliased String (1 .. max_path_len + 1);
766 -- Full name (as required for Name function, and as stored in the
767 -- control block in the Name field) with ASCII.NUL appended.
769 Full_Name_Len : Integer;
770 -- Length of name actually stored in Fullname
772 Encoding : CRTL.Filename_Encoding;
773 -- Filename encoding specified into the form parameter
775 -----------------
776 -- Record_AFCB --
777 -----------------
779 procedure Record_AFCB is
780 begin
781 File_Ptr := AFCB_Allocate (Dummy_FCB);
783 -- Note that we cannot use an aggregate here as File_Ptr is a
784 -- class-wide access to a limited type (Root_Stream_Type).
786 File_Ptr.Is_Regular_File := is_regular_file (fileno (Stream)) /= 0;
787 File_Ptr.Is_System_File := False;
788 File_Ptr.Text_Encoding := Text_Encoding;
789 File_Ptr.Shared_Status := Shared;
790 File_Ptr.Access_Method := Amethod;
791 File_Ptr.Stream := Stream;
792 File_Ptr.Form := new String'(Formstr);
793 File_Ptr.Name := new String'(Fullname
794 (1 .. Full_Name_Len));
795 File_Ptr.Mode := Mode;
796 File_Ptr.Is_Temporary_File := Tempfile;
797 File_Ptr.Encoding := Encoding;
799 Chain_File (File_Ptr);
800 Append_Set (File_Ptr);
801 end Record_AFCB;
803 -- Start of processing for Open
805 begin
806 if File_Ptr /= null then
807 raise Status_Error with "file already open";
808 end if;
810 -- Acquire form string, setting required NUL terminator
812 Formstr (1 .. Form'Length) := Form;
813 Formstr (Formstr'Last) := ASCII.NUL;
815 -- Convert form string to lower case
817 for J in Formstr'Range loop
818 if Formstr (J) in 'A' .. 'Z' then
819 Formstr (J) := Character'Val (Character'Pos (Formstr (J)) + 32);
820 end if;
821 end loop;
823 -- Acquire setting of shared parameter
825 declare
826 V1, V2 : Natural;
828 begin
829 Form_Parameter (Formstr, "shared", V1, V2);
831 if V1 = 0 then
832 Shared := None;
833 elsif Formstr (V1 .. V2) = "yes" then
834 Shared := Yes;
835 elsif Formstr (V1 .. V2) = "no" then
836 Shared := No;
837 else
838 raise Use_Error with "invalid Form";
839 end if;
840 end;
842 -- Acquire setting of encoding parameter
844 declare
845 V1, V2 : Natural;
847 begin
848 Form_Parameter (Formstr, "encoding", V1, V2);
850 if V1 = 0 then
851 Encoding := CRTL.Unspecified;
852 elsif Formstr (V1 .. V2) = "utf8" then
853 Encoding := CRTL.UTF8;
854 elsif Formstr (V1 .. V2) = "8bits" then
855 Encoding := CRTL.ASCII_8bits;
856 else
857 raise Use_Error with "invalid Form";
858 end if;
859 end;
861 -- Acquire setting of text_translation parameter. Only needed if this is
862 -- a [Wide_[Wide_]]Text_IO file, in which case we default to True, but
863 -- if the Form says Text_Translation=No, we use binary mode, so new-line
864 -- will be just LF, even on Windows.
866 if Text then
867 Text_Encoding := Default_Text;
868 else
869 Text_Encoding := None;
870 end if;
872 if Text_Encoding in Text_Content_Encoding then
873 declare
874 V1, V2 : Natural;
876 begin
877 Form_Parameter (Formstr, "text_translation", V1, V2);
879 if V1 = 0 then
880 null;
881 elsif Formstr (V1 .. V2) = "no" then
882 Text_Encoding := None;
883 elsif Formstr (V1 .. V2) = "text"
884 or else Formstr (V1 .. V2) = "yes"
885 then
886 Text_Encoding := Interfaces.C_Streams.Text;
887 elsif Formstr (V1 .. V2) = "wtext" then
888 Text_Encoding := Wtext;
889 elsif Formstr (V1 .. V2) = "u8text" then
890 Text_Encoding := U8text;
891 elsif Formstr (V1 .. V2) = "u16text" then
892 Text_Encoding := U16text;
893 else
894 raise Use_Error with "invalid Form";
895 end if;
896 end;
897 end if;
899 -- If we were given a stream (call from xxx.C_Streams.Open), then set
900 -- the full name to the given one, and skip to end of processing.
902 if Stream /= NULL_Stream then
903 Full_Name_Len := Name'Length + 1;
904 Fullname (1 .. Full_Name_Len - 1) := Name;
905 Fullname (Full_Name_Len) := ASCII.NUL;
907 -- Normal case of Open or Create
909 else
910 -- If temporary file case, get temporary file name and add to the
911 -- list of temporary files to be deleted on exit.
913 if Tempfile then
914 if not Creat then
915 raise Name_Error with "opening temp file without creating it";
916 end if;
918 Tmp_Name (Namestr'Address);
920 if Namestr (1) = ASCII.NUL then
921 raise Use_Error with "invalid temp file name";
922 end if;
924 -- Chain to temp file list, ensuring thread safety with a lock
926 begin
927 SSL.Lock_Task.all;
928 Temp_Files :=
929 new Temp_File_Record'(Name => Namestr, Next => Temp_Files);
930 SSL.Unlock_Task.all;
932 exception
933 when others =>
934 SSL.Unlock_Task.all;
935 raise;
936 end;
938 -- Normal case of non-null name given
940 else
941 if Name'Length > Namelen then
942 raise Name_Error with "file name too long";
943 end if;
945 Namestr (1 .. Name'Length) := Name;
946 Namestr (Name'Length + 1) := ASCII.NUL;
947 end if;
949 -- Get full name in accordance with the advice of RM A.8.2(22)
951 full_name (Namestr'Address, Fullname'Address);
953 if Fullname (1) = ASCII.NUL then
954 raise Use_Error with Errno_Message (Name);
955 end if;
957 Full_Name_Len := 1;
958 while Full_Name_Len < Fullname'Last
959 and then Fullname (Full_Name_Len) /= ASCII.NUL
960 loop
961 Full_Name_Len := Full_Name_Len + 1;
962 end loop;
964 -- Fullname is generated by calling system's full_name. The problem
965 -- is, full_name does nothing about the casing, so a file name
966 -- comparison may generally speaking not be valid on non-case-
967 -- sensitive systems, and in particular we get unexpected failures
968 -- on Windows/Vista because of this. So we use s-casuti to force
969 -- the name to lower case.
971 if not File_Names_Case_Sensitive then
972 To_Lower (Fullname (1 .. Full_Name_Len));
973 end if;
975 -- If Shared=None or Shared=Yes, then check for the existence of
976 -- another file with exactly the same full name.
978 if Shared /= No then
979 declare
980 P : AFCB_Ptr;
982 begin
983 -- Take a task lock to protect Open_Files
985 SSL.Lock_Task.all;
987 -- Search list of open files
989 P := Open_Files;
990 while P /= null loop
991 if Fullname (1 .. Full_Name_Len) = P.Name.all then
993 -- If we get a match, and either file has Shared=None,
994 -- then raise Use_Error, since we don't allow two files
995 -- of the same name to be opened unless they specify the
996 -- required sharing mode.
998 if Shared = None
999 or else P.Shared_Status = None
1000 then
1001 raise Use_Error with "reopening shared file";
1003 -- If both files have Shared=Yes, then we acquire the
1004 -- stream from the located file to use as our stream.
1006 elsif Shared = Yes
1007 and then P.Shared_Status = Yes
1008 then
1009 Stream := P.Stream;
1011 Record_AFCB;
1013 exit;
1015 -- Otherwise one of the files has Shared=Yes and one has
1016 -- Shared=No. If the current file has Shared=No then all
1017 -- is well but we don't want to share any other file's
1018 -- stream. If the current file has Shared=Yes, we would
1019 -- like to share a stream, but not from a file that has
1020 -- Shared=No, so either way, we just continue the search.
1022 else
1023 null;
1024 end if;
1025 end if;
1027 P := P.Next;
1028 end loop;
1030 SSL.Unlock_Task.all;
1032 exception
1033 when others =>
1034 SSL.Unlock_Task.all;
1035 raise;
1036 end;
1037 end if;
1039 -- Open specified file if we did not find an existing stream,
1040 -- otherwise we just return as there is nothing more to be done.
1042 if Stream /= NULL_Stream then
1043 return;
1045 else
1046 Fopen_Mode
1047 (Mode, Text_Encoding in Text_Content_Encoding,
1048 Creat, Amethod, Fopstr);
1050 -- A special case, if we are opening (OPEN case) a file and the
1051 -- mode returned by Fopen_Mode is not "r" or "r+", then we first
1052 -- make sure that the file exists as required by Ada semantics.
1054 if not Creat and then Fopstr (1) /= 'r' then
1055 if file_exists (Namestr'Address) = 0 then
1056 raise Name_Error with Errno_Message (Name);
1057 end if;
1058 end if;
1060 -- Now open the file. Note that we use the name as given in the
1061 -- original Open call for this purpose, since that seems the
1062 -- clearest implementation of the intent. It would presumably
1063 -- work to use the full name here, but if there is any difference,
1064 -- then we should use the name used in the call.
1066 -- Note: for a corresponding delete, we will use the full name,
1067 -- since by the time of the delete, the current working directory
1068 -- may have changed and we do not want to delete a different file.
1070 Stream :=
1071 fopen (Namestr'Address, Fopstr'Address, Encoding);
1073 if Stream = NULL_Stream then
1075 -- Raise Name_Error if trying to open a non-existent file.
1076 -- Otherwise raise Use_Error.
1078 -- Should we raise Device_Error for ENOSPC???
1080 declare
1081 function Is_File_Not_Found_Error
1082 (Errno_Value : Integer) return Integer;
1083 pragma Import
1084 (C, Is_File_Not_Found_Error,
1085 "__gnat_is_file_not_found_error");
1086 -- Non-zero when the given errno value indicates a non-
1087 -- existing file.
1089 Errno : constant Integer := OS_Lib.Errno;
1090 Message : constant String := Errno_Message (Name, Errno);
1092 begin
1093 if Is_File_Not_Found_Error (Errno) /= 0 then
1094 raise Name_Error with Message;
1095 else
1096 raise Use_Error with Message;
1097 end if;
1098 end;
1099 end if;
1100 end if;
1101 end if;
1103 -- Stream has been successfully located or opened, so now we are
1104 -- committed to completing the opening of the file. Allocate block on
1105 -- heap and fill in its fields.
1107 Record_AFCB;
1108 end Open;
1110 ------------------------
1111 -- Raise_Device_Error --
1112 ------------------------
1114 procedure Raise_Device_Error
1115 (File : AFCB_Ptr;
1116 Errno : Integer := OS_Lib.Errno)
1118 begin
1119 -- Clear error status so that the same error is not reported twice
1121 if File /= null then
1122 clearerr (File.Stream);
1123 end if;
1125 raise Device_Error with OS_Lib.Errno_Message (Err => Errno);
1126 end Raise_Device_Error;
1128 --------------
1129 -- Read_Buf --
1130 --------------
1132 procedure Read_Buf (File : AFCB_Ptr; Buf : Address; Siz : size_t) is
1133 Nread : size_t;
1135 begin
1136 Nread := fread (Buf, 1, Siz, File.Stream);
1138 if Nread = Siz then
1139 return;
1141 elsif ferror (File.Stream) /= 0 then
1142 Raise_Device_Error (File);
1144 elsif Nread = 0 then
1145 raise End_Error;
1147 else -- 0 < Nread < Siz
1148 raise Data_Error with "not enough data read";
1149 end if;
1150 end Read_Buf;
1152 procedure Read_Buf
1153 (File : AFCB_Ptr;
1154 Buf : Address;
1155 Siz : Interfaces.C_Streams.size_t;
1156 Count : out Interfaces.C_Streams.size_t)
1158 begin
1159 Count := fread (Buf, 1, Siz, File.Stream);
1161 if Count = 0 and then ferror (File.Stream) /= 0 then
1162 Raise_Device_Error (File);
1163 end if;
1164 end Read_Buf;
1166 -----------
1167 -- Reset --
1168 -----------
1170 -- The reset which does not change the mode simply does a rewind
1172 procedure Reset (File_Ptr : access AFCB_Ptr) is
1173 File : AFCB_Ptr renames File_Ptr.all;
1174 begin
1175 Check_File_Open (File);
1176 Reset (File_Ptr, File.Mode);
1177 end Reset;
1179 -- The reset with a change in mode is done using freopen, and is not
1180 -- permitted except for regular files (since otherwise there is no name for
1181 -- the freopen, and in any case it seems meaningless).
1183 procedure Reset (File_Ptr : access AFCB_Ptr; Mode : File_Mode) is
1184 File : AFCB_Ptr renames File_Ptr.all;
1185 Fopstr : aliased Fopen_String;
1187 begin
1188 Check_File_Open (File);
1190 -- Change of mode not allowed for shared file or file with no name or
1191 -- file that is not a regular file, or for a system file. Note that we
1192 -- allow the "change" of mode if it is not in fact doing a change.
1194 if Mode /= File.Mode then
1195 if File.Shared_Status = Yes then
1196 raise Use_Error with "cannot change mode of shared file";
1197 elsif File.Name'Length <= 1 then
1198 raise Use_Error with "cannot change mode of temp file";
1199 elsif File.Is_System_File then
1200 raise Use_Error with "cannot change mode of system file";
1201 elsif not File.Is_Regular_File then
1202 raise Use_Error with "cannot change mode of non-regular file";
1203 end if;
1204 end if;
1206 -- For In_File or Inout_File for a regular file, we can just do a rewind
1207 -- if the mode is unchanged, which is more efficient than doing a full
1208 -- reopen.
1210 if Mode = File.Mode
1211 and then Mode in Read_File_Mode
1212 then
1213 rewind (File.Stream);
1215 -- Here the change of mode is permitted, we do it by reopening the file
1216 -- in the new mode and replacing the stream with a new stream.
1218 else
1219 Fopen_Mode
1220 (Mode, File.Text_Encoding in Text_Content_Encoding,
1221 False, File.Access_Method, Fopstr);
1223 File.Stream := freopen
1224 (File.Name.all'Address, Fopstr'Address, File.Stream,
1225 File.Encoding);
1227 if File.Stream = NULL_Stream then
1228 Close (File_Ptr);
1229 raise Use_Error;
1230 else
1231 File.Mode := Mode;
1232 Append_Set (File);
1233 end if;
1234 end if;
1235 end Reset;
1237 ---------------
1238 -- Write_Buf --
1239 ---------------
1241 procedure Write_Buf (File : AFCB_Ptr; Buf : Address; Siz : size_t) is
1242 begin
1243 -- Note: for most purposes, the Siz and 1 parameters in the fwrite call
1244 -- could be reversed, but we have encountered systems where this is a
1245 -- better choice, since for some file formats, reversing the parameters
1246 -- results in records of one byte each.
1248 SSL.Abort_Defer.all;
1250 if fwrite (Buf, Siz, 1, File.Stream) /= 1 then
1251 if Siz /= 0 then
1252 SSL.Abort_Undefer.all;
1253 Raise_Device_Error (File);
1254 end if;
1255 end if;
1257 SSL.Abort_Undefer.all;
1258 end Write_Buf;
1260 end System.File_IO;