diff options
Diffstat (limited to 'libgfortran/io/unix.c')
-rw-r--r-- | libgfortran/io/unix.c | 78 |
1 files changed, 17 insertions, 61 deletions
diff --git a/libgfortran/io/unix.c b/libgfortran/io/unix.c index 25cb559db01..00f7c726897 100644 --- a/libgfortran/io/unix.c +++ b/libgfortran/io/unix.c @@ -187,7 +187,7 @@ typedef struct gfc_offset buffer_offset; /* File offset of the start of the buffer */ gfc_offset physical_offset; /* Current physical file offset */ gfc_offset logical_offset; /* Current logical file offset */ - gfc_offset file_length; /* Length of the file, -1 if not seekable. */ + gfc_offset file_length; /* Length of the file. */ char *buffer; /* Pointer to the buffer. */ int fd; /* The POSIX file descriptor. */ @@ -196,8 +196,6 @@ typedef struct int ndirty; /* Dirty bytes starting at buffer_offset */ - int special_file; /* =1 if the fd refers to a special file */ - /* Cached stat(2) values. */ dev_t st_dev; ino_t st_ino; @@ -413,7 +411,7 @@ raw_init (unix_stream * s) Buffered I/O functions. These functions have the same semantics as the raw I/O functions above, except that they are buffered in order to improve performance. The buffer must be flushed when switching from -reading to writing and vice versa. +reading to writing and vice versa. Only supported for regular files. *********************************************************************/ static int @@ -427,7 +425,7 @@ buf_flush (unix_stream * s) if (s->ndirty == 0) return 0; - if (s->file_length != -1 && s->physical_offset != s->buffer_offset + if (s->physical_offset != s->buffer_offset && lseek (s->fd, s->buffer_offset, SEEK_SET) < 0) return -1; @@ -435,8 +433,7 @@ buf_flush (unix_stream * s) s->physical_offset = s->buffer_offset + writelen; - /* Don't increment file_length if the file is non-seekable. */ - if (s->file_length != -1 && s->physical_offset > s->file_length) + if (s->physical_offset > s->file_length) s->file_length = s->physical_offset; s->ndirty -= writelen; @@ -481,7 +478,7 @@ buf_read (unix_stream * s, void * buf, ssize_t nbyte) /* At this point we consider all bytes in the buffer discarded. */ to_read = nbyte - nread; new_logical = s->logical_offset + nread; - if (s->file_length != -1 && s->physical_offset != new_logical + if (s->physical_offset != new_logical && lseek (s->fd, new_logical, SEEK_SET) < 0) return -1; s->buffer_offset = s->physical_offset = new_logical; @@ -539,7 +536,7 @@ buf_write (unix_stream * s, const void * buf, ssize_t nbyte) } else { - if (s->file_length != -1 && s->physical_offset != s->logical_offset) + if (s->physical_offset != s->logical_offset) { if (lseek (s->fd, s->logical_offset, SEEK_SET) < 0) return -1; @@ -551,8 +548,7 @@ buf_write (unix_stream * s, const void * buf, ssize_t nbyte) } } s->logical_offset += nbyte; - /* Don't increment file_length if the file is non-seekable. */ - if (s->file_length != -1 && s->logical_offset > s->file_length) + if (s->logical_offset > s->file_length) s->file_length = s->logical_offset; return nbyte; } @@ -560,11 +556,6 @@ buf_write (unix_stream * s, const void * buf, ssize_t nbyte) static gfc_offset buf_seek (unix_stream * s, gfc_offset offset, int whence) { - if (s->file_length == -1) - { - errno = ESPIPE; - return -1; - } switch (whence) { case SEEK_SET: @@ -953,30 +944,18 @@ fd_to_stream (int fd) s->st_dev = statbuf.st_dev; s->st_ino = statbuf.st_ino; - s->special_file = !S_ISREG (statbuf.st_mode); - - if (S_ISREG (statbuf.st_mode)) - s->file_length = statbuf.st_size; + s->file_length = statbuf.st_size; + + /* Only use buffered IO for regular files. */ + if (S_ISREG (statbuf.st_mode) + && !options.all_unbuffered + && !(options.unbuffered_preconnected && + (s->fd == STDIN_FILENO + || s->fd == STDOUT_FILENO + || s->fd == STDERR_FILENO))) + buf_init (s); else - { - /* Some character special files are seekable but most are not, - so figure it out by trying to seek. On Linux, /dev/null is - an example of such a special file. */ - s->file_length = lseek (fd, 0, SEEK_END); - if (s->file_length > 0) - lseek (fd, 0, SEEK_SET); - } - - if (!(S_ISREG (statbuf.st_mode) || S_ISBLK (statbuf.st_mode)) - || options.all_unbuffered - ||(options.unbuffered_preconnected && - (s->fd == STDIN_FILENO - || s->fd == STDOUT_FILENO - || s->fd == STDERR_FILENO)) - || isatty (s->fd)) raw_init (s); - else - buf_init (s); return (stream *) s; } @@ -1767,8 +1746,6 @@ gfc_offset file_length (stream * s) { gfc_offset curr, end; - if (!is_seekable (s)) - return -1; curr = stell (s); if (curr == -1) return curr; @@ -1778,27 +1755,6 @@ file_length (stream * s) } -/* is_seekable()-- Return nonzero if the stream is seekable, zero if - * it is not */ - -int -is_seekable (stream *s) -{ - /* By convention, if file_length == -1, the file is not - seekable. */ - return ((unix_stream *) s)->file_length!=-1; -} - - -/* is_special()-- Return nonzero if the stream is not a regular file. */ - -int -is_special (stream *s) -{ - return ((unix_stream *) s)->special_file; -} - - int stream_isatty (stream *s) { |