From b65b81f93559efccba6253afefbe639526fe9d8c Mon Sep 17 00:00:00 2001
From: Francois-Xavier Coudert <coudert@clipper.ens.fr>
Date: Sun, 11 Sep 2005 15:34:57 +0200
Subject: [PATCH] re PR libfortran/20179 (cannot mix C and Fortran I/O)

	PR libfortran/20179
	* io/unix.c (is_preconnected): Add function to test if a stream
	corresponds to a preconnected unit.
	* io/io.h: Add prototype for is_preconnected.
	* io/transfer.c (data_transfer_init): Do not truncate
	preconnected units.

From-SVN: r104141
---
 libgfortran/ChangeLog     |  9 +++++++++
 libgfortran/io/io.h       |  3 +++
 libgfortran/io/transfer.c |  2 +-
 libgfortran/io/unix.c     | 11 +++++++++++
 4 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog
index 5dd311ad5890..d36e500ddca4 100644
--- a/libgfortran/ChangeLog
+++ b/libgfortran/ChangeLog
@@ -1,3 +1,12 @@
+2005-09-11  Francois-Xavier Coudert  <coudert@clipper.ens.fr>
+
+	PR libfortran/20179
+	* io/unix.c (is_preconnected): Add function to test if a stream
+	corresponds to a preconnected unit.
+	* io/io.h: Add prototype for is_preconnected.
+	* io/transfer.c (data_transfer_init): Do not truncate
+	preconnected units.
+
 2005-09-10  Janne Blomqvist  <jblomqvi@cc.hut.fi>
 
 	* io/unix.c: Remove mmap code.
diff --git a/libgfortran/io/io.h b/libgfortran/io/io.h
index 6f4023b9e33d..fc8b8873d8a1 100644
--- a/libgfortran/io/io.h
+++ b/libgfortran/io/io.h
@@ -493,6 +493,9 @@ internal_proto(file_position);
 extern int is_seekable (stream *);
 internal_proto(is_seekable);
 
+extern int is_preconnected (stream *);
+internal_proto(is_preconnected);
+
 extern void empty_internal_buffer(stream *);
 internal_proto(empty_internal_buffer);
 
diff --git a/libgfortran/io/transfer.c b/libgfortran/io/transfer.c
index 9251cf8f24f3..cb06a79fba51 100644
--- a/libgfortran/io/transfer.c
+++ b/libgfortran/io/transfer.c
@@ -1183,7 +1183,7 @@ data_transfer_init (int read_flag)
      it is always safe to truncate the file on the first write */
   if (g.mode == WRITING
       && current_unit->flags.access == ACCESS_SEQUENTIAL
-      && current_unit->last_record == 0)
+      && current_unit->last_record == 0 && !is_preconnected(current_unit->s))
 	struncate(current_unit->s);
 
   current_unit->mode = g.mode;
diff --git a/libgfortran/io/unix.c b/libgfortran/io/unix.c
index 1a4beddfc067..ca96c229b48c 100644
--- a/libgfortran/io/unix.c
+++ b/libgfortran/io/unix.c
@@ -218,6 +218,17 @@ fix_fd (int fd)
   return fd;
 }
 
+int
+is_preconnected (stream * s)
+{
+  int fd;
+
+  fd = ((unix_stream *) s)->fd;
+  if (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO)
+    return 1;
+  else
+    return 0;
+}
 
 /* write()-- Write a buffer to a descriptor, allowing for short writes */
 
-- 
GitLab