Debian Codegraph @

gcc/m2/pge-boot/GFIO.cc

raw
1 /* do not edit automatically generated by mc from FIO. */
2 /* FIO.mod provides a simple buffered file input/output library.
3
4 Copyright (C) 2001-2025 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <[email protected]>.
6
7 This file is part of GNU Modula-2.
8
9 GNU Modula-2 is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GNU Modula-2 is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
22
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
27
28 #include <stdbool.h>
29 # if !defined (PROC_D)
30 # define PROC_D
31 typedef void (*PROC_t) (void);
32 typedef struct { PROC_t proc; } PROC;
33 # endif
34
35 # if !defined (TRUE)
36 # define TRUE (1==1)
37 # endif
38
39 # if !defined (FALSE)
40 # define FALSE (1==0)
41 # endif
42
43 #include <stddef.h>
44 #include <string.h>
45 #include <limits.h>
46 #include <stdlib.h>
47 # include "GStorage.h"
48 # include "Gmcrts.h"
49 #include <unistd.h>
50 #if defined(__cplusplus)
51 # undef NULL
52 # define NULL 0
53 #endif
54 #define _FIO_C
55
56 #include "GFIO.h"
57 # include "GSYSTEM.h"
58 # include "GASCII.h"
59 # include "GStrLib.h"
60 # include "GStorage.h"
61 # include "GNumberIO.h"
62 # include "GIndexing.h"
63 # include "GM2RTS.h"
64 # include "Glibc.h"
65 # include "Gwrapc.h"
66
67 typedef unsigned int FIO_File;
68
69 # define MaxBufferLength (1024*16)
70 # define MaxErrorString (1024*8)
71 # define CreatePermissions 0666
72 typedef struct FIO_NameInfo_r FIO_NameInfo;
73
74 typedef struct FIO_buf_r FIO_buf;
75
76 typedef FIO_buf *FIO_Buffer;
77
78 typedef struct FIO_fds_r FIO_fds;
79
80 typedef FIO_fds *FIO_FileDescriptor;
81
82 typedef struct FIO__T7_a FIO__T7;
83
84 typedef char *FIO_PtrToChar;
85
86 typedef enum {FIO_successful, FIO_outofmemory, FIO_toomanyfilesopen, FIO_failed, FIO_connectionfailure, FIO_endofline, FIO_endoffile} FIO_FileStatus;
87
88 typedef enum {FIO_unused, FIO_openedforread, FIO_openedforwrite, FIO_openedforrandom} FIO_FileUsage;
89
90 struct FIO_NameInfo_r {
91 void *address;
92 unsigned int size;
93 };
94
95 struct FIO_buf_r {
96 bool valid;
97 long int bufstart;
98 unsigned int position;
99 void *address;
100 unsigned int filled;
101 unsigned int size;
102 unsigned int left;
103 FIO__T7 *contents;
104 };
105
106 struct FIO__T7_a { char array[MaxBufferLength+1]; };
107 struct FIO_fds_r {
108 int unixfd;
109 FIO_NameInfo name;
110 FIO_FileStatus state;
111 FIO_FileUsage usage;
112 bool output;
113 FIO_Buffer buffer;
114 long int abspos;
115 };
116
117 static Indexing_Index FileInfo;
118 static FIO_File Error;
119
120 /*
121 IsNoError - returns a TRUE if no error has occured on file, f.
122 */
123
124 extern "C" bool FIO_IsNoError (FIO_File f);
125
126 /*
127 IsActive - returns TRUE if the file, f, is still active.
128 */
129
130 extern "C" bool FIO_IsActive (FIO_File f);
131 extern "C" bool FIO_Exists (const char *fname_, unsigned int _fname_high);
132 extern "C" FIO_File FIO_OpenToRead (const char *fname_, unsigned int _fname_high);
133 extern "C" FIO_File FIO_OpenToWrite (const char *fname_, unsigned int _fname_high);
134 extern "C" FIO_File FIO_OpenForRandom (const char *fname_, unsigned int _fname_high, bool towrite, bool newfile);
135
136 /*
137 Close - close a file which has been previously opened using:
138 OpenToRead, OpenToWrite, OpenForRandom.
139 It is correct to close a file which has an error status.
140 */
141
142 extern "C" void FIO_Close (FIO_File f);
143
144 /*
145 exists - returns TRUE if a file named, fname exists for reading.
146 */
147
148 extern "C" bool FIO_exists (void * fname, unsigned int flength);
149
150 /*
151 openToRead - attempts to open a file, fname, for reading and
152 it returns this file.
153 The success of this operation can be checked by
154 calling IsNoError.
155 */
156
157 extern "C" FIO_File FIO_openToRead (void * fname, unsigned int flength);
158
159 /*
160 openToWrite - attempts to open a file, fname, for write and
161 it returns this file.
162 The success of this operation can be checked by
163 calling IsNoError.
164 */
165
166 extern "C" FIO_File FIO_openToWrite (void * fname, unsigned int flength);
167
168 /*
169 openForRandom - attempts to open a file, fname, for random access
170 read or write and it returns this file.
171 The success of this operation can be checked by
172 calling IsNoError.
173 towrite, determines whether the file should be
174 opened for writing or reading.
175 */
176
177 extern "C" FIO_File FIO_openForRandom (void * fname, unsigned int flength, bool towrite, bool newfile);
178
179 /*
180 FlushBuffer - flush contents of file, f.
181 */
182
183 extern "C" void FIO_FlushBuffer (FIO_File f);
184
185 /*
186 ReadNBytes - reads nBytes of a file into memory area, dest, returning
187 the number of bytes actually read.
188 This function will consume from the buffer and then
189 perform direct libc reads. It is ideal for large reads.
190 */
191
192 extern "C" unsigned int FIO_ReadNBytes (FIO_File f, unsigned int nBytes, void * dest);
193
194 /*
195 ReadAny - reads HIGH (a) + 1 bytes into, a. All input
196 is fully buffered, unlike ReadNBytes and thus is more
197 suited to small reads.
198 */
199
200 extern "C" void FIO_ReadAny (FIO_File f, unsigned char *a, unsigned int _a_high);
201
202 /*
203 WriteNBytes - writes nBytes from memory area src to a file
204 returning the number of bytes actually written.
205 This function will flush the buffer and then
206 write the nBytes using a direct write from libc.
207 It is ideal for large writes.
208 */
209
210 extern "C" unsigned int FIO_WriteNBytes (FIO_File f, unsigned int nBytes, void * src);
211
212 /*
213 WriteAny - writes HIGH (a) + 1 bytes onto, file, f. All output
214 is fully buffered, unlike WriteNBytes and thus is more
215 suited to small writes.
216 */
217
218 extern "C" void FIO_WriteAny (FIO_File f, unsigned char *a, unsigned int _a_high);
219
220 /*
221 WriteChar - writes a single character to file, f.
222 */
223
224 extern "C" void FIO_WriteChar (FIO_File f, char ch);
225
226 /*
227 EOF - tests to see whether a file, f, has reached end of file.
228 */
229
230 extern "C" bool FIO_EOF (FIO_File f);
231
232 /*
233 EOLN - tests to see whether a file, f, is upon a newline.
234 It does NOT consume the newline.
235 */
236
237 extern "C" bool FIO_EOLN (FIO_File f);
238
239 /*
240 WasEOLN - tests to see whether a file, f, has just seen a newline.
241 */
242
243 extern "C" bool FIO_WasEOLN (FIO_File f);
244
245 /*
246 ReadChar - returns a character read from file f.
247 Sensible to check with IsNoError or EOF after calling
248 this function.
249 */
250
251 extern "C" char FIO_ReadChar (FIO_File f);
252
253 /*
254 UnReadChar - replaces a character, ch, back into file f.
255 This character must have been read by ReadChar
256 and it does not allow successive calls. It may
257 only be called if the previous read was successful
258 or end of file was seen.
259 If the state was previously endoffile then it
260 is altered to successful.
261 Otherwise it is left alone.
262 */
263
264 extern "C" void FIO_UnReadChar (FIO_File f, char ch);
265
266 /*
267 WriteLine - writes out a linefeed to file, f.
268 */
269
270 extern "C" void FIO_WriteLine (FIO_File f);
271
272 /*
273 WriteString - writes a string to file, f.
274 */
275
276 extern "C" void FIO_WriteString (FIO_File f, const char *a_, unsigned int _a_high);
277
278 /*
279 ReadString - reads a string from file, f, into string, a.
280 It terminates the string if HIGH is reached or
281 if a newline is seen or an error occurs.
282 */
283
284 extern "C" void FIO_ReadString (FIO_File f, char *a, unsigned int _a_high);
285
286 /*
287 WriteCardinal - writes a CARDINAL to file, f.
288 It writes the binary image of the cardinal
289 to file, f.
290 */
291
292 extern "C" void FIO_WriteCardinal (FIO_File f, unsigned int c);
293
294 /*
295 ReadCardinal - reads a CARDINAL from file, f.
296 It reads a binary image of a CARDINAL
297 from a file, f.
298 */
299
300 extern "C" unsigned int FIO_ReadCardinal (FIO_File f);
301
302 /*
303 GetUnixFileDescriptor - returns the UNIX file descriptor of a file.
304 */
305
306 extern "C" int FIO_GetUnixFileDescriptor (FIO_File f);
307
308 /*
309 SetPositionFromBeginning - sets the position from the beginning of the file.
310 */
311
312 extern "C" void FIO_SetPositionFromBeginning (FIO_File f, long int pos);
313
314 /*
315 SetPositionFromEnd - sets the position from the end of the file.
316 */
317
318 extern "C" void FIO_SetPositionFromEnd (FIO_File f, long int pos);
319
320 /*
321 FindPosition - returns the current absolute position in file, f.
322 */
323
324 extern "C" long int FIO_FindPosition (FIO_File f);
325
326 /*
327 GetFileName - assigns, a, with the filename associated with, f.
328 */
329
330 extern "C" void FIO_GetFileName (FIO_File f, char *a, unsigned int _a_high);
331
332 /*
333 getFileName - returns the address of the filename associated with, f.
334 */
335
336 extern "C" void * FIO_getFileName (FIO_File f);
337
338 /*
339 getFileNameLength - returns the number of characters associated with filename, f.
340 */
341
342 extern "C" unsigned int FIO_getFileNameLength (FIO_File f);
343
344 /*
345 FlushOutErr - flushes, StdOut, and, StdErr.
346 It is also called when the application calls M2RTS.Terminate.
347 (which is automatically placed in program modules by the GM2
348 scaffold).
349 */
350
351 extern "C" void FIO_FlushOutErr (void);
352
353 /*
354 Max - returns the maximum of two values.
355 */
356
357 static unsigned int Max (unsigned int a, unsigned int b);
358
359 /*
360 Min - returns the minimum of two values.
361 */
362
363 static unsigned int Min (unsigned int a, unsigned int b);
364
365 /*
366 GetNextFreeDescriptor - returns the index to the FileInfo array indicating
367 the next free slot.
368 */
369
370 static FIO_File GetNextFreeDescriptor (void);
371
372 /*
373 SetState - sets the field, state, of file, f, to, s.
374 */
375
376 static void SetState (FIO_File f, FIO_FileStatus s);
377
378 /*
379 InitializeFile - initialize a file descriptor
380 */
381
382 static FIO_File InitializeFile (FIO_File f, void * fname, unsigned int flength, FIO_FileStatus fstate, FIO_FileUsage use, bool towrite, unsigned int buflength);
383
384 /*
385 ConnectToUnix - connects a FIO file to a UNIX file descriptor.
386 */
387
388 static void ConnectToUnix (FIO_File f, bool towrite, bool newfile);
389
390 /*
391 ReadFromBuffer - attempts to read, nBytes, from file, f.
392 It firstly consumes the buffer and then performs
393 direct unbuffered reads. This should only be used
394 when wishing to read large files.
395
396 The actual number of bytes read is returned.
397 -1 is returned if EOF is reached.
398 */
399
400 static int ReadFromBuffer (FIO_File f, void * a, unsigned int nBytes);
401
402 /*
403 BufferedRead - will read, nBytes, through the buffer.
404 Similar to ReadFromBuffer, but this function will always
405 read into the buffer before copying into memory.
406
407 Useful when performing small reads.
408 */
409
410 static int BufferedRead (FIO_File f, unsigned int nBytes, void * dest);
411
412 /*
413 HandleEscape - translates
414 and \t into their respective ascii codes.
415 */
416
417 static void HandleEscape (char *dest, unsigned int _dest_high, const char *src_, unsigned int _src_high, unsigned int *i, unsigned int *j, unsigned int HighSrc, unsigned int HighDest);
418
419 /*
420 Cast - casts a := b
421 */
422
423 static void Cast (unsigned char *a, unsigned int _a_high, const unsigned char *b_, unsigned int _b_high);
424
425 /*
426 StringFormat1 - converts string, src, into, dest, together with encapsulated
427 entity, w. It only formats the first %s or %d with n.
428 */
429
430 static void StringFormat1 (char *dest, unsigned int _dest_high, const char *src_, unsigned int _src_high, const unsigned char *w_, unsigned int _w_high);
431
432 /*
433 FormatError - provides a orthoganal counterpart to the procedure below.
434 */
435
436 static void FormatError (const char *a_, unsigned int _a_high);
437
438 /*
439 FormatError1 - generic error procedure taking standard format string
440 and single parameter.
441 */
442
443 static void FormatError1 (const char *a_, unsigned int _a_high, const unsigned char *w_, unsigned int _w_high);
444
445 /*
446 FormatError2 - generic error procedure taking standard format string
447 and two parameters.
448 */
449
450 static void FormatError2 (const char *a_, unsigned int _a_high, const unsigned char *w1_, unsigned int _w1_high, const unsigned char *w2_, unsigned int _w2_high);
451
452 /*
453 CheckAccess - checks to see whether a file f has been
454 opened for read/write.
455 */
456
457 static void CheckAccess (FIO_File f, FIO_FileUsage use, bool towrite);
458
459 /*
460 SetEndOfLine -
461 */
462
463 static void SetEndOfLine (FIO_File f, char ch);
464
465 /*
466 BufferedWrite - will write, nBytes, through the buffer.
467 Similar to WriteNBytes, but this function will always
468 write into the buffer before copying into memory.
469
470 Useful when performing small writes.
471 */
472
473 static int BufferedWrite (FIO_File f, unsigned int nBytes, void * src);
474
475 /*
476 PreInitialize - preinitialize the file descriptor.
477 */
478
479 static void PreInitialize (FIO_File f, const char *fname_, unsigned int _fname_high, FIO_FileStatus state, FIO_FileUsage use, bool towrite, int osfd, unsigned int bufsize);
480
481 /*
482 Init - initialize the modules, global variables.
483 */
484
485 static void Init (void);
486
487
488 /*
489 Max - returns the maximum of two values.
490 */
491
492 static unsigned int Max (unsigned int a, unsigned int b)
493 {
494 if (a > b)
495 {
496 return a;
497 }
498 else
499 {
500 return b;
501 }
502 /* static analysis guarentees a RETURN statement will be used before here. */
503 __builtin_unreachable ();
504 }
505
506
507 /*
508 Min - returns the minimum of two values.
509 */
510
511 static unsigned int Min (unsigned int a, unsigned int b)
512 {
513 if (a < b)
514 {
515 return a;
516 }
517 else
518 {
519 return b;
520 }
521 /* static analysis guarentees a RETURN statement will be used before here. */
522 __builtin_unreachable ();
523 }
524
525
526 /*
527 GetNextFreeDescriptor - returns the index to the FileInfo array indicating
528 the next free slot.
529 */
530
531 static FIO_File GetNextFreeDescriptor (void)
532 {
533 FIO_File f;
534 FIO_File h;
535 FIO_FileDescriptor fd;
536
537 f = Error+1;
538 h = Indexing_HighIndice (FileInfo);
539 for (;;)
540 {
541 if (f <= h)
542 {
543 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
544 if (fd == NULL)
545 {
546 return f;
547 }
548 }
549 f += 1;
550 if (f > h)
551 {
552 Indexing_PutIndice (FileInfo, f, NULL); /* create new slot */
553 return f; /* create new slot */
554 }
555 }
556 ReturnException ("../../gcc/m2/gm2-libs/FIO.def", 25, 1);
557 __builtin_unreachable ();
558 }
559
560
561 /*
562 SetState - sets the field, state, of file, f, to, s.
563 */
564
565 static void SetState (FIO_File f, FIO_FileStatus s)
566 {
567 FIO_FileDescriptor fd;
568
569 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
570 fd->state = s;
571 }
572
573
574 /*
575 InitializeFile - initialize a file descriptor
576 */
577
578 static FIO_File InitializeFile (FIO_File f, void * fname, unsigned int flength, FIO_FileStatus fstate, FIO_FileUsage use, bool towrite, unsigned int buflength)
579 {
580 FIO_PtrToChar p;
581 FIO_FileDescriptor fd;
582
583 Storage_ALLOCATE ((void **) &fd, sizeof (FIO_fds));
584 if (fd == NULL)
585 {
586 SetState (Error, FIO_outofmemory);
587 return Error;
588 }
589 else
590 {
591 Indexing_PutIndice (FileInfo, f, reinterpret_cast<void *> (fd));
592 fd->name.size = flength+1; /* need to guarantee the nul for C */
593 fd->usage = use; /* need to guarantee the nul for C */
594 fd->output = towrite;
595 Storage_ALLOCATE (&fd->name.address, fd->name.size);
596 if (fd->name.address == NULL)
597 {
598 fd->state = FIO_outofmemory;
599 return f;
600 }
601 fd->name.address = libc_strncpy (fd->name.address, fname, flength);
602 /* and assign nul to the last byte */
603 p = static_cast<FIO_PtrToChar> (fd->name.address);
604 p += flength;
605 (*p) = ASCII_nul;
606 fd->abspos = 0;
607 /* now for the buffer */
608 Storage_ALLOCATE ((void **) &fd->buffer, sizeof (FIO_buf));
609 if (fd->buffer == NULL)
610 {
611 SetState (Error, FIO_outofmemory);
612 return Error;
613 }
614 else
615 {
616 fd->buffer->valid = false;
617 fd->buffer->bufstart = 0;
618 fd->buffer->size = buflength;
619 fd->buffer->position = 0;
620 fd->buffer->filled = 0;
621 if (fd->buffer->size == 0)
622 {
623 fd->buffer->address = NULL;
624 }
625 else
626 {
627 Storage_ALLOCATE (&fd->buffer->address, fd->buffer->size);
628 if (fd->buffer->address == NULL)
629 {
630 fd->state = FIO_outofmemory;
631 return f;
632 }
633 }
634 if (towrite)
635 {
636 fd->buffer->left = fd->buffer->size;
637 }
638 else
639 {
640 fd->buffer->left = 0;
641 }
642 fd->buffer->contents = static_cast<FIO__T7 *> (fd->buffer->address); /* provides easy access for reading characters */
643 fd->state = fstate; /* provides easy access for reading characters */
644 }
645 }
646 return f;
647 /* static analysis guarentees a RETURN statement will be used before here. */
648 __builtin_unreachable ();
649 }
650
651
652 /*
653 ConnectToUnix - connects a FIO file to a UNIX file descriptor.
654 */
655
656 static void ConnectToUnix (FIO_File f, bool towrite, bool newfile)
657 {
658 FIO_FileDescriptor fd;
659
660 if (f != Error)
661 {
662 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
663 if (fd != NULL)
664 {
665 if (towrite)
666 {
667 if (newfile)
668 {
669 fd->unixfd = libc_creat (fd->name.address, CreatePermissions);
670 }
671 else
672 {
673 fd->unixfd = libc_open (fd->name.address, (int ) (wrapc_WriteOnly ()), 0);
674 }
675 }
676 else
677 {
678 fd->unixfd = libc_open (fd->name.address, (int ) (wrapc_ReadOnly ()), 0);
679 }
680 if (fd->unixfd < 0)
681 {
682 fd->state = FIO_connectionfailure;
683 }
684 }
685 }
686 }
687
688
689 /*
690 ReadFromBuffer - attempts to read, nBytes, from file, f.
691 It firstly consumes the buffer and then performs
692 direct unbuffered reads. This should only be used
693 when wishing to read large files.
694
695 The actual number of bytes read is returned.
696 -1 is returned if EOF is reached.
697 */
698
699 static int ReadFromBuffer (FIO_File f, void * a, unsigned int nBytes)
700 {
701 typedef unsigned char *ReadFromBuffer__T1;
702
703 void * t;
704 int result;
705 unsigned int total;
706 unsigned int n;
707 ReadFromBuffer__T1 p;
708 FIO_FileDescriptor fd;
709
710 if (f != Error)
711 {
712 total = 0; /* how many bytes have we read */
713 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f)); /* how many bytes have we read */
714 /* extract from the buffer first */
715 if ((fd->buffer != NULL) && fd->buffer->valid)
716 {
717 if (fd->buffer->left > 0)
718 {
719 /* avoid gcc warning by using compound statement even if not strictly necessary. */
720 if (nBytes == 1)
721 {
722 /* too expensive to call memcpy for 1 character */
723 p = static_cast<ReadFromBuffer__T1> (a);
724 (*p) = static_cast<unsigned char> ((*fd->buffer->contents).array[fd->buffer->position]);
725 fd->buffer->left -= 1; /* remove consumed bytes */
726 fd->buffer->position += 1; /* move onwards n bytes */
727 nBytes = 0; /* reduce the amount for future direct */
728 /* read */
729 return 1;
730 }
731 else
732 {
733 n = Min (fd->buffer->left, nBytes);
734 t = fd->buffer->address;
735 t = reinterpret_cast<void *> (reinterpret_cast<char *> (t)+fd->buffer->position);
736 p = static_cast<ReadFromBuffer__T1> (libc_memcpy (a, t, static_cast<size_t> (n)));
737 fd->buffer->left -= n; /* remove consumed bytes */
738 fd->buffer->position += n; /* move onwards n bytes */
739 /* move onwards ready for direct reads */
740 a = reinterpret_cast<void *> (reinterpret_cast<char *> (a)+n);
741 nBytes -= n; /* reduce the amount for future direct */
742 /* read */
743 total += n;
744 return total; /* much cleaner to return now, */
745 }
746 /* difficult to record an error if */
747 }
748 /* the read below returns -1 */
749 }
750 if (nBytes > 0)
751 {
752 /* still more to read */
753 result = static_cast<int> (libc_read (fd->unixfd, a, static_cast<size_t> ((int ) (nBytes))));
754 if (result > 0)
755 {
756 /* avoid dangling else. */
757 total += result;
758 fd->abspos += result;
759 /* now disable the buffer as we read directly into, a. */
760 if (fd->buffer != NULL)
761 {
762 fd->buffer->valid = false;
763 }
764 }
765 else
766 {
767 if (result == 0)
768 {
769 /* eof reached */
770 fd->state = FIO_endoffile;
771 }
772 else
773 {
774 fd->state = FIO_failed;
775 }
776 /* indicate buffer is empty */
777 if (fd->buffer != NULL)
778 {
779 fd->buffer->valid = false;
780 fd->buffer->left = 0;
781 fd->buffer->position = 0;
782 if (fd->buffer->address != NULL)
783 {
784 (*fd->buffer->contents).array[fd->buffer->position] = ASCII_nul;
785 }
786 }
787 return -1;
788 }
789 }
790 return total;
791 }
792 else
793 {
794 return -1;
795 }
796 /* static analysis guarentees a RETURN statement will be used before here. */
797 __builtin_unreachable ();
798 }
799
800
801 /*
802 BufferedRead - will read, nBytes, through the buffer.
803 Similar to ReadFromBuffer, but this function will always
804 read into the buffer before copying into memory.
805
806 Useful when performing small reads.
807 */
808
809 static int BufferedRead (FIO_File f, unsigned int nBytes, void * dest)
810 {
811 typedef unsigned char *BufferedRead__T3;
812
813 void * src;
814 int total;
815 int n;
816 BufferedRead__T3 p;
817 FIO_FileDescriptor fd;
818
819 if (f != Error)
820 {
821 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
822 total = 0; /* how many bytes have we read */
823 if (fd != NULL) /* how many bytes have we read */
824 {
825 /* extract from the buffer first */
826 if (fd->buffer != NULL)
827 {
828 while (nBytes > 0)
829 {
830 if ((fd->buffer->left > 0) && fd->buffer->valid)
831 {
832 if (nBytes == 1)
833 {
834 /* too expensive to call memcpy for 1 character */
835 p = static_cast<BufferedRead__T3> (dest);
836 (*p) = static_cast<unsigned char> ((*fd->buffer->contents).array[fd->buffer->position]);
837 fd->buffer->left -= 1; /* remove consumed byte */
838 fd->buffer->position += 1; /* move onwards n byte */
839 total += 1; /* move onwards n byte */
840 return total;
841 }
842 else
843 {
844 n = Min (fd->buffer->left, nBytes);
845 src = fd->buffer->address;
846 src = reinterpret_cast<void *> (reinterpret_cast<char *> (src)+fd->buffer->position);
847 p = static_cast<BufferedRead__T3> (libc_memcpy (dest, src, static_cast<size_t> (n)));
848 fd->buffer->left -= n; /* remove consumed bytes */
849 fd->buffer->position += n; /* move onwards n bytes */
850 /* move onwards ready for direct reads */
851 dest = reinterpret_cast<void *> (reinterpret_cast<char *> (dest)+n);
852 nBytes -= n; /* reduce the amount for future direct */
853 /* read */
854 total += n;
855 }
856 }
857 else
858 {
859 /* refill buffer */
860 n = static_cast<int> (libc_read (fd->unixfd, fd->buffer->address, static_cast<size_t> (fd->buffer->size)));
861 if (n >= 0)
862 {
863 /* avoid dangling else. */
864 fd->buffer->valid = true;
865 fd->buffer->position = 0;
866 fd->buffer->left = n;
867 fd->buffer->filled = n;
868 fd->buffer->bufstart = fd->abspos;
869 fd->abspos += n;
870 if (n == 0)
871 {
872 /* eof reached */
873 fd->state = FIO_endoffile;
874 return -1;
875 }
876 }
877 else
878 {
879 fd->buffer->valid = false;
880 fd->buffer->position = 0;
881 fd->buffer->left = 0;
882 fd->buffer->filled = 0;
883 fd->state = FIO_failed;
884 return total;
885 }
886 }
887 }
888 return total;
889 }
890 }
891 }
892 return -1;
893 /* static analysis guarentees a RETURN statement will be used before here. */
894 __builtin_unreachable ();
895 }
896
897
898 /*
899 HandleEscape - translates
900 and \t into their respective ascii codes.
901 */
902
903 static void HandleEscape (char *dest, unsigned int _dest_high, const char *src_, unsigned int _src_high, unsigned int *i, unsigned int *j, unsigned int HighSrc, unsigned int HighDest)
904 {
905 char src[_src_high+1];
906
907 /* make a local copy of each unbounded array. */
908 memcpy (src, src_, _src_high+1);
909
910 if (((((*i)+1) < HighSrc) && (src[(*i)] == '\\')) && ((*j) < HighDest))
911 {
912 /* avoid gcc warning by using compound statement even if not strictly necessary. */
913 if (src[(*i)+1] == 'n')
914 {
915 /* requires a newline */
916 const_cast<char *>(dest)[(*j)] = ASCII_nl;
917 (*j) += 1;
918 (*i) += 2;
919 }
920 else if (src[(*i)+1] == 't')
921 {
922 /* avoid dangling else. */
923 /* requires a tab (yuck) tempted to fake this but I better not.. */
924 const_cast<char *>(dest)[(*j)] = ASCII_tab;
925 (*j) += 1;
926 (*i) += 2;
927 }
928 else
929 {
930 /* avoid dangling else. */
931 /* copy escaped character */
932 (*i) += 1;
933 const_cast<char *>(dest)[(*j)] = src[(*i)];
934 (*j) += 1;
935 (*i) += 1;
936 }
937 }
938 }
939
940
941 /*
942 Cast - casts a := b
943 */
944
945 static void Cast (unsigned char *a, unsigned int _a_high, const unsigned char *b_, unsigned int _b_high)
946 {
947 unsigned int i;
948 unsigned char b[_b_high+1];
949
950 /* make a local copy of each unbounded array. */
951 memcpy (b, b_, _b_high+1);
952
953 if (_a_high == _b_high)
954 {
955 for (i=0; i<=_a_high; i++)
956 {
957 const_cast<unsigned char *>(a)[i] = b[i];
958 }
959 }
960 else
961 {
962 FormatError ((const char *) "cast failed", 11);
963 }
964 }
965
966
967 /*
968 StringFormat1 - converts string, src, into, dest, together with encapsulated
969 entity, w. It only formats the first %s or %d with n.
970 */
971
972 static void StringFormat1 (char *dest, unsigned int _dest_high, const char *src_, unsigned int _src_high, const unsigned char *w_, unsigned int _w_high)
973 {
974 typedef struct StringFormat1__T8_a StringFormat1__T8;
975
976 typedef char *StringFormat1__T4;
977
978 struct StringFormat1__T8_a { char array[MaxErrorString+1]; };
979 unsigned int HighSrc;
980 unsigned int HighDest;
981 unsigned int c;
982 unsigned int i;
983 unsigned int j;
984 StringFormat1__T8 str;
985 StringFormat1__T4 p;
986 char src[_src_high+1];
987 unsigned char w[_w_high+1];
988
989 /* make a local copy of each unbounded array. */
990 memcpy (src, src_, _src_high+1);
991 memcpy (w, w_, _w_high+1);
992
993 HighSrc = StrLib_StrLen ((const char *) src, _src_high);
994 HighDest = _dest_high;
995 p = NULL;
996 c = 0;
997 i = 0;
998 j = 0;
999 while ((((i < HighSrc) && (src[i] != ASCII_nul)) && (j < HighDest)) && (src[i] != '%'))
1000 {
1001 if (src[i] == '\\')
1002 {
1003 HandleEscape ((char *) dest, _dest_high, (const char *) src, _src_high, &i, &j, HighSrc, HighDest);
1004 }
1005 else
1006 {
1007 const_cast<char *>(dest)[j] = src[i];
1008 i += 1;
1009 j += 1;
1010 }
1011 }
1012 if ((((i+1) < HighSrc) && (src[i] == '%')) && (j < HighDest))
1013 {
1014 /* avoid gcc warning by using compound statement even if not strictly necessary. */
1015 if (src[i+1] == 's')
1016 {
1017 Cast ((unsigned char *) &p, (sizeof (p)-1), (const unsigned char *) w, _w_high);
1018 while ((j < HighDest) && ((*p) != ASCII_nul))
1019 {
1020 const_cast<char *>(dest)[j] = (*p);
1021 j += 1;
1022 p += 1;
1023 }
1024 if (j < HighDest)
1025 {
1026 const_cast<char *>(dest)[j] = ASCII_nul;
1027 }
1028 j = StrLib_StrLen ((const char *) dest, _dest_high);
1029 i += 2;
1030 }
1031 else if (src[i+1] == 'd')
1032 {
1033 /* avoid dangling else. */
1034 const_cast<char *>(dest)[j] = ASCII_nul;
1035 Cast ((unsigned char *) &c, (sizeof (c)-1), (const unsigned char *) w, _w_high);
1036 NumberIO_CardToStr (c, 0, (char *) &str.array[0], MaxErrorString);
1037 StrLib_StrConCat ((const char *) dest, _dest_high, (const char *) &str.array[0], MaxErrorString, (char *) dest, _dest_high);
1038 j = StrLib_StrLen ((const char *) dest, _dest_high);
1039 i += 2;
1040 }
1041 else
1042 {
1043 /* avoid dangling else. */
1044 const_cast<char *>(dest)[j] = src[i];
1045 i += 1;
1046 j += 1;
1047 }
1048 }
1049 /* and finish off copying src into dest */
1050 while (((i < HighSrc) && (src[i] != ASCII_nul)) && (j < HighDest))
1051 {
1052 if (src[i] == '\\')
1053 {
1054 HandleEscape ((char *) dest, _dest_high, (const char *) src, _src_high, &i, &j, HighSrc, HighDest);
1055 }
1056 else
1057 {
1058 const_cast<char *>(dest)[j] = src[i];
1059 i += 1;
1060 j += 1;
1061 }
1062 }
1063 if (j < HighDest)
1064 {
1065 const_cast<char *>(dest)[j] = ASCII_nul;
1066 }
1067 }
1068
1069
1070 /*
1071 FormatError - provides a orthoganal counterpart to the procedure below.
1072 */
1073
1074 static void FormatError (const char *a_, unsigned int _a_high)
1075 {
1076 char a[_a_high+1];
1077
1078 /* make a local copy of each unbounded array. */
1079 memcpy (a, a_, _a_high+1);
1080
1081 FIO_WriteString (FIO_StdErr, (const char *) a, _a_high);
1082 }
1083
1084
1085 /*
1086 FormatError1 - generic error procedure taking standard format string
1087 and single parameter.
1088 */
1089
1090 static void FormatError1 (const char *a_, unsigned int _a_high, const unsigned char *w_, unsigned int _w_high)
1091 {
1092 typedef struct FormatError1__T9_a FormatError1__T9;
1093
1094 struct FormatError1__T9_a { char array[MaxErrorString+1]; };
1095 FormatError1__T9 s;
1096 char a[_a_high+1];
1097 unsigned char w[_w_high+1];
1098
1099 /* make a local copy of each unbounded array. */
1100 memcpy (a, a_, _a_high+1);
1101 memcpy (w, w_, _w_high+1);
1102
1103 StringFormat1 ((char *) &s.array[0], MaxErrorString, (const char *) a, _a_high, (const unsigned char *) w, _w_high);
1104 FormatError ((const char *) &s.array[0], MaxErrorString);
1105 }
1106
1107
1108 /*
1109 FormatError2 - generic error procedure taking standard format string
1110 and two parameters.
1111 */
1112
1113 static void FormatError2 (const char *a_, unsigned int _a_high, const unsigned char *w1_, unsigned int _w1_high, const unsigned char *w2_, unsigned int _w2_high)
1114 {
1115 typedef struct FormatError2__T10_a FormatError2__T10;
1116
1117 struct FormatError2__T10_a { char array[MaxErrorString+1]; };
1118 FormatError2__T10 s;
1119 char a[_a_high+1];
1120 unsigned char w1[_w1_high+1];
1121 unsigned char w2[_w2_high+1];
1122
1123 /* make a local copy of each unbounded array. */
1124 memcpy (a, a_, _a_high+1);
1125 memcpy (w1, w1_, _w1_high+1);
1126 memcpy (w2, w2_, _w2_high+1);
1127
1128 StringFormat1 ((char *) &s.array[0], MaxErrorString, (const char *) a, _a_high, (const unsigned char *) w1, _w1_high);
1129 FormatError1 ((const char *) &s.array[0], MaxErrorString, (const unsigned char *) w2, _w2_high);
1130 }
1131
1132
1133 /*
1134 CheckAccess - checks to see whether a file f has been
1135 opened for read/write.
1136 */
1137
1138 static void CheckAccess (FIO_File f, FIO_FileUsage use, bool towrite)
1139 {
1140 FIO_FileDescriptor fd;
1141
1142 if (f != Error)
1143 {
1144 /* avoid dangling else. */
1145 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
1146 if (fd == NULL)
1147 {
1148 if (f != FIO_StdErr)
1149 {
1150 FormatError ((const char *) "this file has probably been closed and not reopened successfully or alternatively never opened\\n", 96);
1151 }
1152 M2RTS_HALT (-1);
1153 __builtin_unreachable ();
1154 }
1155 else
1156 {
1157 if ((use == FIO_openedforwrite) && (fd->usage == FIO_openedforread))
1158 {
1159 FormatError1 ((const char *) "this file (%s) has been opened for reading but is now being written\\n", 69, (const unsigned char *) &fd->name.address, (sizeof (fd->name.address)-1));
1160 M2RTS_HALT (-1);
1161 __builtin_unreachable ();
1162 }
1163 else if ((use == FIO_openedforread) && (fd->usage == FIO_openedforwrite))
1164 {
1165 /* avoid dangling else. */
1166 FormatError1 ((const char *) "this file (%s) has been opened for writing but is now being read\\n", 66, (const unsigned char *) &fd->name.address, (sizeof (fd->name.address)-1));
1167 M2RTS_HALT (-1);
1168 __builtin_unreachable ();
1169 }
1170 else if (fd->state == FIO_connectionfailure)
1171 {
1172 /* avoid dangling else. */
1173 FormatError1 ((const char *) "this file (%s) was not successfully opened\\n", 44, (const unsigned char *) &fd->name.address, (sizeof (fd->name.address)-1));
1174 M2RTS_HALT (-1);
1175 __builtin_unreachable ();
1176 }
1177 else if (towrite != fd->output)
1178 {
1179 /* avoid dangling else. */
1180 if (fd->output)
1181 {
1182 FormatError1 ((const char *) "this file (%s) was opened for writing but is now being read\\n", 61, (const unsigned char *) &fd->name.address, (sizeof (fd->name.address)-1));
1183 M2RTS_HALT (-1);
1184 __builtin_unreachable ();
1185 }
1186 else
1187 {
1188 FormatError1 ((const char *) "this file (%s) was opened for reading but is now being written\\n", 64, (const unsigned char *) &fd->name.address, (sizeof (fd->name.address)-1));
1189 M2RTS_HALT (-1);
1190 __builtin_unreachable ();
1191 }
1192 }
1193 }
1194 }
1195 else
1196 {
1197 FormatError ((const char *) "this file has not been opened successfully\\n", 44);
1198 M2RTS_HALT (-1);
1199 __builtin_unreachable ();
1200 }
1201 }
1202
1203
1204 /*
1205 SetEndOfLine -
1206 */
1207
1208 static void SetEndOfLine (FIO_File f, char ch)
1209 {
1210 FIO_FileDescriptor fd;
1211
1212 CheckAccess (f, FIO_openedforread, false);
1213 if (f != Error)
1214 {
1215 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
1216 if (ch == ASCII_nl)
1217 {
1218 fd->state = FIO_endofline;
1219 }
1220 else
1221 {
1222 fd->state = FIO_successful;
1223 }
1224 }
1225 }
1226
1227
1228 /*
1229 BufferedWrite - will write, nBytes, through the buffer.
1230 Similar to WriteNBytes, but this function will always
1231 write into the buffer before copying into memory.
1232
1233 Useful when performing small writes.
1234 */
1235
1236 static int BufferedWrite (FIO_File f, unsigned int nBytes, void * src)
1237 {
1238 typedef unsigned char *BufferedWrite__T5;
1239
1240 void * dest;
1241 int total;
1242 int n;
1243 BufferedWrite__T5 p;
1244 FIO_FileDescriptor fd;
1245
1246 if (f != Error)
1247 {
1248 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
1249 if (fd != NULL)
1250 {
1251 total = 0; /* how many bytes have we read */
1252 if (fd->buffer != NULL) /* how many bytes have we read */
1253 {
1254 /* place into the buffer first */
1255 while (nBytes > 0)
1256 {
1257 if (fd->buffer->left > 0)
1258 {
1259 if (nBytes == 1)
1260 {
1261 /* too expensive to call memcpy for 1 character */
1262 p = static_cast<BufferedWrite__T5> (src);
1263 (*fd->buffer->contents).array[fd->buffer->position] = static_cast<char> ((*p));
1264 fd->buffer->left -= 1; /* reduce space */
1265 fd->buffer->position += 1; /* move onwards n byte */
1266 total += 1; /* move onwards n byte */
1267 return total;
1268 }
1269 else
1270 {
1271 n = Min (fd->buffer->left, nBytes);
1272 dest = fd->buffer->address;
1273 dest = reinterpret_cast<void *> (reinterpret_cast<char *> (dest)+fd->buffer->position);
1274 p = static_cast<BufferedWrite__T5> (libc_memcpy (dest, src, static_cast<size_t> ((unsigned int ) (n))));
1275 fd->buffer->left -= n; /* remove consumed bytes */
1276 fd->buffer->position += n; /* move onwards n bytes */
1277 /* move ready for further writes */
1278 src = reinterpret_cast<void *> (reinterpret_cast<char *> (src)+n);
1279 nBytes -= n; /* reduce the amount for future writes */
1280 total += n; /* reduce the amount for future writes */
1281 }
1282 }
1283 else
1284 {
1285 FIO_FlushBuffer (f);
1286 if ((fd->state != FIO_successful) && (fd->state != FIO_endofline))
1287 {
1288 nBytes = 0;
1289 }
1290 }
1291 }
1292 return total;
1293 }
1294 }
1295 }
1296 return -1;
1297 /* static analysis guarentees a RETURN statement will be used before here. */
1298 __builtin_unreachable ();
1299 }
1300
1301
1302 /*
1303 PreInitialize - preinitialize the file descriptor.
1304 */
1305
1306 static void PreInitialize (FIO_File f, const char *fname_, unsigned int _fname_high, FIO_FileStatus state, FIO_FileUsage use, bool towrite, int osfd, unsigned int bufsize)
1307 {
1308 FIO_FileDescriptor fd;
1309 FIO_FileDescriptor fe;
1310 char fname[_fname_high+1];
1311
1312 /* make a local copy of each unbounded array. */
1313 memcpy (fname, fname_, _fname_high+1);
1314
1315 if ((InitializeFile (f, const_cast<void*> (static_cast<const void*>(fname)), StrLib_StrLen ((const char *) fname, _fname_high), state, use, towrite, bufsize)) == f)
1316 {
1317 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
1318 if (f == Error)
1319 {
1320 fe = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, FIO_StdErr));
1321 if (fe == NULL)
1322 {
1323 M2RTS_HALT (-1);
1324 __builtin_unreachable ();
1325 }
1326 else
1327 {
1328 fd->unixfd = fe->unixfd; /* the error channel */
1329 }
1330 }
1331 else
1332 {
1333 fd->unixfd = osfd;
1334 }
1335 }
1336 else
1337 {
1338 M2RTS_HALT (-1);
1339 __builtin_unreachable ();
1340 }
1341 }
1342
1343
1344 /*
1345 Init - initialize the modules, global variables.
1346 */
1347
1348 static void Init (void)
1349 {
1350 FileInfo = Indexing_InitIndex (0);
1351 Error = 0;
1352 PreInitialize (Error, (const char *) "error", 5, FIO_toomanyfilesopen, FIO_unused, false, -1, 0);
1353 FIO_StdIn = 1;
1354 PreInitialize (FIO_StdIn, (const char *) "<stdin>", 7, FIO_successful, FIO_openedforread, false, 0, MaxBufferLength);
1355 FIO_StdOut = 2;
1356 PreInitialize (FIO_StdOut, (const char *) "<stdout>", 8, FIO_successful, FIO_openedforwrite, true, 1, MaxBufferLength);
1357 FIO_StdErr = 3;
1358 PreInitialize (FIO_StdErr, (const char *) "<stderr>", 8, FIO_successful, FIO_openedforwrite, true, 2, MaxBufferLength);
1359 if (! (M2RTS_InstallTerminationProcedure ((PROC ) {(PROC_t) FIO_FlushOutErr})))
1360 {
1361 M2RTS_HALT (-1);
1362 __builtin_unreachable ();
1363 }
1364 }
1365
1366
1367 /*
1368 IsNoError - returns a TRUE if no error has occured on file, f.
1369 */
1370
1371 extern "C" bool FIO_IsNoError (FIO_File f)
1372 {
1373 FIO_FileDescriptor fd;
1374
1375 if (f == Error)
1376 {
1377 return false;
1378 }
1379 else
1380 {
1381 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
1382 return (fd != NULL) && (((fd->state == FIO_successful) || (fd->state == FIO_endoffile)) || (fd->state == FIO_endofline));
1383 }
1384 /* static analysis guarentees a RETURN statement will be used before here. */
1385 __builtin_unreachable ();
1386 }
1387
1388
1389 /*
1390 IsActive - returns TRUE if the file, f, is still active.
1391 */
1392
1393 extern "C" bool FIO_IsActive (FIO_File f)
1394 {
1395 if (f == Error)
1396 {
1397 return false;
1398 }
1399 else
1400 {
1401 return (Indexing_GetIndice (FileInfo, f)) != NULL;
1402 }
1403 /* static analysis guarentees a RETURN statement will be used before here. */
1404 __builtin_unreachable ();
1405 }
1406
1407 extern "C" bool FIO_Exists (const char *fname_, unsigned int _fname_high)
1408 {
1409 char fname[_fname_high+1];
1410
1411 /* make a local copy of each unbounded array. */
1412 memcpy (fname, fname_, _fname_high+1);
1413
1414 /*
1415 The following functions are wrappers for the above.
1416 */
1417 return FIO_exists (const_cast<void*> (static_cast<const void*>(fname)), StrLib_StrLen ((const char *) fname, _fname_high));
1418 /* static analysis guarentees a RETURN statement will be used before here. */
1419 __builtin_unreachable ();
1420 }
1421
1422 extern "C" FIO_File FIO_OpenToRead (const char *fname_, unsigned int _fname_high)
1423 {
1424 char fname[_fname_high+1];
1425
1426 /* make a local copy of each unbounded array. */
1427 memcpy (fname, fname_, _fname_high+1);
1428
1429 return FIO_openToRead (const_cast<void*> (static_cast<const void*>(fname)), StrLib_StrLen ((const char *) fname, _fname_high));
1430 /* static analysis guarentees a RETURN statement will be used before here. */
1431 __builtin_unreachable ();
1432 }
1433
1434 extern "C" FIO_File FIO_OpenToWrite (const char *fname_, unsigned int _fname_high)
1435 {
1436 char fname[_fname_high+1];
1437
1438 /* make a local copy of each unbounded array. */
1439 memcpy (fname, fname_, _fname_high+1);
1440
1441 return FIO_openToWrite (const_cast<void*> (static_cast<const void*>(fname)), StrLib_StrLen ((const char *) fname, _fname_high));
1442 /* static analysis guarentees a RETURN statement will be used before here. */
1443 __builtin_unreachable ();
1444 }
1445
1446 extern "C" FIO_File FIO_OpenForRandom (const char *fname_, unsigned int _fname_high, bool towrite, bool newfile)
1447 {
1448 char fname[_fname_high+1];
1449
1450 /* make a local copy of each unbounded array. */
1451 memcpy (fname, fname_, _fname_high+1);
1452
1453 return FIO_openForRandom (const_cast<void*> (static_cast<const void*>(fname)), StrLib_StrLen ((const char *) fname, _fname_high), towrite, newfile);
1454 /* static analysis guarentees a RETURN statement will be used before here. */
1455 __builtin_unreachable ();
1456 }
1457
1458
1459 /*
1460 Close - close a file which has been previously opened using:
1461 OpenToRead, OpenToWrite, OpenForRandom.
1462 It is correct to close a file which has an error status.
1463 */
1464
1465 extern "C" void FIO_Close (FIO_File f)
1466 {
1467 FIO_FileDescriptor fd;
1468
1469 if (f != Error)
1470 {
1471 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
1472 /*
1473 we allow users to close files which have an error status
1474 */
1475 if (fd != NULL)
1476 {
1477 FIO_FlushBuffer (f);
1478 if (fd->unixfd >= 0)
1479 {
1480 if ((libc_close (fd->unixfd)) != 0)
1481 {
1482 FormatError1 ((const char *) "failed to close file (%s)\\n", 27, (const unsigned char *) &fd->name.address, (sizeof (fd->name.address)-1));
1483 fd->state = FIO_failed; /* --fixme-- too late to notify user (unless we return a BOOLEAN) */
1484 }
1485 }
1486 if (fd->name.address != NULL)
1487 {
1488 Storage_DEALLOCATE (&fd->name.address, fd->name.size);
1489 }
1490 if (fd->buffer != NULL)
1491 {
1492 if (fd->buffer->address != NULL)
1493 {
1494 Storage_DEALLOCATE (&fd->buffer->address, fd->buffer->size);
1495 }
1496 Storage_DEALLOCATE ((void **) &fd->buffer, sizeof (FIO_buf));
1497 fd->buffer = NULL;
1498 }
1499 Storage_DEALLOCATE ((void **) &fd, sizeof (FIO_fds));
1500 Indexing_PutIndice (FileInfo, f, NULL);
1501 }
1502 }
1503 }
1504
1505
1506 /*
1507 exists - returns TRUE if a file named, fname exists for reading.
1508 */
1509
1510 extern "C" bool FIO_exists (void * fname, unsigned int flength)
1511 {
1512 FIO_File f;
1513
1514 f = FIO_openToRead (fname, flength);
1515 if (FIO_IsNoError (f))
1516 {
1517 FIO_Close (f);
1518 return true;
1519 }
1520 else
1521 {
1522 FIO_Close (f);
1523 return false;
1524 }
1525 /* static analysis guarentees a RETURN statement will be used before here. */
1526 __builtin_unreachable ();
1527 }
1528
1529
1530 /*
1531 openToRead - attempts to open a file, fname, for reading and
1532 it returns this file.
1533 The success of this operation can be checked by
1534 calling IsNoError.
1535 */
1536
1537 extern "C" FIO_File FIO_openToRead (void * fname, unsigned int flength)
1538 {
1539 FIO_File f;
1540
1541 f = GetNextFreeDescriptor ();
1542 if (f == Error)
1543 {
1544 SetState (f, FIO_toomanyfilesopen);
1545 }
1546 else
1547 {
1548 f = InitializeFile (f, fname, flength, FIO_successful, FIO_openedforread, false, MaxBufferLength);
1549 ConnectToUnix (f, false, false);
1550 }
1551 return f;
1552 /* static analysis guarentees a RETURN statement will be used before here. */
1553 __builtin_unreachable ();
1554 }
1555
1556
1557 /*
1558 openToWrite - attempts to open a file, fname, for write and
1559 it returns this file.
1560 The success of this operation can be checked by
1561 calling IsNoError.
1562 */
1563
1564 extern "C" FIO_File FIO_openToWrite (void * fname, unsigned int flength)
1565 {
1566 FIO_File f;
1567
1568 f = GetNextFreeDescriptor ();
1569 if (f == Error)
1570 {
1571 SetState (f, FIO_toomanyfilesopen);
1572 }
1573 else
1574 {
1575 f = InitializeFile (f, fname, flength, FIO_successful, FIO_openedforwrite, true, MaxBufferLength);
1576 ConnectToUnix (f, true, true);
1577 }
1578 return f;
1579 /* static analysis guarentees a RETURN statement will be used before here. */
1580 __builtin_unreachable ();
1581 }
1582
1583
1584 /*
1585 openForRandom - attempts to open a file, fname, for random access
1586 read or write and it returns this file.
1587 The success of this operation can be checked by
1588 calling IsNoError.
1589 towrite, determines whether the file should be
1590 opened for writing or reading.
1591 */
1592
1593 extern "C" FIO_File FIO_openForRandom (void * fname, unsigned int flength, bool towrite, bool newfile)
1594 {
1595 FIO_File f;
1596
1597 f = GetNextFreeDescriptor ();
1598 if (f == Error)
1599 {
1600 SetState (f, FIO_toomanyfilesopen);
1601 }
1602 else
1603 {
1604 f = InitializeFile (f, fname, flength, FIO_successful, FIO_openedforrandom, towrite, MaxBufferLength);
1605 ConnectToUnix (f, towrite, newfile);
1606 }
1607 return f;
1608 /* static analysis guarentees a RETURN statement will be used before here. */
1609 __builtin_unreachable ();
1610 }
1611
1612
1613 /*
1614 FlushBuffer - flush contents of file, f.
1615 */
1616
1617 extern "C" void FIO_FlushBuffer (FIO_File f)
1618 {
1619 FIO_FileDescriptor fd;
1620
1621 if (f != Error)
1622 {
1623 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
1624 if (fd != NULL)
1625 {
1626 if (fd->output && (fd->buffer != NULL))
1627 {
1628 if ((fd->buffer->position == 0) || ((libc_write (fd->unixfd, fd->buffer->address, static_cast<size_t> (fd->buffer->position))) == ((int ) (fd->buffer->position))))
1629 {
1630 fd->abspos += fd->buffer->position;
1631 fd->buffer->bufstart = fd->abspos;
1632 fd->buffer->position = 0;
1633 fd->buffer->filled = 0;
1634 fd->buffer->left = fd->buffer->size;
1635 }
1636 else
1637 {
1638 fd->state = FIO_failed;
1639 }
1640 }
1641 }
1642 }
1643 }
1644
1645
1646 /*
1647 ReadNBytes - reads nBytes of a file into memory area, dest, returning
1648 the number of bytes actually read.
1649 This function will consume from the buffer and then
1650 perform direct libc reads. It is ideal for large reads.
1651 */
1652
1653 extern "C" unsigned int FIO_ReadNBytes (FIO_File f, unsigned int nBytes, void * dest)
1654 {
1655 typedef char *ReadNBytes__T2;
1656
1657 int n;
1658 ReadNBytes__T2 p;
1659
1660 if (f != Error)
1661 {
1662 CheckAccess (f, FIO_openedforread, false);
1663 n = ReadFromBuffer (f, dest, nBytes);
1664 if (n <= 0)
1665 {
1666 return 0;
1667 }
1668 else
1669 {
1670 p = static_cast<ReadNBytes__T2> (dest);
1671 p += n-1;
1672 SetEndOfLine (f, (*p));
1673 return n;
1674 }
1675 }
1676 else
1677 {
1678 return 0;
1679 }
1680 /* static analysis guarentees a RETURN statement will be used before here. */
1681 __builtin_unreachable ();
1682 }
1683
1684
1685 /*
1686 ReadAny - reads HIGH (a) + 1 bytes into, a. All input
1687 is fully buffered, unlike ReadNBytes and thus is more
1688 suited to small reads.
1689 */
1690
1691 extern "C" void FIO_ReadAny (FIO_File f, unsigned char *a, unsigned int _a_high)
1692 {
1693 CheckAccess (f, FIO_openedforread, false);
1694 if ((BufferedRead (f, _a_high+1, a)) == ((int ) (_a_high+1)))
1695 {
1696 SetEndOfLine (f, static_cast<char> (a[_a_high]));
1697 }
1698 }
1699
1700
1701 /*
1702 WriteNBytes - writes nBytes from memory area src to a file
1703 returning the number of bytes actually written.
1704 This function will flush the buffer and then
1705 write the nBytes using a direct write from libc.
1706 It is ideal for large writes.
1707 */
1708
1709 extern "C" unsigned int FIO_WriteNBytes (FIO_File f, unsigned int nBytes, void * src)
1710 {
1711 int total;
1712 FIO_FileDescriptor fd;
1713
1714 CheckAccess (f, FIO_openedforwrite, true);
1715 FIO_FlushBuffer (f);
1716 if (f != Error)
1717 {
1718 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
1719 if (fd != NULL)
1720 {
1721 total = static_cast<int> (libc_write (fd->unixfd, src, static_cast<size_t> ((int ) (nBytes))));
1722 if (total < 0)
1723 {
1724 fd->state = FIO_failed;
1725 return 0;
1726 }
1727 else
1728 {
1729 fd->abspos += (unsigned int ) (total);
1730 if (fd->buffer != NULL)
1731 {
1732 fd->buffer->bufstart = fd->abspos;
1733 }
1734 return (unsigned int ) (total);
1735 }
1736 }
1737 }
1738 return 0;
1739 /* static analysis guarentees a RETURN statement will be used before here. */
1740 __builtin_unreachable ();
1741 }
1742
1743
1744 /*
1745 WriteAny - writes HIGH (a) + 1 bytes onto, file, f. All output
1746 is fully buffered, unlike WriteNBytes and thus is more
1747 suited to small writes.
1748 */
1749
1750 extern "C" void FIO_WriteAny (FIO_File f, unsigned char *a, unsigned int _a_high)
1751 {
1752 CheckAccess (f, FIO_openedforwrite, true);
1753 if ((BufferedWrite (f, _a_high+1, a)) == ((int ) (_a_high+1)))
1754 {} /* empty. */
1755 }
1756
1757
1758 /*
1759 WriteChar - writes a single character to file, f.
1760 */
1761
1762 extern "C" void FIO_WriteChar (FIO_File f, char ch)
1763 {
1764 CheckAccess (f, FIO_openedforwrite, true);
1765 if ((BufferedWrite (f, sizeof (ch), &ch)) == ((int ) (sizeof (ch))))
1766 {} /* empty. */
1767 }
1768
1769
1770 /*
1771 EOF - tests to see whether a file, f, has reached end of file.
1772 */
1773
1774 extern "C" bool FIO_EOF (FIO_File f)
1775 {
1776 FIO_FileDescriptor fd;
1777
1778 CheckAccess (f, FIO_openedforread, false);
1779 if (f != Error)
1780 {
1781 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
1782 if (fd != NULL)
1783 {
1784 return fd->state == FIO_endoffile;
1785 }
1786 }
1787 return true;
1788 /* static analysis guarentees a RETURN statement will be used before here. */
1789 __builtin_unreachable ();
1790 }
1791
1792
1793 /*
1794 EOLN - tests to see whether a file, f, is upon a newline.
1795 It does NOT consume the newline.
1796 */
1797
1798 extern "C" bool FIO_EOLN (FIO_File f)
1799 {
1800 char ch;
1801 FIO_FileDescriptor fd;
1802
1803 CheckAccess (f, FIO_openedforread, false);
1804 /*
1805 we will read a character and then push it back onto the input stream,
1806 having noted the file status, we also reset the status.
1807 */
1808 if (f != Error)
1809 {
1810 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
1811 if (fd != NULL)
1812 {
1813 if ((fd->state == FIO_successful) || (fd->state == FIO_endofline))
1814 {
1815 ch = FIO_ReadChar (f);
1816 if ((fd->state == FIO_successful) || (fd->state == FIO_endofline))
1817 {
1818 FIO_UnReadChar (f, ch);
1819 }
1820 return ch == ASCII_nl;
1821 }
1822 }
1823 }
1824 return false;
1825 /* static analysis guarentees a RETURN statement will be used before here. */
1826 __builtin_unreachable ();
1827 }
1828
1829
1830 /*
1831 WasEOLN - tests to see whether a file, f, has just seen a newline.
1832 */
1833
1834 extern "C" bool FIO_WasEOLN (FIO_File f)
1835 {
1836 FIO_FileDescriptor fd;
1837
1838 CheckAccess (f, FIO_openedforread, false);
1839 if (f == Error)
1840 {
1841 return false;
1842 }
1843 else
1844 {
1845 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
1846 return (fd != NULL) && (fd->state == FIO_endofline);
1847 }
1848 /* static analysis guarentees a RETURN statement will be used before here. */
1849 __builtin_unreachable ();
1850 }
1851
1852
1853 /*
1854 ReadChar - returns a character read from file f.
1855 Sensible to check with IsNoError or EOF after calling
1856 this function.
1857 */
1858
1859 extern "C" char FIO_ReadChar (FIO_File f)
1860 {
1861 char ch;
1862
1863 CheckAccess (f, FIO_openedforread, false);
1864 if ((BufferedRead (f, sizeof (ch), &ch)) == ((int ) (sizeof (ch))))
1865 {
1866 SetEndOfLine (f, ch);
1867 return ch;
1868 }
1869 else
1870 {
1871 return ASCII_nul;
1872 }
1873 /* static analysis guarentees a RETURN statement will be used before here. */
1874 __builtin_unreachable ();
1875 }
1876
1877
1878 /*
1879 UnReadChar - replaces a character, ch, back into file f.
1880 This character must have been read by ReadChar
1881 and it does not allow successive calls. It may
1882 only be called if the previous read was successful
1883 or end of file was seen.
1884 If the state was previously endoffile then it
1885 is altered to successful.
1886 Otherwise it is left alone.
1887 */
1888
1889 extern "C" void FIO_UnReadChar (FIO_File f, char ch)
1890 {
1891 FIO_FileDescriptor fd;
1892 unsigned int n;
1893 void * a;
1894 void * b;
1895
1896 CheckAccess (f, FIO_openedforread, false);
1897 if (f != Error)
1898 {
1899 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
1900 if (((fd->state == FIO_successful) || (fd->state == FIO_endoffile)) || (fd->state == FIO_endofline))
1901 {
1902 /* avoid dangling else. */
1903 if ((fd->buffer != NULL) && fd->buffer->valid)
1904 {
1905 /* we assume that a ReadChar has occurred, we will check just in case. */
1906 if (fd->state == FIO_endoffile)
1907 {
1908 fd->buffer->position = MaxBufferLength;
1909 fd->buffer->left = 0;
1910 fd->buffer->filled = 0;
1911 fd->state = FIO_successful;
1912 }
1913 if (fd->buffer->position > 0)
1914 {
1915 fd->buffer->position -= 1;
1916 fd->buffer->left += 1;
1917 (*fd->buffer->contents).array[fd->buffer->position] = ch;
1918 }
1919 else
1920 {
1921 /* if possible make room and store ch */
1922 if (fd->buffer->filled == fd->buffer->size)
1923 {
1924 FormatError1 ((const char *) "performing too many UnReadChar calls on file (%d)\\n", 51, (const unsigned char *) &f, (sizeof (f)-1));
1925 }
1926 else
1927 {
1928 n = fd->buffer->filled-fd->buffer->position;
1929 b = &(*fd->buffer->contents).array[fd->buffer->position];
1930 a = &(*fd->buffer->contents).array[fd->buffer->position+1];
1931 a = libc_memcpy (a, b, static_cast<size_t> (n));
1932 fd->buffer->filled += 1;
1933 (*fd->buffer->contents).array[fd->buffer->position] = ch;
1934 }
1935 }
1936 }
1937 }
1938 else
1939 {
1940 FormatError1 ((const char *) "UnReadChar can only be called if the previous read was successful or end of file, error on file (%d)\\n", 102, (const unsigned char *) &f, (sizeof (f)-1));
1941 }
1942 }
1943 }
1944
1945
1946 /*
1947 WriteLine - writes out a linefeed to file, f.
1948 */
1949
1950 extern "C" void FIO_WriteLine (FIO_File f)
1951 {
1952 FIO_WriteChar (f, ASCII_nl);
1953 }
1954
1955
1956 /*
1957 WriteString - writes a string to file, f.
1958 */
1959
1960 extern "C" void FIO_WriteString (FIO_File f, const char *a_, unsigned int _a_high)
1961 {
1962 unsigned int l;
1963 char a[_a_high+1];
1964
1965 /* make a local copy of each unbounded array. */
1966 memcpy (a, a_, _a_high+1);
1967
1968 l = StrLib_StrLen ((const char *) a, _a_high);
1969 if ((FIO_WriteNBytes (f, l, const_cast<void*> (static_cast<const void*>(a)))) != l)
1970 {} /* empty. */
1971 }
1972
1973
1974 /*
1975 ReadString - reads a string from file, f, into string, a.
1976 It terminates the string if HIGH is reached or
1977 if a newline is seen or an error occurs.
1978 */
1979
1980 extern "C" void FIO_ReadString (FIO_File f, char *a, unsigned int _a_high)
1981 {
1982 unsigned int high;
1983 unsigned int i;
1984 char ch;
1985
1986 CheckAccess (f, FIO_openedforread, false);
1987 high = _a_high;
1988 i = 0;
1989 do {
1990 ch = FIO_ReadChar (f);
1991 if (i <= high)
1992 {
1993 /* avoid gcc warning by using compound statement even if not strictly necessary. */
1994 if (((ch == ASCII_nl) || (! (FIO_IsNoError (f)))) || (FIO_EOF (f)))
1995 {
1996 const_cast<char *>(a)[i] = ASCII_nul;
1997 i += 1;
1998 }
1999 else
2000 {
2001 const_cast<char *>(a)[i] = ch;
2002 i += 1;
2003 }
2004 }
2005 } while (! ((((ch == ASCII_nl) || (i > high)) || (! (FIO_IsNoError (f)))) || (FIO_EOF (f))));
2006 }
2007
2008
2009 /*
2010 WriteCardinal - writes a CARDINAL to file, f.
2011 It writes the binary image of the cardinal
2012 to file, f.
2013 */
2014
2015 extern "C" void FIO_WriteCardinal (FIO_File f, unsigned int c)
2016 {
2017 FIO_WriteAny (f, (unsigned char *) &c, (sizeof (c)-1));
2018 }
2019
2020
2021 /*
2022 ReadCardinal - reads a CARDINAL from file, f.
2023 It reads a binary image of a CARDINAL
2024 from a file, f.
2025 */
2026
2027 extern "C" unsigned int FIO_ReadCardinal (FIO_File f)
2028 {
2029 unsigned int c;
2030
2031 FIO_ReadAny (f, (unsigned char *) &c, (sizeof (c)-1));
2032 return c;
2033 /* static analysis guarentees a RETURN statement will be used before here. */
2034 __builtin_unreachable ();
2035 }
2036
2037
2038 /*
2039 GetUnixFileDescriptor - returns the UNIX file descriptor of a file.
2040 */
2041
2042 extern "C" int FIO_GetUnixFileDescriptor (FIO_File f)
2043 {
2044 FIO_FileDescriptor fd;
2045
2046 if (f != Error)
2047 {
2048 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
2049 if (fd != NULL)
2050 {
2051 return fd->unixfd;
2052 }
2053 }
2054 FormatError1 ((const char *) "file %d has not been opened or is out of range\\n", 48, (const unsigned char *) &f, (sizeof (f)-1));
2055 return -1;
2056 /* static analysis guarentees a RETURN statement will be used before here. */
2057 __builtin_unreachable ();
2058 }
2059
2060
2061 /*
2062 SetPositionFromBeginning - sets the position from the beginning of the file.
2063 */
2064
2065 extern "C" void FIO_SetPositionFromBeginning (FIO_File f, long int pos)
2066 {
2067 long int offset;
2068 FIO_FileDescriptor fd;
2069
2070 if (f != Error)
2071 {
2072 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
2073 if (fd != NULL)
2074 {
2075 /* always force the lseek, until we are confident that abspos is always correct,
2076 basically it needs some hard testing before we should remove the OR TRUE. */
2077 if ((fd->abspos != pos) || true)
2078 {
2079 FIO_FlushBuffer (f);
2080 if (fd->buffer != NULL)
2081 {
2082 if (fd->output)
2083 {
2084 fd->buffer->left = fd->buffer->size;
2085 }
2086 else
2087 {
2088 fd->buffer->left = 0;
2089 }
2090 fd->buffer->position = 0;
2091 fd->buffer->filled = 0;
2092 }
2093 offset = static_cast<long int> (libc_lseek (fd->unixfd, (off_t ) (pos), wrapc_SeekSet ()));
2094 if ((offset >= 0) && (pos == offset))
2095 {
2096 fd->abspos = pos;
2097 }
2098 else
2099 {
2100 fd->state = FIO_failed;
2101 fd->abspos = 0;
2102 }
2103 if (fd->buffer != NULL)
2104 {
2105 fd->buffer->valid = false;
2106 fd->buffer->bufstart = fd->abspos;
2107 }
2108 }
2109 }
2110 }
2111 }
2112
2113
2114 /*
2115 SetPositionFromEnd - sets the position from the end of the file.
2116 */
2117
2118 extern "C" void FIO_SetPositionFromEnd (FIO_File f, long int pos)
2119 {
2120 long int offset;
2121 FIO_FileDescriptor fd;
2122
2123 if (f != Error)
2124 {
2125 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
2126 if (fd != NULL)
2127 {
2128 FIO_FlushBuffer (f);
2129 if (fd->buffer != NULL)
2130 {
2131 if (fd->output)
2132 {
2133 fd->buffer->left = fd->buffer->size;
2134 }
2135 else
2136 {
2137 fd->buffer->left = 0;
2138 }
2139 fd->buffer->position = 0;
2140 fd->buffer->filled = 0;
2141 }
2142 offset = static_cast<long int> (libc_lseek (fd->unixfd, (off_t ) (pos), wrapc_SeekEnd ()));
2143 if (offset >= 0)
2144 {
2145 fd->abspos = offset;
2146 }
2147 else
2148 {
2149 fd->state = FIO_failed;
2150 fd->abspos = 0;
2151 offset = 0;
2152 }
2153 if (fd->buffer != NULL)
2154 {
2155 fd->buffer->valid = false;
2156 fd->buffer->bufstart = offset;
2157 }
2158 }
2159 }
2160 }
2161
2162
2163 /*
2164 FindPosition - returns the current absolute position in file, f.
2165 */
2166
2167 extern "C" long int FIO_FindPosition (FIO_File f)
2168 {
2169 FIO_FileDescriptor fd;
2170
2171 if (f != Error)
2172 {
2173 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
2174 if (fd != NULL)
2175 {
2176 if ((fd->buffer == NULL) || ! fd->buffer->valid)
2177 {
2178 return fd->abspos;
2179 }
2180 else
2181 {
2182 return fd->buffer->bufstart+((long int ) (fd->buffer->position));
2183 }
2184 }
2185 }
2186 return 0;
2187 /* static analysis guarentees a RETURN statement will be used before here. */
2188 __builtin_unreachable ();
2189 }
2190
2191
2192 /*
2193 GetFileName - assigns, a, with the filename associated with, f.
2194 */
2195
2196 extern "C" void FIO_GetFileName (FIO_File f, char *a, unsigned int _a_high)
2197 {
2198 typedef char *GetFileName__T6;
2199
2200 unsigned int i;
2201 GetFileName__T6 p;
2202 FIO_FileDescriptor fd;
2203
2204 if (f != Error)
2205 {
2206 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
2207 if (fd == NULL)
2208 {
2209 FormatError ((const char *) "this file has probably been closed and not reopened successfully or alternatively never opened\\n", 96);
2210 M2RTS_HALT (-1);
2211 __builtin_unreachable ();
2212 }
2213 else
2214 {
2215 if (fd->name.address == NULL)
2216 {
2217 StrLib_StrCopy ((const char *) "", 0, (char *) a, _a_high);
2218 }
2219 else
2220 {
2221 p = static_cast<GetFileName__T6> (fd->name.address);
2222 i = 0;
2223 while (((*p) != ASCII_nul) && (i <= _a_high))
2224 {
2225 const_cast<char *>(a)[i] = (*p);
2226 p += 1;
2227 i += 1;
2228 }
2229 }
2230 }
2231 }
2232 }
2233
2234
2235 /*
2236 getFileName - returns the address of the filename associated with, f.
2237 */
2238
2239 extern "C" void * FIO_getFileName (FIO_File f)
2240 {
2241 FIO_FileDescriptor fd;
2242
2243 if (f != Error)
2244 {
2245 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
2246 if (fd == NULL)
2247 {
2248 FormatError ((const char *) "this file has probably been closed and not reopened successfully or alternatively never opened\\n", 96);
2249 M2RTS_HALT (-1);
2250 __builtin_unreachable ();
2251 }
2252 else
2253 {
2254 return fd->name.address;
2255 }
2256 }
2257 return NULL;
2258 /* static analysis guarentees a RETURN statement will be used before here. */
2259 __builtin_unreachable ();
2260 }
2261
2262
2263 /*
2264 getFileNameLength - returns the number of characters associated with filename, f.
2265 */
2266
2267 extern "C" unsigned int FIO_getFileNameLength (FIO_File f)
2268 {
2269 FIO_FileDescriptor fd;
2270
2271 if (f != Error)
2272 {
2273 fd = static_cast<FIO_FileDescriptor> (Indexing_GetIndice (FileInfo, f));
2274 if (fd == NULL)
2275 {
2276 FormatError ((const char *) "this file has probably been closed and not reopened successfully or alternatively never opened\\n", 96);
2277 M2RTS_HALT (-1);
2278 __builtin_unreachable ();
2279 }
2280 else
2281 {
2282 return fd->name.size;
2283 }
2284 }
2285 return 0;
2286 /* static analysis guarentees a RETURN statement will be used before here. */
2287 __builtin_unreachable ();
2288 }
2289
2290
2291 /*
2292 FlushOutErr - flushes, StdOut, and, StdErr.
2293 It is also called when the application calls M2RTS.Terminate.
2294 (which is automatically placed in program modules by the GM2
2295 scaffold).
2296 */
2297
2298 extern "C" void FIO_FlushOutErr (void)
2299 {
2300 if (FIO_IsNoError (FIO_StdOut))
2301 {
2302 FIO_FlushBuffer (FIO_StdOut);
2303 }
2304 if (FIO_IsNoError (FIO_StdErr))
2305 {
2306 FIO_FlushBuffer (FIO_StdErr);
2307 }
2308 }
2309
2310 extern "C" void _M2_FIO_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
2311 {
2312 Init ();
2313 }
2314
2315 extern "C" void _M2_FIO_fini (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
2316 {
2317 FIO_FlushOutErr ();
2318 }