about summary refs log tree commit diff
path: root/libio
diff options
context:
space:
mode:
Diffstat (limited to 'libio')
-rw-r--r--libio/Makefile3
-rw-r--r--libio/freopen.c27
-rw-r--r--libio/freopen64.c10
-rw-r--r--libio/stdio.h9
-rw-r--r--libio/tst-freopen.c103
5 files changed, 138 insertions, 14 deletions
diff --git a/libio/Makefile b/libio/Makefile
index 59948668b2..f4c5095e5f 100644
--- a/libio/Makefile
+++ b/libio/Makefile
@@ -49,7 +49,8 @@ routines	:=							      \
 tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc   \
 	tst_wprintf2 tst-widetext test-fmemopen tst-ext tst-fopenloc	      \
 	tst-fgetws tst-ungetwc1 tst-ungetwc2 tst-swscanf tst-sscanf	      \
-	tst-mmap-setvbuf bug-ungetwc1 bug-ungetwc2 tst-atime tst-eof
+	tst-mmap-setvbuf bug-ungetwc1 bug-ungetwc2 tst-atime tst-eof          \
+	tst-freopen
 test-srcs = test-freopen
 
 all: # Make this the default target; it will be defined in Rules.
diff --git a/libio/freopen.c b/libio/freopen.c
index a38313c334..01816a179a 100644
--- a/libio/freopen.c
+++ b/libio/freopen.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1993,95,96,97,98,2000,2001 Free Software Foundation, Inc.
+/* Copyright (C) 1993,95,96,97,98,2000,2001,2002 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -53,15 +53,26 @@ freopen (filename, mode, fp)
     }
 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
   if (&_IO_stdin_used == NULL)
-    /* If the shared C library is used by the application binary which
-       was linked against the older version of libio, we just use the
-       older one even for internal use to avoid trouble since a pointer
-       to the old libio may be passed into shared C library and wind
-       up here. */
-    result = _IO_old_freopen (filename, mode, fp);
+    {
+      /* If the shared C library is used by the application binary which
+	 was linked against the older version of libio, we just use the
+	 older one even for internal use to avoid trouble since a pointer
+	 to the old libio may be passed into shared C library and wind
+	 up here. */
+      _IO_old_file_close_it (fp);
+      _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &_IO_old_file_jumps;
+      result = _IO_old_file_fopen (fp, filename, mode);
+    }
   else
 #endif
-    result = _IO_freopen (filename, mode, fp);
+    {
+      INTUSE(_IO_file_close_it) (fp);
+      _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &INTUSE(_IO_file_jumps);
+      fp->_wide_data->_wide_vtable = &INTUSE(_IO_wfile_jumps);
+      result = INTUSE(_IO_file_fopen) (fp, filename, mode, 1);
+      if (result != NULL)
+	result = __fopen_maybe_mmap (result);
+    }
   if (result != NULL)
     /* unbound stream orientation */
     result->_mode = 0;
diff --git a/libio/freopen64.c b/libio/freopen64.c
index 941eda0aa9..7ab9bd315a 100644
--- a/libio/freopen64.c
+++ b/libio/freopen64.c
@@ -1,4 +1,5 @@
-/* Copyright (C) 1993,1995,1996,1997,1998,2000,2001 Free Software Foundation, Inc.
+/* Copyright (C) 1993,1995,1996,1997,1998,2000,2001,2002
+   Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -51,7 +52,12 @@ freopen64 (filename, mode, fp)
       if (fd != -1)
 	filename = fd_to_filename (fd);
     }
-  result = _IO_freopen64 (filename, mode, fp);
+  INTUSE(_IO_file_close_it) (fp);
+  _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &INTUSE(_IO_file_jumps);
+  fp->_wide_data->_wide_vtable = &INTUSE(_IO_wfile_jumps);
+  result = INTUSE(_IO_file_fopen) (fp, filename, mode, 0);
+  if (result != NULL)
+    result = __fopen_maybe_mmap (result);
   if (result != NULL)
     /* unbound stream orientation */
     result->_mode = 0;
diff --git a/libio/stdio.h b/libio/stdio.h
index 47f80d88c8..bcdf64d72e 100644
--- a/libio/stdio.h
+++ b/libio/stdio.h
@@ -1,5 +1,6 @@
 /* Define ISO C stdio on top of C++ iostreams.
-   Copyright (C) 1991, 1994-1999, 2000, 2001 Free Software Foundation, Inc.
+   Copyright (C) 1991, 1994-1999, 2000, 2001, 2002
+   Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -595,10 +596,12 @@ struct obstack;			/* See <obstack.h>.  */
 
 /* Write formatted output to an obstack.  */
 extern int obstack_printf (struct obstack *__restrict __obstack,
-			   __const char *__restrict __format, ...) __THROW;
+			   __const char *__restrict __format, ...)
+     __THROW __attribute__ ((__format__ (__printf__, 2, 3)));
 extern int obstack_vprintf (struct obstack *__restrict __obstack,
 			    __const char *__restrict __format,
-			    _G_va_list __args) __THROW;
+			    _G_va_list __args)
+     __THROW __attribute__ ((__format__ (__printf__, 2, 0)));
 #endif /* Use GNU.  */
 
 
diff --git a/libio/tst-freopen.c b/libio/tst-freopen.c
new file mode 100644
index 0000000000..20e5f2f874
--- /dev/null
+++ b/libio/tst-freopen.c
@@ -0,0 +1,103 @@
+/* Test freopen with mmap stdio.
+   Copyright (C) 2002 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Jakub Jelinek <jakub@redhat.com>, 2002.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+int main (void)
+{
+  char name[] = "/tmp/tst-freopen.XXXXXX";
+  char buf[4096];
+  const char * const test = "Let's test freopen.\n";
+  char temp[strlen (test) + 1];
+  int fd = mkstemp (name);
+  FILE *f;
+
+  if (fd == -1)
+    {
+      printf ("%Zd: cannot open temporary file: %m\n", __LINE__);
+      exit (1);
+    }
+
+  f = fdopen (fd, "w");
+  if (f == NULL)
+    {
+      printf ("%Zd: cannot fdopen temporary file: %m\n", __LINE__);
+      exit (1);
+    }
+
+  fputs (test, f);
+  fclose (f);
+
+  f = fopen (name, "r");
+  if (f == NULL)
+    {
+      printf ("%Zd: cannot fopen temporary file: %m\n", __LINE__);
+      exit (1);
+    }
+
+  if (fread (temp, 1, strlen (test), f) != strlen (test))
+    {
+      printf ("%Zd: couldn't read the file back: %m\n", __LINE__);
+      exit (1);
+    }
+  temp [strlen (test)] = '\0';
+
+  if (strcmp (test, temp))
+    {
+      printf ("%Zd: read different string than was written:\n%s%s",
+	      __LINE__, test, temp);
+      exit (1);
+    }
+
+  f = freopen (name, "r+", f);
+  if (f == NULL)
+    {
+      printf ("%Zd: cannot freopen temporary file: %m\n", __LINE__);
+      exit (1);
+    }
+
+  if (fseek (f, 0, SEEK_SET) != 0)
+    {
+      printf ("%Zd: couldn't fseek to start: %m\n", __LINE__);
+      exit (1);
+    }
+
+  if (fread (temp, 1, strlen (test), f) != strlen (test))
+    {
+      printf ("%Zd: couldn't read the file back: %m\n", __LINE__);
+      exit (1);
+    }
+  temp [strlen (test)] = '\0';
+
+  if (strcmp (test, temp))
+    {
+      printf ("%Zd: read different string than was written:\n%s%s",
+	      __LINE__, test, temp);
+      exit (1);
+    }
+
+  fclose (f);
+
+  unlink (name);
+  exit (0);
+}