This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: yet another freopen(NULL) patch


Patch checked in.

-- Jeff J.

Eric Blake wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

It turns out that on cygwin, when stdin is connected to a tty,
STDIN_FILENO is open O_RDWR even though stdin is read-only.  So, from the
point of view of freopen(NULL, "rb", stdin), a read-only FILE atop an
O_RDWR file descriptor is a better option than the current behavior of
failing and closing stdin altogether because the desired O_RDONLY was not
the same as the existing O_RDWR.

2006-03-06 Eric Blake <ebb9@byu.net>

	* libc/stdio/freopen.c (_freopen_r) [HAVE_FCNTL]: For NULL
	filename, allow read-only or write-only FILE atop O_RDWR file
	descriptor.
	* libc/stdio64/freopen64.c (_freopen64_r) [HAVE_FCNTL]: Likewise.

- --
Life is short - so eat dessert first!

Eric Blake             ebb9@byu.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.1 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEDDvZ84KuGfSFAYARAvVmAJwKGnYVwS1HPWkX+fEtB7tfz/kYuQCeMtkw
qmbsMFYDBiGfDTHrnnAuDnM=
=n5+c
-----END PGP SIGNATURE-----


------------------------------------------------------------------------


Index: newlib/libc/stdio/freopen.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/freopen.c,v
retrieving revision 1.15
diff -u -r1.15 freopen.c
--- newlib/libc/stdio/freopen.c 10 Jan 2006 17:09:43 -0000 1.15
+++ newlib/libc/stdio/freopen.c 6 Mar 2006 05:01:42 -0000
@@ -56,10 +56,10 @@
<[file]> and <[mode]> are used just as in <<fopen>>.
If <[file]> is <<NULL>>, the underlying stream is modified rather than
-closed. The file cannot change access mode (for example, if it was
-previously read-only, <[mode]> must be "r", "rb", or "rt"), but can
-change status such as append or binary mode. If modification is not
-possible, failure occurs.
+closed. The file cannot be given a more permissive access mode (for
+example, a <[mode]> of "w" will fail on a read-only file descriptor),
+but can change status such as append or binary mode. If modification
+is not possible, failure occurs.
RETURNS
If successful, the result is the same as the argument <[fp]>. If the
@@ -148,12 +148,14 @@
#ifdef HAVE_FCNTL
int oldflags;
/*
- * Reuse the file descriptor, but only if the access mode is
- * unchanged. F_SETFL correctly ignores creation flags.
+ * Reuse the file descriptor, but only if the new access mode is
+ * equal or less permissive than the old. F_SETFL correctly
+ * ignores creation flags.
*/
f = fp->_file;
if ((oldflags = _fcntl_r (ptr, f, F_GETFL, 0)) == -1
- || ((oldflags ^ oflags) & O_ACCMODE) != 0
+ || ! ((oldflags & O_ACCMODE) == O_RDWR
+ || ((oldflags ^ oflags) & O_ACCMODE) == 0)
|| _fcntl_r (ptr, f, F_SETFL, oflags) == -1)
f = -1;
#else
Index: newlib/libc/stdio64/freopen64.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio64/freopen64.c,v
retrieving revision 1.10
diff -u -r1.10 freopen64.c
--- newlib/libc/stdio64/freopen64.c 10 Jan 2006 17:09:43 -0000 1.10
+++ newlib/libc/stdio64/freopen64.c 6 Mar 2006 05:01:42 -0000
@@ -56,10 +56,10 @@
<[file]> and <[mode]> are used just as in <<fopen>>.
If <[file]> is <<NULL>>, the underlying stream is modified rather than
-closed. The file cannot change access mode (for example, if it was
-previously read-only, <[mode]> must be "r", "rb", or "rt"), but can
-change status such as append or binary mode. If modification is not
-possible, failure occurs.
+closed. The file cannot be given a more permissive access mode (for
+example, a <[mode]> of "w" will fail on a read-only file descriptor),
+but can change status such as append or binary mode. If modification
+is not possible, failure occurs.
RETURNS
If successful, the result is the same as the argument <[fp]>. If the
@@ -148,12 +148,14 @@
#ifdef HAVE_FCNTL
int oldflags;
/*
- * Reuse the file descriptor, but only if the access mode is
- * unchanged. F_SETFL correctly ignores creation flags.
+ * Reuse the file descriptor, but only if the new access mode is
+ * equal or less permissive than the old. F_SETFL correctly
+ * ignores creation flags.
*/
f = fp->_file;
if ((oldflags = _fcntl_r (ptr, f, F_GETFL, 0)) == -1
- || ((oldflags ^ oflags) & O_ACCMODE) != 0
+ || ! ((oldflags & O_ACCMODE) == O_RDWR
+ || ((oldflags ^ oflags) & O_ACCMODE) == 0)
|| _fcntl_r (ptr, f, F_SETFL, oflags) == -1)
f = -1;
#else


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]