summary refs log tree commit diff
path: root/libio
diff options
context:
space:
mode:
Diffstat (limited to 'libio')
-rw-r--r--libio/fileops.c30
-rw-r--r--libio/ftello.c2
-rw-r--r--libio/ftello64.c2
-rw-r--r--libio/genops.c21
-rw-r--r--libio/iofclose.c2
-rw-r--r--libio/iofgetpos.c2
-rw-r--r--libio/iofgetpos64.c2
-rw-r--r--libio/ioftell.c2
-rw-r--r--libio/ioseekoff.c3
-rw-r--r--libio/libio.h4
10 files changed, 58 insertions, 12 deletions
diff --git a/libio/fileops.c b/libio/fileops.c
index a2017f23bc..be65d42fb2 100644
--- a/libio/fileops.c
+++ b/libio/fileops.c
@@ -34,6 +34,9 @@
 #include <sys/stat.h>
 #include <string.h>
 #include <errno.h>
+#ifdef __STDC__
+#include <stdlib.h>
+#endif
 #ifndef errno
 extern int errno;
 #endif
@@ -341,7 +344,15 @@ _IO_new_file_underflow (fp)
     return *(unsigned char *) fp->_IO_read_ptr;
 
   if (fp->_IO_buf_base == NULL)
-    _IO_doallocbuf (fp);
+    {
+      /* Maybe we already have a push back pointer.  */
+      if (fp->_IO_save_base != NULL)
+	{
+	  free (fp->_IO_save_base);
+	  fp->_flags &= ~_IO_IN_BACKUP;
+	}
+      _IO_doallocbuf (fp);
+    }
 
   /* Flush all line buffered files before reading. */
   /* FIXME This can/should be moved to genops ?? */
@@ -493,6 +504,12 @@ _IO_new_file_seekoff (fp, offset, dir, mode)
 
   if (fp->_IO_buf_base == NULL)
     {
+      /* It could be that we already have a pushback buffer.  */
+      if (fp->_IO_read_base != NULL)
+	{
+	  free (fp->_IO_read_base);
+	  fp->_flags &= ~_IO_IN_BACKUP;
+	}
       _IO_doallocbuf (fp);
       _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
       _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
@@ -526,6 +543,10 @@ _IO_new_file_seekoff (fp, offset, dir, mode)
     }
   /* At this point, dir==_IO_seek_set. */
 
+  /* If we are only interested in the current position we've found it now.  */
+  if (mode == 0)
+    return offset;
+
   /* If destination is within current buffer, optimize: */
   if (fp->_offset != _IO_pos_BAD && fp->_IO_read_base != NULL
       && !_IO_in_backup (fp))
@@ -544,7 +565,10 @@ _IO_new_file_seekoff (fp, offset, dir, mode)
 	      _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base + rel_offset,
 			fp->_IO_read_end);
 	      _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
-	      return offset;
+	      {
+		_IO_mask_flags (fp, 0, _IO_EOF_SEEN);
+		return offset;
+	      }
 	    }
 #ifdef TODO
 	    /* If we have streammarkers, seek forward by reading ahead. */
@@ -554,6 +578,7 @@ _IO_new_file_seekoff (fp, offset, dir, mode)
 		  - (fp->_IO_read_ptr - fp->_IO_read_base);
 		if (ignore (to_skip) != to_skip)
 		  goto dumb;
+		_IO_mask_flags (fp, 0, _IO_EOF_SEEN);
 		return offset;
 	      }
 #endif
@@ -564,6 +589,7 @@ _IO_new_file_seekoff (fp, offset, dir, mode)
 	  if (!_IO_in_backup (fp))
 	    _IO_switch_to_backup_area (fp);
 	  gbump (fp->_IO_read_end + rel_offset - fp->_IO_read_ptr);
+	  _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
 	  return offset;
 	}
 #endif
diff --git a/libio/ftello.c b/libio/ftello.c
index 662b954c54..2d8a8a7167 100644
--- a/libio/ftello.c
+++ b/libio/ftello.c
@@ -37,6 +37,8 @@ ftello (fp)
   _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile, fp);
   _IO_flockfile (fp);
   pos = _IO_seekoff (fp, 0, _IO_seek_cur, 0);
+  if (_IO_in_backup (fp))
+    pos -= fp->_IO_save_end - fp->_IO_save_base;
   _IO_funlockfile (fp);
   _IO_cleanup_region_end (0);
   if (pos == _IO_pos_BAD)
diff --git a/libio/ftello64.c b/libio/ftello64.c
index 96eeb184a4..621454974e 100644
--- a/libio/ftello64.c
+++ b/libio/ftello64.c
@@ -38,6 +38,8 @@ ftello64 (fp)
   _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile, fp);
   _IO_flockfile (fp);
   pos = _IO_seekoff (fp, 0, _IO_seek_cur, 0);
+  if (_IO_in_backup (fp))
+    pos -= fp->_IO_save_end - fp->_IO_save_base;
   _IO_funlockfile (fp);
   _IO_cleanup_region_end (0);
   if (pos == _IO_pos_BAD)
diff --git a/libio/genops.c b/libio/genops.c
index 4286eef6c2..2dce95f842 100644
--- a/libio/genops.c
+++ b/libio/genops.c
@@ -94,8 +94,10 @@ _IO_switch_to_main_get_area (fp)
   tmp = fp->_IO_read_base;
   fp->_IO_read_base = fp->_IO_save_base;
   fp->_IO_save_base = tmp;
-
-  fp->_IO_read_ptr = fp->_IO_read_base;
+  /* Swap _IO_read_base and _IO_save_ptr. */
+  tmp = fp->_IO_read_ptr;
+  fp->_IO_read_ptr = fp->_IO_save_ptr;
+  fp->_IO_save_ptr = tmp;
 }
 
 /* Switch current get area from main get area to (end of) backup area. */
@@ -114,7 +116,8 @@ _IO_switch_to_backup_area (fp)
   tmp = fp->_IO_read_base;
   fp->_IO_read_base = fp->_IO_save_base;
   fp->_IO_save_base = tmp;
-
+  /* read _IO_read_ptr.  */
+  fp->_IO_save_ptr = fp->_IO_read_ptr;
   fp->_IO_read_ptr = fp->_IO_read_end;
 }
 
@@ -868,7 +871,10 @@ _IO_default_pbackfail (fp, c)
      _IO_FILE *fp;
      int c;
 {
-  if (fp->_IO_read_ptr <= fp->_IO_read_base)
+  if (fp->_IO_read_ptr > fp->_IO_read_base && !_IO_in_backup (fp)
+      && fp->_IO_read_ptr[-1] == c)
+    --fp->_IO_read_ptr;
+  else
     {
       /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
       if (_IO_have_backup (fp) && !_IO_in_backup (fp))
@@ -904,11 +910,10 @@ _IO_default_pbackfail (fp, c)
 		    new_buf + new_size);
 	  fp->_IO_backup_base = fp->_IO_read_ptr;
 	}
+
+      *--fp->_IO_read_ptr = c;
     }
-  --fp->_IO_read_ptr;
-  if (c != EOF && *fp->_IO_read_ptr != c)
-    *fp->_IO_read_ptr = c;
-  return (unsigned char) *fp->_IO_read_ptr;
+  return (unsigned char) c;
 }
 
 _IO_fpos64_t
diff --git a/libio/iofclose.c b/libio/iofclose.c
index f896e09b7e..61f7800ed8 100644
--- a/libio/iofclose.c
+++ b/libio/iofclose.c
@@ -45,6 +45,8 @@ _IO_new_fclose (fp)
   _IO_FINISH (fp);
   _IO_funlockfile (fp);
   _IO_cleanup_region_end (0);
+  if (_IO_have_backup (fp))
+    _IO_free_backup_area (fp);
   if (fp != _IO_stdin && fp != _IO_stdout && fp != _IO_stderr)
     {
       fp->_IO_file_flags = 0;
diff --git a/libio/iofgetpos.c b/libio/iofgetpos.c
index 5fed6c3685..27f018d07f 100644
--- a/libio/iofgetpos.c
+++ b/libio/iofgetpos.c
@@ -36,6 +36,8 @@ _IO_fgetpos (fp, posp)
   _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile, fp);
   _IO_flockfile (fp);
   pos = _IO_seekoff (fp, 0, _IO_seek_cur, 0);
+  if (_IO_in_backup (fp))
+    pos -= fp->_IO_save_end - fp->_IO_save_base;
   _IO_funlockfile (fp);
   _IO_cleanup_region_end (0);
   if (pos == _IO_pos_BAD)
diff --git a/libio/iofgetpos64.c b/libio/iofgetpos64.c
index 8a7733bfb5..a705e9e91a 100644
--- a/libio/iofgetpos64.c
+++ b/libio/iofgetpos64.c
@@ -37,6 +37,8 @@ _IO_fgetpos64 (fp, posp)
   _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile, fp);
   _IO_flockfile (fp);
   pos = _IO_seekoff (fp, 0, _IO_seek_cur, 0);
+  if (_IO_in_backup (fp))
+    pos -= fp->_IO_save_end - fp->_IO_save_base;
   _IO_funlockfile (fp);
   _IO_cleanup_region_end (0);
   if (pos == _IO_pos_BAD)
diff --git a/libio/ioftell.c b/libio/ioftell.c
index 3a0e7a6bc3..3de1ee9c02 100644
--- a/libio/ioftell.c
+++ b/libio/ioftell.c
@@ -36,6 +36,8 @@ _IO_ftell (fp)
   _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile, fp);
   _IO_flockfile (fp);
   pos = _IO_seekoff (fp, 0, _IO_seek_cur, 0);
+  if (_IO_in_backup (fp))
+    pos -= fp->_IO_save_end - fp->_IO_save_base;
   _IO_funlockfile (fp);
   _IO_cleanup_region_end (0);
   if (pos == _IO_pos_BAD)
diff --git a/libio/ioseekoff.c b/libio/ioseekoff.c
index b83e1ad797..54a8d19f0d 100644
--- a/libio/ioseekoff.c
+++ b/libio/ioseekoff.c
@@ -41,12 +41,13 @@ _IO_seekoff (fp, offset, dir, mode)
   _IO_flockfile (fp);
 
 
-  if (_IO_have_backup (fp))
+  if (mode != 0 && _IO_have_backup (fp))
     {
       if (dir == _IO_seek_cur && _IO_in_backup (fp))
 	offset -= fp->_IO_read_end - fp->_IO_read_ptr;
       _IO_free_backup_area (fp);
     }
+
   retval = _IO_SEEKOFF (fp, offset, dir, mode);
 
   _IO_funlockfile (fp);
diff --git a/libio/libio.h b/libio/libio.h
index a060b42395..7b2f70cdab 100644
--- a/libio/libio.h
+++ b/libio/libio.h
@@ -229,7 +229,9 @@ struct _IO_FILE_complete
 #endif
 #if defined _G_IO_IO_FILE_VERSION && _G_IO_IO_FILE_VERSION == 0x20001
   _IO_off64_t _offset;
-  int _unused2[16];	/* Make sure we don't get into trouble again.  */
+  char *_IO_save_ptr;
+  /* Make sure we don't get into trouble again.  */
+  char _unused2[16 * sizeof (int) - sizeof (char *)];
 #endif
 };