1 /* Copyright (C) 2002, 2003, 2004, 2005
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)
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
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 */
46 #include "libgfortran.h"
51 #define SSIZE_MAX SHRT_MAX
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 /*move_pos_offset()-- Move the record pointer right or left
121 *relative to current position */
124 move_pos_offset (stream* st, int pos_off)
126 unix_stream * str = (unix_stream*)st;
129 str->logical_offset += pos_off;
131 if (str->dirty_offset + str->ndirty > str->logical_offset)
133 if (str->ndirty + pos_off > 0)
134 str->ndirty += pos_off;
137 str->dirty_offset += pos_off + pos_off;
148 /* fix_fd()-- Given a file descriptor, make sure it is not one of the
149 * standard descriptors, returning a non-standard descriptor. If the
150 * user specifies that system errors should go to standard output,
151 * then closes standard output, we don't want the system errors to a
152 * file that has been given file descriptor 1 or 0. We want to send
153 * the error to the invalid descriptor. */
158 int input, output, error;
160 input = output = error = 0;
162 /* Unix allocates the lowest descriptors first, so a loop is not
163 required, but this order is. */
165 if (fd == STDIN_FILENO)
170 if (fd == STDOUT_FILENO)
175 if (fd == STDERR_FILENO)
182 close (STDIN_FILENO);
184 close (STDOUT_FILENO);
186 close (STDERR_FILENO);
192 is_preconnected (stream * s)
196 fd = ((unix_stream *) s)->fd;
197 if (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO)
203 /* If the stream corresponds to a preconnected unit, we flush the
204 corresponding C stream. This is bugware for mixed C-Fortran codes
205 where the C code doesn't flush I/O before returning. */
207 flush_if_preconnected (stream * s)
211 fd = ((unix_stream *) s)->fd;
212 if (fd == STDIN_FILENO)
214 else if (fd == STDOUT_FILENO)
216 else if (fd == STDERR_FILENO)
221 /* Reset a stream after reading/writing. Assumes that the buffers have
225 reset_stream (unix_stream * s, size_t bytes_rw)
227 s->physical_offset += bytes_rw;
228 s->logical_offset = s->physical_offset;
229 if (s->file_length != -1 && s->physical_offset > s->file_length)
230 s->file_length = s->physical_offset;
234 /* Read bytes into a buffer, allowing for short reads. If the nbytes
235 * argument is less on return than on entry, it is because we've hit
236 * the end of file. */
239 do_read (unix_stream * s, void * buf, size_t * nbytes)
247 bytes_left = *nbytes;
248 buf_st = (char *) buf;
250 /* We must read in a loop since some systems don't restart system
251 calls in case of a signal. */
252 while (bytes_left > 0)
254 /* Requests between SSIZE_MAX and SIZE_MAX are undefined by SUSv3,
255 so we must read in chunks smaller than SSIZE_MAX. */
256 trans = (bytes_left < SSIZE_MAX) ? bytes_left : SSIZE_MAX;
257 trans = read (s->fd, buf_st, trans);
268 else if (trans == 0) /* We hit EOF. */
274 *nbytes -= bytes_left;
279 /* Write a buffer to a stream, allowing for short writes. */
282 do_write (unix_stream * s, const void * buf, size_t * nbytes)
290 bytes_left = *nbytes;
291 buf_st = (char *) buf;
293 /* We must write in a loop since some systems don't restart system
294 calls in case of a signal. */
295 while (bytes_left > 0)
297 /* Requests between SSIZE_MAX and SIZE_MAX are undefined by SUSv3,
298 so we must write in chunks smaller than SSIZE_MAX. */
299 trans = (bytes_left < SSIZE_MAX) ? bytes_left : SSIZE_MAX;
300 trans = write (s->fd, buf_st, trans);
315 *nbytes -= bytes_left;
320 /* get_oserror()-- Get the most recent operating system error. For
321 * unix, this is errno. */
326 return strerror (errno);
330 /* sys_exit()-- Terminate the program with an exit code */
339 /*********************************************************************
340 File descriptor stream functions
341 *********************************************************************/
344 /* fd_flush()-- Write bytes that need to be written */
347 fd_flush (unix_stream * s)
354 if (s->physical_offset != s->dirty_offset &&
355 lseek (s->fd, s->dirty_offset, SEEK_SET) < 0)
358 writelen = s->ndirty;
359 if (do_write (s, s->buffer + (s->dirty_offset - s->buffer_offset),
363 s->physical_offset = s->dirty_offset + writelen;
365 /* don't increment file_length if the file is non-seekable */
366 if (s->file_length != -1 && s->physical_offset > s->file_length)
367 s->file_length = s->physical_offset;
369 s->ndirty -= writelen;
377 /* fd_alloc()-- Arrange a buffer such that the salloc() request can be
378 * satisfied. This subroutine gets the buffer ready for whatever is
382 fd_alloc (unix_stream * s, gfc_offset where,
383 int *len __attribute__ ((unused)))
388 if (*len <= BUFFER_SIZE)
390 new_buffer = s->small_buffer;
391 read_len = BUFFER_SIZE;
395 new_buffer = get_mem (*len);
399 /* Salvage bytes currently within the buffer. This is important for
400 * devices that cannot seek. */
402 if (s->buffer != NULL && s->buffer_offset <= where &&
403 where <= s->buffer_offset + s->active)
406 n = s->active - (where - s->buffer_offset);
407 memmove (new_buffer, s->buffer + (where - s->buffer_offset), n);
412 { /* new buffer starts off empty */
416 s->buffer_offset = where;
418 /* free the old buffer if necessary */
420 if (s->buffer != NULL && s->buffer != s->small_buffer)
421 free_mem (s->buffer);
423 s->buffer = new_buffer;
428 /* fd_alloc_r_at()-- Allocate a stream buffer for reading. Either
429 * we've already buffered the data or we need to load it. Returns
430 * NULL on I/O error. */
433 fd_alloc_r_at (unix_stream * s, int *len, gfc_offset where)
438 where = s->logical_offset;
440 if (s->buffer != NULL && s->buffer_offset <= where &&
441 where + *len <= s->buffer_offset + s->active)
444 /* Return a position within the current buffer */
446 s->logical_offset = where + *len;
447 return s->buffer + where - s->buffer_offset;
450 fd_alloc (s, where, len);
452 m = where + s->active;
454 if (s->physical_offset != m && lseek (s->fd, m, SEEK_SET) < 0)
457 /* do_read() hangs on read from terminals for *BSD-systems. Only
458 use read() in that case. */
464 n = read (s->fd, s->buffer + s->active, s->len - s->active);
468 s->physical_offset = where + n;
475 n = s->len - s->active;
476 if (do_read (s, s->buffer + s->active, &n) != 0)
479 s->physical_offset = where + n;
483 if (s->active < *len)
484 *len = s->active; /* Bytes actually available */
486 s->logical_offset = where + *len;
492 /* fd_alloc_w_at()-- Allocate a stream buffer for writing. Either
493 * we've already buffered the data or we need to load it. */
496 fd_alloc_w_at (unix_stream * s, int *len, gfc_offset where)
501 where = s->logical_offset;
503 if (s->buffer == NULL || s->buffer_offset > where ||
504 where + *len > s->buffer_offset + s->len)
507 if (fd_flush (s) == FAILURE)
509 fd_alloc (s, where, len);
512 /* Return a position within the current buffer */
514 || where > s->dirty_offset + s->ndirty
515 || s->dirty_offset > where + *len)
516 { /* Discontiguous blocks, start with a clean buffer. */
517 /* Flush the buffer. */
520 s->dirty_offset = where;
525 gfc_offset start; /* Merge with the existing data. */
526 if (where < s->dirty_offset)
529 start = s->dirty_offset;
530 if (where + *len > s->dirty_offset + s->ndirty)
531 s->ndirty = where + *len - start;
533 s->ndirty = s->dirty_offset + s->ndirty - start;
534 s->dirty_offset = start;
537 s->logical_offset = where + *len;
539 if (where + *len > s->file_length)
540 s->file_length = where + *len;
542 n = s->logical_offset - s->buffer_offset;
546 return s->buffer + where - s->buffer_offset;
551 fd_sfree (unix_stream * s)
553 if (s->ndirty != 0 &&
554 (s->buffer != s->small_buffer || options.all_unbuffered ||
563 fd_seek (unix_stream * s, gfc_offset offset)
565 if (s->physical_offset == offset) /* Are we lucky and avoid syscall? */
567 s->logical_offset = offset;
571 s->physical_offset = s->logical_offset = offset;
573 return (lseek (s->fd, offset, SEEK_SET) < 0) ? FAILURE : SUCCESS;
577 /* truncate_file()-- Given a unit, truncate the file at the current
578 * position. Sets the physical location to the new end of the file.
579 * Returns nonzero on error. */
582 fd_truncate (unix_stream * s)
584 if (lseek (s->fd, s->logical_offset, SEEK_SET) == -1)
587 /* non-seekable files, like terminals and fifo's fail the lseek.
588 Using ftruncate on a seekable special file (like /dev/null)
589 is undefined, so we treat it as if the ftruncate succeeded.
591 #ifdef HAVE_FTRUNCATE
592 if (s->special_file || ftruncate (s->fd, s->logical_offset))
595 if (s->special_file || chsize (s->fd, s->logical_offset))
599 s->physical_offset = s->file_length = 0;
603 s->physical_offset = s->file_length = s->logical_offset;
609 /* Similar to memset(), but operating on a stream instead of a string.
610 Takes care of not using too much memory. */
613 fd_sset (unix_stream * s, int c, size_t n)
621 while (bytes_left > 0)
623 /* memset() in chunks of BUFFER_SIZE. */
624 trans = (bytes_left < BUFFER_SIZE) ? bytes_left : BUFFER_SIZE;
626 p = fd_alloc_w_at (s, &trans, -1);
628 memset (p, c, trans);
639 /* Stream read function. Avoids using a buffer for big reads. The
640 interface is like POSIX read(), but the nbytes argument is a
641 pointer; on return it contains the number of bytes written. The
642 function return value is the status indicator (0 for success). */
645 fd_read (unix_stream * s, void * buf, size_t * nbytes)
650 if (*nbytes < BUFFER_SIZE && !s->unbuffered)
653 p = fd_alloc_r_at (s, &tmp, -1);
657 memcpy (buf, p, *nbytes);
667 /* If the request is bigger than BUFFER_SIZE we flush the buffers
668 and read directly. */
669 if (fd_flush (s) == FAILURE)
675 if (is_seekable ((stream *) s) && fd_seek (s, s->logical_offset) == FAILURE)
681 status = do_read (s, buf, nbytes);
682 reset_stream (s, *nbytes);
687 /* Stream write function. Avoids using a buffer for big writes. The
688 interface is like POSIX write(), but the nbytes argument is a
689 pointer; on return it contains the number of bytes written. The
690 function return value is the status indicator (0 for success). */
693 fd_write (unix_stream * s, const void * buf, size_t * nbytes)
698 if (*nbytes < BUFFER_SIZE && !s->unbuffered)
701 p = fd_alloc_w_at (s, &tmp, -1);
705 memcpy (p, buf, *nbytes);
715 /* If the request is bigger than BUFFER_SIZE we flush the buffers
716 and write directly. */
717 if (fd_flush (s) == FAILURE)
723 if (is_seekable ((stream *) s) && fd_seek (s, s->logical_offset) == FAILURE)
729 status = do_write (s, buf, nbytes);
730 reset_stream (s, *nbytes);
736 fd_close (unix_stream * s)
738 if (fd_flush (s) == FAILURE)
741 if (s->buffer != NULL && s->buffer != s->small_buffer)
742 free_mem (s->buffer);
744 if (s->fd != STDOUT_FILENO && s->fd != STDERR_FILENO)
746 if (close (s->fd) < 0)
757 fd_open (unix_stream * s)
762 s->st.alloc_r_at = (void *) fd_alloc_r_at;
763 s->st.alloc_w_at = (void *) fd_alloc_w_at;
764 s->st.sfree = (void *) fd_sfree;
765 s->st.close = (void *) fd_close;
766 s->st.seek = (void *) fd_seek;
767 s->st.truncate = (void *) fd_truncate;
768 s->st.read = (void *) fd_read;
769 s->st.write = (void *) fd_write;
770 s->st.set = (void *) fd_sset;
778 /*********************************************************************
779 memory stream functions - These are used for internal files
781 The idea here is that a single stream structure is created and all
782 requests must be satisfied from it. The location and size of the
783 buffer is the character variable supplied to the READ or WRITE
786 *********************************************************************/
790 mem_alloc_r_at (unix_stream * s, int *len, gfc_offset where)
795 where = s->logical_offset;
797 if (where < s->buffer_offset || where > s->buffer_offset + s->active)
800 s->logical_offset = where + *len;
802 n = s->buffer_offset + s->active - where;
806 return s->buffer + (where - s->buffer_offset);
811 mem_alloc_w_at (unix_stream * s, int *len, gfc_offset where)
815 assert (*len >= 0); /* Negative values not allowed. */
818 where = s->logical_offset;
822 if (where < s->buffer_offset)
825 if (m > s->file_length)
828 s->logical_offset = m;
830 return s->buffer + (where - s->buffer_offset);
834 /* Stream read function for internal units. This is not actually used
835 at the moment, as all internal IO is formatted and the formatted IO
836 routines use mem_alloc_r_at. */
839 mem_read (unix_stream * s, void * buf, size_t * nbytes)
845 p = mem_alloc_r_at (s, &tmp, -1);
849 memcpy (buf, p, *nbytes);
860 /* Stream write function for internal units. This is not actually used
861 at the moment, as all internal IO is formatted and the formatted IO
862 routines use mem_alloc_w_at. */
865 mem_write (unix_stream * s, const void * buf, size_t * nbytes)
873 p = mem_alloc_w_at (s, &tmp, -1);
877 memcpy (p, buf, *nbytes);
889 mem_seek (unix_stream * s, gfc_offset offset)
891 if (offset > s->file_length)
897 s->logical_offset = offset;
903 mem_set (unix_stream * s, int c, size_t n)
910 p = mem_alloc_w_at (s, &len, -1);
922 mem_truncate (unix_stream * s __attribute__ ((unused)))
929 mem_close (unix_stream * s)
938 mem_sfree (unix_stream * s __attribute__ ((unused)))
945 /*********************************************************************
946 Public functions -- A reimplementation of this module needs to
947 define functional equivalents of the following.
948 *********************************************************************/
950 /* empty_internal_buffer()-- Zero the buffer of Internal file */
953 empty_internal_buffer(stream *strm)
955 unix_stream * s = (unix_stream *) strm;
956 memset(s->buffer, ' ', s->file_length);
959 /* open_internal()-- Returns a stream structure from an internal file */
962 open_internal (char *base, int length)
966 s = get_mem (sizeof (unix_stream));
967 memset (s, '\0', sizeof (unix_stream));
970 s->buffer_offset = 0;
972 s->logical_offset = 0;
973 s->active = s->file_length = length;
975 s->st.alloc_r_at = (void *) mem_alloc_r_at;
976 s->st.alloc_w_at = (void *) mem_alloc_w_at;
977 s->st.sfree = (void *) mem_sfree;
978 s->st.close = (void *) mem_close;
979 s->st.seek = (void *) mem_seek;
980 s->st.truncate = (void *) mem_truncate;
981 s->st.read = (void *) mem_read;
982 s->st.write = (void *) mem_write;
983 s->st.set = (void *) mem_set;
989 /* fd_to_stream()-- Given an open file descriptor, build a stream
993 fd_to_stream (int fd, int prot)
998 s = get_mem (sizeof (unix_stream));
999 memset (s, '\0', sizeof (unix_stream));
1002 s->buffer_offset = 0;
1003 s->physical_offset = 0;
1004 s->logical_offset = 0;
1007 /* Get the current length of the file. */
1009 fstat (fd, &statbuf);
1010 s->file_length = S_ISREG (statbuf.st_mode) ? statbuf.st_size : -1;
1011 s->special_file = !S_ISREG (statbuf.st_mode);
1015 return (stream *) s;
1019 /* Given the Fortran unit number, convert it to a C file descriptor. */
1022 unit_to_fd (int unit)
1027 us = find_unit (unit);
1031 fd = ((unix_stream *) us->s)->fd;
1037 /* unpack_filename()-- Given a fortran string and a pointer to a
1038 * buffer that is PATH_MAX characters, convert the fortran string to a
1039 * C string in the buffer. Returns nonzero if this is not possible. */
1042 unpack_filename (char *cstring, const char *fstring, int len)
1044 len = fstrlen (fstring, len);
1045 if (len >= PATH_MAX)
1048 memmove (cstring, fstring, len);
1049 cstring[len] = '\0';
1055 /* tempfile()-- Generate a temporary filename for a scratch file and
1056 * open it. mkstemp() opens the file for reading and writing, but the
1057 * library mode prevents anything that is not allowed. The descriptor
1058 * is returned, which is -1 on error. The template is pointed to by
1059 * opp->file, which is copied into the unit structure
1060 * and freed later. */
1063 tempfile (st_parameter_open *opp)
1065 const char *tempdir;
1069 tempdir = getenv ("GFORTRAN_TMPDIR");
1070 if (tempdir == NULL)
1071 tempdir = getenv ("TMP");
1072 if (tempdir == NULL)
1073 tempdir = getenv ("TEMP");
1074 if (tempdir == NULL)
1075 tempdir = DEFAULT_TEMPDIR;
1077 template = get_mem (strlen (tempdir) + 20);
1079 st_sprintf (template, "%s/gfortrantmpXXXXXX", tempdir);
1083 fd = mkstemp (template);
1085 #else /* HAVE_MKSTEMP */
1087 if (mktemp (template))
1089 #if defined(HAVE_CRLF) && defined(O_BINARY)
1090 fd = open (template, O_RDWR | O_CREAT | O_EXCL | O_BINARY,
1091 S_IREAD | S_IWRITE);
1093 fd = open (template, O_RDWR | O_CREAT | O_EXCL, S_IREAD | S_IWRITE);
1095 while (!(fd == -1 && errno == EEXIST) && mktemp (template));
1099 #endif /* HAVE_MKSTEMP */
1102 free_mem (template);
1105 opp->file = template;
1106 opp->file_len = strlen (template); /* Don't include trailing nul */
1113 /* regular_file()-- Open a regular file.
1114 * Change flags->action if it is ACTION_UNSPECIFIED on entry,
1115 * unless an error occurs.
1116 * Returns the descriptor, which is less than zero on error. */
1119 regular_file (st_parameter_open *opp, unit_flags *flags)
1121 char path[PATH_MAX + 1];
1127 if (unpack_filename (path, opp->file, opp->file_len))
1129 errno = ENOENT; /* Fake an OS error */
1135 switch (flags->action)
1145 case ACTION_READWRITE:
1146 case ACTION_UNSPECIFIED:
1151 internal_error (&opp->common, "regular_file(): Bad action");
1154 switch (flags->status)
1157 crflag = O_CREAT | O_EXCL;
1160 case STATUS_OLD: /* open will fail if the file does not exist*/
1164 case STATUS_UNKNOWN:
1165 case STATUS_SCRATCH:
1169 case STATUS_REPLACE:
1170 crflag = O_CREAT | O_TRUNC;
1174 internal_error (&opp->common, "regular_file(): Bad status");
1177 /* rwflag |= O_LARGEFILE; */
1179 #if defined(HAVE_CRLF) && defined(O_BINARY)
1183 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1184 fd = open (path, rwflag | crflag, mode);
1185 if (flags->action != ACTION_UNSPECIFIED)
1190 flags->action = ACTION_READWRITE;
1193 if (errno != EACCES)
1196 /* retry for read-only access */
1198 fd = open (path, rwflag | crflag, mode);
1201 flags->action = ACTION_READ;
1202 return fd; /* success */
1205 if (errno != EACCES)
1206 return fd; /* failure */
1208 /* retry for write-only access */
1210 fd = open (path, rwflag | crflag, mode);
1213 flags->action = ACTION_WRITE;
1214 return fd; /* success */
1216 return fd; /* failure */
1220 /* open_external()-- Open an external file, unix specific version.
1221 * Change flags->action if it is ACTION_UNSPECIFIED on entry.
1222 * Returns NULL on operating system error. */
1225 open_external (st_parameter_open *opp, unit_flags *flags)
1229 if (flags->status == STATUS_SCRATCH)
1231 fd = tempfile (opp);
1232 if (flags->action == ACTION_UNSPECIFIED)
1233 flags->action = ACTION_READWRITE;
1235 #if HAVE_UNLINK_OPEN_FILE
1236 /* We can unlink scratch files now and it will go away when closed. */
1243 /* regular_file resets flags->action if it is ACTION_UNSPECIFIED and
1245 fd = regular_file (opp, flags);
1252 switch (flags->action)
1262 case ACTION_READWRITE:
1263 prot = PROT_READ | PROT_WRITE;
1267 internal_error (&opp->common, "open_external(): Bad action");
1270 return fd_to_stream (fd, prot);
1274 /* input_stream()-- Return a stream pointer to the default input stream.
1275 * Called on initialization. */
1280 return fd_to_stream (STDIN_FILENO, PROT_READ);
1284 /* output_stream()-- Return a stream pointer to the default output stream.
1285 * Called on initialization. */
1288 output_stream (void)
1290 return fd_to_stream (STDOUT_FILENO, PROT_WRITE);
1294 /* error_stream()-- Return a stream pointer to the default error stream.
1295 * Called on initialization. */
1300 return fd_to_stream (STDERR_FILENO, PROT_WRITE);
1303 /* init_error_stream()-- Return a pointer to the error stream. This
1304 * subroutine is called when the stream is needed, rather than at
1305 * initialization. We want to work even if memory has been seriously
1309 init_error_stream (unix_stream *error)
1311 memset (error, '\0', sizeof (*error));
1313 error->fd = options.use_stderr ? STDERR_FILENO : STDOUT_FILENO;
1315 error->st.alloc_w_at = (void *) fd_alloc_w_at;
1316 error->st.sfree = (void *) fd_sfree;
1318 error->unbuffered = 1;
1319 error->buffer = error->small_buffer;
1321 return (stream *) error;
1325 /* compare_file_filename()-- Given an open stream and a fortran string
1326 * that is a filename, figure out if the file is the same as the
1330 compare_file_filename (gfc_unit *u, const char *name, int len)
1332 char path[PATH_MAX + 1];
1334 #ifdef HAVE_WORKING_STAT
1338 if (unpack_filename (path, name, len))
1339 return 0; /* Can't be the same */
1341 /* If the filename doesn't exist, then there is no match with the
1344 if (stat (path, &st1) < 0)
1347 #ifdef HAVE_WORKING_STAT
1348 fstat (((unix_stream *) (u->s))->fd, &st2);
1349 return (st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino);
1351 if (len != u->file_len)
1353 return (memcmp(path, u->file, len) == 0);
1358 #ifdef HAVE_WORKING_STAT
1359 # define FIND_FILE0_DECL struct stat *st
1360 # define FIND_FILE0_ARGS st
1362 # define FIND_FILE0_DECL const char *file, gfc_charlen_type file_len
1363 # define FIND_FILE0_ARGS file, file_len
1366 /* find_file0()-- Recursive work function for find_file() */
1369 find_file0 (gfc_unit *u, FIND_FILE0_DECL)
1376 #ifdef HAVE_WORKING_STAT
1378 && fstat (((unix_stream *) u->s)->fd, &st[1]) >= 0 &&
1379 st[0].st_dev == st[1].st_dev && st[0].st_ino == st[1].st_ino)
1382 if (compare_string (u->file_len, u->file, file_len, file) == 0)
1386 v = find_file0 (u->left, FIND_FILE0_ARGS);
1390 v = find_file0 (u->right, FIND_FILE0_ARGS);
1398 /* find_file()-- Take the current filename and see if there is a unit
1399 * that has the file already open. Returns a pointer to the unit if so. */
1402 find_file (const char *file, gfc_charlen_type file_len)
1404 char path[PATH_MAX + 1];
1408 if (unpack_filename (path, file, file_len))
1411 if (stat (path, &st[0]) < 0)
1414 __gthread_mutex_lock (&unit_lock);
1416 u = find_file0 (unit_root, FIND_FILE0_ARGS);
1420 if (! __gthread_mutex_trylock (&u->lock))
1422 /* assert (u->closed == 0); */
1423 __gthread_mutex_unlock (&unit_lock);
1427 inc_waiting_locked (u);
1429 __gthread_mutex_unlock (&unit_lock);
1432 __gthread_mutex_lock (&u->lock);
1435 __gthread_mutex_lock (&unit_lock);
1436 __gthread_mutex_unlock (&u->lock);
1437 if (predec_waiting_locked (u) == 0)
1442 dec_waiting_unlocked (u);
1448 flush_all_units_1 (gfc_unit *u, int min_unit)
1452 if (u->unit_number > min_unit)
1454 gfc_unit *r = flush_all_units_1 (u->left, min_unit);
1458 if (u->unit_number >= min_unit)
1460 if (__gthread_mutex_trylock (&u->lock))
1464 __gthread_mutex_unlock (&u->lock);
1472 flush_all_units (void)
1477 __gthread_mutex_lock (&unit_lock);
1480 u = flush_all_units_1 (unit_root, min_unit);
1482 inc_waiting_locked (u);
1483 __gthread_mutex_unlock (&unit_lock);
1487 __gthread_mutex_lock (&u->lock);
1489 min_unit = u->unit_number + 1;
1494 __gthread_mutex_lock (&unit_lock);
1495 __gthread_mutex_unlock (&u->lock);
1496 (void) predec_waiting_locked (u);
1500 __gthread_mutex_lock (&unit_lock);
1501 __gthread_mutex_unlock (&u->lock);
1502 if (predec_waiting_locked (u) == 0)
1510 /* stream_at_bof()-- Returns nonzero if the stream is at the beginning
1514 stream_at_bof (stream * s)
1518 if (!is_seekable (s))
1521 us = (unix_stream *) s;
1523 return us->logical_offset == 0;
1527 /* stream_at_eof()-- Returns nonzero if the stream is at the end
1531 stream_at_eof (stream * s)
1535 if (!is_seekable (s))
1538 us = (unix_stream *) s;
1540 return us->logical_offset == us->dirty_offset;
1544 /* delete_file()-- Given a unit structure, delete the file associated
1545 * with the unit. Returns nonzero if something went wrong. */
1548 delete_file (gfc_unit * u)
1550 char path[PATH_MAX + 1];
1552 if (unpack_filename (path, u->file, u->file_len))
1553 { /* Shouldn't be possible */
1558 return unlink (path);
1562 /* file_exists()-- Returns nonzero if the current filename exists on
1566 file_exists (const char *file, gfc_charlen_type file_len)
1568 char path[PATH_MAX + 1];
1569 struct stat statbuf;
1571 if (unpack_filename (path, file, file_len))
1574 if (stat (path, &statbuf) < 0)
1582 static const char yes[] = "YES", no[] = "NO", unknown[] = "UNKNOWN";
1584 /* inquire_sequential()-- Given a fortran string, determine if the
1585 * file is suitable for sequential access. Returns a C-style
1589 inquire_sequential (const char *string, int len)
1591 char path[PATH_MAX + 1];
1592 struct stat statbuf;
1594 if (string == NULL ||
1595 unpack_filename (path, string, len) || stat (path, &statbuf) < 0)
1598 if (S_ISREG (statbuf.st_mode) ||
1599 S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))
1602 if (S_ISDIR (statbuf.st_mode) || S_ISBLK (statbuf.st_mode))
1609 /* inquire_direct()-- Given a fortran string, determine if the file is
1610 * suitable for direct access. Returns a C-style string. */
1613 inquire_direct (const char *string, int len)
1615 char path[PATH_MAX + 1];
1616 struct stat statbuf;
1618 if (string == NULL ||
1619 unpack_filename (path, string, len) || stat (path, &statbuf) < 0)
1622 if (S_ISREG (statbuf.st_mode) || S_ISBLK (statbuf.st_mode))
1625 if (S_ISDIR (statbuf.st_mode) ||
1626 S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))
1633 /* inquire_formatted()-- Given a fortran string, determine if the file
1634 * is suitable for formatted form. Returns a C-style string. */
1637 inquire_formatted (const char *string, int len)
1639 char path[PATH_MAX + 1];
1640 struct stat statbuf;
1642 if (string == NULL ||
1643 unpack_filename (path, string, len) || stat (path, &statbuf) < 0)
1646 if (S_ISREG (statbuf.st_mode) ||
1647 S_ISBLK (statbuf.st_mode) ||
1648 S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))
1651 if (S_ISDIR (statbuf.st_mode))
1658 /* inquire_unformatted()-- Given a fortran string, determine if the file
1659 * is suitable for unformatted form. Returns a C-style string. */
1662 inquire_unformatted (const char *string, int len)
1664 return inquire_formatted (string, len);
1668 /* inquire_access()-- Given a fortran string, determine if the file is
1669 * suitable for access. */
1672 inquire_access (const char *string, int len, int mode)
1674 char path[PATH_MAX + 1];
1676 if (string == NULL || unpack_filename (path, string, len) ||
1677 access (path, mode) < 0)
1684 /* inquire_read()-- Given a fortran string, determine if the file is
1685 * suitable for READ access. */
1688 inquire_read (const char *string, int len)
1690 return inquire_access (string, len, R_OK);
1694 /* inquire_write()-- Given a fortran string, determine if the file is
1695 * suitable for READ access. */
1698 inquire_write (const char *string, int len)
1700 return inquire_access (string, len, W_OK);
1704 /* inquire_readwrite()-- Given a fortran string, determine if the file is
1705 * suitable for read and write access. */
1708 inquire_readwrite (const char *string, int len)
1710 return inquire_access (string, len, R_OK | W_OK);
1714 /* file_length()-- Return the file length in bytes, -1 if unknown */
1717 file_length (stream * s)
1719 return ((unix_stream *) s)->file_length;
1723 /* file_position()-- Return the current position of the file */
1726 file_position (stream * s)
1728 return ((unix_stream *) s)->logical_offset;
1732 /* is_seekable()-- Return nonzero if the stream is seekable, zero if
1736 is_seekable (stream * s)
1738 /* By convention, if file_length == -1, the file is not
1740 return ((unix_stream *) s)->file_length!=-1;
1746 return fd_flush( (unix_stream *) s);
1750 stream_isatty (stream *s)
1752 return isatty (((unix_stream *) s)->fd);
1756 stream_ttyname (stream *s)
1759 return ttyname (((unix_stream *) s)->fd);
1766 stream_offset (stream *s)
1768 return (((unix_stream *) s)->logical_offset);
1772 /* How files are stored: This is an operating-system specific issue,
1773 and therefore belongs here. There are three cases to consider.
1776 Records are written as block of bytes corresponding to the record
1777 length of the file. This goes for both formatted and unformatted
1778 records. Positioning is done explicitly for each data transfer,
1779 so positioning is not much of an issue.
1781 Sequential Formatted:
1782 Records are separated by newline characters. The newline character
1783 is prohibited from appearing in a string. If it does, this will be
1784 messed up on the next read. End of file is also the end of a record.
1786 Sequential Unformatted:
1787 In this case, we are merely copying bytes to and from main storage,
1788 yet we need to keep track of varying record lengths. We adopt
1789 the solution used by f2c. Each record contains a pair of length
1792 Length of record n in bytes
1794 Length of record n in bytes
1796 Length of record n+1 in bytes
1798 Length of record n+1 in bytes
1800 The length is stored at the end of a record to allow backspacing to the
1801 previous record. Between data transfer statements, the file pointer
1802 is left pointing to the first length of the current record.
1804 ENDFILE records are never explicitly stored.