about summary refs log tree commit diff
path: root/stdio-common
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2000-10-31 05:20:53 +0000
committerUlrich Drepper <drepper@redhat.com>2000-10-31 05:20:53 +0000
commitde153e7f50baa4ea7fac013f3b77b3a4fe314664 (patch)
tree50fb80d978c873cb63952226707a8683ba31e929 /stdio-common
parent02fb3d179ddd3c88f4d4f31d4b27948b48bced2b (diff)
downloadglibc-de153e7f50baa4ea7fac013f3b77b3a4fe314664.tar.gz
glibc-de153e7f50baa4ea7fac013f3b77b3a4fe314664.tar.xz
glibc-de153e7f50baa4ea7fac013f3b77b3a4fe314664.zip
Update.
	* stdio-common/Makefile (tests): Add tst-fmemopen.
	* stdio-common/tst-fmemopen.c: New file.
	Test case by Ben Collins <bcollins@debian.org>.

	* libio/iofopncook.c (_IO_cookie_seek): Correct test for error.

	* libio/fmemopen.c (fmemopen_read): Return 0 at end of buffer.
	(fmemopen_write): Set errno at end of buffer.
Diffstat (limited to 'stdio-common')
-rw-r--r--stdio-common/tst-fmemopen.c111
1 files changed, 111 insertions, 0 deletions
diff --git a/stdio-common/tst-fmemopen.c b/stdio-common/tst-fmemopen.c
new file mode 100644
index 0000000000..f22ebd52fe
--- /dev/null
+++ b/stdio-common/tst-fmemopen.c
@@ -0,0 +1,111 @@
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#define TEST_FILE "test-1"
+
+int
+main (void)
+{
+  const char blah[] = "BLAH";
+  FILE *fp;
+  char *mmap_data;
+  int ch, fd;
+  struct stat fs;
+  const char *cp;
+
+  /* setup the physical file, and use it */
+  if ((fp = fopen (TEST_FILE, "w+")) == NULL)
+    exit (1);
+  if (fwrite (blah, 1, strlen (blah), fp) != strlen (blah))
+    exit (2);
+
+  rewind (fp);
+  printf ("file: ");
+  cp = blah;
+  while ((ch = getc (fp)) != EOF)
+    {
+      fputc (ch, stdout);
+      if (ch != *cp)
+	{
+	  printf ("\ncharacter %d: '%c' instead of '%c'\n",
+		  cp - blah, ch, *cp);
+	  exit (1);
+	}
+      ++cp;
+    }
+  fputc ('\n', stdout);
+  if (ferror (fp))
+    {
+      puts ("fp: error");
+      exit (1);
+    }
+  if (feof (fp))
+    printf ("fp: EOF\n");
+  else
+    {
+      puts ("not EOF");
+      exit (1);
+    }
+  fclose (fp);
+
+  /* Now, mmap the file into a buffer, and do that too */
+  if ((fd = open (TEST_FILE, O_RDONLY)) == -1)
+    exit (3);
+  if (fstat (fd, &fs) == -1)
+    exit (4);
+
+  if ((mmap_data = (char *) mmap (NULL, fs.st_size, PROT_READ,
+				  MAP_SHARED, fd, 0)) == NULL)
+    {
+      if (errno == ENOSYS)
+	exit (0);
+      exit (5);
+    }
+
+  if ((fp = fmemopen (mmap_data, fs.st_size, "r")) == NULL)
+    exit (1);
+
+  printf ("mem: ");
+  cp = blah;
+  while ((ch = getc (fp)) != EOF)
+    {
+      fputc (ch, stdout);
+      if (ch != *cp)
+	{
+	  printf ("%d character: '%c' instead of '%c'\n",
+		  cp - blah, ch, *cp);
+	  exit (1);
+	}
+      ++cp;
+    }
+
+  fputc ('\n', stdout);
+
+  if (ferror (fp))
+    {
+      puts ("fp: error");
+      exit (1);
+    }
+  if (feof (fp))
+    printf ("fp: EOF\n");
+  else
+    {
+      puts ("not EOF");
+      exit (1);
+    }
+
+  fclose (fp);
+
+  munmap (mmap_data, fs.st_size);
+
+  unlink (TEST_FILE);
+
+  return 0;
+}