about summary refs log tree commit diff
path: root/libio
diff options
context:
space:
mode:
Diffstat (limited to 'libio')
-rw-r--r--libio/Makefile4
-rw-r--r--libio/bug-ftell.c56
-rw-r--r--libio/bug-ungetc2.c98
-rw-r--r--libio/fileops.c16
-rw-r--r--libio/wfileops.c1
5 files changed, 165 insertions, 10 deletions
diff --git a/libio/Makefile b/libio/Makefile
index 38245a8c3d..c7253ff571 100644
--- a/libio/Makefile
+++ b/libio/Makefile
@@ -53,7 +53,8 @@ tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc   \
 	tst-mmap-setvbuf bug-ungetwc1 bug-ungetwc2 tst-atime tst-eof          \
 	tst-freopen bug-rewind bug-rewind2 bug-ungetc bug-fseek \
 	tst-mmap-eofsync tst-mmap-fflushsync bug-mmap-fflush \
-	tst-mmap2-eofsync tst-mmap-offend bug-fopena+ bug-wfflush
+	tst-mmap2-eofsync tst-mmap-offend bug-fopena+ bug-wfflush \
+	bug-ungetc2 bug-ftell
 test-srcs = test-freopen
 
 all: # Make this the default target; it will be defined in Rules.
@@ -147,6 +148,7 @@ tst-ungetwc1-ENV = LOCPATH=$(common-objpfx)localedata
 tst-ungetwc2-ENV = LOCPATH=$(common-objpfx)localedata
 bug-ungetwc2-ENV = LOCPATH=$(common-objpfx)localedata
 tst-swscanf-ENV = LOCPATH=$(common-objpfx)localedata
+bug-ftell-ENV = LOCPATH=$(common-objpfx)localedata
 
 generated = tst-fopenloc.mtrace tst-fopenloc.check
 
diff --git a/libio/bug-ftell.c b/libio/bug-ftell.c
new file mode 100644
index 0000000000..f7a9a776a2
--- /dev/null
+++ b/libio/bug-ftell.c
@@ -0,0 +1,56 @@
+#include <locale.h>
+#include <stdio.h>
+#include <wchar.h>
+
+
+static int
+do_test (void)
+{
+  if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
+    {
+      puts ("setlocale failed");
+      return 1;
+    }
+
+  FILE *fp = tmpfile ();
+  if (fp == NULL)
+    {
+      puts ("tmpfile failed");
+      return 1;
+    }
+
+  if (fputws (L"hello", fp) == EOF)
+    {
+      puts ("fputws failed");
+      return 1;
+    }
+
+  rewind (fp);
+
+  wchar_t *cp;
+  unsigned int cnt;
+  for (cp = L"hello", cnt = 1; *cp != L'\0'; ++cp, ++cnt)
+    {
+      wint_t wc = fgetwc (fp);
+      if (wc != (wint_t) *cp)
+	{
+	  printf ("fgetwc failed: got L'%lc', expected L'%lc'\n", wc, *cp);
+	  return 1;
+	}
+      off_t o = ftello (fp);
+      if (o != cnt)
+	{
+	  printf ("ftello failed: got %lu, expected %u\n",
+		  (unsigned long int) o, cnt);
+	  return 1;
+	}
+      printf ("round %u OK\n", cnt);
+    }
+
+  fclose (fp);
+
+  return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
diff --git a/libio/bug-ungetc2.c b/libio/bug-ungetc2.c
new file mode 100644
index 0000000000..652f7e956c
--- /dev/null
+++ b/libio/bug-ungetc2.c
@@ -0,0 +1,98 @@
+#include <stdio.h>
+#include <sys/types.h>
+
+
+static int
+check (FILE *fp, off_t o)
+{
+  int result = 0;
+  if (feof (fp))
+    {
+      puts ("feof !");
+      result = 1;
+    }
+  if (ferror (fp))
+    {
+      puts ("ferror !");
+      result = 1;
+    }
+  if (ftello (fp) != o)
+    {
+      printf ("position = %lu, not %lu\n", (unsigned long int) ftello (fp),
+	      (unsigned long int) o);
+      result = 1;
+    }
+  return result;
+}
+
+
+static int
+do_test (void)
+{
+  FILE *fp = tmpfile ();
+  if (fp == NULL)
+    {
+      puts ("tmpfile failed");
+      return 1;
+    }
+  if (check (fp, 0) != 0)
+    return 1;
+
+  puts ("going to write");
+  if (fputs ("hello", fp) == EOF)
+    {
+      puts ("fputs failed");
+      return 1;
+    }
+  if (check (fp, 5) != 0)
+    return 1;
+
+  puts ("going to rewind");
+  rewind (fp);
+  if (check (fp, 0) != 0)
+    return 1;
+
+  puts ("going to read char");
+  int c = fgetc (fp);
+  if (c != 'h')
+    {
+      printf ("read %c, not %c\n", c, 'h');
+      return 1;
+    }
+  if (check (fp, 1) != 0)
+    return 1;
+
+  puts ("going to put back");
+  if (ungetc (' ', fp) == EOF)
+    {
+      puts ("ungetc failed");
+      return 1;
+    }
+  if (check (fp, 0) != 0)
+    return 1;
+
+  puts ("going to write again");
+  if (fputs ("world", fp) == EOF)
+    {
+      puts ("2nd fputs failed");
+      return 1;
+    }
+  if (check (fp, 5) != 0)
+    return 1;
+
+  puts ("going to rewind again");
+  rewind (fp);
+  if (check (fp, 0) != 0)
+    return 1;
+
+  if (fclose (fp) != 0)
+    {
+      puts ("fclose failed");
+      return 1;
+    }
+
+  return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
diff --git a/libio/fileops.c b/libio/fileops.c
index 1a633190c7..050fa66a46 100644
--- a/libio/fileops.c
+++ b/libio/fileops.c
@@ -849,15 +849,13 @@ _IO_new_file_overflow (f, ch)
 				   f->_IO_read_base - f->_IO_buf_base);
 	  f->_IO_read_ptr = f->_IO_read_base;
 	}
-      else
-	{
-	  if (f->_IO_read_ptr == f->_IO_buf_end)
-	    f->_IO_read_end = f->_IO_read_ptr = f->_IO_buf_base;
-	  f->_IO_write_ptr = f->_IO_read_ptr;
-	  f->_IO_write_base = f->_IO_write_ptr;
-	  f->_IO_write_end = f->_IO_buf_end;
-	  f->_IO_read_base = f->_IO_read_ptr = f->_IO_read_end;
-	}
+
+      if (f->_IO_read_ptr == f->_IO_buf_end)
+	f->_IO_read_end = f->_IO_read_ptr = f->_IO_buf_base;
+      f->_IO_write_ptr = f->_IO_read_ptr;
+      f->_IO_write_base = f->_IO_write_ptr;
+      f->_IO_write_end = f->_IO_buf_end;
+      f->_IO_read_base = f->_IO_read_ptr = f->_IO_read_end;
 
       f->_flags |= _IO_CURRENTLY_PUTTING;
       if (f->_mode <= 0 && f->_flags & (_IO_LINE_BUF+_IO_UNBUFFERED))
diff --git a/libio/wfileops.c b/libio/wfileops.c
index 5292f4854f..aa4daa905e 100644
--- a/libio/wfileops.c
+++ b/libio/wfileops.c
@@ -153,6 +153,7 @@ _IO_wfile_underflow (fp)
 				       fp->_wide_data->_IO_buf_end,
 				       &fp->_wide_data->_IO_read_end);
 
+      fp->_IO_read_base = fp->_IO_read_ptr;
       fp->_IO_read_ptr = (char *) read_stop;
 
       /* If we managed to generate some text return the next character.  */