-/* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
+/* Copyright (C) 2002, 2003, 2004, 2005
+ Free Software Foundation, Inc.
Contributed by Andy Vaught
This file is part of the GNU Fortran 95 runtime library (libgfortran).
You should have received a copy of the GNU General Public License
along with Libgfortran; see the file COPYING. If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA. */
+the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA. */
/* Unix stream I/O module */
int prot;
int ndirty; /* Dirty bytes starting at dirty_offset */
+ int special_file; /* =1 if the fd refers to a special file */
+
unsigned unbuffered:1, mmaped:1;
char small_buffer[BUFFER_SIZE];
unix_stream * str = (unix_stream*)st;
if (pos_off < 0)
{
- str->active += pos_off;
- if (str->active < 0)
- str->active = 0;
-
- str->logical_offset += pos_off;
+ str->logical_offset += pos_off;
- if (str->dirty_offset+str->ndirty > str->logical_offset)
+ if (str->dirty_offset + str->ndirty > str->logical_offset)
{
- if (str->ndirty + pos_off > 0)
- str->ndirty += pos_off ;
+ if (str->ndirty + pos_off > 0)
+ str->ndirty += pos_off;
else
{
str->dirty_offset += pos_off + pos_off;
- str->ndirty = 0 ;
+ str->ndirty = 0;
}
}
- return pos_off ;
+ return pos_off;
}
- return 0 ;
+ return 0;
}
* to come next. */
static void
-fd_alloc (unix_stream * s, gfc_offset where, int *len)
+fd_alloc (unix_stream * s, gfc_offset where,
+ int *len __attribute__ ((unused)))
{
char *new_buffer;
int n, read_len;
return FAILURE;
/* non-seekable files, like terminals and fifo's fail the lseek.
- the fd is a regular file at this point */
-
- if (ftruncate (s->fd, s->logical_offset))
- return FAILURE;
+ Using ftruncate on a seekable special file (like /dev/null)
+ is undefined, so we treat it as if the ftruncate failed.
+ */
+#ifdef HAVE_FTRUNCATE
+ if (s->special_file || ftruncate (s->fd, s->logical_offset))
+#else
+#ifdef HAVE_CHSIZE
+ if (s->special_file || chsize (s->fd, s->logical_offset))
+#endif
+#endif
+ {
+ s->physical_offset = s->file_length = 0;
+ return FAILURE;
+ }
s->physical_offset = s->file_length = s->logical_offset;
if (s->buffer != NULL && s->buffer != s->small_buffer)
free_mem (s->buffer);
- if (close (s->fd) < 0)
- return FAILURE;
+ if (s->fd != STDOUT_FILENO && s->fd != STDERR_FILENO)
+ {
+ if (close (s->fd) < 0)
+ return FAILURE;
+ }
free_mem (s);
* guaranteed to be mappable. */
static try
-mmap_alloc (unix_stream * s, gfc_offset where, int *len)
+mmap_alloc (unix_stream * s, gfc_offset where,
+ int *len __attribute__ ((unused)))
{
gfc_offset offset;
int length;
static try
-mmap_sfree (unix_stream * s)
+mmap_sfree (unix_stream * s __attribute__ ((unused)))
{
return SUCCESS;
}
* mmap()-ed, we fall back to the file descriptor functions. */
static try
-mmap_open (unix_stream * s)
+mmap_open (unix_stream * s __attribute__ ((unused)))
{
char *p;
int i;
static int
-mem_truncate (unix_stream * s)
+mem_truncate (unix_stream * s __attribute__ ((unused)))
{
return SUCCESS;
}
static try
-mem_sfree (unix_stream * s)
+mem_sfree (unix_stream * s __attribute__ ((unused)))
{
return SUCCESS;
}
unix_stream *s;
s = get_mem (sizeof (unix_stream));
+ memset (s, '\0', sizeof (unix_stream));
s->buffer = base;
s->buffer_offset = 0;
* around it. */
static stream *
-fd_to_stream (int fd, int prot)
+fd_to_stream (int fd, int prot, int avoid_mmap)
{
struct stat statbuf;
unix_stream *s;
s = get_mem (sizeof (unix_stream));
+ memset (s, '\0', sizeof (unix_stream));
s->fd = fd;
s->buffer_offset = 0;
fstat (fd, &statbuf);
s->file_length = S_ISREG (statbuf.st_mode) ? statbuf.st_size : -1;
+ s->special_file = !S_ISREG (statbuf.st_mode);
#if HAVE_MMAP
- mmap_open (s);
+ if (avoid_mmap)
+ fd_open (s);
+ else
+ mmap_open (s);
#else
fd_open (s);
#endif
* buffer that is PATH_MAX characters, convert the fortran string to a
* C string in the buffer. Returns nonzero if this is not possible. */
-static int
+int
unpack_filename (char *cstring, const char *fstring, int len)
{
len = fstrlen (fstring, len);
if (tempdir == NULL)
tempdir = getenv ("TMP");
if (tempdir == NULL)
+ tempdir = getenv ("TEMP");
+ if (tempdir == NULL)
tempdir = DEFAULT_TEMPDIR;
template = get_mem (strlen (tempdir) + 20);
if (mktemp (template))
do
- fd = open (template, O_CREAT | O_EXCL, S_IREAD | S_IWRITE);
+ fd = open (template, O_RDWR | O_CREAT | O_EXCL, S_IREAD | S_IWRITE);
while (!(fd == -1 && errno == EEXIST) && mktemp (template));
else
fd = -1;
fd = tempfile ();
if (flags->action == ACTION_UNSPECIFIED)
flags->action = ACTION_READWRITE;
+
+#if HAVE_UNLINK_OPEN_FILE
/* We can unlink scratch files now and it will go away when closed. */
unlink (ioparm.file);
+#endif
}
else
{
internal_error ("open_external(): Bad action");
}
- return fd_to_stream (fd, prot);
+ return fd_to_stream (fd, prot, 0);
}
stream *
input_stream (void)
{
- return fd_to_stream (STDIN_FILENO, PROT_READ);
+ return fd_to_stream (STDIN_FILENO, PROT_READ, 1);
}
-/* output_stream()-- Return a stream pointer to the default input stream.
+/* output_stream()-- Return a stream pointer to the default output stream.
* Called on initialization. */
stream *
output_stream (void)
{
- return fd_to_stream (STDOUT_FILENO, PROT_WRITE);
+ return fd_to_stream (STDOUT_FILENO, PROT_WRITE, 1);
}
+/* error_stream()-- Return a stream pointer to the default error stream.
+ * Called on initialization. */
+
+stream *
+error_stream (void)
+{
+ return fd_to_stream (STDERR_FILENO, PROT_WRITE, 1);
+}
+
/* init_error_stream()-- Return a pointer to the error stream. This
* subroutine is called when the stream is needed, rather than at
* initialization. We want to work even if memory has been seriously
{
unix_stream *us;
- us = (unix_stream *) s;
+ if (!is_seekable (s))
+ return 0;
- if (!us->mmaped)
- return 0; /* File is not seekable */
+ us = (unix_stream *) s;
return us->logical_offset == 0;
}
{
unix_stream *us;
- us = (unix_stream *) s;
+ if (!is_seekable (s))
+ return 0;
- if (!us->mmaped)
- return 0; /* File is not seekable */
+ us = (unix_stream *) s;
return us->logical_offset == us->dirty_offset;
}
return fd_flush( (unix_stream *) s);
}
+int
+stream_isatty (stream *s)
+{
+ return isatty (((unix_stream *) s)->fd);
+}
+
+char *
+stream_ttyname (stream *s)
+{
+ return ttyname (((unix_stream *) s)->fd);
+}
+
/* How files are stored: This is an operating-system specific issue,
and therefore belongs here. There are three cases to consider.