gimple-pretty-print.c (buffer): Remove.
[official-gcc.git] / libgfortran / io / unix.c
blobdd2715b6b6d8caf28ac7993af62cbf51b88cebb8
1 /* Copyright (C) 2002-2013 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 F2003 I/O support contributed by Jerry DeLisle
5 This file is part of the GNU Fortran runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 /* Unix stream I/O module */
28 #include "io.h"
29 #include "unix.h"
30 #include <stdlib.h>
31 #include <limits.h>
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <assert.h>
41 #include <string.h>
42 #include <errno.h>
45 /* For mingw, we don't identify files by their inode number, but by a
46 64-bit identifier created from a BY_HANDLE_FILE_INFORMATION. */
47 #ifdef __MINGW32__
49 #define WIN32_LEAN_AND_MEAN
50 #include <windows.h>
52 #if !defined(_FILE_OFFSET_BITS) || _FILE_OFFSET_BITS != 64
53 #undef lseek
54 #define lseek _lseeki64
55 #undef fstat
56 #define fstat _fstati64
57 #undef stat
58 #define stat _stati64
59 #endif
61 #ifndef HAVE_WORKING_STAT
62 static uint64_t
63 id_from_handle (HANDLE hFile)
65 BY_HANDLE_FILE_INFORMATION FileInformation;
67 if (hFile == INVALID_HANDLE_VALUE)
68 return 0;
70 memset (&FileInformation, 0, sizeof(FileInformation));
71 if (!GetFileInformationByHandle (hFile, &FileInformation))
72 return 0;
74 return ((uint64_t) FileInformation.nFileIndexLow)
75 | (((uint64_t) FileInformation.nFileIndexHigh) << 32);
79 static uint64_t
80 id_from_path (const char *path)
82 HANDLE hFile;
83 uint64_t res;
85 if (!path || !*path || access (path, F_OK))
86 return (uint64_t) -1;
88 hFile = CreateFile (path, 0, 0, NULL, OPEN_EXISTING,
89 FILE_FLAG_BACKUP_SEMANTICS | FILE_ATTRIBUTE_READONLY,
90 NULL);
91 res = id_from_handle (hFile);
92 CloseHandle (hFile);
93 return res;
97 static uint64_t
98 id_from_fd (const int fd)
100 return id_from_handle ((HANDLE) _get_osfhandle (fd));
103 #endif /* HAVE_WORKING_STAT */
104 #endif /* __MINGW32__ */
107 /* min macro that evaluates its arguments only once. */
108 #ifdef min
109 #undef min
110 #endif
112 #define min(a,b) \
113 ({ typeof (a) _a = (a); \
114 typeof (b) _b = (b); \
115 _a < _b ? _a : _b; })
117 #ifndef PATH_MAX
118 #define PATH_MAX 1024
119 #endif
121 /* These flags aren't defined on all targets (mingw32), so provide them
122 here. */
123 #ifndef S_IRGRP
124 #define S_IRGRP 0
125 #endif
127 #ifndef S_IWGRP
128 #define S_IWGRP 0
129 #endif
131 #ifndef S_IROTH
132 #define S_IROTH 0
133 #endif
135 #ifndef S_IWOTH
136 #define S_IWOTH 0
137 #endif
140 #ifndef HAVE_ACCESS
142 #ifndef W_OK
143 #define W_OK 2
144 #endif
146 #ifndef R_OK
147 #define R_OK 4
148 #endif
150 #ifndef F_OK
151 #define F_OK 0
152 #endif
154 /* Fallback implementation of access() on systems that don't have it.
155 Only modes R_OK, W_OK and F_OK are used in this file. */
157 static int
158 fallback_access (const char *path, int mode)
160 int fd;
162 if ((mode & R_OK) && (fd = open (path, O_RDONLY)) < 0)
163 return -1;
164 close (fd);
166 if ((mode & W_OK) && (fd = open (path, O_WRONLY)) < 0)
167 return -1;
168 close (fd);
170 if (mode == F_OK)
172 struct stat st;
173 return stat (path, &st);
176 return 0;
179 #undef access
180 #define access fallback_access
181 #endif
184 /* Fallback directory for creating temporary files. P_tmpdir is
185 defined on many POSIX platforms. */
186 #ifndef P_tmpdir
187 #ifdef _P_tmpdir
188 #define P_tmpdir _P_tmpdir /* MinGW */
189 #else
190 #define P_tmpdir "/tmp"
191 #endif
192 #endif
195 /* Unix and internal stream I/O module */
197 static const int BUFFER_SIZE = 8192;
199 typedef struct
201 stream st;
203 gfc_offset buffer_offset; /* File offset of the start of the buffer */
204 gfc_offset physical_offset; /* Current physical file offset */
205 gfc_offset logical_offset; /* Current logical file offset */
206 gfc_offset file_length; /* Length of the file. */
208 char *buffer; /* Pointer to the buffer. */
209 int fd; /* The POSIX file descriptor. */
211 int active; /* Length of valid bytes in the buffer */
213 int ndirty; /* Dirty bytes starting at buffer_offset */
215 /* Cached stat(2) values. */
216 dev_t st_dev;
217 ino_t st_ino;
219 bool unbuffered; /* Buffer should be flushed after each I/O statement. */
221 unix_stream;
224 /* fix_fd()-- Given a file descriptor, make sure it is not one of the
225 * standard descriptors, returning a non-standard descriptor. If the
226 * user specifies that system errors should go to standard output,
227 * then closes standard output, we don't want the system errors to a
228 * file that has been given file descriptor 1 or 0. We want to send
229 * the error to the invalid descriptor. */
231 static int
232 fix_fd (int fd)
234 #ifdef HAVE_DUP
235 int input, output, error;
237 input = output = error = 0;
239 /* Unix allocates the lowest descriptors first, so a loop is not
240 required, but this order is. */
241 if (fd == STDIN_FILENO)
243 fd = dup (fd);
244 input = 1;
246 if (fd == STDOUT_FILENO)
248 fd = dup (fd);
249 output = 1;
251 if (fd == STDERR_FILENO)
253 fd = dup (fd);
254 error = 1;
257 if (input)
258 close (STDIN_FILENO);
259 if (output)
260 close (STDOUT_FILENO);
261 if (error)
262 close (STDERR_FILENO);
263 #endif
265 return fd;
269 /* If the stream corresponds to a preconnected unit, we flush the
270 corresponding C stream. This is bugware for mixed C-Fortran codes
271 where the C code doesn't flush I/O before returning. */
272 void
273 flush_if_preconnected (stream * s)
275 int fd;
277 fd = ((unix_stream *) s)->fd;
278 if (fd == STDIN_FILENO)
279 fflush (stdin);
280 else if (fd == STDOUT_FILENO)
281 fflush (stdout);
282 else if (fd == STDERR_FILENO)
283 fflush (stderr);
287 /********************************************************************
288 Raw I/O functions (read, write, seek, tell, truncate, close).
290 These functions wrap the basic POSIX I/O syscalls. Any deviation in
291 semantics is a bug, except the following: write restarts in case
292 of being interrupted by a signal, and as the first argument the
293 functions take the unix_stream struct rather than an integer file
294 descriptor. Also, for POSIX read() and write() a nbyte argument larger
295 than SSIZE_MAX is undefined; here the type of nbyte is ssize_t rather
296 than size_t as for POSIX read/write.
297 *********************************************************************/
299 static int
300 raw_flush (unix_stream * s __attribute__ ((unused)))
302 return 0;
305 static ssize_t
306 raw_read (unix_stream * s, void * buf, ssize_t nbyte)
308 /* For read we can't do I/O in a loop like raw_write does, because
309 that will break applications that wait for interactive I/O. */
310 return read (s->fd, buf, nbyte);
313 static ssize_t
314 raw_write (unix_stream * s, const void * buf, ssize_t nbyte)
316 ssize_t trans, bytes_left;
317 char *buf_st;
319 bytes_left = nbyte;
320 buf_st = (char *) buf;
322 /* We must write in a loop since some systems don't restart system
323 calls in case of a signal. */
324 while (bytes_left > 0)
326 trans = write (s->fd, buf_st, bytes_left);
327 if (trans < 0)
329 if (errno == EINTR)
330 continue;
331 else
332 return trans;
334 buf_st += trans;
335 bytes_left -= trans;
338 return nbyte - bytes_left;
341 static gfc_offset
342 raw_seek (unix_stream * s, gfc_offset offset, int whence)
344 return lseek (s->fd, offset, whence);
347 static gfc_offset
348 raw_tell (unix_stream * s)
350 return lseek (s->fd, 0, SEEK_CUR);
353 static gfc_offset
354 raw_size (unix_stream * s)
356 struct stat statbuf;
357 int ret = fstat (s->fd, &statbuf);
358 if (ret == -1)
359 return ret;
360 if (S_ISREG (statbuf.st_mode))
361 return statbuf.st_size;
362 else
363 return 0;
366 static int
367 raw_truncate (unix_stream * s, gfc_offset length)
369 #ifdef __MINGW32__
370 HANDLE h;
371 gfc_offset cur;
373 if (isatty (s->fd))
375 errno = EBADF;
376 return -1;
378 h = (HANDLE) _get_osfhandle (s->fd);
379 if (h == INVALID_HANDLE_VALUE)
381 errno = EBADF;
382 return -1;
384 cur = lseek (s->fd, 0, SEEK_CUR);
385 if (cur == -1)
386 return -1;
387 if (lseek (s->fd, length, SEEK_SET) == -1)
388 goto error;
389 if (!SetEndOfFile (h))
391 errno = EBADF;
392 goto error;
394 if (lseek (s->fd, cur, SEEK_SET) == -1)
395 return -1;
396 return 0;
397 error:
398 lseek (s->fd, cur, SEEK_SET);
399 return -1;
400 #elif defined HAVE_FTRUNCATE
401 return ftruncate (s->fd, length);
402 #elif defined HAVE_CHSIZE
403 return chsize (s->fd, length);
404 #else
405 runtime_error ("required ftruncate or chsize support not present");
406 return -1;
407 #endif
410 static int
411 raw_close (unix_stream * s)
413 int retval;
415 if (s->fd != STDOUT_FILENO
416 && s->fd != STDERR_FILENO
417 && s->fd != STDIN_FILENO)
418 retval = close (s->fd);
419 else
420 retval = 0;
421 free (s);
422 return retval;
425 static const struct stream_vtable raw_vtable = {
426 .read = (void *) raw_read,
427 .write = (void *) raw_write,
428 .seek = (void *) raw_seek,
429 .tell = (void *) raw_tell,
430 .size = (void *) raw_size,
431 .trunc = (void *) raw_truncate,
432 .close = (void *) raw_close,
433 .flush = (void *) raw_flush
436 static int
437 raw_init (unix_stream * s)
439 s->st.vptr = &raw_vtable;
441 s->buffer = NULL;
442 return 0;
446 /*********************************************************************
447 Buffered I/O functions. These functions have the same semantics as the
448 raw I/O functions above, except that they are buffered in order to
449 improve performance. The buffer must be flushed when switching from
450 reading to writing and vice versa.
451 *********************************************************************/
453 static int
454 buf_flush (unix_stream * s)
456 int writelen;
458 /* Flushing in read mode means discarding read bytes. */
459 s->active = 0;
461 if (s->ndirty == 0)
462 return 0;
464 if (s->physical_offset != s->buffer_offset
465 && lseek (s->fd, s->buffer_offset, SEEK_SET) < 0)
466 return -1;
468 writelen = raw_write (s, s->buffer, s->ndirty);
470 s->physical_offset = s->buffer_offset + writelen;
472 if (s->physical_offset > s->file_length)
473 s->file_length = s->physical_offset;
475 s->ndirty -= writelen;
476 if (s->ndirty != 0)
477 return -1;
479 return 0;
482 static ssize_t
483 buf_read (unix_stream * s, void * buf, ssize_t nbyte)
485 if (s->active == 0)
486 s->buffer_offset = s->logical_offset;
488 /* Is the data we want in the buffer? */
489 if (s->logical_offset + nbyte <= s->buffer_offset + s->active
490 && s->buffer_offset <= s->logical_offset)
491 memcpy (buf, s->buffer + (s->logical_offset - s->buffer_offset), nbyte);
492 else
494 /* First copy the active bytes if applicable, then read the rest
495 either directly or filling the buffer. */
496 char *p;
497 int nread = 0;
498 ssize_t to_read, did_read;
499 gfc_offset new_logical;
501 p = (char *) buf;
502 if (s->logical_offset >= s->buffer_offset
503 && s->buffer_offset + s->active >= s->logical_offset)
505 nread = s->active - (s->logical_offset - s->buffer_offset);
506 memcpy (buf, s->buffer + (s->logical_offset - s->buffer_offset),
507 nread);
508 p += nread;
510 /* At this point we consider all bytes in the buffer discarded. */
511 to_read = nbyte - nread;
512 new_logical = s->logical_offset + nread;
513 if (s->physical_offset != new_logical
514 && lseek (s->fd, new_logical, SEEK_SET) < 0)
515 return -1;
516 s->buffer_offset = s->physical_offset = new_logical;
517 if (to_read <= BUFFER_SIZE/2)
519 did_read = raw_read (s, s->buffer, BUFFER_SIZE);
520 s->physical_offset += did_read;
521 s->active = did_read;
522 did_read = (did_read > to_read) ? to_read : did_read;
523 memcpy (p, s->buffer, did_read);
525 else
527 did_read = raw_read (s, p, to_read);
528 s->physical_offset += did_read;
529 s->active = 0;
531 nbyte = did_read + nread;
533 s->logical_offset += nbyte;
534 return nbyte;
537 static ssize_t
538 buf_write (unix_stream * s, const void * buf, ssize_t nbyte)
540 if (s->ndirty == 0)
541 s->buffer_offset = s->logical_offset;
543 /* Does the data fit into the buffer? As a special case, if the
544 buffer is empty and the request is bigger than BUFFER_SIZE/2,
545 write directly. This avoids the case where the buffer would have
546 to be flushed at every write. */
547 if (!(s->ndirty == 0 && nbyte > BUFFER_SIZE/2)
548 && s->logical_offset + nbyte <= s->buffer_offset + BUFFER_SIZE
549 && s->buffer_offset <= s->logical_offset
550 && s->buffer_offset + s->ndirty >= s->logical_offset)
552 memcpy (s->buffer + (s->logical_offset - s->buffer_offset), buf, nbyte);
553 int nd = (s->logical_offset - s->buffer_offset) + nbyte;
554 if (nd > s->ndirty)
555 s->ndirty = nd;
557 else
559 /* Flush, and either fill the buffer with the new data, or if
560 the request is bigger than the buffer size, write directly
561 bypassing the buffer. */
562 buf_flush (s);
563 if (nbyte <= BUFFER_SIZE/2)
565 memcpy (s->buffer, buf, nbyte);
566 s->buffer_offset = s->logical_offset;
567 s->ndirty += nbyte;
569 else
571 if (s->physical_offset != s->logical_offset)
573 if (lseek (s->fd, s->logical_offset, SEEK_SET) < 0)
574 return -1;
575 s->physical_offset = s->logical_offset;
578 nbyte = raw_write (s, buf, nbyte);
579 s->physical_offset += nbyte;
582 s->logical_offset += nbyte;
583 if (s->logical_offset > s->file_length)
584 s->file_length = s->logical_offset;
585 return nbyte;
588 static gfc_offset
589 buf_seek (unix_stream * s, gfc_offset offset, int whence)
591 switch (whence)
593 case SEEK_SET:
594 break;
595 case SEEK_CUR:
596 offset += s->logical_offset;
597 break;
598 case SEEK_END:
599 offset += s->file_length;
600 break;
601 default:
602 return -1;
604 if (offset < 0)
606 errno = EINVAL;
607 return -1;
609 s->logical_offset = offset;
610 return offset;
613 static gfc_offset
614 buf_tell (unix_stream * s)
616 return buf_seek (s, 0, SEEK_CUR);
619 static gfc_offset
620 buf_size (unix_stream * s)
622 return s->file_length;
625 static int
626 buf_truncate (unix_stream * s, gfc_offset length)
628 int r;
630 if (buf_flush (s) != 0)
631 return -1;
632 r = raw_truncate (s, length);
633 if (r == 0)
634 s->file_length = length;
635 return r;
638 static int
639 buf_close (unix_stream * s)
641 if (buf_flush (s) != 0)
642 return -1;
643 free (s->buffer);
644 return raw_close (s);
647 static const struct stream_vtable buf_vtable = {
648 .read = (void *) buf_read,
649 .write = (void *) buf_write,
650 .seek = (void *) buf_seek,
651 .tell = (void *) buf_tell,
652 .size = (void *) buf_size,
653 .trunc = (void *) buf_truncate,
654 .close = (void *) buf_close,
655 .flush = (void *) buf_flush
658 static int
659 buf_init (unix_stream * s)
661 s->st.vptr = &buf_vtable;
663 s->buffer = xmalloc (BUFFER_SIZE);
664 return 0;
668 /*********************************************************************
669 memory stream functions - These are used for internal files
671 The idea here is that a single stream structure is created and all
672 requests must be satisfied from it. The location and size of the
673 buffer is the character variable supplied to the READ or WRITE
674 statement.
676 *********************************************************************/
678 char *
679 mem_alloc_r (stream * strm, int * len)
681 unix_stream * s = (unix_stream *) strm;
682 gfc_offset n;
683 gfc_offset where = s->logical_offset;
685 if (where < s->buffer_offset || where > s->buffer_offset + s->active)
686 return NULL;
688 n = s->buffer_offset + s->active - where;
689 if (*len > n)
690 *len = n;
692 s->logical_offset = where + *len;
694 return s->buffer + (where - s->buffer_offset);
698 char *
699 mem_alloc_r4 (stream * strm, int * len)
701 unix_stream * s = (unix_stream *) strm;
702 gfc_offset n;
703 gfc_offset where = s->logical_offset;
705 if (where < s->buffer_offset || where > s->buffer_offset + s->active)
706 return NULL;
708 n = s->buffer_offset + s->active - where;
709 if (*len > n)
710 *len = n;
712 s->logical_offset = where + *len;
714 return s->buffer + (where - s->buffer_offset) * 4;
718 char *
719 mem_alloc_w (stream * strm, int * len)
721 unix_stream * s = (unix_stream *) strm;
722 gfc_offset m;
723 gfc_offset where = s->logical_offset;
725 m = where + *len;
727 if (where < s->buffer_offset)
728 return NULL;
730 if (m > s->file_length)
731 return NULL;
733 s->logical_offset = m;
735 return s->buffer + (where - s->buffer_offset);
739 gfc_char4_t *
740 mem_alloc_w4 (stream * strm, int * len)
742 unix_stream * s = (unix_stream *) strm;
743 gfc_offset m;
744 gfc_offset where = s->logical_offset;
745 gfc_char4_t *result = (gfc_char4_t *) s->buffer;
747 m = where + *len;
749 if (where < s->buffer_offset)
750 return NULL;
752 if (m > s->file_length)
753 return NULL;
755 s->logical_offset = m;
756 return &result[where - s->buffer_offset];
760 /* Stream read function for character(kind=1) internal units. */
762 static ssize_t
763 mem_read (stream * s, void * buf, ssize_t nbytes)
765 void *p;
766 int nb = nbytes;
768 p = mem_alloc_r (s, &nb);
769 if (p)
771 memcpy (buf, p, nb);
772 return (ssize_t) nb;
774 else
775 return 0;
779 /* Stream read function for chracter(kind=4) internal units. */
781 static ssize_t
782 mem_read4 (stream * s, void * buf, ssize_t nbytes)
784 void *p;
785 int nb = nbytes;
787 p = mem_alloc_r (s, &nb);
788 if (p)
790 memcpy (buf, p, nb);
791 return (ssize_t) nb;
793 else
794 return 0;
798 /* Stream write function for character(kind=1) internal units. */
800 static ssize_t
801 mem_write (stream * s, const void * buf, ssize_t nbytes)
803 void *p;
804 int nb = nbytes;
806 p = mem_alloc_w (s, &nb);
807 if (p)
809 memcpy (p, buf, nb);
810 return (ssize_t) nb;
812 else
813 return 0;
817 /* Stream write function for character(kind=4) internal units. */
819 static ssize_t
820 mem_write4 (stream * s, const void * buf, ssize_t nwords)
822 gfc_char4_t *p;
823 int nw = nwords;
825 p = mem_alloc_w4 (s, &nw);
826 if (p)
828 while (nw--)
829 *p++ = (gfc_char4_t) *((char *) buf);
830 return nwords;
832 else
833 return 0;
837 static gfc_offset
838 mem_seek (stream * strm, gfc_offset offset, int whence)
840 unix_stream * s = (unix_stream *) strm;
841 switch (whence)
843 case SEEK_SET:
844 break;
845 case SEEK_CUR:
846 offset += s->logical_offset;
847 break;
848 case SEEK_END:
849 offset += s->file_length;
850 break;
851 default:
852 return -1;
855 /* Note that for internal array I/O it's actually possible to have a
856 negative offset, so don't check for that. */
857 if (offset > s->file_length)
859 errno = EINVAL;
860 return -1;
863 s->logical_offset = offset;
865 /* Returning < 0 is the error indicator for sseek(), so return 0 if
866 offset is negative. Thus if the return value is 0, the caller
867 has to use stell() to get the real value of logical_offset. */
868 if (offset >= 0)
869 return offset;
870 return 0;
874 static gfc_offset
875 mem_tell (stream * s)
877 return ((unix_stream *)s)->logical_offset;
881 static int
882 mem_truncate (unix_stream * s __attribute__ ((unused)),
883 gfc_offset length __attribute__ ((unused)))
885 return 0;
889 static int
890 mem_flush (unix_stream * s __attribute__ ((unused)))
892 return 0;
896 static int
897 mem_close (unix_stream * s)
899 free (s);
901 return 0;
904 static const struct stream_vtable mem_vtable = {
905 .read = (void *) mem_read,
906 .write = (void *) mem_write,
907 .seek = (void *) mem_seek,
908 .tell = (void *) mem_tell,
909 /* buf_size is not a typo, we just reuse an identical
910 implementation. */
911 .size = (void *) buf_size,
912 .trunc = (void *) mem_truncate,
913 .close = (void *) mem_close,
914 .flush = (void *) mem_flush
917 static const struct stream_vtable mem4_vtable = {
918 .read = (void *) mem_read4,
919 .write = (void *) mem_write4,
920 .seek = (void *) mem_seek,
921 .tell = (void *) mem_tell,
922 /* buf_size is not a typo, we just reuse an identical
923 implementation. */
924 .size = (void *) buf_size,
925 .trunc = (void *) mem_truncate,
926 .close = (void *) mem_close,
927 .flush = (void *) mem_flush
930 /*********************************************************************
931 Public functions -- A reimplementation of this module needs to
932 define functional equivalents of the following.
933 *********************************************************************/
935 /* open_internal()-- Returns a stream structure from a character(kind=1)
936 internal file */
938 stream *
939 open_internal (char *base, int length, gfc_offset offset)
941 unix_stream *s;
943 s = xcalloc (1, sizeof (unix_stream));
945 s->buffer = base;
946 s->buffer_offset = offset;
948 s->active = s->file_length = length;
950 s->st.vptr = &mem_vtable;
952 return (stream *) s;
955 /* open_internal4()-- Returns a stream structure from a character(kind=4)
956 internal file */
958 stream *
959 open_internal4 (char *base, int length, gfc_offset offset)
961 unix_stream *s;
963 s = xcalloc (1, sizeof (unix_stream));
965 s->buffer = base;
966 s->buffer_offset = offset;
968 s->active = s->file_length = length * sizeof (gfc_char4_t);
970 s->st.vptr = &mem4_vtable;
972 return (stream *) s;
976 /* "Unbuffered" really means I/O statement buffering. For formatted
977 I/O, the fbuf manages this, and then uses raw I/O. For unformatted
978 I/O, buffered I/O is used, and the buffer is flushed at the end of
979 each I/O statement, where this function is called. */
982 flush_if_unbuffered (stream* s)
984 unix_stream* us = (unix_stream*) s;
985 if (us->unbuffered)
986 return sflush (s);
987 return 0;
991 /* fd_to_stream()-- Given an open file descriptor, build a stream
992 * around it. */
994 static stream *
995 fd_to_stream (int fd, bool unformatted)
997 struct stat statbuf;
998 unix_stream *s;
1000 s = xcalloc (1, sizeof (unix_stream));
1002 s->fd = fd;
1004 /* Get the current length of the file. */
1006 fstat (fd, &statbuf);
1008 s->st_dev = statbuf.st_dev;
1009 s->st_ino = statbuf.st_ino;
1010 s->file_length = statbuf.st_size;
1012 /* Only use buffered IO for regular files. */
1013 if (S_ISREG (statbuf.st_mode)
1014 && !options.all_unbuffered
1015 && !(options.unbuffered_preconnected &&
1016 (s->fd == STDIN_FILENO
1017 || s->fd == STDOUT_FILENO
1018 || s->fd == STDERR_FILENO)))
1019 buf_init (s);
1020 else
1022 if (unformatted)
1024 s->unbuffered = true;
1025 buf_init (s);
1027 else
1028 raw_init (s);
1031 return (stream *) s;
1035 /* Given the Fortran unit number, convert it to a C file descriptor. */
1038 unit_to_fd (int unit)
1040 gfc_unit *us;
1041 int fd;
1043 us = find_unit (unit);
1044 if (us == NULL)
1045 return -1;
1047 fd = ((unix_stream *) us->s)->fd;
1048 unlock_unit (us);
1049 return fd;
1053 /* unpack_filename()-- Given a fortran string and a pointer to a
1054 * buffer that is PATH_MAX characters, convert the fortran string to a
1055 * C string in the buffer. Returns nonzero if this is not possible. */
1058 unpack_filename (char *cstring, const char *fstring, int len)
1060 if (fstring == NULL)
1061 return EFAULT;
1062 len = fstrlen (fstring, len);
1063 if (len >= PATH_MAX)
1064 return ENAMETOOLONG;
1066 memmove (cstring, fstring, len);
1067 cstring[len] = '\0';
1069 return 0;
1073 /* Helper function for tempfile(). Tries to open a temporary file in
1074 the directory specified by tempdir. If successful, the file name is
1075 stored in fname and the descriptor returned. Returns -1 on
1076 failure. */
1078 static int
1079 tempfile_open (const char *tempdir, char **fname)
1081 int fd;
1082 const char *slash = "/";
1083 #if defined(HAVE_UMASK) && defined(HAVE_MKSTEMP)
1084 mode_t mode_mask;
1085 #endif
1087 if (!tempdir)
1088 return -1;
1090 /* Check for the special case that tempdir ends with a slash or
1091 backslash. */
1092 size_t tempdirlen = strlen (tempdir);
1093 if (*tempdir == 0 || tempdir[tempdirlen - 1] == '/'
1094 #ifdef __MINGW32__
1095 || tempdir[tempdirlen - 1] == '\\'
1096 #endif
1098 slash = "";
1100 // Take care that the template is longer in the mktemp() branch.
1101 char * template = xmalloc (tempdirlen + 23);
1103 #ifdef HAVE_MKSTEMP
1104 snprintf (template, tempdirlen + 23, "%s%sgfortrantmpXXXXXX",
1105 tempdir, slash);
1107 #ifdef HAVE_UMASK
1108 /* Temporarily set the umask such that the file has 0600 permissions. */
1109 mode_mask = umask (S_IXUSR | S_IRWXG | S_IRWXO);
1110 #endif
1112 fd = mkstemp (template);
1114 #ifdef HAVE_UMASK
1115 (void) umask (mode_mask);
1116 #endif
1118 #else /* HAVE_MKSTEMP */
1119 fd = -1;
1120 int count = 0;
1121 size_t slashlen = strlen (slash);
1124 snprintf (template, tempdirlen + 23, "%s%sgfortrantmpaaaXXXXXX",
1125 tempdir, slash);
1126 if (count > 0)
1128 int c = count;
1129 template[tempdirlen + slashlen + 13] = 'a' + (c% 26);
1130 c /= 26;
1131 template[tempdirlen + slashlen + 12] = 'a' + (c % 26);
1132 c /= 26;
1133 template[tempdirlen + slashlen + 11] = 'a' + (c % 26);
1134 if (c >= 26)
1135 break;
1138 if (!mktemp (template))
1140 errno = EEXIST;
1141 count++;
1142 continue;
1145 #if defined(HAVE_CRLF) && defined(O_BINARY)
1146 fd = open (template, O_RDWR | O_CREAT | O_EXCL | O_BINARY,
1147 S_IRUSR | S_IWUSR);
1148 #else
1149 fd = open (template, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
1150 #endif
1152 while (fd == -1 && errno == EEXIST);
1153 #endif /* HAVE_MKSTEMP */
1155 *fname = template;
1156 return fd;
1160 /* tempfile()-- Generate a temporary filename for a scratch file and
1161 * open it. mkstemp() opens the file for reading and writing, but the
1162 * library mode prevents anything that is not allowed. The descriptor
1163 * is returned, which is -1 on error. The template is pointed to by
1164 * opp->file, which is copied into the unit structure
1165 * and freed later. */
1167 static int
1168 tempfile (st_parameter_open *opp)
1170 const char *tempdir;
1171 char *fname;
1172 int fd = -1;
1174 tempdir = secure_getenv ("TMPDIR");
1175 fd = tempfile_open (tempdir, &fname);
1176 #ifdef __MINGW32__
1177 if (fd == -1)
1179 char buffer[MAX_PATH + 1];
1180 DWORD ret;
1181 ret = GetTempPath (MAX_PATH, buffer);
1182 /* If we are not able to get a temp-directory, we use
1183 current directory. */
1184 if (ret > MAX_PATH || !ret)
1185 buffer[0] = 0;
1186 else
1187 buffer[ret] = 0;
1188 tempdir = strdup (buffer);
1189 fd = tempfile_open (tempdir, &fname);
1191 #elif defined(__CYGWIN__)
1192 if (fd == -1)
1194 tempdir = secure_getenv ("TMP");
1195 fd = tempfile_open (tempdir, &fname);
1197 if (fd == -1)
1199 tempdir = secure_getenv ("TEMP");
1200 fd = tempfile_open (tempdir, &fname);
1202 #endif
1203 if (fd == -1)
1204 fd = tempfile_open (P_tmpdir, &fname);
1206 opp->file = fname;
1207 opp->file_len = strlen (fname); /* Don't include trailing nul */
1209 return fd;
1213 /* regular_file()-- Open a regular file.
1214 * Change flags->action if it is ACTION_UNSPECIFIED on entry,
1215 * unless an error occurs.
1216 * Returns the descriptor, which is less than zero on error. */
1218 static int
1219 regular_file (st_parameter_open *opp, unit_flags *flags)
1221 char path[min(PATH_MAX, opp->file_len + 1)];
1222 int mode;
1223 int rwflag;
1224 int crflag;
1225 int fd;
1226 int err;
1228 err = unpack_filename (path, opp->file, opp->file_len);
1229 if (err)
1231 errno = err; /* Fake an OS error */
1232 return -1;
1235 #ifdef __CYGWIN__
1236 if (opp->file_len == 7)
1238 if (strncmp (path, "CONOUT$", 7) == 0
1239 || strncmp (path, "CONERR$", 7) == 0)
1241 fd = open ("/dev/conout", O_WRONLY);
1242 flags->action = ACTION_WRITE;
1243 return fd;
1247 if (opp->file_len == 6 && strncmp (path, "CONIN$", 6) == 0)
1249 fd = open ("/dev/conin", O_RDONLY);
1250 flags->action = ACTION_READ;
1251 return fd;
1253 #endif
1256 #ifdef __MINGW32__
1257 if (opp->file_len == 7)
1259 if (strncmp (path, "CONOUT$", 7) == 0
1260 || strncmp (path, "CONERR$", 7) == 0)
1262 fd = open ("CONOUT$", O_WRONLY);
1263 flags->action = ACTION_WRITE;
1264 return fd;
1268 if (opp->file_len == 6 && strncmp (path, "CONIN$", 6) == 0)
1270 fd = open ("CONIN$", O_RDONLY);
1271 flags->action = ACTION_READ;
1272 return fd;
1274 #endif
1276 rwflag = 0;
1278 switch (flags->action)
1280 case ACTION_READ:
1281 rwflag = O_RDONLY;
1282 break;
1284 case ACTION_WRITE:
1285 rwflag = O_WRONLY;
1286 break;
1288 case ACTION_READWRITE:
1289 case ACTION_UNSPECIFIED:
1290 rwflag = O_RDWR;
1291 break;
1293 default:
1294 internal_error (&opp->common, "regular_file(): Bad action");
1297 switch (flags->status)
1299 case STATUS_NEW:
1300 crflag = O_CREAT | O_EXCL;
1301 break;
1303 case STATUS_OLD: /* open will fail if the file does not exist*/
1304 crflag = 0;
1305 break;
1307 case STATUS_UNKNOWN:
1308 case STATUS_SCRATCH:
1309 crflag = O_CREAT;
1310 break;
1312 case STATUS_REPLACE:
1313 crflag = O_CREAT | O_TRUNC;
1314 break;
1316 default:
1317 internal_error (&opp->common, "regular_file(): Bad status");
1320 /* rwflag |= O_LARGEFILE; */
1322 #if defined(HAVE_CRLF) && defined(O_BINARY)
1323 crflag |= O_BINARY;
1324 #endif
1326 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1327 fd = open (path, rwflag | crflag, mode);
1328 if (flags->action != ACTION_UNSPECIFIED)
1329 return fd;
1331 if (fd >= 0)
1333 flags->action = ACTION_READWRITE;
1334 return fd;
1336 if (errno != EACCES && errno != EROFS)
1337 return fd;
1339 /* retry for read-only access */
1340 rwflag = O_RDONLY;
1341 fd = open (path, rwflag | crflag, mode);
1342 if (fd >=0)
1344 flags->action = ACTION_READ;
1345 return fd; /* success */
1348 if (errno != EACCES)
1349 return fd; /* failure */
1351 /* retry for write-only access */
1352 rwflag = O_WRONLY;
1353 fd = open (path, rwflag | crflag, mode);
1354 if (fd >=0)
1356 flags->action = ACTION_WRITE;
1357 return fd; /* success */
1359 return fd; /* failure */
1363 /* open_external()-- Open an external file, unix specific version.
1364 * Change flags->action if it is ACTION_UNSPECIFIED on entry.
1365 * Returns NULL on operating system error. */
1367 stream *
1368 open_external (st_parameter_open *opp, unit_flags *flags)
1370 int fd;
1372 if (flags->status == STATUS_SCRATCH)
1374 fd = tempfile (opp);
1375 if (flags->action == ACTION_UNSPECIFIED)
1376 flags->action = ACTION_READWRITE;
1378 #if HAVE_UNLINK_OPEN_FILE
1379 /* We can unlink scratch files now and it will go away when closed. */
1380 if (fd >= 0)
1381 unlink (opp->file);
1382 #endif
1384 else
1386 /* regular_file resets flags->action if it is ACTION_UNSPECIFIED and
1387 * if it succeeds */
1388 fd = regular_file (opp, flags);
1391 if (fd < 0)
1392 return NULL;
1393 fd = fix_fd (fd);
1395 return fd_to_stream (fd, flags->form == FORM_UNFORMATTED);
1399 /* input_stream()-- Return a stream pointer to the default input stream.
1400 * Called on initialization. */
1402 stream *
1403 input_stream (void)
1405 return fd_to_stream (STDIN_FILENO, false);
1409 /* output_stream()-- Return a stream pointer to the default output stream.
1410 * Called on initialization. */
1412 stream *
1413 output_stream (void)
1415 stream * s;
1417 #if defined(HAVE_CRLF) && defined(HAVE_SETMODE)
1418 setmode (STDOUT_FILENO, O_BINARY);
1419 #endif
1421 s = fd_to_stream (STDOUT_FILENO, false);
1422 return s;
1426 /* error_stream()-- Return a stream pointer to the default error stream.
1427 * Called on initialization. */
1429 stream *
1430 error_stream (void)
1432 stream * s;
1434 #if defined(HAVE_CRLF) && defined(HAVE_SETMODE)
1435 setmode (STDERR_FILENO, O_BINARY);
1436 #endif
1438 s = fd_to_stream (STDERR_FILENO, false);
1439 return s;
1443 /* compare_file_filename()-- Given an open stream and a fortran string
1444 * that is a filename, figure out if the file is the same as the
1445 * filename. */
1448 compare_file_filename (gfc_unit *u, const char *name, int len)
1450 char path[min(PATH_MAX, len + 1)];
1451 struct stat st;
1452 #ifdef HAVE_WORKING_STAT
1453 unix_stream *s;
1454 #else
1455 # ifdef __MINGW32__
1456 uint64_t id1, id2;
1457 # endif
1458 #endif
1460 if (unpack_filename (path, name, len))
1461 return 0; /* Can't be the same */
1463 /* If the filename doesn't exist, then there is no match with the
1464 * existing file. */
1466 if (stat (path, &st) < 0)
1467 return 0;
1469 #ifdef HAVE_WORKING_STAT
1470 s = (unix_stream *) (u->s);
1471 return (st.st_dev == s->st_dev) && (st.st_ino == s->st_ino);
1472 #else
1474 # ifdef __MINGW32__
1475 /* We try to match files by a unique ID. On some filesystems (network
1476 fs and FAT), we can't generate this unique ID, and will simply compare
1477 filenames. */
1478 id1 = id_from_path (path);
1479 id2 = id_from_fd (((unix_stream *) (u->s))->fd);
1480 if (id1 || id2)
1481 return (id1 == id2);
1482 # endif
1484 if (len != u->file_len)
1485 return 0;
1486 return (memcmp(path, u->file, len) == 0);
1487 #endif
1491 #ifdef HAVE_WORKING_STAT
1492 # define FIND_FILE0_DECL struct stat *st
1493 # define FIND_FILE0_ARGS st
1494 #else
1495 # define FIND_FILE0_DECL uint64_t id, const char *file, gfc_charlen_type file_len
1496 # define FIND_FILE0_ARGS id, file, file_len
1497 #endif
1499 /* find_file0()-- Recursive work function for find_file() */
1501 static gfc_unit *
1502 find_file0 (gfc_unit *u, FIND_FILE0_DECL)
1504 gfc_unit *v;
1505 #if defined(__MINGW32__) && !HAVE_WORKING_STAT
1506 uint64_t id1;
1507 #endif
1509 if (u == NULL)
1510 return NULL;
1512 #ifdef HAVE_WORKING_STAT
1513 if (u->s != NULL)
1515 unix_stream *s = (unix_stream *) (u->s);
1516 if (st[0].st_dev == s->st_dev && st[0].st_ino == s->st_ino)
1517 return u;
1519 #else
1520 # ifdef __MINGW32__
1521 if (u->s && ((id1 = id_from_fd (((unix_stream *) u->s)->fd)) || id1))
1523 if (id == id1)
1524 return u;
1526 else
1527 # endif
1528 if (compare_string (u->file_len, u->file, file_len, file) == 0)
1529 return u;
1530 #endif
1532 v = find_file0 (u->left, FIND_FILE0_ARGS);
1533 if (v != NULL)
1534 return v;
1536 v = find_file0 (u->right, FIND_FILE0_ARGS);
1537 if (v != NULL)
1538 return v;
1540 return NULL;
1544 /* find_file()-- Take the current filename and see if there is a unit
1545 * that has the file already open. Returns a pointer to the unit if so. */
1547 gfc_unit *
1548 find_file (const char *file, gfc_charlen_type file_len)
1550 char path[min(PATH_MAX, file_len + 1)];
1551 struct stat st[1];
1552 gfc_unit *u;
1553 #if defined(__MINGW32__) && !HAVE_WORKING_STAT
1554 uint64_t id = 0ULL;
1555 #endif
1557 if (unpack_filename (path, file, file_len))
1558 return NULL;
1560 if (stat (path, &st[0]) < 0)
1561 return NULL;
1563 #if defined(__MINGW32__) && !HAVE_WORKING_STAT
1564 id = id_from_path (path);
1565 #endif
1567 __gthread_mutex_lock (&unit_lock);
1568 retry:
1569 u = find_file0 (unit_root, FIND_FILE0_ARGS);
1570 if (u != NULL)
1572 /* Fast path. */
1573 if (! __gthread_mutex_trylock (&u->lock))
1575 /* assert (u->closed == 0); */
1576 __gthread_mutex_unlock (&unit_lock);
1577 return u;
1580 inc_waiting_locked (u);
1582 __gthread_mutex_unlock (&unit_lock);
1583 if (u != NULL)
1585 __gthread_mutex_lock (&u->lock);
1586 if (u->closed)
1588 __gthread_mutex_lock (&unit_lock);
1589 __gthread_mutex_unlock (&u->lock);
1590 if (predec_waiting_locked (u) == 0)
1591 free (u);
1592 goto retry;
1595 dec_waiting_unlocked (u);
1597 return u;
1600 static gfc_unit *
1601 flush_all_units_1 (gfc_unit *u, int min_unit)
1603 while (u != NULL)
1605 if (u->unit_number > min_unit)
1607 gfc_unit *r = flush_all_units_1 (u->left, min_unit);
1608 if (r != NULL)
1609 return r;
1611 if (u->unit_number >= min_unit)
1613 if (__gthread_mutex_trylock (&u->lock))
1614 return u;
1615 if (u->s)
1616 sflush (u->s);
1617 __gthread_mutex_unlock (&u->lock);
1619 u = u->right;
1621 return NULL;
1624 void
1625 flush_all_units (void)
1627 gfc_unit *u;
1628 int min_unit = 0;
1630 __gthread_mutex_lock (&unit_lock);
1633 u = flush_all_units_1 (unit_root, min_unit);
1634 if (u != NULL)
1635 inc_waiting_locked (u);
1636 __gthread_mutex_unlock (&unit_lock);
1637 if (u == NULL)
1638 return;
1640 __gthread_mutex_lock (&u->lock);
1642 min_unit = u->unit_number + 1;
1644 if (u->closed == 0)
1646 sflush (u->s);
1647 __gthread_mutex_lock (&unit_lock);
1648 __gthread_mutex_unlock (&u->lock);
1649 (void) predec_waiting_locked (u);
1651 else
1653 __gthread_mutex_lock (&unit_lock);
1654 __gthread_mutex_unlock (&u->lock);
1655 if (predec_waiting_locked (u) == 0)
1656 free (u);
1659 while (1);
1663 /* delete_file()-- Given a unit structure, delete the file associated
1664 * with the unit. Returns nonzero if something went wrong. */
1667 delete_file (gfc_unit * u)
1669 char path[min(PATH_MAX, u->file_len + 1)];
1670 int err = unpack_filename (path, u->file, u->file_len);
1672 if (err)
1673 { /* Shouldn't be possible */
1674 errno = err;
1675 return 1;
1678 return unlink (path);
1682 /* file_exists()-- Returns nonzero if the current filename exists on
1683 * the system */
1686 file_exists (const char *file, gfc_charlen_type file_len)
1688 char path[min(PATH_MAX, file_len + 1)];
1690 if (unpack_filename (path, file, file_len))
1691 return 0;
1693 return !(access (path, F_OK));
1697 /* file_size()-- Returns the size of the file. */
1699 GFC_IO_INT
1700 file_size (const char *file, gfc_charlen_type file_len)
1702 char path[min(PATH_MAX, file_len + 1)];
1703 struct stat statbuf;
1705 if (unpack_filename (path, file, file_len))
1706 return -1;
1708 if (stat (path, &statbuf) < 0)
1709 return -1;
1711 return (GFC_IO_INT) statbuf.st_size;
1714 static const char yes[] = "YES", no[] = "NO", unknown[] = "UNKNOWN";
1716 /* inquire_sequential()-- Given a fortran string, determine if the
1717 * file is suitable for sequential access. Returns a C-style
1718 * string. */
1720 const char *
1721 inquire_sequential (const char *string, int len)
1723 char path[min(PATH_MAX, len + 1)];
1724 struct stat statbuf;
1726 if (string == NULL ||
1727 unpack_filename (path, string, len) || stat (path, &statbuf) < 0)
1728 return unknown;
1730 if (S_ISREG (statbuf.st_mode) ||
1731 S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))
1732 return unknown;
1734 if (S_ISDIR (statbuf.st_mode) || S_ISBLK (statbuf.st_mode))
1735 return no;
1737 return unknown;
1741 /* inquire_direct()-- Given a fortran string, determine if the file is
1742 * suitable for direct access. Returns a C-style string. */
1744 const char *
1745 inquire_direct (const char *string, int len)
1747 char path[min(PATH_MAX, len + 1)];
1748 struct stat statbuf;
1750 if (string == NULL ||
1751 unpack_filename (path, string, len) || stat (path, &statbuf) < 0)
1752 return unknown;
1754 if (S_ISREG (statbuf.st_mode) || S_ISBLK (statbuf.st_mode))
1755 return unknown;
1757 if (S_ISDIR (statbuf.st_mode) ||
1758 S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))
1759 return no;
1761 return unknown;
1765 /* inquire_formatted()-- Given a fortran string, determine if the file
1766 * is suitable for formatted form. Returns a C-style string. */
1768 const char *
1769 inquire_formatted (const char *string, int len)
1771 char path[min(PATH_MAX, len + 1)];
1772 struct stat statbuf;
1774 if (string == NULL ||
1775 unpack_filename (path, string, len) || stat (path, &statbuf) < 0)
1776 return unknown;
1778 if (S_ISREG (statbuf.st_mode) ||
1779 S_ISBLK (statbuf.st_mode) ||
1780 S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))
1781 return unknown;
1783 if (S_ISDIR (statbuf.st_mode))
1784 return no;
1786 return unknown;
1790 /* inquire_unformatted()-- Given a fortran string, determine if the file
1791 * is suitable for unformatted form. Returns a C-style string. */
1793 const char *
1794 inquire_unformatted (const char *string, int len)
1796 return inquire_formatted (string, len);
1800 /* inquire_access()-- Given a fortran string, determine if the file is
1801 * suitable for access. */
1803 static const char *
1804 inquire_access (const char *string, int len, int mode)
1806 char path[min(PATH_MAX, len + 1)];
1808 if (string == NULL || unpack_filename (path, string, len) ||
1809 access (path, mode) < 0)
1810 return no;
1812 return yes;
1816 /* inquire_read()-- Given a fortran string, determine if the file is
1817 * suitable for READ access. */
1819 const char *
1820 inquire_read (const char *string, int len)
1822 return inquire_access (string, len, R_OK);
1826 /* inquire_write()-- Given a fortran string, determine if the file is
1827 * suitable for READ access. */
1829 const char *
1830 inquire_write (const char *string, int len)
1832 return inquire_access (string, len, W_OK);
1836 /* inquire_readwrite()-- Given a fortran string, determine if the file is
1837 * suitable for read and write access. */
1839 const char *
1840 inquire_readwrite (const char *string, int len)
1842 return inquire_access (string, len, R_OK | W_OK);
1847 stream_isatty (stream *s)
1849 return isatty (((unix_stream *) s)->fd);
1853 stream_ttyname (stream *s __attribute__ ((unused)),
1854 char * buf __attribute__ ((unused)),
1855 size_t buflen __attribute__ ((unused)))
1857 #ifdef HAVE_TTYNAME_R
1858 return ttyname_r (((unix_stream *) s)->fd, buf, buflen);
1859 #elif defined HAVE_TTYNAME
1860 char *p;
1861 size_t plen;
1862 p = ttyname (((unix_stream *) s)->fd);
1863 if (!p)
1864 return errno;
1865 plen = strlen (p);
1866 if (buflen < plen)
1867 plen = buflen;
1868 memcpy (buf, p, plen);
1869 return 0;
1870 #else
1871 return ENOSYS;
1872 #endif
1878 /* How files are stored: This is an operating-system specific issue,
1879 and therefore belongs here. There are three cases to consider.
1881 Direct Access:
1882 Records are written as block of bytes corresponding to the record
1883 length of the file. This goes for both formatted and unformatted
1884 records. Positioning is done explicitly for each data transfer,
1885 so positioning is not much of an issue.
1887 Sequential Formatted:
1888 Records are separated by newline characters. The newline character
1889 is prohibited from appearing in a string. If it does, this will be
1890 messed up on the next read. End of file is also the end of a record.
1892 Sequential Unformatted:
1893 In this case, we are merely copying bytes to and from main storage,
1894 yet we need to keep track of varying record lengths. We adopt
1895 the solution used by f2c. Each record contains a pair of length
1896 markers:
1898 Length of record n in bytes
1899 Data of record n
1900 Length of record n in bytes
1902 Length of record n+1 in bytes
1903 Data of record n+1
1904 Length of record n+1 in bytes
1906 The length is stored at the end of a record to allow backspacing to the
1907 previous record. Between data transfer statements, the file pointer
1908 is left pointing to the first length of the current record.
1910 ENDFILE records are never explicitly stored.