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 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
20 Libgfortran is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with Libgfortran; see the file COPYING. If not, write to
27 the Free Software Foundation, 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA. */
30 /* Unix stream I/O module */
41 #ifdef HAVE_SYS_MMAN_H
47 #include "libgfortran.h"
55 #define MAP_FAILED ((void *) -1)
66 /* These flags aren't defined on all targets (mingw32), so provide them
84 /* This implementation of stream I/O is based on the paper:
86 * "Exploiting the advantages of mapped files for stream I/O",
87 * O. Krieger, M. Stumm and R. Umrau, "Proceedings of the 1992 Winter
88 * USENIX conference", p. 27-42.
90 * It differs in a number of ways from the version described in the
91 * paper. First of all, threads are not an issue during I/O and we
92 * also don't have to worry about having multiple regions, since
93 * fortran's I/O model only allows you to be one place at a time.
95 * On the other hand, we have to be able to writing at the end of a
96 * stream, read from the start of a stream or read and write blocks of
97 * bytes from an arbitrary position. After opening a file, a pointer
98 * to a stream structure is returned, which is used to handle file
99 * accesses until the file is closed.
101 * salloc_at_r(stream, len, where)-- Given a stream pointer, return a
102 * pointer to a block of memory that mirror the file at position
103 * 'where' that is 'len' bytes long. The len integer is updated to
104 * reflect how many bytes were actually read. The only reason for a
105 * short read is end of file. The file pointer is updated. The
106 * pointer is valid until the next call to salloc_*.
108 * salloc_at_w(stream, len, where)-- Given the stream pointer, returns
109 * a pointer to a block of memory that is updated to reflect the state
110 * of the file. The length of the buffer is always equal to that
111 * requested. The buffer must be completely set by the caller. When
112 * data has been written, the sfree() function must be called to
113 * indicate that the caller is done writing data to the buffer. This
114 * may or may not cause a physical write.
116 * Short forms of these are salloc_r() and salloc_w() which drop the
117 * 'where' parameter and use the current file pointer. */
120 #define BUFFER_SIZE 8192
127 gfc_offset buffer_offset
; /* File offset of the start of the buffer */
128 gfc_offset physical_offset
; /* Current physical file offset */
129 gfc_offset logical_offset
; /* Current logical file offset */
130 gfc_offset dirty_offset
; /* Start of modified bytes in buffer */
131 gfc_offset file_length
; /* Length of the file, -1 if not seekable. */
134 int len
; /* Physical length of the current buffer */
135 int active
; /* Length of valid bytes in the buffer */
138 int ndirty
; /* Dirty bytes starting at dirty_offset */
140 unsigned unbuffered
:1, mmaped
:1;
142 char small_buffer
[BUFFER_SIZE
];
147 /*move_pos_offset()-- Move the record pointer right or left
148 *relative to current position */
151 move_pos_offset (stream
* st
, int pos_off
)
153 unix_stream
* str
= (unix_stream
*)st
;
156 str
->active
+= pos_off
;
160 str
->logical_offset
+= pos_off
;
162 if (str
->dirty_offset
+str
->ndirty
> str
->logical_offset
)
164 if (str
->ndirty
+ pos_off
> 0)
165 str
->ndirty
+= pos_off
;
168 str
->dirty_offset
+= pos_off
+ pos_off
;
179 /* fix_fd()-- Given a file descriptor, make sure it is not one of the
180 * standard descriptors, returning a non-standard descriptor. If the
181 * user specifies that system errors should go to standard output,
182 * then closes standard output, we don't want the system errors to a
183 * file that has been given file descriptor 1 or 0. We want to send
184 * the error to the invalid descriptor. */
189 int input
, output
, error
;
191 input
= output
= error
= 0;
193 /* Unix allocates the lowest descriptors first, so a loop is not
194 required, but this order is. */
196 if (fd
== STDIN_FILENO
)
201 if (fd
== STDOUT_FILENO
)
206 if (fd
== STDERR_FILENO
)
213 close (STDIN_FILENO
);
215 close (STDOUT_FILENO
);
217 close (STDERR_FILENO
);
223 /* write()-- Write a buffer to a descriptor, allowing for short writes */
226 writen (int fd
, char *buffer
, int len
)
234 n
= write (fd
, buffer
, len
);
247 /* readn()-- Read bytes into a buffer, allowing for short reads. If
248 * fewer than len bytes are returned, it is because we've hit the end
252 readn (int fd
, char *buffer
, int len
)
260 n
= read (fd
, buffer
, len
);
277 /* get_oserror()-- Get the most recent operating system error. For
278 * unix, this is errno. */
283 return strerror (errno
);
287 /* sys_exit()-- Terminate the program with an exit code */
296 /*********************************************************************
297 File descriptor stream functions
298 *********************************************************************/
300 /* fd_flush()-- Write bytes that need to be written */
303 fd_flush (unix_stream
* s
)
308 if (s
->physical_offset
!= s
->dirty_offset
&&
309 lseek (s
->fd
, s
->dirty_offset
, SEEK_SET
) < 0)
312 if (writen (s
->fd
, s
->buffer
+ (s
->dirty_offset
- s
->buffer_offset
),
316 s
->physical_offset
= s
->dirty_offset
+ s
->ndirty
;
318 /* don't increment file_length if the file is non-seekable */
319 if (s
->file_length
!= -1 && s
->physical_offset
> s
->file_length
)
320 s
->file_length
= s
->physical_offset
;
327 /* fd_alloc()-- Arrange a buffer such that the salloc() request can be
328 * satisfied. This subroutine gets the buffer ready for whatever is
332 fd_alloc (unix_stream
* s
, gfc_offset where
, int *len
)
337 if (*len
<= BUFFER_SIZE
)
339 new_buffer
= s
->small_buffer
;
340 read_len
= BUFFER_SIZE
;
344 new_buffer
= get_mem (*len
);
348 /* Salvage bytes currently within the buffer. This is important for
349 * devices that cannot seek. */
351 if (s
->buffer
!= NULL
&& s
->buffer_offset
<= where
&&
352 where
<= s
->buffer_offset
+ s
->active
)
355 n
= s
->active
- (where
- s
->buffer_offset
);
356 memmove (new_buffer
, s
->buffer
+ (where
- s
->buffer_offset
), n
);
361 { /* new buffer starts off empty */
365 s
->buffer_offset
= where
;
367 /* free the old buffer if necessary */
369 if (s
->buffer
!= NULL
&& s
->buffer
!= s
->small_buffer
)
370 free_mem (s
->buffer
);
372 s
->buffer
= new_buffer
;
378 /* fd_alloc_r_at()-- Allocate a stream buffer for reading. Either
379 * we've already buffered the data or we need to load it. Returns
380 * NULL on I/O error. */
383 fd_alloc_r_at (unix_stream
* s
, int *len
, gfc_offset where
)
389 where
= s
->logical_offset
;
391 if (s
->buffer
!= NULL
&& s
->buffer_offset
<= where
&&
392 where
+ *len
<= s
->buffer_offset
+ s
->active
)
395 /* Return a position within the current buffer */
397 s
->logical_offset
= where
+ *len
;
398 return s
->buffer
+ where
- s
->buffer_offset
;
401 fd_alloc (s
, where
, len
);
403 m
= where
+ s
->active
;
405 if (s
->physical_offset
!= m
&& lseek (s
->fd
, m
, SEEK_SET
) < 0)
408 n
= read (s
->fd
, s
->buffer
+ s
->active
, s
->len
- s
->active
);
412 s
->physical_offset
= where
+ n
;
415 if (s
->active
< *len
)
416 *len
= s
->active
; /* Bytes actually available */
418 s
->logical_offset
= where
+ *len
;
424 /* fd_alloc_w_at()-- Allocate a stream buffer for writing. Either
425 * we've already buffered the data or we need to load it. */
428 fd_alloc_w_at (unix_stream
* s
, int *len
, gfc_offset where
)
433 where
= s
->logical_offset
;
435 if (s
->buffer
== NULL
|| s
->buffer_offset
> where
||
436 where
+ *len
> s
->buffer_offset
+ s
->len
)
439 if (fd_flush (s
) == FAILURE
)
441 fd_alloc (s
, where
, len
);
444 /* Return a position within the current buffer */
446 || where
> s
->dirty_offset
+ s
->ndirty
447 || s
->dirty_offset
> where
+ *len
)
448 { /* Discontiguous blocks, start with a clean buffer. */
449 /* Flush the buffer. */
452 s
->dirty_offset
= where
;
457 gfc_offset start
; /* Merge with the existing data. */
458 if (where
< s
->dirty_offset
)
461 start
= s
->dirty_offset
;
462 if (where
+ *len
> s
->dirty_offset
+ s
->ndirty
)
463 s
->ndirty
= where
+ *len
- start
;
465 s
->ndirty
= s
->dirty_offset
+ s
->ndirty
- start
;
466 s
->dirty_offset
= start
;
469 s
->logical_offset
= where
+ *len
;
471 if (where
+ *len
> s
->file_length
)
472 s
->file_length
= where
+ *len
;
474 n
= s
->logical_offset
- s
->buffer_offset
;
478 return s
->buffer
+ where
- s
->buffer_offset
;
483 fd_sfree (unix_stream
* s
)
485 if (s
->ndirty
!= 0 &&
486 (s
->buffer
!= s
->small_buffer
|| options
.all_unbuffered
||
495 fd_seek (unix_stream
* s
, gfc_offset offset
)
497 s
->physical_offset
= s
->logical_offset
= offset
;
499 return (lseek (s
->fd
, offset
, SEEK_SET
) < 0) ? FAILURE
: SUCCESS
;
503 /* truncate_file()-- Given a unit, truncate the file at the current
504 * position. Sets the physical location to the new end of the file.
505 * Returns nonzero on error. */
508 fd_truncate (unix_stream
* s
)
510 if (lseek (s
->fd
, s
->logical_offset
, SEEK_SET
) == -1)
513 /* non-seekable files, like terminals and fifo's fail the lseek.
514 the fd is a regular file at this point */
516 if (ftruncate (s
->fd
, s
->logical_offset
))
519 s
->physical_offset
= s
->file_length
= s
->logical_offset
;
526 fd_close (unix_stream
* s
)
528 if (fd_flush (s
) == FAILURE
)
531 if (s
->buffer
!= NULL
&& s
->buffer
!= s
->small_buffer
)
532 free_mem (s
->buffer
);
534 if (close (s
->fd
) < 0)
544 fd_open (unix_stream
* s
)
549 s
->st
.alloc_r_at
= (void *) fd_alloc_r_at
;
550 s
->st
.alloc_w_at
= (void *) fd_alloc_w_at
;
551 s
->st
.sfree
= (void *) fd_sfree
;
552 s
->st
.close
= (void *) fd_close
;
553 s
->st
.seek
= (void *) fd_seek
;
554 s
->st
.truncate
= (void *) fd_truncate
;
560 /*********************************************************************
561 mmap stream functions
563 Because mmap() is not capable of extending a file, we have to keep
564 track of how long the file is. We also have to be able to detect end
565 of file conditions. If there are multiple writers to the file (which
566 can only happen outside the current program), things will get
567 confused. Then again, things will get confused anyway.
569 *********************************************************************/
573 static int page_size
, page_mask
;
575 /* mmap_flush()-- Deletes a memory mapping if something is mapped. */
578 mmap_flush (unix_stream
* s
)
583 if (s
->buffer
== NULL
)
586 if (munmap (s
->buffer
, s
->active
))
596 /* mmap_alloc()-- mmap() a section of the file. The whole section is
597 * guaranteed to be mappable. */
600 mmap_alloc (unix_stream
* s
, gfc_offset where
, int *len
)
606 if (mmap_flush (s
) == FAILURE
)
609 offset
= where
& page_mask
; /* Round down to the next page */
611 length
= ((where
- offset
) & page_mask
) + 2 * page_size
;
613 p
= mmap (NULL
, length
, s
->prot
, MAP_SHARED
, s
->fd
, offset
);
614 if (p
== (char *) MAP_FAILED
)
619 s
->buffer_offset
= offset
;
627 mmap_alloc_r_at (unix_stream
* s
, int *len
, gfc_offset where
)
632 where
= s
->logical_offset
;
636 if ((s
->buffer
== NULL
|| s
->buffer_offset
> where
||
637 m
> s
->buffer_offset
+ s
->active
) &&
638 mmap_alloc (s
, where
, len
) == FAILURE
)
641 if (m
> s
->file_length
)
643 *len
= s
->file_length
- s
->logical_offset
;
644 s
->logical_offset
= s
->file_length
;
647 s
->logical_offset
= m
;
649 return s
->buffer
+ (where
- s
->buffer_offset
);
654 mmap_alloc_w_at (unix_stream
* s
, int *len
, gfc_offset where
)
657 where
= s
->logical_offset
;
659 /* If we're extending the file, we have to use file descriptor
662 if (where
+ *len
> s
->file_length
)
666 return fd_alloc_w_at (s
, len
, where
);
669 if ((s
->buffer
== NULL
|| s
->buffer_offset
> where
||
670 where
+ *len
> s
->buffer_offset
+ s
->active
||
671 where
< s
->buffer_offset
+ s
->active
) &&
672 mmap_alloc (s
, where
, len
) == FAILURE
)
675 s
->logical_offset
= where
+ *len
;
677 return s
->buffer
+ where
- s
->buffer_offset
;
682 mmap_seek (unix_stream
* s
, gfc_offset offset
)
684 s
->logical_offset
= offset
;
690 mmap_close (unix_stream
* s
)
696 if (close (s
->fd
) < 0)
705 mmap_sfree (unix_stream
* s
)
711 /* mmap_open()-- mmap_specific open. If the particular file cannot be
712 * mmap()-ed, we fall back to the file descriptor functions. */
715 mmap_open (unix_stream
* s
)
720 page_size
= getpagesize ();
723 p
= mmap (0, page_size
, s
->prot
, MAP_SHARED
, s
->fd
, 0);
724 if (p
== (char *) MAP_FAILED
)
730 munmap (p
, page_size
);
739 s
->st
.alloc_r_at
= (void *) mmap_alloc_r_at
;
740 s
->st
.alloc_w_at
= (void *) mmap_alloc_w_at
;
741 s
->st
.sfree
= (void *) mmap_sfree
;
742 s
->st
.close
= (void *) mmap_close
;
743 s
->st
.seek
= (void *) mmap_seek
;
744 s
->st
.truncate
= (void *) fd_truncate
;
746 if (lseek (s
->fd
, s
->file_length
, SEEK_SET
) < 0)
755 /*********************************************************************
756 memory stream functions - These are used for internal files
758 The idea here is that a single stream structure is created and all
759 requests must be satisfied from it. The location and size of the
760 buffer is the character variable supplied to the READ or WRITE
763 *********************************************************************/
767 mem_alloc_r_at (unix_stream
* s
, int *len
, gfc_offset where
)
772 where
= s
->logical_offset
;
774 if (where
< s
->buffer_offset
|| where
> s
->buffer_offset
+ s
->active
)
777 s
->logical_offset
= where
+ *len
;
779 n
= s
->buffer_offset
+ s
->active
- where
;
783 return s
->buffer
+ (where
- s
->buffer_offset
);
788 mem_alloc_w_at (unix_stream
* s
, int *len
, gfc_offset where
)
793 where
= s
->logical_offset
;
797 if (where
< s
->buffer_offset
|| m
> s
->buffer_offset
+ s
->active
)
800 s
->logical_offset
= m
;
802 return s
->buffer
+ (where
- s
->buffer_offset
);
807 mem_seek (unix_stream
* s
, gfc_offset offset
)
809 if (offset
> s
->file_length
)
815 s
->logical_offset
= offset
;
821 mem_truncate (unix_stream
* s
)
828 mem_close (unix_stream
* s
)
837 mem_sfree (unix_stream
* s
)
844 /*********************************************************************
845 Public functions -- A reimplementation of this module needs to
846 define functional equivalents of the following.
847 *********************************************************************/
849 /* empty_internal_buffer()-- Zero the buffer of Internal file */
852 empty_internal_buffer(stream
*strm
)
854 unix_stream
* s
= (unix_stream
*) strm
;
855 memset(s
->buffer
, ' ', s
->file_length
);
858 /* open_internal()-- Returns a stream structure from an internal file */
861 open_internal (char *base
, int length
)
865 s
= get_mem (sizeof (unix_stream
));
868 s
->buffer_offset
= 0;
870 s
->logical_offset
= 0;
871 s
->active
= s
->file_length
= length
;
873 s
->st
.alloc_r_at
= (void *) mem_alloc_r_at
;
874 s
->st
.alloc_w_at
= (void *) mem_alloc_w_at
;
875 s
->st
.sfree
= (void *) mem_sfree
;
876 s
->st
.close
= (void *) mem_close
;
877 s
->st
.seek
= (void *) mem_seek
;
878 s
->st
.truncate
= (void *) mem_truncate
;
884 /* fd_to_stream()-- Given an open file descriptor, build a stream
888 fd_to_stream (int fd
, int prot
)
893 s
= get_mem (sizeof (unix_stream
));
896 s
->buffer_offset
= 0;
897 s
->physical_offset
= 0;
898 s
->logical_offset
= 0;
901 /* Get the current length of the file. */
903 fstat (fd
, &statbuf
);
904 s
->file_length
= S_ISREG (statbuf
.st_mode
) ? statbuf
.st_size
: -1;
916 /* Given the Fortran unit number, convert it to a C file descriptor. */
923 us
= find_unit(unit
);
927 return ((unix_stream
*) us
->s
)->fd
;
931 /* unpack_filename()-- Given a fortran string and a pointer to a
932 * buffer that is PATH_MAX characters, convert the fortran string to a
933 * C string in the buffer. Returns nonzero if this is not possible. */
936 unpack_filename (char *cstring
, const char *fstring
, int len
)
938 len
= fstrlen (fstring
, len
);
942 memmove (cstring
, fstring
, len
);
949 /* tempfile()-- Generate a temporary filename for a scratch file and
950 * open it. mkstemp() opens the file for reading and writing, but the
951 * library mode prevents anything that is not allowed. The descriptor
952 * is returned, which is -1 on error. The template is pointed to by
953 * ioparm.file, which is copied into the unit structure
954 * and freed later. */
963 tempdir
= getenv ("GFORTRAN_TMPDIR");
965 tempdir
= getenv ("TMP");
967 tempdir
= DEFAULT_TEMPDIR
;
969 template = get_mem (strlen (tempdir
) + 20);
971 st_sprintf (template, "%s/gfortrantmpXXXXXX", tempdir
);
975 fd
= mkstemp (template);
977 #else /* HAVE_MKSTEMP */
979 if (mktemp (template))
981 fd
= open (template, O_CREAT
| O_EXCL
, S_IREAD
| S_IWRITE
);
982 while (!(fd
== -1 && errno
== EEXIST
) && mktemp (template));
986 #endif /* HAVE_MKSTEMP */
992 ioparm
.file
= template;
993 ioparm
.file_len
= strlen (template); /* Don't include trailing nul */
1000 /* regular_file()-- Open a regular file.
1001 * Change flags->action if it is ACTION_UNSPECIFIED on entry,
1002 * unless an error occurs.
1003 * Returns the descriptor, which is less than zero on error. */
1006 regular_file (unit_flags
*flags
)
1008 char path
[PATH_MAX
+ 1];
1014 if (unpack_filename (path
, ioparm
.file
, ioparm
.file_len
))
1016 errno
= ENOENT
; /* Fake an OS error */
1022 switch (flags
->action
)
1032 case ACTION_READWRITE
:
1033 case ACTION_UNSPECIFIED
:
1038 internal_error ("regular_file(): Bad action");
1041 switch (flags
->status
)
1044 crflag
= O_CREAT
| O_EXCL
;
1047 case STATUS_OLD
: /* open will fail if the file does not exist*/
1051 case STATUS_UNKNOWN
:
1052 case STATUS_SCRATCH
:
1056 case STATUS_REPLACE
:
1057 crflag
= O_CREAT
| O_TRUNC
;
1061 internal_error ("regular_file(): Bad status");
1064 /* rwflag |= O_LARGEFILE; */
1066 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
1067 fd
= open (path
, rwflag
| crflag
, mode
);
1068 if (flags
->action
!= ACTION_UNSPECIFIED
)
1073 flags
->action
= ACTION_READWRITE
;
1076 if (errno
!= EACCES
)
1079 /* retry for read-only access */
1081 fd
= open (path
, rwflag
| crflag
, mode
);
1084 flags
->action
= ACTION_READ
;
1085 return fd
; /* success */
1088 if (errno
!= EACCES
)
1089 return fd
; /* failure */
1091 /* retry for write-only access */
1093 fd
= open (path
, rwflag
| crflag
, mode
);
1096 flags
->action
= ACTION_WRITE
;
1097 return fd
; /* success */
1099 return fd
; /* failure */
1103 /* open_external()-- Open an external file, unix specific version.
1104 * Change flags->action if it is ACTION_UNSPECIFIED on entry.
1105 * Returns NULL on operating system error. */
1108 open_external (unit_flags
*flags
)
1112 if (flags
->status
== STATUS_SCRATCH
)
1115 if (flags
->action
== ACTION_UNSPECIFIED
)
1116 flags
->action
= ACTION_READWRITE
;
1117 /* We can unlink scratch files now and it will go away when closed. */
1118 unlink (ioparm
.file
);
1122 /* regular_file resets flags->action if it is ACTION_UNSPECIFIED and
1124 fd
= regular_file (flags
);
1131 switch (flags
->action
)
1141 case ACTION_READWRITE
:
1142 prot
= PROT_READ
| PROT_WRITE
;
1146 internal_error ("open_external(): Bad action");
1149 return fd_to_stream (fd
, prot
);
1153 /* input_stream()-- Return a stream pointer to the default input stream.
1154 * Called on initialization. */
1159 return fd_to_stream (STDIN_FILENO
, PROT_READ
);
1163 /* output_stream()-- Return a stream pointer to the default output stream.
1164 * Called on initialization. */
1167 output_stream (void)
1169 return fd_to_stream (STDOUT_FILENO
, PROT_WRITE
);
1173 /* error_stream()-- Return a stream pointer to the default error stream.
1174 * Called on initialization. */
1179 return fd_to_stream (STDERR_FILENO
, PROT_WRITE
);
1182 /* init_error_stream()-- Return a pointer to the error stream. This
1183 * subroutine is called when the stream is needed, rather than at
1184 * initialization. We want to work even if memory has been seriously
1188 init_error_stream (void)
1190 static unix_stream error
;
1192 memset (&error
, '\0', sizeof (error
));
1194 error
.fd
= options
.use_stderr
? STDERR_FILENO
: STDOUT_FILENO
;
1196 error
.st
.alloc_w_at
= (void *) fd_alloc_w_at
;
1197 error
.st
.sfree
= (void *) fd_sfree
;
1199 error
.unbuffered
= 1;
1200 error
.buffer
= error
.small_buffer
;
1202 return (stream
*) & error
;
1206 /* compare_file_filename()-- Given an open stream and a fortran string
1207 * that is a filename, figure out if the file is the same as the
1211 compare_file_filename (stream
* s
, const char *name
, int len
)
1213 char path
[PATH_MAX
+ 1];
1214 struct stat st1
, st2
;
1216 if (unpack_filename (path
, name
, len
))
1217 return 0; /* Can't be the same */
1219 /* If the filename doesn't exist, then there is no match with the
1222 if (stat (path
, &st1
) < 0)
1225 fstat (((unix_stream
*) s
)->fd
, &st2
);
1227 return (st1
.st_dev
== st2
.st_dev
) && (st1
.st_ino
== st2
.st_ino
);
1231 /* find_file0()-- Recursive work function for find_file() */
1234 find_file0 (gfc_unit
* u
, struct stat
*st1
)
1242 if (fstat (((unix_stream
*) u
->s
)->fd
, &st2
) >= 0 &&
1243 st1
->st_dev
== st2
.st_dev
&& st1
->st_ino
== st2
.st_ino
)
1246 v
= find_file0 (u
->left
, st1
);
1250 v
= find_file0 (u
->right
, st1
);
1258 /* find_file()-- Take the current filename and see if there is a unit
1259 * that has the file already open. Returns a pointer to the unit if so. */
1264 char path
[PATH_MAX
+ 1];
1265 struct stat statbuf
;
1267 if (unpack_filename (path
, ioparm
.file
, ioparm
.file_len
))
1270 if (stat (path
, &statbuf
) < 0)
1273 return find_file0 (g
.unit_root
, &statbuf
);
1277 /* stream_at_bof()-- Returns nonzero if the stream is at the beginning
1281 stream_at_bof (stream
* s
)
1285 us
= (unix_stream
*) s
;
1288 return 0; /* File is not seekable */
1290 return us
->logical_offset
== 0;
1294 /* stream_at_eof()-- Returns nonzero if the stream is at the beginning
1298 stream_at_eof (stream
* s
)
1302 us
= (unix_stream
*) s
;
1305 return 0; /* File is not seekable */
1307 return us
->logical_offset
== us
->dirty_offset
;
1311 /* delete_file()-- Given a unit structure, delete the file associated
1312 * with the unit. Returns nonzero if something went wrong. */
1315 delete_file (gfc_unit
* u
)
1317 char path
[PATH_MAX
+ 1];
1319 if (unpack_filename (path
, u
->file
, u
->file_len
))
1320 { /* Shouldn't be possible */
1325 return unlink (path
);
1329 /* file_exists()-- Returns nonzero if the current filename exists on
1335 char path
[PATH_MAX
+ 1];
1336 struct stat statbuf
;
1338 if (unpack_filename (path
, ioparm
.file
, ioparm
.file_len
))
1341 if (stat (path
, &statbuf
) < 0)
1349 static const char *yes
= "YES", *no
= "NO", *unknown
= "UNKNOWN";
1351 /* inquire_sequential()-- Given a fortran string, determine if the
1352 * file is suitable for sequential access. Returns a C-style
1356 inquire_sequential (const char *string
, int len
)
1358 char path
[PATH_MAX
+ 1];
1359 struct stat statbuf
;
1361 if (string
== NULL
||
1362 unpack_filename (path
, string
, len
) || stat (path
, &statbuf
) < 0)
1365 if (S_ISREG (statbuf
.st_mode
) ||
1366 S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
))
1369 if (S_ISDIR (statbuf
.st_mode
) || S_ISBLK (statbuf
.st_mode
))
1376 /* inquire_direct()-- Given a fortran string, determine if the file is
1377 * suitable for direct access. Returns a C-style string. */
1380 inquire_direct (const char *string
, int len
)
1382 char path
[PATH_MAX
+ 1];
1383 struct stat statbuf
;
1385 if (string
== NULL
||
1386 unpack_filename (path
, string
, len
) || stat (path
, &statbuf
) < 0)
1389 if (S_ISREG (statbuf
.st_mode
) || S_ISBLK (statbuf
.st_mode
))
1392 if (S_ISDIR (statbuf
.st_mode
) ||
1393 S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
))
1400 /* inquire_formatted()-- Given a fortran string, determine if the file
1401 * is suitable for formatted form. Returns a C-style string. */
1404 inquire_formatted (const char *string
, int len
)
1406 char path
[PATH_MAX
+ 1];
1407 struct stat statbuf
;
1409 if (string
== NULL
||
1410 unpack_filename (path
, string
, len
) || stat (path
, &statbuf
) < 0)
1413 if (S_ISREG (statbuf
.st_mode
) ||
1414 S_ISBLK (statbuf
.st_mode
) ||
1415 S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
))
1418 if (S_ISDIR (statbuf
.st_mode
))
1425 /* inquire_unformatted()-- Given a fortran string, determine if the file
1426 * is suitable for unformatted form. Returns a C-style string. */
1429 inquire_unformatted (const char *string
, int len
)
1431 return inquire_formatted (string
, len
);
1435 /* inquire_access()-- Given a fortran string, determine if the file is
1436 * suitable for access. */
1439 inquire_access (const char *string
, int len
, int mode
)
1441 char path
[PATH_MAX
+ 1];
1443 if (string
== NULL
|| unpack_filename (path
, string
, len
) ||
1444 access (path
, mode
) < 0)
1451 /* inquire_read()-- Given a fortran string, determine if the file is
1452 * suitable for READ access. */
1455 inquire_read (const char *string
, int len
)
1457 return inquire_access (string
, len
, R_OK
);
1461 /* inquire_write()-- Given a fortran string, determine if the file is
1462 * suitable for READ access. */
1465 inquire_write (const char *string
, int len
)
1467 return inquire_access (string
, len
, W_OK
);
1471 /* inquire_readwrite()-- Given a fortran string, determine if the file is
1472 * suitable for read and write access. */
1475 inquire_readwrite (const char *string
, int len
)
1477 return inquire_access (string
, len
, R_OK
| W_OK
);
1481 /* file_length()-- Return the file length in bytes, -1 if unknown */
1484 file_length (stream
* s
)
1486 return ((unix_stream
*) s
)->file_length
;
1490 /* file_position()-- Return the current position of the file */
1493 file_position (stream
* s
)
1495 return ((unix_stream
*) s
)->logical_offset
;
1499 /* is_seekable()-- Return nonzero if the stream is seekable, zero if
1503 is_seekable (stream
* s
)
1505 /* by convention, if file_length == -1, the file is not seekable
1506 note that a mmapped file is always seekable, an fd_ file may
1508 return ((unix_stream
*) s
)->file_length
!=-1;
1514 return fd_flush( (unix_stream
*) s
);
1518 /* How files are stored: This is an operating-system specific issue,
1519 and therefore belongs here. There are three cases to consider.
1522 Records are written as block of bytes corresponding to the record
1523 length of the file. This goes for both formatted and unformatted
1524 records. Positioning is done explicitly for each data transfer,
1525 so positioning is not much of an issue.
1527 Sequential Formatted:
1528 Records are separated by newline characters. The newline character
1529 is prohibited from appearing in a string. If it does, this will be
1530 messed up on the next read. End of file is also the end of a record.
1532 Sequential Unformatted:
1533 In this case, we are merely copying bytes to and from main storage,
1534 yet we need to keep track of varying record lengths. We adopt
1535 the solution used by f2c. Each record contains a pair of length
1538 Length of record n in bytes
1540 Length of record n in bytes
1542 Length of record n+1 in bytes
1544 Length of record n+1 in bytes
1546 The length is stored at the end of a record to allow backspacing to the
1547 previous record. Between data transfer statements, the file pointer
1548 is left pointing to the first length of the current record.
1550 ENDFILE records are never explicitly stored.