1 /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Libgfortran; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Unix stream I/O module */
31 #ifdef HAVE_SYS_MMAN_H
37 #include "libgfortran.h"
45 #define MAP_FAILED ((void *) -1)
56 /* This implementation of stream I/O is based on the paper:
58 * "Exploiting the advantages of mapped files for stream I/O",
59 * O. Krieger, M. Stumm and R. Umrau, "Proceedings of the 1992 Winter
60 * USENIX conference", p. 27-42.
62 * It differs in a number of ways from the version described in the
63 * paper. First of all, threads are not an issue during I/O and we
64 * also don't have to worry about having multiple regions, since
65 * fortran's I/O model only allows you to be one place at a time.
67 * On the other hand, we have to be able to writing at the end of a
68 * stream, read from the start of a stream or read and write blocks of
69 * bytes from an arbitrary position. After opening a file, a pointer
70 * to a stream structure is returned, which is used to handle file
71 * accesses until the file is closed.
73 * salloc_at_r(stream, len, where)-- Given a stream pointer, return a
74 * pointer to a block of memory that mirror the file at position
75 * 'where' that is 'len' bytes long. The len integer is updated to
76 * reflect how many bytes were actually read. The only reason for a
77 * short read is end of file. The file pointer is updated. The
78 * pointer is valid until the next call to salloc_*.
80 * salloc_at_w(stream, len, where)-- Given the stream pointer, returns
81 * a pointer to a block of memory that is updated to reflect the state
82 * of the file. The length of the buffer is always equal to that
83 * requested. The buffer must be completely set by the caller. When
84 * data has been written, the sfree() function must be called to
85 * indicate that the caller is done writing data to the buffer. This
86 * may or may not cause a physical write.
88 * Short forms of these are salloc_r() and salloc_w() which drop the
89 * 'where' parameter and use the current file pointer. */
92 #define BUFFER_SIZE 8192
99 gfc_offset buffer_offset
; /* File offset of the start of the buffer */
100 gfc_offset physical_offset
; /* Current physical file offset */
101 gfc_offset logical_offset
; /* Current logical file offset */
102 gfc_offset dirty_offset
; /* Start of modified bytes in buffer */
103 gfc_offset file_length
; /* Length of the file, -1 if not seekable. */
106 int len
; /* Physical length of the current buffer */
107 int active
; /* Length of valid bytes in the buffer */
110 int ndirty
; /* Dirty bytes starting at dirty_offset */
112 unsigned unbuffered
:1, mmaped
:1;
114 char small_buffer
[BUFFER_SIZE
];
119 /*move_pos_offset()-- Move the record pointer right or left
120 *relative to current position */
123 move_pos_offset (stream
* st
, int pos_off
)
125 unix_stream
* str
= (unix_stream
*)st
;
128 str
->active
+= pos_off
;
132 str
->logical_offset
+= pos_off
;
134 if (str
->dirty_offset
+str
->ndirty
> str
->logical_offset
)
136 if (str
->ndirty
+ pos_off
> 0)
137 str
->ndirty
+= pos_off
;
140 str
->dirty_offset
+= pos_off
+ pos_off
;
151 /* fix_fd()-- Given a file descriptor, make sure it is not one of the
152 * standard descriptors, returning a non-standard descriptor. If the
153 * user specifies that system errors should go to standard output,
154 * then closes standard output, we don't want the system errors to a
155 * file that has been given file descriptor 1 or 0. We want to send
156 * the error to the invalid descriptor. */
161 int input
, output
, error
;
163 input
= output
= error
= 0;
165 /* Unix allocates the lowest descriptors first, so a loop is not
166 * required, but this order is. */
168 if (fd
== STDIN_FILENO
)
173 if (fd
== STDOUT_FILENO
)
178 if (fd
== STDERR_FILENO
)
185 close (STDIN_FILENO
);
187 close (STDOUT_FILENO
);
189 close (STDERR_FILENO
);
195 /* write()-- Write a buffer to a descriptor, allowing for short writes */
198 writen (int fd
, char *buffer
, int len
)
206 n
= write (fd
, buffer
, len
);
219 /* readn()-- Read bytes into a buffer, allowing for short reads. If
220 * fewer than len bytes are returned, it is because we've hit the end
224 readn (int fd
, char *buffer
, int len
)
232 n
= read (fd
, buffer
, len
);
249 /* get_oserror()-- Get the most recent operating system error. For
250 * unix, this is errno. */
256 return strerror (errno
);
260 /* sys_exit()-- Terminate the program with an exit code */
271 /*********************************************************************
272 File descriptor stream functions
273 *********************************************************************/
275 /* fd_flush()-- Write bytes that need to be written */
278 fd_flush (unix_stream
* s
)
284 if (s
->physical_offset
!= s
->dirty_offset
&&
285 lseek (s
->fd
, s
->dirty_offset
, SEEK_SET
) < 0)
288 if (writen (s
->fd
, s
->buffer
+ (s
->dirty_offset
- s
->buffer_offset
),
292 s
->physical_offset
= s
->dirty_offset
+ s
->ndirty
;
294 /* don't increment file_length if the file is non-seekable */
295 if (s
->file_length
!= -1 && s
->physical_offset
> s
->file_length
)
296 s
->file_length
= s
->physical_offset
;
303 /* fd_alloc()-- Arrange a buffer such that the salloc() request can be
304 * satisfied. This subroutine gets the buffer ready for whatever is
308 fd_alloc (unix_stream
* s
, gfc_offset where
, int *len
)
313 if (*len
<= BUFFER_SIZE
)
315 new_buffer
= s
->small_buffer
;
316 read_len
= BUFFER_SIZE
;
320 new_buffer
= get_mem (*len
);
324 /* Salvage bytes currently within the buffer. This is important for
325 * devices that cannot seek. */
327 if (s
->buffer
!= NULL
&& s
->buffer_offset
<= where
&&
328 where
<= s
->buffer_offset
+ s
->active
)
331 n
= s
->active
- (where
- s
->buffer_offset
);
332 memmove (new_buffer
, s
->buffer
+ (where
- s
->buffer_offset
), n
);
337 { /* new buffer starts off empty */
341 s
->buffer_offset
= where
;
343 /* free the old buffer if necessary */
345 if (s
->buffer
!= NULL
&& s
->buffer
!= s
->small_buffer
)
346 free_mem (s
->buffer
);
348 s
->buffer
= new_buffer
;
354 /* fd_alloc_r_at()-- Allocate a stream buffer for reading. Either
355 * we've already buffered the data or we need to load it. Returns
356 * NULL on I/O error. */
359 fd_alloc_r_at (unix_stream
* s
, int *len
, gfc_offset where
)
365 where
= s
->logical_offset
;
367 if (s
->buffer
!= NULL
&& s
->buffer_offset
<= where
&&
368 where
+ *len
<= s
->buffer_offset
+ s
->active
)
371 /* Return a position within the current buffer */
373 s
->logical_offset
= where
+ *len
;
374 return s
->buffer
+ where
- s
->buffer_offset
;
377 fd_alloc (s
, where
, len
);
379 m
= where
+ s
->active
;
381 if (s
->physical_offset
!= m
&& lseek (s
->fd
, m
, SEEK_SET
) < 0)
384 n
= read (s
->fd
, s
->buffer
+ s
->active
, s
->len
- s
->active
);
388 s
->physical_offset
= where
+ n
;
391 if (s
->active
< *len
)
392 *len
= s
->active
; /* Bytes actually available */
394 s
->logical_offset
= where
+ *len
;
400 /* fd_alloc_w_at()-- Allocate a stream buffer for writing. Either
401 * we've already buffered the data or we need to load it. */
404 fd_alloc_w_at (unix_stream
* s
, int *len
, gfc_offset where
)
409 where
= s
->logical_offset
;
411 if (s
->buffer
== NULL
|| s
->buffer_offset
> where
||
412 where
+ *len
> s
->buffer_offset
+ s
->len
)
415 if (fd_flush (s
) == FAILURE
)
417 fd_alloc (s
, where
, len
);
420 /* Return a position within the current buffer */
422 || where
> s
->dirty_offset
+ s
->ndirty
423 || s
->dirty_offset
> where
+ *len
)
424 { /* Discontiguous blocks, start with a clean buffer. */
425 /* Flush the buffer. */
428 s
->dirty_offset
= where
;
433 gfc_offset start
; /* Merge with the existing data. */
434 if (where
< s
->dirty_offset
)
437 start
= s
->dirty_offset
;
438 if (where
+ *len
> s
->dirty_offset
+ s
->ndirty
)
439 s
->ndirty
= where
+ *len
- start
;
441 s
->ndirty
= s
->dirty_offset
+ s
->ndirty
- start
;
442 s
->dirty_offset
= start
;
445 s
->logical_offset
= where
+ *len
;
447 n
= s
->logical_offset
- s
->buffer_offset
;
451 return s
->buffer
+ where
- s
->buffer_offset
;
456 fd_sfree (unix_stream
* s
)
459 if (s
->ndirty
!= 0 &&
460 (s
->buffer
!= s
->small_buffer
|| options
.all_unbuffered
||
469 fd_seek (unix_stream
* s
, gfc_offset offset
)
472 s
->physical_offset
= s
->logical_offset
= offset
;
474 return (lseek (s
->fd
, offset
, SEEK_SET
) < 0) ? FAILURE
: SUCCESS
;
478 /* truncate_file()-- Given a unit, truncate the file at the current
479 * position. Sets the physical location to the new end of the file.
480 * Returns nonzero on error. */
483 fd_truncate (unix_stream
* s
)
486 if (lseek (s
->fd
, s
->logical_offset
, SEEK_SET
) == -1)
489 /* non-seekable files, like terminals and fifo's fail the lseek.
490 the fd is a regular file at this point */
492 if (ftruncate (s
->fd
, s
->logical_offset
))
497 s
->physical_offset
= s
->file_length
= s
->logical_offset
;
504 fd_close (unix_stream
* s
)
507 if (fd_flush (s
) == FAILURE
)
510 if (s
->buffer
!= NULL
&& s
->buffer
!= s
->small_buffer
)
511 free_mem (s
->buffer
);
513 if (close (s
->fd
) < 0)
523 fd_open (unix_stream
* s
)
529 s
->st
.alloc_r_at
= (void *) fd_alloc_r_at
;
530 s
->st
.alloc_w_at
= (void *) fd_alloc_w_at
;
531 s
->st
.sfree
= (void *) fd_sfree
;
532 s
->st
.close
= (void *) fd_close
;
533 s
->st
.seek
= (void *) fd_seek
;
534 s
->st
.truncate
= (void *) fd_truncate
;
540 /*********************************************************************
541 mmap stream functions
543 Because mmap() is not capable of extending a file, we have to keep
544 track of how long the file is. We also have to be able to detect end
545 of file conditions. If there are multiple writers to the file (which
546 can only happen outside the current program), things will get
547 confused. Then again, things will get confused anyway.
549 *********************************************************************/
553 static int page_size
, page_mask
;
555 /* mmap_flush()-- Deletes a memory mapping if something is mapped. */
558 mmap_flush (unix_stream
* s
)
564 if (s
->buffer
== NULL
)
567 if (munmap (s
->buffer
, s
->active
))
577 /* mmap_alloc()-- mmap() a section of the file. The whole section is
578 * guaranteed to be mappable. */
581 mmap_alloc (unix_stream
* s
, gfc_offset where
, int *len
)
587 if (mmap_flush (s
) == FAILURE
)
590 offset
= where
& page_mask
; /* Round down to the next page */
592 length
= ((where
- offset
) & page_mask
) + 2 * page_size
;
594 p
= mmap (NULL
, length
, s
->prot
, MAP_SHARED
, s
->fd
, offset
);
595 if (p
== (char *) MAP_FAILED
)
600 s
->buffer_offset
= offset
;
608 mmap_alloc_r_at (unix_stream
* s
, int *len
, gfc_offset where
)
613 where
= s
->logical_offset
;
617 if ((s
->buffer
== NULL
|| s
->buffer_offset
> where
||
618 m
> s
->buffer_offset
+ s
->active
) &&
619 mmap_alloc (s
, where
, len
) == FAILURE
)
622 if (m
> s
->file_length
)
624 *len
= s
->file_length
- s
->logical_offset
;
625 s
->logical_offset
= s
->file_length
;
628 s
->logical_offset
= m
;
630 return s
->buffer
+ (where
- s
->buffer_offset
);
635 mmap_alloc_w_at (unix_stream
* s
, int *len
, gfc_offset where
)
638 where
= s
->logical_offset
;
640 /* If we're extending the file, we have to use file descriptor
643 if (where
+ *len
> s
->file_length
)
647 return fd_alloc_w_at (s
, len
, where
);
650 if ((s
->buffer
== NULL
|| s
->buffer_offset
> where
||
651 where
+ *len
> s
->buffer_offset
+ s
->active
) &&
652 mmap_alloc (s
, where
, len
) == FAILURE
)
655 s
->logical_offset
= where
+ *len
;
657 return s
->buffer
+ where
- s
->buffer_offset
;
662 mmap_seek (unix_stream
* s
, gfc_offset offset
)
665 s
->logical_offset
= offset
;
671 mmap_close (unix_stream
* s
)
677 if (close (s
->fd
) < 0)
686 mmap_sfree (unix_stream
* s
)
693 /* mmap_open()-- mmap_specific open. If the particular file cannot be
694 * mmap()-ed, we fall back to the file descriptor functions. */
697 mmap_open (unix_stream
* s
)
702 page_size
= getpagesize ();
705 p
= mmap (0, page_size
, s
->prot
, MAP_SHARED
, s
->fd
, 0);
706 if (p
== (char *) MAP_FAILED
)
712 munmap (p
, page_size
);
721 s
->st
.alloc_r_at
= (void *) mmap_alloc_r_at
;
722 s
->st
.alloc_w_at
= (void *) mmap_alloc_w_at
;
723 s
->st
.sfree
= (void *) mmap_sfree
;
724 s
->st
.close
= (void *) mmap_close
;
725 s
->st
.seek
= (void *) mmap_seek
;
726 s
->st
.truncate
= (void *) fd_truncate
;
728 if (lseek (s
->fd
, s
->file_length
, SEEK_SET
) < 0)
737 /*********************************************************************
738 memory stream functions - These are used for internal files
740 The idea here is that a single stream structure is created and all
741 requests must be satisfied from it. The location and size of the
742 buffer is the character variable supplied to the READ or WRITE
745 *********************************************************************/
749 mem_alloc_r_at (unix_stream
* s
, int *len
, gfc_offset where
)
754 where
= s
->logical_offset
;
756 if (where
< s
->buffer_offset
|| where
> s
->buffer_offset
+ s
->active
)
759 s
->logical_offset
= where
+ *len
;
761 n
= s
->buffer_offset
+ s
->active
- where
;
765 return s
->buffer
+ (where
- s
->buffer_offset
);
770 mem_alloc_w_at (unix_stream
* s
, int *len
, gfc_offset where
)
775 where
= s
->logical_offset
;
779 if (where
< s
->buffer_offset
|| m
> s
->buffer_offset
+ s
->active
)
782 s
->logical_offset
= m
;
784 return s
->buffer
+ (where
- s
->buffer_offset
);
789 mem_seek (unix_stream
* s
, gfc_offset offset
)
792 if (offset
> s
->file_length
)
798 s
->logical_offset
= offset
;
804 mem_truncate (unix_stream
* s
)
812 mem_close (unix_stream
* s
)
821 mem_sfree (unix_stream
* s
)
829 /*********************************************************************
830 Public functions -- A reimplementation of this module needs to
831 define functional equivalents of the following.
832 *********************************************************************/
834 /* empty_internal_buffer()-- Zero the buffer of Internal file */
837 empty_internal_buffer(stream
*strm
)
839 unix_stream
* s
= (unix_stream
*) strm
;
840 memset(s
->buffer
, ' ', s
->file_length
);
843 /* open_internal()-- Returns a stream structure from an internal file */
846 open_internal (char *base
, int length
)
850 s
= get_mem (sizeof (unix_stream
));
853 s
->buffer_offset
= 0;
855 s
->logical_offset
= 0;
856 s
->active
= s
->file_length
= length
;
858 s
->st
.alloc_r_at
= (void *) mem_alloc_r_at
;
859 s
->st
.alloc_w_at
= (void *) mem_alloc_w_at
;
860 s
->st
.sfree
= (void *) mem_sfree
;
861 s
->st
.close
= (void *) mem_close
;
862 s
->st
.seek
= (void *) mem_seek
;
863 s
->st
.truncate
= (void *) mem_truncate
;
869 /* fd_to_stream()-- Given an open file descriptor, build a stream
873 fd_to_stream (int fd
, int prot
)
878 s
= get_mem (sizeof (unix_stream
));
881 s
->buffer_offset
= 0;
882 s
->physical_offset
= 0;
883 s
->logical_offset
= 0;
886 /* Get the current length of the file. */
888 fstat (fd
, &statbuf
);
889 s
->file_length
= S_ISREG (statbuf
.st_mode
) ? statbuf
.st_size
: -1;
901 /* unpack_filename()-- Given a fortran string and a pointer to a
902 * buffer that is PATH_MAX characters, convert the fortran string to a
903 * C string in the buffer. Returns nonzero if this is not possible. */
906 unpack_filename (char *cstring
, const char *fstring
, int len
)
909 len
= fstrlen (fstring
, len
);
913 memmove (cstring
, fstring
, len
);
920 /* tempfile()-- Generate a temporary filename for a scratch file and
921 * open it. mkstemp() opens the file for reading and writing, but the
922 * library mode prevents anything that is not allowed. The descriptor
923 * is returns, which is less than zero on error. The template is
924 * pointed to by ioparm.file, which is copied into the unit structure
925 * and freed later. */
934 tempdir
= getenv ("GFORTRAN_TMPDIR");
936 tempdir
= getenv ("TMP");
938 tempdir
= DEFAULT_TEMPDIR
;
940 template = get_mem (strlen (tempdir
) + 20);
942 st_sprintf (template, "%s/gfortantmpXXXXXX", tempdir
);
944 fd
= mkstemp (template);
950 ioparm
.file
= template;
951 ioparm
.file_len
= strlen (template); /* Don't include trailing nul */
958 /* regular_file()-- Open a regular file. Returns the descriptor, which is less than zero on error. */
961 regular_file (unit_action action
, unit_status status
)
963 char path
[PATH_MAX
+ 1];
967 if (unpack_filename (path
, ioparm
.file
, ioparm
.file_len
))
969 errno
= ENOENT
; /* Fake an OS error */
985 case ACTION_READWRITE
:
990 internal_error ("regular_file(): Bad action");
996 mode
|= O_CREAT
| O_EXCL
;
999 case STATUS_OLD
: /* file must exist, so check for its existence */
1000 if (stat (path
, &statbuf
) < 0)
1004 case STATUS_UNKNOWN
:
1005 case STATUS_SCRATCH
:
1009 case STATUS_REPLACE
:
1010 mode
|= O_CREAT
| O_TRUNC
;
1014 internal_error ("regular_file(): Bad status");
1017 // mode |= O_LARGEFILE;
1019 return open (path
, mode
,
1020 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
1024 /* open_external()-- Open an external file, unix specific version.
1025 * Returns NULL on operating system error. */
1028 open_external (unit_action action
, unit_status status
)
1033 (status
== STATUS_SCRATCH
) ? tempfile () : regular_file (action
, status
);
1049 case ACTION_READWRITE
:
1050 prot
= PROT_READ
| PROT_WRITE
;
1054 internal_error ("open_external(): Bad action");
1057 /* If this is a scratch file, we can unlink it now and the file will
1058 * go away when it is closed. */
1060 if (status
== STATUS_SCRATCH
)
1061 unlink (ioparm
.file
);
1063 return fd_to_stream (fd
, prot
);
1067 /* input_stream()-- Return a stream pointer to the default input stream.
1068 * Called on initialization. */
1074 return fd_to_stream (STDIN_FILENO
, PROT_READ
);
1078 /* output_stream()-- Return a stream pointer to the default input stream.
1079 * Called on initialization. */
1082 output_stream (void)
1085 return fd_to_stream (STDOUT_FILENO
, PROT_WRITE
);
1089 /* init_error_stream()-- Return a pointer to the error stream. This
1090 * subroutine is called when the stream is needed, rather than at
1091 * initialization. We want to work even if memory has been seriously
1095 init_error_stream (void)
1097 static unix_stream error
;
1099 memset (&error
, '\0', sizeof (error
));
1101 error
.fd
= options
.use_stderr
? STDERR_FILENO
: STDOUT_FILENO
;
1103 error
.st
.alloc_w_at
= (void *) fd_alloc_w_at
;
1104 error
.st
.sfree
= (void *) fd_sfree
;
1106 error
.unbuffered
= 1;
1107 error
.buffer
= error
.small_buffer
;
1109 return (stream
*) & error
;
1113 /* compare_file_filename()-- Given an open stream and a fortran string
1114 * that is a filename, figure out if the file is the same as the
1118 compare_file_filename (stream
* s
, const char *name
, int len
)
1120 char path
[PATH_MAX
+ 1];
1121 struct stat st1
, st2
;
1123 if (unpack_filename (path
, name
, len
))
1124 return 0; /* Can't be the same */
1126 /* If the filename doesn't exist, then there is no match with the
1129 if (stat (path
, &st1
) < 0)
1132 fstat (((unix_stream
*) s
)->fd
, &st2
);
1134 return (st1
.st_dev
== st2
.st_dev
) && (st1
.st_ino
== st2
.st_ino
);
1138 /* find_file0()-- Recursive work function for find_file() */
1141 find_file0 (gfc_unit
* u
, struct stat
*st1
)
1149 if (fstat (((unix_stream
*) u
->s
)->fd
, &st2
) >= 0 &&
1150 st1
->st_dev
== st2
.st_dev
&& st1
->st_ino
== st2
.st_ino
)
1153 v
= find_file0 (u
->left
, st1
);
1157 v
= find_file0 (u
->right
, st1
);
1165 /* find_file()-- Take the current filename and see if there is a unit
1166 * that has the file already open. Returns a pointer to the unit if so. */
1171 char path
[PATH_MAX
+ 1];
1172 struct stat statbuf
;
1174 if (unpack_filename (path
, ioparm
.file
, ioparm
.file_len
))
1177 if (stat (path
, &statbuf
) < 0)
1180 return find_file0 (g
.unit_root
, &statbuf
);
1184 /* stream_at_bof()-- Returns nonzero if the stream is at the beginning
1188 stream_at_bof (stream
* s
)
1192 us
= (unix_stream
*) s
;
1195 return 0; /* File is not seekable */
1197 return us
->logical_offset
== 0;
1201 /* stream_at_eof()-- Returns nonzero if the stream is at the beginning
1205 stream_at_eof (stream
* s
)
1209 us
= (unix_stream
*) s
;
1212 return 0; /* File is not seekable */
1214 return us
->logical_offset
== us
->dirty_offset
;
1218 /* delete_file()-- Given a unit structure, delete the file associated
1219 * with the unit. Returns nonzero if something went wrong. */
1222 delete_file (gfc_unit
* u
)
1224 char path
[PATH_MAX
+ 1];
1226 if (unpack_filename (path
, u
->file
, u
->file_len
))
1227 { /* Shouldn't be possible */
1232 return unlink (path
);
1236 /* file_exists()-- Returns nonzero if the current filename exists on
1242 char path
[PATH_MAX
+ 1];
1243 struct stat statbuf
;
1245 if (unpack_filename (path
, ioparm
.file
, ioparm
.file_len
))
1248 if (stat (path
, &statbuf
) < 0)
1256 static const char *yes
= "YES", *no
= "NO", *unknown
= "UNKNOWN";
1258 /* inquire_sequential()-- Given a fortran string, determine if the
1259 * file is suitable for sequential access. Returns a C-style
1263 inquire_sequential (const char *string
, int len
)
1265 char path
[PATH_MAX
+ 1];
1266 struct stat statbuf
;
1268 if (string
== NULL
||
1269 unpack_filename (path
, string
, len
) || stat (path
, &statbuf
) < 0)
1272 if (S_ISREG (statbuf
.st_mode
) ||
1273 S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
))
1276 if (S_ISDIR (statbuf
.st_mode
) || S_ISBLK (statbuf
.st_mode
))
1283 /* inquire_direct()-- Given a fortran string, determine if the file is
1284 * suitable for direct access. Returns a C-style string. */
1287 inquire_direct (const char *string
, int len
)
1289 char path
[PATH_MAX
+ 1];
1290 struct stat statbuf
;
1292 if (string
== NULL
||
1293 unpack_filename (path
, string
, len
) || stat (path
, &statbuf
) < 0)
1296 if (S_ISREG (statbuf
.st_mode
) || S_ISBLK (statbuf
.st_mode
))
1299 if (S_ISDIR (statbuf
.st_mode
) ||
1300 S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
))
1307 /* inquire_formatted()-- Given a fortran string, determine if the file
1308 * is suitable for formatted form. Returns a C-style string. */
1311 inquire_formatted (const char *string
, int len
)
1313 char path
[PATH_MAX
+ 1];
1314 struct stat statbuf
;
1316 if (string
== NULL
||
1317 unpack_filename (path
, string
, len
) || stat (path
, &statbuf
) < 0)
1320 if (S_ISREG (statbuf
.st_mode
) ||
1321 S_ISBLK (statbuf
.st_mode
) ||
1322 S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
))
1325 if (S_ISDIR (statbuf
.st_mode
))
1332 /* inquire_unformatted()-- Given a fortran string, determine if the file
1333 * is suitable for unformatted form. Returns a C-style string. */
1336 inquire_unformatted (const char *string
, int len
)
1339 return inquire_formatted (string
, len
);
1343 /* inquire_access()-- Given a fortran string, determine if the file is
1344 * suitable for access. */
1347 inquire_access (const char *string
, int len
, int mode
)
1349 char path
[PATH_MAX
+ 1];
1351 if (string
== NULL
|| unpack_filename (path
, string
, len
) ||
1352 access (path
, mode
) < 0)
1359 /* inquire_read()-- Given a fortran string, determine if the file is
1360 * suitable for READ access. */
1363 inquire_read (const char *string
, int len
)
1366 return inquire_access (string
, len
, R_OK
);
1370 /* inquire_write()-- Given a fortran string, determine if the file is
1371 * suitable for READ access. */
1374 inquire_write (const char *string
, int len
)
1377 return inquire_access (string
, len
, W_OK
);
1381 /* inquire_readwrite()-- Given a fortran string, determine if the file is
1382 * suitable for read and write access. */
1385 inquire_readwrite (const char *string
, int len
)
1388 return inquire_access (string
, len
, R_OK
| W_OK
);
1392 /* file_length()-- Return the file length in bytes, -1 if unknown */
1395 file_length (stream
* s
)
1398 return ((unix_stream
*) s
)->file_length
;
1402 /* file_position()-- Return the current position of the file */
1405 file_position (stream
* s
)
1408 return ((unix_stream
*) s
)->logical_offset
;
1412 /* is_seekable()-- Return nonzero if the stream is seekable, zero if
1416 is_seekable (stream
* s
)
1418 /* by convention, if file_length == -1, the file is not seekable
1419 note that a mmapped file is always seekable, an fd_ file may
1421 return ((unix_stream
*) s
)->file_length
!=-1;
1427 return fd_flush( (unix_stream
*) s
);
1431 /* How files are stored: This is an operating-system specific issue,
1432 and therefore belongs here. There are three cases to consider.
1435 Records are written as block of bytes corresponding to the record
1436 length of the file. This goes for both formatted and unformatted
1437 records. Positioning is done explicitly for each data transfer,
1438 so positioning is not much of an issue.
1440 Sequential Formatted:
1441 Records are separated by newline characters. The newline character
1442 is prohibited from appearing in a string. If it does, this will be
1443 messed up on the next read. End of file is also the end of a record.
1445 Sequential Unformatted:
1446 In this case, we are merely copying bytes to and from main storage,
1447 yet we need to keep track of varying record lengths. We adopt
1448 the solution used by f2c. Each record contains a pair of length
1451 Length of record n in bytes
1453 Length of record n in bytes
1455 Length of record n+1 in bytes
1457 Length of record n+1 in bytes
1459 The length is stored at the end of a record to allow backspacing to the
1460 previous record. Between data transfer statements, the file pointer
1461 is left pointing to the first length of the current record.
1463 ENDFILE records are never explicitly stored.