re PR tree-optimization/33615 (Hoisting of potentially-throwing values for -fnon...
[official-gcc.git] / libgfortran / io / unix.c
blob9b61507e27407df56c3a8e7cef2d34f49310f073
1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
2 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of the GNU Fortran 95 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 2, or (at your option)
10 any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with Libgfortran; see the file COPYING. If not, write to
28 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
31 /* Unix stream I/O module */
33 #include "io.h"
34 #include <stdlib.h>
35 #include <limits.h>
37 #include <unistd.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <assert.h>
42 #include <string.h>
43 #include <errno.h>
46 /* For mingw, we don't identify files by their inode number, but by a
47 64-bit identifier created from a BY_HANDLE_FILE_INFORMATION. */
48 #if defined(__MINGW32__) && !HAVE_WORKING_STAT
50 #define WIN32_LEAN_AND_MEAN
51 #include <windows.h>
53 static uint64_t
54 id_from_handle (HANDLE hFile)
56 BY_HANDLE_FILE_INFORMATION FileInformation;
58 if (hFile == INVALID_HANDLE_VALUE)
59 return 0;
61 memset (&FileInformation, 0, sizeof(FileInformation));
62 if (!GetFileInformationByHandle (hFile, &FileInformation))
63 return 0;
65 return ((uint64_t) FileInformation.nFileIndexLow)
66 | (((uint64_t) FileInformation.nFileIndexHigh) << 32);
70 static uint64_t
71 id_from_path (const char *path)
73 HANDLE hFile;
74 uint64_t res;
76 if (!path || !*path || access (path, F_OK))
77 return (uint64_t) -1;
79 hFile = CreateFile (path, 0, 0, NULL, OPEN_EXISTING,
80 FILE_FLAG_BACKUP_SEMANTICS | FILE_ATTRIBUTE_READONLY,
81 NULL);
82 res = id_from_handle (hFile);
83 CloseHandle (hFile);
84 return res;
88 static uint64_t
89 id_from_fd (const int fd)
91 return id_from_handle ((HANDLE) _get_osfhandle (fd));
94 #endif
98 #ifndef SSIZE_MAX
99 #define SSIZE_MAX SHRT_MAX
100 #endif
102 #ifndef PATH_MAX
103 #define PATH_MAX 1024
104 #endif
106 #ifndef PROT_READ
107 #define PROT_READ 1
108 #endif
110 #ifndef PROT_WRITE
111 #define PROT_WRITE 2
112 #endif
114 /* These flags aren't defined on all targets (mingw32), so provide them
115 here. */
116 #ifndef S_IRGRP
117 #define S_IRGRP 0
118 #endif
120 #ifndef S_IWGRP
121 #define S_IWGRP 0
122 #endif
124 #ifndef S_IROTH
125 #define S_IROTH 0
126 #endif
128 #ifndef S_IWOTH
129 #define S_IWOTH 0
130 #endif
133 /* Unix stream I/O module */
135 #define BUFFER_SIZE 8192
137 typedef struct
139 stream st;
141 int fd;
142 gfc_offset buffer_offset; /* File offset of the start of the buffer */
143 gfc_offset physical_offset; /* Current physical file offset */
144 gfc_offset logical_offset; /* Current logical file offset */
145 gfc_offset dirty_offset; /* Start of modified bytes in buffer */
146 gfc_offset file_length; /* Length of the file, -1 if not seekable. */
148 int len; /* Physical length of the current buffer */
149 int active; /* Length of valid bytes in the buffer */
151 int prot;
152 int ndirty; /* Dirty bytes starting at dirty_offset */
154 int special_file; /* =1 if the fd refers to a special file */
156 int unbuffered; /* =1 if the stream is not buffered */
158 char *buffer;
159 char small_buffer[BUFFER_SIZE];
161 unix_stream;
164 /* Stream structure for internal files. Fields must be kept in sync
165 with unix_stream above, except for the buffer. For internal files
166 we point the buffer pointer directly at the destination memory. */
168 typedef struct
170 stream st;
172 int fd;
173 gfc_offset buffer_offset; /* File offset of the start of the buffer */
174 gfc_offset physical_offset; /* Current physical file offset */
175 gfc_offset logical_offset; /* Current logical file offset */
176 gfc_offset dirty_offset; /* Start of modified bytes in buffer */
177 gfc_offset file_length; /* Length of the file, -1 if not seekable. */
179 int len; /* Physical length of the current buffer */
180 int active; /* Length of valid bytes in the buffer */
182 int prot;
183 int ndirty; /* Dirty bytes starting at dirty_offset */
185 int special_file; /* =1 if the fd refers to a special file */
187 int unbuffered; /* =1 if the stream is not buffered */
189 char *buffer;
191 int_stream;
193 /* This implementation of stream I/O is based on the paper:
195 * "Exploiting the advantages of mapped files for stream I/O",
196 * O. Krieger, M. Stumm and R. Umrau, "Proceedings of the 1992 Winter
197 * USENIX conference", p. 27-42.
199 * It differs in a number of ways from the version described in the
200 * paper. First of all, threads are not an issue during I/O and we
201 * also don't have to worry about having multiple regions, since
202 * fortran's I/O model only allows you to be one place at a time.
204 * On the other hand, we have to be able to writing at the end of a
205 * stream, read from the start of a stream or read and write blocks of
206 * bytes from an arbitrary position. After opening a file, a pointer
207 * to a stream structure is returned, which is used to handle file
208 * accesses until the file is closed.
210 * salloc_at_r(stream, len, where)-- Given a stream pointer, return a
211 * pointer to a block of memory that mirror the file at position
212 * 'where' that is 'len' bytes long. The len integer is updated to
213 * reflect how many bytes were actually read. The only reason for a
214 * short read is end of file. The file pointer is updated. The
215 * pointer is valid until the next call to salloc_*.
217 * salloc_at_w(stream, len, where)-- Given the stream pointer, returns
218 * a pointer to a block of memory that is updated to reflect the state
219 * of the file. The length of the buffer is always equal to that
220 * requested. The buffer must be completely set by the caller. When
221 * data has been written, the sfree() function must be called to
222 * indicate that the caller is done writing data to the buffer. This
223 * may or may not cause a physical write.
225 * Short forms of these are salloc_r() and salloc_w() which drop the
226 * 'where' parameter and use the current file pointer. */
229 /*move_pos_offset()-- Move the record pointer right or left
230 *relative to current position */
233 move_pos_offset (stream* st, int pos_off)
235 unix_stream * str = (unix_stream*)st;
236 if (pos_off < 0)
238 str->logical_offset += pos_off;
240 if (str->dirty_offset + str->ndirty > str->logical_offset)
242 if (str->ndirty + pos_off > 0)
243 str->ndirty += pos_off;
244 else
246 str->dirty_offset += pos_off + pos_off;
247 str->ndirty = 0;
251 return pos_off;
253 return 0;
257 /* fix_fd()-- Given a file descriptor, make sure it is not one of the
258 * standard descriptors, returning a non-standard descriptor. If the
259 * user specifies that system errors should go to standard output,
260 * then closes standard output, we don't want the system errors to a
261 * file that has been given file descriptor 1 or 0. We want to send
262 * the error to the invalid descriptor. */
264 static int
265 fix_fd (int fd)
267 #ifdef HAVE_DUP
268 int input, output, error;
270 input = output = error = 0;
272 /* Unix allocates the lowest descriptors first, so a loop is not
273 required, but this order is. */
274 if (fd == STDIN_FILENO)
276 fd = dup (fd);
277 input = 1;
279 if (fd == STDOUT_FILENO)
281 fd = dup (fd);
282 output = 1;
284 if (fd == STDERR_FILENO)
286 fd = dup (fd);
287 error = 1;
290 if (input)
291 close (STDIN_FILENO);
292 if (output)
293 close (STDOUT_FILENO);
294 if (error)
295 close (STDERR_FILENO);
296 #endif
298 return fd;
302 is_preconnected (stream * s)
304 int fd;
306 fd = ((unix_stream *) s)->fd;
307 if (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO)
308 return 1;
309 else
310 return 0;
313 /* If the stream corresponds to a preconnected unit, we flush the
314 corresponding C stream. This is bugware for mixed C-Fortran codes
315 where the C code doesn't flush I/O before returning. */
316 void
317 flush_if_preconnected (stream * s)
319 int fd;
321 fd = ((unix_stream *) s)->fd;
322 if (fd == STDIN_FILENO)
323 fflush (stdin);
324 else if (fd == STDOUT_FILENO)
325 fflush (stdout);
326 else if (fd == STDERR_FILENO)
327 fflush (stderr);
331 /* Reset a stream after reading/writing. Assumes that the buffers have
332 been flushed. */
334 inline static void
335 reset_stream (unix_stream * s, size_t bytes_rw)
337 s->physical_offset += bytes_rw;
338 s->logical_offset = s->physical_offset;
339 if (s->file_length != -1 && s->physical_offset > s->file_length)
340 s->file_length = s->physical_offset;
344 /* Read bytes into a buffer, allowing for short reads. If the nbytes
345 * argument is less on return than on entry, it is because we've hit
346 * the end of file. */
348 static int
349 do_read (unix_stream * s, void * buf, size_t * nbytes)
351 ssize_t trans;
352 size_t bytes_left;
353 char *buf_st;
354 int status;
356 status = 0;
357 bytes_left = *nbytes;
358 buf_st = (char *) buf;
360 /* We must read in a loop since some systems don't restart system
361 calls in case of a signal. */
362 while (bytes_left > 0)
364 /* Requests between SSIZE_MAX and SIZE_MAX are undefined by SUSv3,
365 so we must read in chunks smaller than SSIZE_MAX. */
366 trans = (bytes_left < SSIZE_MAX) ? bytes_left : SSIZE_MAX;
367 trans = read (s->fd, buf_st, trans);
368 if (trans < 0)
370 if (errno == EINTR)
371 continue;
372 else
374 status = errno;
375 break;
378 else if (trans == 0) /* We hit EOF. */
379 break;
380 buf_st += trans;
381 bytes_left -= trans;
384 *nbytes -= bytes_left;
385 return status;
389 /* Write a buffer to a stream, allowing for short writes. */
391 static int
392 do_write (unix_stream * s, const void * buf, size_t * nbytes)
394 ssize_t trans;
395 size_t bytes_left;
396 char *buf_st;
397 int status;
399 status = 0;
400 bytes_left = *nbytes;
401 buf_st = (char *) buf;
403 /* We must write in a loop since some systems don't restart system
404 calls in case of a signal. */
405 while (bytes_left > 0)
407 /* Requests between SSIZE_MAX and SIZE_MAX are undefined by SUSv3,
408 so we must write in chunks smaller than SSIZE_MAX. */
409 trans = (bytes_left < SSIZE_MAX) ? bytes_left : SSIZE_MAX;
410 trans = write (s->fd, buf_st, trans);
411 if (trans < 0)
413 if (errno == EINTR)
414 continue;
415 else
417 status = errno;
418 break;
421 buf_st += trans;
422 bytes_left -= trans;
425 *nbytes -= bytes_left;
426 return status;
430 /* get_oserror()-- Get the most recent operating system error. For
431 * unix, this is errno. */
433 const char *
434 get_oserror (void)
436 return strerror (errno);
440 /*********************************************************************
441 File descriptor stream functions
442 *********************************************************************/
445 /* fd_flush()-- Write bytes that need to be written */
447 static try
448 fd_flush (unix_stream * s)
450 size_t writelen;
452 if (s->ndirty == 0)
453 return SUCCESS;
455 if (s->file_length != -1 && s->physical_offset != s->dirty_offset &&
456 lseek (s->fd, s->dirty_offset, SEEK_SET) < 0)
457 return FAILURE;
459 writelen = s->ndirty;
460 if (do_write (s, s->buffer + (s->dirty_offset - s->buffer_offset),
461 &writelen) != 0)
462 return FAILURE;
464 s->physical_offset = s->dirty_offset + writelen;
466 /* don't increment file_length if the file is non-seekable */
467 if (s->file_length != -1 && s->physical_offset > s->file_length)
468 s->file_length = s->physical_offset;
470 s->ndirty -= writelen;
471 if (s->ndirty != 0)
472 return FAILURE;
474 return SUCCESS;
478 /* fd_alloc()-- Arrange a buffer such that the salloc() request can be
479 * satisfied. This subroutine gets the buffer ready for whatever is
480 * to come next. */
482 static void
483 fd_alloc (unix_stream * s, gfc_offset where,
484 int *len __attribute__ ((unused)))
486 char *new_buffer;
487 int n, read_len;
489 if (*len <= BUFFER_SIZE)
491 new_buffer = s->small_buffer;
492 read_len = BUFFER_SIZE;
494 else
496 new_buffer = get_mem (*len);
497 read_len = *len;
500 /* Salvage bytes currently within the buffer. This is important for
501 * devices that cannot seek. */
503 if (s->buffer != NULL && s->buffer_offset <= where &&
504 where <= s->buffer_offset + s->active)
507 n = s->active - (where - s->buffer_offset);
508 memmove (new_buffer, s->buffer + (where - s->buffer_offset), n);
510 s->active = n;
512 else
513 { /* new buffer starts off empty */
514 s->active = 0;
517 s->buffer_offset = where;
519 /* free the old buffer if necessary */
521 if (s->buffer != NULL && s->buffer != s->small_buffer)
522 free_mem (s->buffer);
524 s->buffer = new_buffer;
525 s->len = read_len;
529 /* fd_alloc_r_at()-- Allocate a stream buffer for reading. Either
530 * we've already buffered the data or we need to load it. Returns
531 * NULL on I/O error. */
533 static char *
534 fd_alloc_r_at (unix_stream * s, int *len, gfc_offset where)
536 gfc_offset m;
538 if (where == -1)
539 where = s->logical_offset;
541 if (s->buffer != NULL && s->buffer_offset <= where &&
542 where + *len <= s->buffer_offset + s->active)
545 /* Return a position within the current buffer */
547 s->logical_offset = where + *len;
548 return s->buffer + where - s->buffer_offset;
551 fd_alloc (s, where, len);
553 m = where + s->active;
555 if (s->physical_offset != m && lseek (s->fd, m, SEEK_SET) < 0)
556 return NULL;
558 /* do_read() hangs on read from terminals for *BSD-systems. Only
559 use read() in that case. */
561 if (s->special_file)
563 ssize_t n;
565 n = read (s->fd, s->buffer + s->active, s->len - s->active);
566 if (n < 0)
567 return NULL;
569 s->physical_offset = m + n;
570 s->active += n;
572 else
574 size_t n;
576 n = s->len - s->active;
577 if (do_read (s, s->buffer + s->active, &n) != 0)
578 return NULL;
580 s->physical_offset = m + n;
581 s->active += n;
584 if (s->active < *len)
585 *len = s->active; /* Bytes actually available */
587 s->logical_offset = where + *len;
589 return s->buffer;
593 /* fd_alloc_w_at()-- Allocate a stream buffer for writing. Either
594 * we've already buffered the data or we need to load it. */
596 static char *
597 fd_alloc_w_at (unix_stream * s, int *len, gfc_offset where)
599 gfc_offset n;
601 if (where == -1)
602 where = s->logical_offset;
604 if (s->buffer == NULL || s->buffer_offset > where ||
605 where + *len > s->buffer_offset + s->len)
608 if (fd_flush (s) == FAILURE)
609 return NULL;
610 fd_alloc (s, where, len);
613 /* Return a position within the current buffer */
614 if (s->ndirty == 0
615 || where > s->dirty_offset + s->ndirty
616 || s->dirty_offset > where + *len)
617 { /* Discontiguous blocks, start with a clean buffer. */
618 /* Flush the buffer. */
619 if (s->ndirty != 0)
620 fd_flush (s);
621 s->dirty_offset = where;
622 s->ndirty = *len;
624 else
626 gfc_offset start; /* Merge with the existing data. */
627 if (where < s->dirty_offset)
628 start = where;
629 else
630 start = s->dirty_offset;
631 if (where + *len > s->dirty_offset + s->ndirty)
632 s->ndirty = where + *len - start;
633 else
634 s->ndirty = s->dirty_offset + s->ndirty - start;
635 s->dirty_offset = start;
638 s->logical_offset = where + *len;
640 /* Don't increment file_length if the file is non-seekable. */
642 if (s->file_length != -1 && s->logical_offset > s->file_length)
643 s->file_length = s->logical_offset;
645 n = s->logical_offset - s->buffer_offset;
646 if (n > s->active)
647 s->active = n;
649 return s->buffer + where - s->buffer_offset;
653 static try
654 fd_sfree (unix_stream * s)
656 if (s->ndirty != 0 &&
657 (s->buffer != s->small_buffer || options.all_unbuffered ||
658 s->unbuffered))
659 return fd_flush (s);
661 return SUCCESS;
665 static try
666 fd_seek (unix_stream * s, gfc_offset offset)
669 if (s->file_length == -1)
670 return SUCCESS;
672 if (s->physical_offset == offset) /* Are we lucky and avoid syscall? */
674 s->logical_offset = offset;
675 return SUCCESS;
678 if (lseek (s->fd, offset, SEEK_SET) >= 0)
680 s->physical_offset = s->logical_offset = offset;
681 s->active = 0;
682 return SUCCESS;
685 return FAILURE;
689 /* truncate_file()-- Given a unit, truncate the file at the current
690 * position. Sets the physical location to the new end of the file.
691 * Returns nonzero on error. */
693 static try
694 fd_truncate (unix_stream * s)
696 /* Non-seekable files, like terminals and fifo's fail the lseek so just
697 return success, there is nothing to truncate. If its not a pipe there
698 is a real problem. */
699 if (lseek (s->fd, s->logical_offset, SEEK_SET) == -1)
701 if (errno == ESPIPE)
702 return SUCCESS;
703 else
704 return FAILURE;
707 /* Using ftruncate on a seekable special file (like /dev/null)
708 is undefined, so we treat it as if the ftruncate succeeded. */
709 #ifdef HAVE_FTRUNCATE
710 if (s->special_file || ftruncate (s->fd, s->logical_offset))
711 #else
712 #ifdef HAVE_CHSIZE
713 if (s->special_file || chsize (s->fd, s->logical_offset))
714 #endif
715 #endif
717 s->physical_offset = s->file_length = 0;
718 return SUCCESS;
721 s->physical_offset = s->file_length = s->logical_offset;
722 s->active = 0;
723 return SUCCESS;
727 /* Similar to memset(), but operating on a stream instead of a string.
728 Takes care of not using too much memory. */
730 static try
731 fd_sset (unix_stream * s, int c, size_t n)
733 size_t bytes_left;
734 int trans;
735 void *p;
737 bytes_left = n;
739 while (bytes_left > 0)
741 /* memset() in chunks of BUFFER_SIZE. */
742 trans = (bytes_left < BUFFER_SIZE) ? bytes_left : BUFFER_SIZE;
744 p = fd_alloc_w_at (s, &trans, -1);
745 if (p)
746 memset (p, c, trans);
747 else
748 return FAILURE;
750 bytes_left -= trans;
753 return SUCCESS;
757 /* Stream read function. Avoids using a buffer for big reads. The
758 interface is like POSIX read(), but the nbytes argument is a
759 pointer; on return it contains the number of bytes written. The
760 function return value is the status indicator (0 for success). */
762 static int
763 fd_read (unix_stream * s, void * buf, size_t * nbytes)
765 void *p;
766 int tmp, status;
768 if (*nbytes < BUFFER_SIZE && !s->unbuffered)
770 tmp = *nbytes;
771 p = fd_alloc_r_at (s, &tmp, -1);
772 if (p)
774 *nbytes = tmp;
775 memcpy (buf, p, *nbytes);
776 return 0;
778 else
780 *nbytes = 0;
781 return errno;
785 /* If the request is bigger than BUFFER_SIZE we flush the buffers
786 and read directly. */
787 if (fd_flush (s) == FAILURE)
789 *nbytes = 0;
790 return errno;
793 if (is_seekable ((stream *) s) && fd_seek (s, s->logical_offset) == FAILURE)
795 *nbytes = 0;
796 return errno;
799 status = do_read (s, buf, nbytes);
800 reset_stream (s, *nbytes);
801 return status;
805 /* Stream write function. Avoids using a buffer for big writes. The
806 interface is like POSIX write(), but the nbytes argument is a
807 pointer; on return it contains the number of bytes written. The
808 function return value is the status indicator (0 for success). */
810 static int
811 fd_write (unix_stream * s, const void * buf, size_t * nbytes)
813 void *p;
814 int tmp, status;
816 if (*nbytes < BUFFER_SIZE && !s->unbuffered)
818 tmp = *nbytes;
819 p = fd_alloc_w_at (s, &tmp, -1);
820 if (p)
822 *nbytes = tmp;
823 memcpy (p, buf, *nbytes);
824 return 0;
826 else
828 *nbytes = 0;
829 return errno;
833 /* If the request is bigger than BUFFER_SIZE we flush the buffers
834 and write directly. */
835 if (fd_flush (s) == FAILURE)
837 *nbytes = 0;
838 return errno;
841 if (is_seekable ((stream *) s) && fd_seek (s, s->logical_offset) == FAILURE)
843 *nbytes = 0;
844 return errno;
847 status = do_write (s, buf, nbytes);
848 reset_stream (s, *nbytes);
849 return status;
853 static try
854 fd_close (unix_stream * s)
856 if (fd_flush (s) == FAILURE)
857 return FAILURE;
859 if (s->buffer != NULL && s->buffer != s->small_buffer)
860 free_mem (s->buffer);
862 if (s->fd != STDOUT_FILENO && s->fd != STDERR_FILENO)
864 if (close (s->fd) < 0)
865 return FAILURE;
868 free_mem (s);
870 return SUCCESS;
874 static void
875 fd_open (unix_stream * s)
877 if (isatty (s->fd))
878 s->unbuffered = 1;
880 s->st.alloc_r_at = (void *) fd_alloc_r_at;
881 s->st.alloc_w_at = (void *) fd_alloc_w_at;
882 s->st.sfree = (void *) fd_sfree;
883 s->st.close = (void *) fd_close;
884 s->st.seek = (void *) fd_seek;
885 s->st.trunc = (void *) fd_truncate;
886 s->st.read = (void *) fd_read;
887 s->st.write = (void *) fd_write;
888 s->st.set = (void *) fd_sset;
890 s->buffer = NULL;
896 /*********************************************************************
897 memory stream functions - These are used for internal files
899 The idea here is that a single stream structure is created and all
900 requests must be satisfied from it. The location and size of the
901 buffer is the character variable supplied to the READ or WRITE
902 statement.
904 *********************************************************************/
907 static char *
908 mem_alloc_r_at (int_stream * s, int *len, gfc_offset where)
910 gfc_offset n;
912 if (where == -1)
913 where = s->logical_offset;
915 if (where < s->buffer_offset || where > s->buffer_offset + s->active)
916 return NULL;
918 s->logical_offset = where + *len;
920 n = s->buffer_offset + s->active - where;
921 if (*len > n)
922 *len = n;
924 return s->buffer + (where - s->buffer_offset);
928 static char *
929 mem_alloc_w_at (int_stream * s, int *len, gfc_offset where)
931 gfc_offset m;
933 assert (*len >= 0); /* Negative values not allowed. */
935 if (where == -1)
936 where = s->logical_offset;
938 m = where + *len;
940 if (where < s->buffer_offset)
941 return NULL;
943 if (m > s->file_length)
944 return NULL;
946 s->logical_offset = m;
948 return s->buffer + (where - s->buffer_offset);
952 /* Stream read function for internal units. This is not actually used
953 at the moment, as all internal IO is formatted and the formatted IO
954 routines use mem_alloc_r_at. */
956 static int
957 mem_read (int_stream * s, void * buf, size_t * nbytes)
959 void *p;
960 int tmp;
962 tmp = *nbytes;
963 p = mem_alloc_r_at (s, &tmp, -1);
964 if (p)
966 *nbytes = tmp;
967 memcpy (buf, p, *nbytes);
968 return 0;
970 else
972 *nbytes = 0;
973 return errno;
978 /* Stream write function for internal units. This is not actually used
979 at the moment, as all internal IO is formatted and the formatted IO
980 routines use mem_alloc_w_at. */
982 static int
983 mem_write (int_stream * s, const void * buf, size_t * nbytes)
985 void *p;
986 int tmp;
988 errno = 0;
990 tmp = *nbytes;
991 p = mem_alloc_w_at (s, &tmp, -1);
992 if (p)
994 *nbytes = tmp;
995 memcpy (p, buf, *nbytes);
996 return 0;
998 else
1000 *nbytes = 0;
1001 return errno;
1006 static int
1007 mem_seek (int_stream * s, gfc_offset offset)
1009 if (offset > s->file_length)
1011 errno = ESPIPE;
1012 return FAILURE;
1015 s->logical_offset = offset;
1016 return SUCCESS;
1020 static try
1021 mem_set (int_stream * s, int c, size_t n)
1023 void *p;
1024 int len;
1026 len = n;
1028 p = mem_alloc_w_at (s, &len, -1);
1029 if (p)
1031 memset (p, c, len);
1032 return SUCCESS;
1034 else
1035 return FAILURE;
1039 static int
1040 mem_truncate (int_stream * s __attribute__ ((unused)))
1042 return SUCCESS;
1046 static try
1047 mem_close (int_stream * s)
1049 if (s != NULL)
1050 free_mem (s);
1052 return SUCCESS;
1056 static try
1057 mem_sfree (int_stream * s __attribute__ ((unused)))
1059 return SUCCESS;
1064 /*********************************************************************
1065 Public functions -- A reimplementation of this module needs to
1066 define functional equivalents of the following.
1067 *********************************************************************/
1069 /* empty_internal_buffer()-- Zero the buffer of Internal file */
1071 void
1072 empty_internal_buffer(stream *strm)
1074 int_stream * s = (int_stream *) strm;
1075 memset(s->buffer, ' ', s->file_length);
1078 /* open_internal()-- Returns a stream structure from an internal file */
1080 stream *
1081 open_internal (char *base, int length)
1083 int_stream *s;
1085 s = get_mem (sizeof (int_stream));
1086 memset (s, '\0', sizeof (int_stream));
1088 s->buffer = base;
1089 s->buffer_offset = 0;
1091 s->logical_offset = 0;
1092 s->active = s->file_length = length;
1094 s->st.alloc_r_at = (void *) mem_alloc_r_at;
1095 s->st.alloc_w_at = (void *) mem_alloc_w_at;
1096 s->st.sfree = (void *) mem_sfree;
1097 s->st.close = (void *) mem_close;
1098 s->st.seek = (void *) mem_seek;
1099 s->st.trunc = (void *) mem_truncate;
1100 s->st.read = (void *) mem_read;
1101 s->st.write = (void *) mem_write;
1102 s->st.set = (void *) mem_set;
1104 return (stream *) s;
1108 /* fd_to_stream()-- Given an open file descriptor, build a stream
1109 * around it. */
1111 static stream *
1112 fd_to_stream (int fd, int prot)
1114 struct stat statbuf;
1115 unix_stream *s;
1117 s = get_mem (sizeof (unix_stream));
1118 memset (s, '\0', sizeof (unix_stream));
1120 s->fd = fd;
1121 s->buffer_offset = 0;
1122 s->physical_offset = 0;
1123 s->logical_offset = 0;
1124 s->prot = prot;
1126 /* Get the current length of the file. */
1128 fstat (fd, &statbuf);
1130 if (lseek (fd, 0, SEEK_CUR) == (off_t) -1)
1131 s->file_length = -1;
1132 else
1133 s->file_length = S_ISREG (statbuf.st_mode) ? statbuf.st_size : -1;
1135 s->special_file = !S_ISREG (statbuf.st_mode);
1137 fd_open (s);
1139 return (stream *) s;
1143 /* Given the Fortran unit number, convert it to a C file descriptor. */
1146 unit_to_fd (int unit)
1148 gfc_unit *us;
1149 int fd;
1151 us = find_unit (unit);
1152 if (us == NULL)
1153 return -1;
1155 fd = ((unix_stream *) us->s)->fd;
1156 unlock_unit (us);
1157 return fd;
1161 /* unpack_filename()-- Given a fortran string and a pointer to a
1162 * buffer that is PATH_MAX characters, convert the fortran string to a
1163 * C string in the buffer. Returns nonzero if this is not possible. */
1166 unpack_filename (char *cstring, const char *fstring, int len)
1168 len = fstrlen (fstring, len);
1169 if (len >= PATH_MAX)
1170 return 1;
1172 memmove (cstring, fstring, len);
1173 cstring[len] = '\0';
1175 return 0;
1179 /* tempfile()-- Generate a temporary filename for a scratch file and
1180 * open it. mkstemp() opens the file for reading and writing, but the
1181 * library mode prevents anything that is not allowed. The descriptor
1182 * is returned, which is -1 on error. The template is pointed to by
1183 * opp->file, which is copied into the unit structure
1184 * and freed later. */
1186 static int
1187 tempfile (st_parameter_open *opp)
1189 const char *tempdir;
1190 char *template;
1191 int fd;
1193 tempdir = getenv ("GFORTRAN_TMPDIR");
1194 if (tempdir == NULL)
1195 tempdir = getenv ("TMP");
1196 if (tempdir == NULL)
1197 tempdir = getenv ("TEMP");
1198 if (tempdir == NULL)
1199 tempdir = DEFAULT_TEMPDIR;
1201 template = get_mem (strlen (tempdir) + 20);
1203 sprintf (template, "%s/gfortrantmpXXXXXX", tempdir);
1205 #ifdef HAVE_MKSTEMP
1207 fd = mkstemp (template);
1209 #else /* HAVE_MKSTEMP */
1211 if (mktemp (template))
1213 #if defined(HAVE_CRLF) && defined(O_BINARY)
1214 fd = open (template, O_RDWR | O_CREAT | O_EXCL | O_BINARY,
1215 S_IREAD | S_IWRITE);
1216 #else
1217 fd = open (template, O_RDWR | O_CREAT | O_EXCL, S_IREAD | S_IWRITE);
1218 #endif
1219 while (!(fd == -1 && errno == EEXIST) && mktemp (template));
1220 else
1221 fd = -1;
1223 #endif /* HAVE_MKSTEMP */
1225 if (fd < 0)
1226 free_mem (template);
1227 else
1229 opp->file = template;
1230 opp->file_len = strlen (template); /* Don't include trailing nul */
1233 return fd;
1237 /* regular_file()-- Open a regular file.
1238 * Change flags->action if it is ACTION_UNSPECIFIED on entry,
1239 * unless an error occurs.
1240 * Returns the descriptor, which is less than zero on error. */
1242 static int
1243 regular_file (st_parameter_open *opp, unit_flags *flags)
1245 char path[PATH_MAX + 1];
1246 int mode;
1247 int rwflag;
1248 int crflag;
1249 int fd;
1251 if (unpack_filename (path, opp->file, opp->file_len))
1253 errno = ENOENT; /* Fake an OS error */
1254 return -1;
1257 rwflag = 0;
1259 switch (flags->action)
1261 case ACTION_READ:
1262 rwflag = O_RDONLY;
1263 break;
1265 case ACTION_WRITE:
1266 rwflag = O_WRONLY;
1267 break;
1269 case ACTION_READWRITE:
1270 case ACTION_UNSPECIFIED:
1271 rwflag = O_RDWR;
1272 break;
1274 default:
1275 internal_error (&opp->common, "regular_file(): Bad action");
1278 switch (flags->status)
1280 case STATUS_NEW:
1281 crflag = O_CREAT | O_EXCL;
1282 break;
1284 case STATUS_OLD: /* open will fail if the file does not exist*/
1285 crflag = 0;
1286 break;
1288 case STATUS_UNKNOWN:
1289 case STATUS_SCRATCH:
1290 crflag = O_CREAT;
1291 break;
1293 case STATUS_REPLACE:
1294 crflag = O_CREAT | O_TRUNC;
1295 break;
1297 default:
1298 internal_error (&opp->common, "regular_file(): Bad status");
1301 /* rwflag |= O_LARGEFILE; */
1303 #if defined(HAVE_CRLF) && defined(O_BINARY)
1304 crflag |= O_BINARY;
1305 #endif
1307 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1308 fd = open (path, rwflag | crflag, mode);
1309 if (flags->action != ACTION_UNSPECIFIED)
1310 return fd;
1312 if (fd >= 0)
1314 flags->action = ACTION_READWRITE;
1315 return fd;
1317 if (errno != EACCES && errno != EROFS)
1318 return fd;
1320 /* retry for read-only access */
1321 rwflag = O_RDONLY;
1322 fd = open (path, rwflag | crflag, mode);
1323 if (fd >=0)
1325 flags->action = ACTION_READ;
1326 return fd; /* success */
1329 if (errno != EACCES)
1330 return fd; /* failure */
1332 /* retry for write-only access */
1333 rwflag = O_WRONLY;
1334 fd = open (path, rwflag | crflag, mode);
1335 if (fd >=0)
1337 flags->action = ACTION_WRITE;
1338 return fd; /* success */
1340 return fd; /* failure */
1344 /* open_external()-- Open an external file, unix specific version.
1345 * Change flags->action if it is ACTION_UNSPECIFIED on entry.
1346 * Returns NULL on operating system error. */
1348 stream *
1349 open_external (st_parameter_open *opp, unit_flags *flags)
1351 int fd, prot;
1353 if (flags->status == STATUS_SCRATCH)
1355 fd = tempfile (opp);
1356 if (flags->action == ACTION_UNSPECIFIED)
1357 flags->action = ACTION_READWRITE;
1359 #if HAVE_UNLINK_OPEN_FILE
1360 /* We can unlink scratch files now and it will go away when closed. */
1361 if (fd >= 0)
1362 unlink (opp->file);
1363 #endif
1365 else
1367 /* regular_file resets flags->action if it is ACTION_UNSPECIFIED and
1368 * if it succeeds */
1369 fd = regular_file (opp, flags);
1372 if (fd < 0)
1373 return NULL;
1374 fd = fix_fd (fd);
1376 switch (flags->action)
1378 case ACTION_READ:
1379 prot = PROT_READ;
1380 break;
1382 case ACTION_WRITE:
1383 prot = PROT_WRITE;
1384 break;
1386 case ACTION_READWRITE:
1387 prot = PROT_READ | PROT_WRITE;
1388 break;
1390 default:
1391 internal_error (&opp->common, "open_external(): Bad action");
1394 return fd_to_stream (fd, prot);
1398 /* input_stream()-- Return a stream pointer to the default input stream.
1399 * Called on initialization. */
1401 stream *
1402 input_stream (void)
1404 return fd_to_stream (STDIN_FILENO, PROT_READ);
1408 /* output_stream()-- Return a stream pointer to the default output stream.
1409 * Called on initialization. */
1411 stream *
1412 output_stream (void)
1414 #if defined(HAVE_CRLF) && defined(HAVE_SETMODE)
1415 setmode (STDOUT_FILENO, O_BINARY);
1416 #endif
1417 return fd_to_stream (STDOUT_FILENO, PROT_WRITE);
1421 /* error_stream()-- Return a stream pointer to the default error stream.
1422 * Called on initialization. */
1424 stream *
1425 error_stream (void)
1427 #if defined(HAVE_CRLF) && defined(HAVE_SETMODE)
1428 setmode (STDERR_FILENO, O_BINARY);
1429 #endif
1430 return fd_to_stream (STDERR_FILENO, PROT_WRITE);
1434 /* st_vprintf()-- vprintf function for error output. To avoid buffer
1435 overruns, we limit the length of the buffer to ST_VPRINTF_SIZE. 2k
1436 is big enough to completely fill a 80x25 terminal, so it shuld be
1437 OK. We use a direct write() because it is simpler and least likely
1438 to be clobbered by memory corruption. Writing an error message
1439 longer than that is an error. */
1441 #define ST_VPRINTF_SIZE 2048
1444 st_vprintf (const char *format, va_list ap)
1446 static char buffer[ST_VPRINTF_SIZE];
1447 int written;
1448 int fd;
1450 fd = options.use_stderr ? STDERR_FILENO : STDOUT_FILENO;
1451 #ifdef HAVE_VSNPRINTF
1452 written = vsnprintf(buffer, ST_VPRINTF_SIZE, format, ap);
1453 #else
1454 written = vsprintf(buffer, format, ap);
1456 if (written >= ST_VPRINTF_SIZE-1)
1458 /* The error message was longer than our buffer. Ouch. Because
1459 we may have messed up things badly, report the error and
1460 quit. */
1461 #define ERROR_MESSAGE "Internal error: buffer overrun in st_vprintf()\n"
1462 write (fd, buffer, ST_VPRINTF_SIZE-1);
1463 write (fd, ERROR_MESSAGE, strlen(ERROR_MESSAGE));
1464 sys_exit(2);
1465 #undef ERROR_MESSAGE
1468 #endif
1470 written = write (fd, buffer, written);
1471 return written;
1474 /* st_printf()-- printf() function for error output. This just calls
1475 st_vprintf() to do the actual work. */
1478 st_printf (const char *format, ...)
1480 int written;
1481 va_list ap;
1482 va_start (ap, format);
1483 written = st_vprintf(format, ap);
1484 va_end (ap);
1485 return written;
1489 /* compare_file_filename()-- Given an open stream and a fortran string
1490 * that is a filename, figure out if the file is the same as the
1491 * filename. */
1494 compare_file_filename (gfc_unit *u, const char *name, int len)
1496 char path[PATH_MAX + 1];
1497 struct stat st1;
1498 #ifdef HAVE_WORKING_STAT
1499 struct stat st2;
1500 #else
1501 # ifdef __MINGW32__
1502 uint64_t id1, id2;
1503 # endif
1504 #endif
1506 if (unpack_filename (path, name, len))
1507 return 0; /* Can't be the same */
1509 /* If the filename doesn't exist, then there is no match with the
1510 * existing file. */
1512 if (stat (path, &st1) < 0)
1513 return 0;
1515 #ifdef HAVE_WORKING_STAT
1516 fstat (((unix_stream *) (u->s))->fd, &st2);
1517 return (st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino);
1518 #else
1520 # ifdef __MINGW32__
1521 /* We try to match files by a unique ID. On some filesystems (network
1522 fs and FAT), we can't generate this unique ID, and will simply compare
1523 filenames. */
1524 id1 = id_from_path (path);
1525 id2 = id_from_fd (((unix_stream *) (u->s))->fd);
1526 if (id1 || id2)
1527 return (id1 == id2);
1528 # endif
1530 if (len != u->file_len)
1531 return 0;
1532 return (memcmp(path, u->file, len) == 0);
1533 #endif
1537 #ifdef HAVE_WORKING_STAT
1538 # define FIND_FILE0_DECL struct stat *st
1539 # define FIND_FILE0_ARGS st
1540 #else
1541 # define FIND_FILE0_DECL uint64_t id, const char *file, gfc_charlen_type file_len
1542 # define FIND_FILE0_ARGS id, file, file_len
1543 #endif
1545 /* find_file0()-- Recursive work function for find_file() */
1547 static gfc_unit *
1548 find_file0 (gfc_unit *u, FIND_FILE0_DECL)
1550 gfc_unit *v;
1551 #if defined(__MINGW32__) && !HAVE_WORKING_STAT
1552 uint64_t id1;
1553 #endif
1555 if (u == NULL)
1556 return NULL;
1558 #ifdef HAVE_WORKING_STAT
1559 if (u->s != NULL
1560 && fstat (((unix_stream *) u->s)->fd, &st[1]) >= 0 &&
1561 st[0].st_dev == st[1].st_dev && st[0].st_ino == st[1].st_ino)
1562 return u;
1563 #else
1564 # ifdef __MINGW32__
1565 if (u->s && ((id1 = id_from_fd (((unix_stream *) u->s)->fd)) || id1))
1567 if (id == id1)
1568 return u;
1570 else
1571 # endif
1572 if (compare_string (u->file_len, u->file, file_len, file) == 0)
1573 return u;
1574 #endif
1576 v = find_file0 (u->left, FIND_FILE0_ARGS);
1577 if (v != NULL)
1578 return v;
1580 v = find_file0 (u->right, FIND_FILE0_ARGS);
1581 if (v != NULL)
1582 return v;
1584 return NULL;
1588 /* find_file()-- Take the current filename and see if there is a unit
1589 * that has the file already open. Returns a pointer to the unit if so. */
1591 gfc_unit *
1592 find_file (const char *file, gfc_charlen_type file_len)
1594 char path[PATH_MAX + 1];
1595 struct stat st[2];
1596 gfc_unit *u;
1597 uint64_t id;
1599 if (unpack_filename (path, file, file_len))
1600 return NULL;
1602 if (stat (path, &st[0]) < 0)
1603 return NULL;
1605 #if defined(__MINGW32__) && !HAVE_WORKING_STAT
1606 id = id_from_path (path);
1607 #else
1608 id = 0;
1609 #endif
1611 __gthread_mutex_lock (&unit_lock);
1612 retry:
1613 u = find_file0 (unit_root, FIND_FILE0_ARGS);
1614 if (u != NULL)
1616 /* Fast path. */
1617 if (! __gthread_mutex_trylock (&u->lock))
1619 /* assert (u->closed == 0); */
1620 __gthread_mutex_unlock (&unit_lock);
1621 return u;
1624 inc_waiting_locked (u);
1626 __gthread_mutex_unlock (&unit_lock);
1627 if (u != NULL)
1629 __gthread_mutex_lock (&u->lock);
1630 if (u->closed)
1632 __gthread_mutex_lock (&unit_lock);
1633 __gthread_mutex_unlock (&u->lock);
1634 if (predec_waiting_locked (u) == 0)
1635 free_mem (u);
1636 goto retry;
1639 dec_waiting_unlocked (u);
1641 return u;
1644 static gfc_unit *
1645 flush_all_units_1 (gfc_unit *u, int min_unit)
1647 while (u != NULL)
1649 if (u->unit_number > min_unit)
1651 gfc_unit *r = flush_all_units_1 (u->left, min_unit);
1652 if (r != NULL)
1653 return r;
1655 if (u->unit_number >= min_unit)
1657 if (__gthread_mutex_trylock (&u->lock))
1658 return u;
1659 if (u->s)
1660 flush (u->s);
1661 __gthread_mutex_unlock (&u->lock);
1663 u = u->right;
1665 return NULL;
1668 void
1669 flush_all_units (void)
1671 gfc_unit *u;
1672 int min_unit = 0;
1674 __gthread_mutex_lock (&unit_lock);
1677 u = flush_all_units_1 (unit_root, min_unit);
1678 if (u != NULL)
1679 inc_waiting_locked (u);
1680 __gthread_mutex_unlock (&unit_lock);
1681 if (u == NULL)
1682 return;
1684 __gthread_mutex_lock (&u->lock);
1686 min_unit = u->unit_number + 1;
1688 if (u->closed == 0)
1690 flush (u->s);
1691 __gthread_mutex_lock (&unit_lock);
1692 __gthread_mutex_unlock (&u->lock);
1693 (void) predec_waiting_locked (u);
1695 else
1697 __gthread_mutex_lock (&unit_lock);
1698 __gthread_mutex_unlock (&u->lock);
1699 if (predec_waiting_locked (u) == 0)
1700 free_mem (u);
1703 while (1);
1707 /* stream_at_bof()-- Returns nonzero if the stream is at the beginning
1708 * of the file. */
1711 stream_at_bof (stream * s)
1713 unix_stream *us;
1715 if (!is_seekable (s))
1716 return 0;
1718 us = (unix_stream *) s;
1720 return us->logical_offset == 0;
1724 /* stream_at_eof()-- Returns nonzero if the stream is at the end
1725 * of the file. */
1728 stream_at_eof (stream * s)
1730 unix_stream *us;
1732 if (!is_seekable (s))
1733 return 0;
1735 us = (unix_stream *) s;
1737 return us->logical_offset == us->dirty_offset;
1741 /* delete_file()-- Given a unit structure, delete the file associated
1742 * with the unit. Returns nonzero if something went wrong. */
1745 delete_file (gfc_unit * u)
1747 char path[PATH_MAX + 1];
1749 if (unpack_filename (path, u->file, u->file_len))
1750 { /* Shouldn't be possible */
1751 errno = ENOENT;
1752 return 1;
1755 return unlink (path);
1759 /* file_exists()-- Returns nonzero if the current filename exists on
1760 * the system */
1763 file_exists (const char *file, gfc_charlen_type file_len)
1765 char path[PATH_MAX + 1];
1766 struct stat statbuf;
1768 if (unpack_filename (path, file, file_len))
1769 return 0;
1771 if (stat (path, &statbuf) < 0)
1772 return 0;
1774 return 1;
1779 static const char yes[] = "YES", no[] = "NO", unknown[] = "UNKNOWN";
1781 /* inquire_sequential()-- Given a fortran string, determine if the
1782 * file is suitable for sequential access. Returns a C-style
1783 * string. */
1785 const char *
1786 inquire_sequential (const char *string, int len)
1788 char path[PATH_MAX + 1];
1789 struct stat statbuf;
1791 if (string == NULL ||
1792 unpack_filename (path, string, len) || stat (path, &statbuf) < 0)
1793 return unknown;
1795 if (S_ISREG (statbuf.st_mode) ||
1796 S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))
1797 return yes;
1799 if (S_ISDIR (statbuf.st_mode) || S_ISBLK (statbuf.st_mode))
1800 return no;
1802 return unknown;
1806 /* inquire_direct()-- Given a fortran string, determine if the file is
1807 * suitable for direct access. Returns a C-style string. */
1809 const char *
1810 inquire_direct (const char *string, int len)
1812 char path[PATH_MAX + 1];
1813 struct stat statbuf;
1815 if (string == NULL ||
1816 unpack_filename (path, string, len) || stat (path, &statbuf) < 0)
1817 return unknown;
1819 if (S_ISREG (statbuf.st_mode) || S_ISBLK (statbuf.st_mode))
1820 return yes;
1822 if (S_ISDIR (statbuf.st_mode) ||
1823 S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))
1824 return no;
1826 return unknown;
1830 /* inquire_formatted()-- Given a fortran string, determine if the file
1831 * is suitable for formatted form. Returns a C-style string. */
1833 const char *
1834 inquire_formatted (const char *string, int len)
1836 char path[PATH_MAX + 1];
1837 struct stat statbuf;
1839 if (string == NULL ||
1840 unpack_filename (path, string, len) || stat (path, &statbuf) < 0)
1841 return unknown;
1843 if (S_ISREG (statbuf.st_mode) ||
1844 S_ISBLK (statbuf.st_mode) ||
1845 S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))
1846 return yes;
1848 if (S_ISDIR (statbuf.st_mode))
1849 return no;
1851 return unknown;
1855 /* inquire_unformatted()-- Given a fortran string, determine if the file
1856 * is suitable for unformatted form. Returns a C-style string. */
1858 const char *
1859 inquire_unformatted (const char *string, int len)
1861 return inquire_formatted (string, len);
1865 #ifndef HAVE_ACCESS
1867 #ifndef W_OK
1868 #define W_OK 2
1869 #endif
1871 #ifndef R_OK
1872 #define R_OK 4
1873 #endif
1875 /* Fallback implementation of access() on systems that don't have it.
1876 Only modes R_OK and W_OK are used in this file. */
1878 static int
1879 fallback_access (const char *path, int mode)
1881 if ((mode & R_OK) && open (path, O_RDONLY) < 0)
1882 return -1;
1884 if ((mode & W_OK) && open (path, O_WRONLY) < 0)
1885 return -1;
1887 return 0;
1890 #undef access
1891 #define access fallback_access
1892 #endif
1895 /* inquire_access()-- Given a fortran string, determine if the file is
1896 * suitable for access. */
1898 static const char *
1899 inquire_access (const char *string, int len, int mode)
1901 char path[PATH_MAX + 1];
1903 if (string == NULL || unpack_filename (path, string, len) ||
1904 access (path, mode) < 0)
1905 return no;
1907 return yes;
1911 /* inquire_read()-- Given a fortran string, determine if the file is
1912 * suitable for READ access. */
1914 const char *
1915 inquire_read (const char *string, int len)
1917 return inquire_access (string, len, R_OK);
1921 /* inquire_write()-- Given a fortran string, determine if the file is
1922 * suitable for READ access. */
1924 const char *
1925 inquire_write (const char *string, int len)
1927 return inquire_access (string, len, W_OK);
1931 /* inquire_readwrite()-- Given a fortran string, determine if the file is
1932 * suitable for read and write access. */
1934 const char *
1935 inquire_readwrite (const char *string, int len)
1937 return inquire_access (string, len, R_OK | W_OK);
1941 /* file_length()-- Return the file length in bytes, -1 if unknown */
1943 gfc_offset
1944 file_length (stream * s)
1946 return ((unix_stream *) s)->file_length;
1950 /* file_position()-- Return the current position of the file */
1952 gfc_offset
1953 file_position (stream *s)
1955 return ((unix_stream *) s)->logical_offset;
1959 /* is_seekable()-- Return nonzero if the stream is seekable, zero if
1960 * it is not */
1963 is_seekable (stream *s)
1965 /* By convention, if file_length == -1, the file is not
1966 seekable. */
1967 return ((unix_stream *) s)->file_length!=-1;
1971 /* is_special()-- Return nonzero if the stream is not a regular file. */
1974 is_special (stream *s)
1976 return ((unix_stream *) s)->special_file;
1981 flush (stream *s)
1983 return fd_flush( (unix_stream *) s);
1987 stream_isatty (stream *s)
1989 return isatty (((unix_stream *) s)->fd);
1992 char *
1993 stream_ttyname (stream *s __attribute__ ((unused)))
1995 #ifdef HAVE_TTYNAME
1996 return ttyname (((unix_stream *) s)->fd);
1997 #else
1998 return NULL;
1999 #endif
2002 gfc_offset
2003 stream_offset (stream *s)
2005 return (((unix_stream *) s)->logical_offset);
2009 /* How files are stored: This is an operating-system specific issue,
2010 and therefore belongs here. There are three cases to consider.
2012 Direct Access:
2013 Records are written as block of bytes corresponding to the record
2014 length of the file. This goes for both formatted and unformatted
2015 records. Positioning is done explicitly for each data transfer,
2016 so positioning is not much of an issue.
2018 Sequential Formatted:
2019 Records are separated by newline characters. The newline character
2020 is prohibited from appearing in a string. If it does, this will be
2021 messed up on the next read. End of file is also the end of a record.
2023 Sequential Unformatted:
2024 In this case, we are merely copying bytes to and from main storage,
2025 yet we need to keep track of varying record lengths. We adopt
2026 the solution used by f2c. Each record contains a pair of length
2027 markers:
2029 Length of record n in bytes
2030 Data of record n
2031 Length of record n in bytes
2033 Length of record n+1 in bytes
2034 Data of record n+1
2035 Length of record n+1 in bytes
2037 The length is stored at the end of a record to allow backspacing to the
2038 previous record. Between data transfer statements, the file pointer
2039 is left pointing to the first length of the current record.
2041 ENDFILE records are never explicitly stored.