diff mbox

[libfortran] Don't update position flag for non-seekable files

Message ID AANLkTi=b1mR6-fw6hAJp7Xx4D6vj99ZxCv1AXJXD6Jpb@mail.gmail.com
State New
Headers show

Commit Message

Janne Blomqvist Aug. 2, 2010, 6:23 a.m. UTC
Hi,

I've committed the attached patch as obvious to trunk (r162810). The
practical offshoot is that for writing to stdout (or other
non-seekable non-buffered units) we avoid two failed lseek() syscalls
after every record which previously resulted in the position flag
being set to POSITION_APPEND, now it's left at whatever it was before.

2010-08-02  Janne Blomqvist  <jb@gcc.gnu.org>

	* io/unit.c (update_position): Don't update the position flag for
	non-seekable files, check for stell() error.
diff mbox

Patch

diff --git a/libgfortran/io/unit.c b/libgfortran/io/unit.c
index a0018db..1d52217 100644
--- a/libgfortran/io/unit.c
+++ b/libgfortran/io/unit.c
@@ -714,12 +714,19 @@  close_units (void)
 void
 update_position (gfc_unit *u)
 {
-  if (stell (u->s) == 0)
-    u->flags.position = POSITION_REWIND;
-  else if (file_length (u->s) == stell (u->s))
-    u->flags.position = POSITION_APPEND;
-  else
-    u->flags.position = POSITION_ASIS;
+  /* If unit is not seekable, this makes no sense (and the standard is
+     silent on this matter), and thus we don't change the position for
+     a non-seekable file.  */
+  if (is_seekable (u->s))
+    {
+      gfc_offset cur = stell (u->s);
+      if (cur == 0)
+       u->flags.position = POSITION_REWIND;
+      else if (cur != -1 && (file_length (u->s) == cur))
+       u->flags.position = POSITION_APPEND;
+      else
+       u->flags.position = POSITION_ASIS;
+    }
 }