re PR libfortran/20179 (cannot mix C and Fortran I/O)
authorFrancois-Xavier Coudert <coudert@clipper.ens.fr>
Sun, 30 Oct 2005 12:48:52 +0000 (13:48 +0100)
committerFrançois-Xavier Coudert <fxcoudert@gcc.gnu.org>
Sun, 30 Oct 2005 12:48:52 +0000 (12:48 +0000)
PR libfortran/20179
* io/unix.c (flush_if_preconnected): New function.
* io/io.h: Add prototype for flush_if_preconnected.
* io/transfer.c (data_transfer_init): Use flush_if_preconnected
to workaround buggy mixed C-Fortran code.

* gfortran.dg/mixed_io_1.f90: New test.
* gfortran.dg/mixed_io_1.c: New file.

From-SVN: r106017

gcc/testsuite/ChangeLog
gcc/testsuite/gfortran.dg/mixed_io_1.c [new file with mode: 0644]
gcc/testsuite/gfortran.dg/mixed_io_1.f90 [new file with mode: 0644]
libgfortran/ChangeLog
libgfortran/io/io.h
libgfortran/io/transfer.c
libgfortran/io/unix.c

index e76658c78c9918b4affce57b9adfc793e91bc8c7..802b4c680db14ab315cacc8a0faeaf5e2f82f100 100644 (file)
@@ -1,3 +1,13 @@
+2005-10-30  Francois-Xavier Coudert  <coudert@clipper.ens.fr>
+
+       PR libfortran/20179
+       * gfortran.dg/mixed_io_1.f90: New test.
+       * gfortran.dg/mixed_io_1.c: New file.
+
+2005-10-30  Francois-Xavier Coudert  <coudert@clipper.ens.fr>
+
+       * gfortran.dg/malloc_free_1.f90: New test.
+
 2005-10-29  Hans-Peter Nilsson  <hp@axis.com>
 
        * gcc.dg/nested-func-4.c: Require profiling -pg.
diff --git a/gcc/testsuite/gfortran.dg/mixed_io_1.c b/gcc/testsuite/gfortran.dg/mixed_io_1.c
new file mode 100644 (file)
index 0000000..0f8d9cd
--- /dev/null
@@ -0,0 +1,4 @@
+#include <stdio.h>
+void cio_(void){
+  printf("12345");
+}
diff --git a/gcc/testsuite/gfortran.dg/mixed_io_1.f90 b/gcc/testsuite/gfortran.dg/mixed_io_1.f90
new file mode 100644 (file)
index 0000000..0dc985c
--- /dev/null
@@ -0,0 +1,5 @@
+! { dg-do run }
+! { dg-additional-sources mixed_io_1.c }
+      call cio
+      write(*,"(A)") '6789' ! { dg-output "123456789" }
+      end
index 85ea74080cc29396f2557f5c2155f3064238e248..0ea6adbb71244e9210dd634fc88e7fdaf31ebaab 100644 (file)
@@ -1,3 +1,11 @@
+2005-10-30  Francois-Xavier Coudert  <coudert@clipper.ens.fr>
+
+       PR libfortran/20179
+       * io/unix.c (flush_if_preconnected): New function.
+       * io/io.h: Add prototype for flush_if_preconnected.
+       * io/transfer.c (data_transfer_init): Use flush_if_preconnected
+       to workaround buggy mixed C-Fortran code.
+
 2005-10-30  Francois-Xavier Coudert  <coudert@clipper.ens.fr>
 
        * Makefile.am: Add intrinsics/malloc.c file.
index 90ee36cd73fb3da444c4ea7ad2794670a09b7a03..53a21b71f988974262d43590b8bd070f552a862b 100644 (file)
@@ -505,6 +505,9 @@ internal_proto(is_seekable);
 extern int is_preconnected (stream *);
 internal_proto(is_preconnected);
 
+extern void flush_if_preconnected (stream *);
+internal_proto(flush_if_preconnected);
+
 extern void empty_internal_buffer(stream *);
 internal_proto(empty_internal_buffer);
 
index 391885b5e3c6ad180d977bc0c47ee78597b38a65..0e1e099d00acbeab74d13fe49c1efb3c211ad568 100644 (file)
@@ -1379,6 +1379,9 @@ data_transfer_init (int read_flag)
       && current_unit->last_record == 0 && !is_preconnected(current_unit->s))
        struncate(current_unit->s);
 
+  /* Bugware for badly written mixed C-Fortran I/O.  */
+  flush_if_preconnected(current_unit->s);
+
   current_unit->mode = g.mode;
 
   /* Set the initial value of flags.  */
index 2026a3649278d75ca46f53f55ceaa69019e55ada..0fe8c4bad84cd9b723a6bde278709a5a3ef1a80c 100644 (file)
@@ -228,6 +228,23 @@ is_preconnected (stream * s)
     return 0;
 }
 
+/* If the stream corresponds to a preconnected unit, we flush the
+   corresponding C stream.  This is bugware for mixed C-Fortran codes
+   where the C code doesn't flush I/O before returning.  */
+void
+flush_if_preconnected (stream * s)
+{
+  int fd;
+
+  fd = ((unix_stream *) s)->fd;
+  if (fd == STDIN_FILENO)
+    fflush (stdin);
+  else if (fd == STDOUT_FILENO)
+    fflush (stdout);
+  else if (fd == STDERR_FILENO)
+    fflush (stderr);
+}
+
 
 /* Reset a stream after reading/writing. Assumes that the buffers have
    been flushed.  */