From ce10f1789e0a4b1f968203b23a25006f9e7d9e6c Mon Sep 17 00:00:00 2001 From: Jeff Johnston Date: Tue, 7 Mar 2006 22:20:18 +0000 Subject: 2006-03-07 Eric Blake * 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. --- newlib/ChangeLog | 7 +++++++ newlib/libc/stdio/freopen.c | 16 +++++++++------- newlib/libc/stdio64/freopen64.c | 16 +++++++++------- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/newlib/ChangeLog b/newlib/ChangeLog index 5b7f21b5d..552742b0a 100644 --- a/newlib/ChangeLog +++ b/newlib/ChangeLog @@ -1,3 +1,10 @@ +2006-03-07 Eric Blake + + * 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. + 2006-03-07 Jeff Johnston * libm/common/sf_isinf.c[_DOUBLE_IS_32BITS]: Undef isinf. diff --git a/newlib/libc/stdio/freopen.c b/newlib/libc/stdio/freopen.c index d5582d421..ae3444b1b 100644 --- a/newlib/libc/stdio/freopen.c +++ b/newlib/libc/stdio/freopen.c @@ -56,10 +56,10 @@ it). <[file]> and <[mode]> are used just as in <>. If <[file]> is <>, 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 @@ _DEFUN(_freopen_r, (ptr, file, mode, fp), #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 diff --git a/newlib/libc/stdio64/freopen64.c b/newlib/libc/stdio64/freopen64.c index 8acb957da..6081e03dc 100644 --- a/newlib/libc/stdio64/freopen64.c +++ b/newlib/libc/stdio64/freopen64.c @@ -56,10 +56,10 @@ it). <[file]> and <[mode]> are used just as in <>. If <[file]> is <>, 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 @@ _DEFUN (_freopen64_r, (ptr, file, mode, fp), #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 -- cgit v1.2.3