2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / s-fileio.adb
blobbfe7d6b0cc502ba950dedd905ff0e62ce80e341a
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-2008, 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 with Ada.Finalization; use Ada.Finalization;
35 with Ada.IO_Exceptions; use Ada.IO_Exceptions;
36 with Interfaces.C_Streams; use Interfaces.C_Streams;
38 with System.CRTL;
39 with System.Case_Util; use System.Case_Util;
40 with System.Soft_Links;
42 with Ada.Unchecked_Deallocation;
44 package body System.File_IO is
46 use System.File_Control_Block;
48 package SSL renames System.Soft_Links;
50 use type System.CRTL.size_t;
52 ----------------------
53 -- Global Variables --
54 ----------------------
56 Open_Files : AFCB_Ptr;
57 -- This points to a list of AFCB's for all open files. This is a doubly
58 -- linked list, with the Prev pointer of the first entry, and the Next
59 -- pointer of the last entry containing null. Note that this global
60 -- variable must be properly protected to provide thread safety.
62 type Temp_File_Record;
63 type Temp_File_Record_Ptr is access all Temp_File_Record;
65 type Temp_File_Record is record
66 Name : String (1 .. max_path_len + 1);
67 Next : Temp_File_Record_Ptr;
68 end record;
69 -- One of these is allocated for each temporary file created
71 Temp_Files : Temp_File_Record_Ptr;
72 -- Points to list of names of temporary files. Note that this global
73 -- variable must be properly protected to provide thread safety.
75 type File_IO_Clean_Up_Type is new Controlled with null record;
76 -- The closing of all open files and deletion of temporary files is an
77 -- action which takes place at the end of execution of the main program.
78 -- This action can be implemented using a library level object which
79 -- gets finalized at the end of the main program execution. The above is
80 -- a controlled type introduced for this purpose.
82 procedure Finalize (V : in out File_IO_Clean_Up_Type);
83 -- This is the finalize operation that is used to do the cleanup
85 File_IO_Clean_Up_Object : File_IO_Clean_Up_Type;
86 pragma Warnings (Off, File_IO_Clean_Up_Object);
87 -- This is the single object of the type that triggers the finalization
88 -- call. Since it is at the library level, this happens just before the
89 -- environment task is finalized.
91 text_translation_required : Boolean;
92 for text_translation_required'Size use Character'Size;
93 pragma Import
94 (C, text_translation_required, "__gnat_text_translation_required");
95 -- If true, add appropriate suffix to control string for Open
97 function Get_Case_Sensitive return Integer;
98 pragma Import (C, Get_Case_Sensitive,
99 "__gnat_get_file_names_case_sensitive");
100 File_Names_Case_Sensitive : constant Boolean := Get_Case_Sensitive /= 0;
101 -- Set to indicate whether the operating system convention is for file
102 -- names to be case sensitive (e.g., in Unix, set True), or non case
103 -- sensitive (e.g., in OS/2, set False).
105 -----------------------
106 -- Local Subprograms --
107 -----------------------
109 procedure Free_String is new Ada.Unchecked_Deallocation (String, Pstring);
111 subtype Fopen_String is String (1 .. 4);
112 -- Holds open string (longest is "w+b" & nul)
114 procedure Fopen_Mode
115 (Mode : File_Mode;
116 Text : Boolean;
117 Creat : Boolean;
118 Amethod : Character;
119 Fopstr : out Fopen_String);
120 -- Determines proper open mode for a file to be opened in the given
121 -- Ada mode. Text is true for a text file and false otherwise, and
122 -- Creat is true for a create call, and False for an open call. The
123 -- value stored in Fopstr is a nul-terminated string suitable for a
124 -- call to fopen or freopen. Amethod is the character designating
125 -- the access method from the Access_Method field of the FCB.
127 ----------------
128 -- Append_Set --
129 ----------------
131 procedure Append_Set (File : AFCB_Ptr) is
132 begin
133 if File.Mode = Append_File then
134 if fseek (File.Stream, 0, SEEK_END) /= 0 then
135 raise Device_Error;
136 end if;
137 end if;
138 end Append_Set;
140 ----------------
141 -- Chain_File --
142 ----------------
144 procedure Chain_File (File : AFCB_Ptr) is
145 begin
146 -- Take a task lock, to protect the global data value Open_Files
148 SSL.Lock_Task.all;
150 -- Do the chaining operation locked
152 File.Next := Open_Files;
153 File.Prev := null;
154 Open_Files := File;
156 if File.Next /= null then
157 File.Next.Prev := File;
158 end if;
160 SSL.Unlock_Task.all;
162 exception
163 when others =>
164 SSL.Unlock_Task.all;
165 raise;
166 end Chain_File;
168 ---------------------
169 -- Check_File_Open --
170 ---------------------
172 procedure Check_File_Open (File : AFCB_Ptr) is
173 begin
174 if File = null then
175 raise Status_Error;
176 end if;
177 end Check_File_Open;
179 -----------------------
180 -- Check_Read_Status --
181 -----------------------
183 procedure Check_Read_Status (File : AFCB_Ptr) is
184 begin
185 if File = null then
186 raise Status_Error;
187 elsif File.Mode > Inout_File then
188 raise Mode_Error;
189 end if;
190 end Check_Read_Status;
192 ------------------------
193 -- Check_Write_Status --
194 ------------------------
196 procedure Check_Write_Status (File : AFCB_Ptr) is
197 begin
198 if File = null then
199 raise Status_Error;
200 elsif File.Mode = In_File then
201 raise Mode_Error;
202 end if;
203 end Check_Write_Status;
205 -----------
206 -- Close --
207 -----------
209 procedure Close (File_Ptr : access AFCB_Ptr) is
210 Close_Status : int := 0;
211 Dup_Strm : Boolean := False;
212 File : AFCB_Ptr renames File_Ptr.all;
214 begin
215 -- Take a task lock, to protect the global data value Open_Files
217 SSL.Lock_Task.all;
219 Check_File_Open (File);
220 AFCB_Close (File);
222 -- Sever the association between the given file and its associated
223 -- external file. The given file is left closed. Do not perform system
224 -- closes on the standard input, output and error files and also do
225 -- not attempt to close a stream that does not exist (signalled by a
226 -- null stream value -- happens in some error situations).
228 if not File.Is_System_File
229 and then File.Stream /= NULL_Stream
230 then
231 -- Do not do an fclose if this is a shared file and there is
232 -- at least one other instance of the stream that is open.
234 if File.Shared_Status = Yes then
235 declare
236 P : AFCB_Ptr;
238 begin
239 P := Open_Files;
240 while P /= null loop
241 if P /= File
242 and then File.Stream = P.Stream
243 then
244 Dup_Strm := True;
245 exit;
246 end if;
248 P := P.Next;
249 end loop;
250 end;
251 end if;
253 -- Do the fclose unless this was a duplicate in the shared case
255 if not Dup_Strm then
256 Close_Status := fclose (File.Stream);
257 end if;
258 end if;
260 -- Dechain file from list of open files and then free the storage
262 if File.Prev = null then
263 Open_Files := File.Next;
264 else
265 File.Prev.Next := File.Next;
266 end if;
268 if File.Next /= null then
269 File.Next.Prev := File.Prev;
270 end if;
272 -- Deallocate some parts of the file structure that were kept in heap
273 -- storage with the exception of system files (standard input, output
274 -- and error) since they had some information allocated in the stack.
276 if not File.Is_System_File then
277 Free_String (File.Name);
278 Free_String (File.Form);
279 AFCB_Free (File);
280 end if;
282 File := null;
284 if Close_Status /= 0 then
285 raise Device_Error;
286 end if;
288 SSL.Unlock_Task.all;
290 exception
291 when others =>
292 SSL.Unlock_Task.all;
293 raise;
294 end Close;
296 ------------
297 -- Delete --
298 ------------
300 procedure Delete (File_Ptr : access AFCB_Ptr) is
301 File : AFCB_Ptr renames File_Ptr.all;
302 begin
303 Check_File_Open (File);
305 if not File.Is_Regular_File then
306 raise Use_Error;
307 end if;
309 declare
310 Filename : aliased constant String := File.Name.all;
312 begin
313 Close (File_Ptr);
315 -- Now unlink the external file. Note that we use the full name
316 -- in this unlink, because the working directory may have changed
317 -- since we did the open, and we want to unlink the right file!
319 if unlink (Filename'Address) = -1 then
320 raise Use_Error;
321 end if;
322 end;
323 end Delete;
325 -----------------
326 -- End_Of_File --
327 -----------------
329 function End_Of_File (File : AFCB_Ptr) return Boolean is
330 begin
331 Check_File_Open (File);
333 if feof (File.Stream) /= 0 then
334 return True;
336 else
337 Check_Read_Status (File);
339 if ungetc (fgetc (File.Stream), File.Stream) = EOF then
340 clearerr (File.Stream);
341 return True;
342 else
343 return False;
344 end if;
345 end if;
346 end End_Of_File;
348 --------------
349 -- Finalize --
350 --------------
352 -- Note: we do not need to worry about locking against multiple task
353 -- access in this routine, since it is called only from the environment
354 -- task just before terminating execution.
356 procedure Finalize (V : in out File_IO_Clean_Up_Type) is
357 pragma Warnings (Off, V);
359 Fptr1 : aliased AFCB_Ptr;
360 Fptr2 : AFCB_Ptr;
362 Discard : int;
363 pragma Unreferenced (Discard);
365 begin
366 -- Take a lock to protect global Open_Files data structure
368 SSL.Lock_Task.all;
370 -- First close all open files (the slightly complex form of this loop
371 -- is required because Close as a side effect nulls out its argument)
373 Fptr1 := Open_Files;
374 while Fptr1 /= null loop
375 Fptr2 := Fptr1.Next;
376 Close (Fptr1'Access);
377 Fptr1 := Fptr2;
378 end loop;
380 -- Now unlink all temporary files. We do not bother to free the
381 -- blocks because we are just about to terminate the program. We
382 -- also ignore any errors while attempting these unlink operations.
384 while Temp_Files /= null loop
385 Discard := unlink (Temp_Files.Name'Address);
386 Temp_Files := Temp_Files.Next;
387 end loop;
389 SSL.Unlock_Task.all;
391 exception
392 when others =>
393 SSL.Unlock_Task.all;
394 raise;
395 end Finalize;
397 -----------
398 -- Flush --
399 -----------
401 procedure Flush (File : AFCB_Ptr) is
402 begin
403 Check_Write_Status (File);
405 if fflush (File.Stream) = 0 then
406 return;
407 else
408 raise Device_Error;
409 end if;
410 end Flush;
412 ----------------
413 -- Fopen_Mode --
414 ----------------
416 -- The fopen mode to be used is shown by the following table:
418 -- OPEN CREATE
419 -- Append_File "r+" "w+"
420 -- In_File "r" "w+"
421 -- Out_File (Direct_IO) "r+" "w"
422 -- Out_File (all others) "w" "w"
423 -- Inout_File "r+" "w+"
425 -- Note: we do not use "a" or "a+" for Append_File, since this would not
426 -- work in the case of stream files, where even if in append file mode,
427 -- you can reset to earlier points in the file. The caller must use the
428 -- Append_Set routine to deal with the necessary positioning.
430 -- Note: in several cases, the fopen mode used allows reading and
431 -- writing, but the setting of the Ada mode is more restrictive. For
432 -- instance, Create in In_File mode uses "w+" which allows writing,
433 -- but the Ada mode In_File will cause any write operations to be
434 -- rejected with Mode_Error in any case.
436 -- Note: for the Out_File/Open cases for other than the Direct_IO case,
437 -- an initial call will be made by the caller to first open the file in
438 -- "r" mode to be sure that it exists. The real open, in "w" mode, will
439 -- then destroy this file. This is peculiar, but that's what Ada semantics
440 -- require and the ACVT tests insist on!
442 -- If text file translation is required, then either b or t is
443 -- added to the mode, depending on the setting of Text.
445 procedure Fopen_Mode
446 (Mode : File_Mode;
447 Text : Boolean;
448 Creat : Boolean;
449 Amethod : Character;
450 Fopstr : out Fopen_String)
452 Fptr : Positive;
454 begin
455 case Mode is
456 when In_File =>
457 if Creat then
458 Fopstr (1) := 'w';
459 Fopstr (2) := '+';
460 Fptr := 3;
461 else
462 Fopstr (1) := 'r';
463 Fptr := 2;
464 end if;
466 when Out_File =>
467 if Amethod = 'D' and not Creat then
468 Fopstr (1) := 'r';
469 Fopstr (2) := '+';
470 Fptr := 3;
471 else
472 Fopstr (1) := 'w';
473 Fptr := 2;
474 end if;
476 when Inout_File | Append_File =>
477 if Creat then
478 Fopstr (1) := 'w';
479 else
480 Fopstr (1) := 'r';
481 end if;
483 Fopstr (2) := '+';
484 Fptr := 3;
486 end case;
488 -- If text_translation_required is true then we need to append
489 -- either a t or b to the string to get the right mode
491 if text_translation_required then
492 if Text then
493 Fopstr (Fptr) := 't';
494 else
495 Fopstr (Fptr) := 'b';
496 end if;
498 Fptr := Fptr + 1;
499 end if;
501 Fopstr (Fptr) := ASCII.NUL;
502 end Fopen_Mode;
504 ----------
505 -- Form --
506 ----------
508 function Form (File : AFCB_Ptr) return String is
509 begin
510 if File = null then
511 raise Status_Error;
512 else
513 return File.Form.all (1 .. File.Form'Length - 1);
514 end if;
515 end Form;
517 ------------------
518 -- Form_Boolean --
519 ------------------
521 function Form_Boolean
522 (Form : String;
523 Keyword : String;
524 Default : Boolean)
525 return Boolean
527 V1, V2 : Natural;
528 pragma Unreferenced (V2);
530 begin
531 Form_Parameter (Form, Keyword, V1, V2);
533 if V1 = 0 then
534 return Default;
536 elsif Form (V1) = 'y' then
537 return True;
539 elsif Form (V1) = 'n' then
540 return False;
542 else
543 raise Use_Error;
544 end if;
545 end Form_Boolean;
547 ------------------
548 -- Form_Integer --
549 ------------------
551 function Form_Integer
552 (Form : String;
553 Keyword : String;
554 Default : Integer)
555 return Integer
557 V1, V2 : Natural;
558 V : Integer;
560 begin
561 Form_Parameter (Form, Keyword, V1, V2);
563 if V1 = 0 then
564 return Default;
566 else
567 V := 0;
569 for J in V1 .. V2 loop
570 if Form (J) not in '0' .. '9' then
571 raise Use_Error;
572 else
573 V := V * 10 + Character'Pos (Form (J)) - Character'Pos ('0');
574 end if;
576 if V > 999_999 then
577 raise Use_Error;
578 end if;
579 end loop;
581 return V;
582 end if;
583 end Form_Integer;
585 --------------------
586 -- Form_Parameter --
587 --------------------
589 procedure Form_Parameter
590 (Form : String;
591 Keyword : String;
592 Start : out Natural;
593 Stop : out Natural)
595 Klen : constant Integer := Keyword'Length;
597 -- Start of processing for Form_Parameter
599 begin
600 for J in Form'First + Klen .. Form'Last - 1 loop
601 if Form (J) = '='
602 and then Form (J - Klen .. J - 1) = Keyword
603 then
604 Start := J + 1;
605 Stop := Start - 1;
607 while Form (Stop + 1) /= ASCII.NUL
608 and then Form (Stop + 1) /= ','
609 loop
610 Stop := Stop + 1;
611 end loop;
613 return;
614 end if;
615 end loop;
617 Start := 0;
618 Stop := 0;
619 end Form_Parameter;
621 -------------
622 -- Is_Open --
623 -------------
625 function Is_Open (File : AFCB_Ptr) return Boolean is
626 begin
627 -- We return True if the file is open, and the underlying file stream is
628 -- usable. In particular on Windows an application linked with -mwindows
629 -- option set does not have a console attached. In this case standard
630 -- files (Current_Output, Current_Error, Current_Input) are not created.
631 -- We want Is_Open (Current_Output) to return False in this case.
633 return File /= null and then fileno (File.Stream) /= -1;
634 end Is_Open;
636 -------------------
637 -- Make_Buffered --
638 -------------------
640 procedure Make_Buffered
641 (File : AFCB_Ptr;
642 Buf_Siz : Interfaces.C_Streams.size_t)
644 status : Integer;
645 pragma Unreferenced (status);
647 begin
648 status := setvbuf (File.Stream, Null_Address, IOFBF, Buf_Siz);
649 end Make_Buffered;
651 ------------------------
652 -- Make_Line_Buffered --
653 ------------------------
655 procedure Make_Line_Buffered
656 (File : AFCB_Ptr;
657 Line_Siz : Interfaces.C_Streams.size_t)
659 status : Integer;
660 pragma Unreferenced (status);
662 begin
663 status := setvbuf (File.Stream, Null_Address, IOLBF, Line_Siz);
664 end Make_Line_Buffered;
666 ---------------------
667 -- Make_Unbuffered --
668 ---------------------
670 procedure Make_Unbuffered (File : AFCB_Ptr) is
671 status : Integer;
672 pragma Unreferenced (status);
674 begin
675 status := setvbuf (File.Stream, Null_Address, IONBF, 0);
676 end Make_Unbuffered;
678 ----------
679 -- Mode --
680 ----------
682 function Mode (File : AFCB_Ptr) return File_Mode is
683 begin
684 if File = null then
685 raise Status_Error;
686 else
687 return File.Mode;
688 end if;
689 end Mode;
691 ----------
692 -- Name --
693 ----------
695 function Name (File : AFCB_Ptr) return String is
696 begin
697 if File = null then
698 raise Status_Error;
699 else
700 return File.Name.all (1 .. File.Name'Length - 1);
701 end if;
702 end Name;
704 ----------
705 -- Open --
706 ----------
708 procedure Open
709 (File_Ptr : in out AFCB_Ptr;
710 Dummy_FCB : AFCB'Class;
711 Mode : File_Mode;
712 Name : String;
713 Form : String;
714 Amethod : Character;
715 Creat : Boolean;
716 Text : Boolean;
717 C_Stream : FILEs := NULL_Stream)
719 pragma Warnings (Off, Dummy_FCB);
720 -- Yes we know this is never assigned a value. That's intended, since
721 -- all we ever use of this value is the tag for dispatching purposes.
723 procedure Tmp_Name (Buffer : Address);
724 pragma Import (C, Tmp_Name, "__gnat_tmp_name");
725 -- set buffer (a String address) with a temporary filename
727 Stream : FILEs := C_Stream;
728 -- Stream which we open in response to this request
730 Shared : Shared_Status_Type;
731 -- Setting of Shared_Status field for file
733 Fopstr : aliased Fopen_String;
734 -- Mode string used in fopen call
736 Formstr : aliased String (1 .. Form'Length + 1);
737 -- Form string with ASCII.NUL appended, folded to lower case
739 Tempfile : constant Boolean := (Name'Length = 0);
740 -- Indicates temporary file case
742 Namelen : constant Integer := max_path_len;
743 -- Length required for file name, not including final ASCII.NUL
744 -- Note that we used to reference L_tmpnam here, which is not
745 -- reliable since __gnat_tmp_name does not always use tmpnam.
747 Namestr : aliased String (1 .. Namelen + 1);
748 -- Name as given or temporary file name with ASCII.NUL appended
750 Fullname : aliased String (1 .. max_path_len + 1);
751 -- Full name (as required for Name function, and as stored in the
752 -- control block in the Name field) with ASCII.NUL appended.
754 Full_Name_Len : Integer;
755 -- Length of name actually stored in Fullname
757 Encoding : System.CRTL.Filename_Encoding;
758 -- Filename encoding specified into the form parameter
760 begin
761 if File_Ptr /= null then
762 raise Status_Error;
763 end if;
765 -- Acquire form string, setting required NUL terminator
767 Formstr (1 .. Form'Length) := Form;
768 Formstr (Formstr'Last) := ASCII.NUL;
770 -- Convert form string to lower case
772 for J in Formstr'Range loop
773 if Formstr (J) in 'A' .. 'Z' then
774 Formstr (J) := Character'Val (Character'Pos (Formstr (J)) + 32);
775 end if;
776 end loop;
778 -- Acquire setting of shared parameter
780 declare
781 V1, V2 : Natural;
783 begin
784 Form_Parameter (Formstr, "shared", V1, V2);
786 if V1 = 0 then
787 Shared := None;
789 elsif Formstr (V1 .. V2) = "yes" then
790 Shared := Yes;
792 elsif Formstr (V1 .. V2) = "no" then
793 Shared := No;
795 else
796 raise Use_Error;
797 end if;
798 end;
800 -- Acquire setting of shared parameter
802 declare
803 V1, V2 : Natural;
805 begin
806 Form_Parameter (Formstr, "encoding", V1, V2);
808 if V1 = 0 then
809 Encoding := System.CRTL.UTF8;
811 elsif Formstr (V1 .. V2) = "utf8" then
812 Encoding := System.CRTL.UTF8;
814 elsif Formstr (V1 .. V2) = "8bits" then
815 Encoding := System.CRTL.ASCII_8bits;
817 else
818 raise Use_Error;
819 end if;
820 end;
822 -- If we were given a stream (call from xxx.C_Streams.Open), then set
823 -- the full name to the given one, and skip to end of processing.
825 if Stream /= NULL_Stream then
826 Full_Name_Len := Name'Length + 1;
827 Fullname (1 .. Full_Name_Len - 1) := Name;
828 Fullname (Full_Name_Len) := ASCII.NUL;
830 -- Normal case of Open or Create
832 else
833 -- If temporary file case, get temporary file name and add
834 -- to the list of temporary files to be deleted on exit.
836 if Tempfile then
837 if not Creat then
838 raise Name_Error;
839 end if;
841 Tmp_Name (Namestr'Address);
843 if Namestr (1) = ASCII.NUL then
844 raise Use_Error;
845 end if;
847 -- Chain to temp file list, ensuring thread safety with a lock
849 begin
850 SSL.Lock_Task.all;
851 Temp_Files :=
852 new Temp_File_Record'(Name => Namestr, Next => Temp_Files);
853 SSL.Unlock_Task.all;
855 exception
856 when others =>
857 SSL.Unlock_Task.all;
858 raise;
859 end;
861 -- Normal case of non-null name given
863 else
864 if Name'Length > Namelen then
865 raise Name_Error;
866 end if;
868 Namestr (1 .. Name'Length) := Name;
869 Namestr (Name'Length + 1) := ASCII.NUL;
870 end if;
872 -- Get full name in accordance with the advice of RM A.8.2(22)
874 full_name (Namestr'Address, Fullname'Address);
876 if Fullname (1) = ASCII.NUL then
877 raise Use_Error;
878 end if;
880 Full_Name_Len := 1;
881 while Full_Name_Len < Fullname'Last
882 and then Fullname (Full_Name_Len) /= ASCII.NUL
883 loop
884 Full_Name_Len := Full_Name_Len + 1;
885 end loop;
887 -- Fullname is generated by calling system's full_name. The problem
888 -- is, full_name does nothing about the casing, so a file name
889 -- comparison may generally speaking not be valid on non-case
890 -- sensitive systems, and in particular we get unexpected failures
891 -- on Windows/Vista because of this. So we use s-casuti to force
892 -- the name to lower case.
894 if not File_Names_Case_Sensitive then
895 To_Lower (Fullname (1 .. Full_Name_Len));
896 end if;
898 -- If Shared=None or Shared=Yes, then check for the existence
899 -- of another file with exactly the same full name.
901 if Shared /= No then
902 declare
903 P : AFCB_Ptr;
905 begin
906 -- Take a task lock to protect Open_Files
908 SSL.Lock_Task.all;
910 -- Search list of open files
912 P := Open_Files;
913 while P /= null loop
914 if Fullname (1 .. Full_Name_Len) = P.Name.all then
916 -- If we get a match, and either file has Shared=None,
917 -- then raise Use_Error, since we don't allow two files
918 -- of the same name to be opened unless they specify the
919 -- required sharing mode.
921 if Shared = None
922 or else P.Shared_Status = None
923 then
924 raise Use_Error;
926 -- If both files have Shared=Yes, then we acquire the
927 -- stream from the located file to use as our stream.
929 elsif Shared = Yes
930 and then P.Shared_Status = Yes
931 then
932 Stream := P.Stream;
933 exit;
935 -- Otherwise one of the files has Shared=Yes and one has
936 -- Shared=No. If the current file has Shared=No then all
937 -- is well but we don't want to share any other file's
938 -- stream. If the current file has Shared=Yes, we would
939 -- like to share a stream, but not from a file that has
940 -- Shared=No, so either way, we just continue the search.
942 else
943 null;
944 end if;
945 end if;
947 P := P.Next;
948 end loop;
950 SSL.Unlock_Task.all;
952 exception
953 when others =>
954 SSL.Unlock_Task.all;
955 raise;
956 end;
957 end if;
959 -- Open specified file if we did not find an existing stream
961 if Stream = NULL_Stream then
962 Fopen_Mode (Mode, Text, Creat, Amethod, Fopstr);
964 -- A special case, if we are opening (OPEN case) a file and the
965 -- mode returned by Fopen_Mode is not "r" or "r+", then we first
966 -- make sure that the file exists as required by Ada semantics.
968 if Creat = False and then Fopstr (1) /= 'r' then
969 if file_exists (Namestr'Address) = 0 then
970 raise Name_Error;
971 end if;
972 end if;
974 -- Now open the file. Note that we use the name as given in the
975 -- original Open call for this purpose, since that seems the
976 -- clearest implementation of the intent. It would presumably
977 -- work to use the full name here, but if there is any difference,
978 -- then we should use the name used in the call.
980 -- Note: for a corresponding delete, we will use the full name,
981 -- since by the time of the delete, the current working directory
982 -- may have changed and we do not want to delete a different file!
984 Stream := fopen (Namestr'Address, Fopstr'Address, Encoding);
986 if Stream = NULL_Stream then
987 if not Tempfile and then file_exists (Namestr'Address) = 0 then
988 raise Name_Error;
989 else
990 raise Use_Error;
991 end if;
992 end if;
993 end if;
994 end if;
996 -- Stream has been successfully located or opened, so now we are
997 -- committed to completing the opening of the file. Allocate block
998 -- on heap and fill in its fields.
1000 File_Ptr := AFCB_Allocate (Dummy_FCB);
1002 File_Ptr.Is_Regular_File := (is_regular_file (fileno (Stream)) /= 0);
1003 File_Ptr.Is_System_File := False;
1004 File_Ptr.Is_Text_File := Text;
1005 File_Ptr.Shared_Status := Shared;
1006 File_Ptr.Access_Method := Amethod;
1007 File_Ptr.Stream := Stream;
1008 File_Ptr.Form := new String'(Formstr);
1009 File_Ptr.Name := new String'(Fullname (1 .. Full_Name_Len));
1010 File_Ptr.Mode := Mode;
1011 File_Ptr.Is_Temporary_File := Tempfile;
1012 File_Ptr.Encoding := Encoding;
1014 Chain_File (File_Ptr);
1015 Append_Set (File_Ptr);
1016 end Open;
1018 --------------
1019 -- Read_Buf --
1020 --------------
1022 procedure Read_Buf (File : AFCB_Ptr; Buf : Address; Siz : size_t) is
1023 Nread : size_t;
1025 begin
1026 Nread := fread (Buf, 1, Siz, File.Stream);
1028 if Nread = Siz then
1029 return;
1031 elsif ferror (File.Stream) /= 0 then
1032 raise Device_Error;
1034 elsif Nread = 0 then
1035 raise End_Error;
1037 else -- 0 < Nread < Siz
1038 raise Data_Error;
1039 end if;
1041 end Read_Buf;
1043 procedure Read_Buf
1044 (File : AFCB_Ptr;
1045 Buf : Address;
1046 Siz : Interfaces.C_Streams.size_t;
1047 Count : out Interfaces.C_Streams.size_t)
1049 begin
1050 Count := fread (Buf, 1, Siz, File.Stream);
1052 if Count = 0 and then ferror (File.Stream) /= 0 then
1053 raise Device_Error;
1054 end if;
1055 end Read_Buf;
1057 -----------
1058 -- Reset --
1059 -----------
1061 -- The reset which does not change the mode simply does a rewind
1063 procedure Reset (File_Ptr : access AFCB_Ptr) is
1064 File : AFCB_Ptr renames File_Ptr.all;
1065 begin
1066 Check_File_Open (File);
1067 Reset (File_Ptr, File.Mode);
1068 end Reset;
1070 -- The reset with a change in mode is done using freopen, and is
1071 -- not permitted except for regular files (since otherwise there
1072 -- is no name for the freopen, and in any case it seems meaningless)
1074 procedure Reset (File_Ptr : access AFCB_Ptr; Mode : File_Mode) is
1075 File : AFCB_Ptr renames File_Ptr.all;
1076 Fopstr : aliased Fopen_String;
1078 begin
1079 Check_File_Open (File);
1081 -- Change of mode not allowed for shared file or file with no name or
1082 -- file that is not a regular file, or for a system file. Note that we
1083 -- allow the "change" of mode if it is not in fact doing a change.
1085 if Mode /= File.Mode
1086 and then (File.Shared_Status = Yes
1087 or else File.Name'Length <= 1
1088 or else File.Is_System_File
1089 or else not File.Is_Regular_File)
1090 then
1091 raise Use_Error;
1093 -- For In_File or Inout_File for a regular file, we can just do a
1094 -- rewind if the mode is unchanged, which is more efficient than
1095 -- doing a full reopen.
1097 elsif Mode = File.Mode
1098 and then Mode <= Inout_File
1099 then
1100 rewind (File.Stream);
1102 -- Here the change of mode is permitted, we do it by reopening the
1103 -- file in the new mode and replacing the stream with a new stream.
1105 else
1106 Fopen_Mode
1107 (Mode, File.Is_Text_File, False, File.Access_Method, Fopstr);
1109 File.Stream := freopen
1110 (File.Name.all'Address, Fopstr'Address, File.Stream, File.Encoding);
1112 if File.Stream = NULL_Stream then
1113 Close (File_Ptr);
1114 raise Use_Error;
1116 else
1117 File.Mode := Mode;
1118 Append_Set (File);
1119 end if;
1120 end if;
1121 end Reset;
1123 ---------------
1124 -- Write_Buf --
1125 ---------------
1127 procedure Write_Buf (File : AFCB_Ptr; Buf : Address; Siz : size_t) is
1128 begin
1129 -- Note: for most purposes, the Siz and 1 parameters in the fwrite
1130 -- call could be reversed, but on VMS, this is a better choice, since
1131 -- for some file formats, reversing the parameters results in records
1132 -- of one byte each.
1134 SSL.Abort_Defer.all;
1136 if fwrite (Buf, Siz, 1, File.Stream) /= 1 then
1137 if Siz /= 0 then
1138 SSL.Abort_Undefer.all;
1139 raise Device_Error;
1140 end if;
1141 end if;
1143 SSL.Abort_Undefer.all;
1144 end Write_Buf;
1146 end System.File_IO;