From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 82939 invoked by alias); 18 Dec 2017 19:18:27 -0000 Mailing-List: contact newlib-cvs-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: newlib-cvs-owner@sourceware.org Received: (qmail 82913 invoked by uid 9078); 18 Dec 2017 19:18:26 -0000 Date: Mon, 18 Dec 2017 19:18:00 -0000 Message-ID: <20171218191826.82911.qmail@sourceware.org> Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Corinna Vinschen To: newlib-cvs@sourceware.org Subject: [newlib-cygwin] newlib: ftello{64}: Handle appending stream without fflushing X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/master X-Git-Oldrev: dc2d175721df8ceb801d50581df95b49c26a6ff7 X-Git-Newrev: 6e5b39940ae95d098fb89cf80d48f6e03d3833bf X-SW-Source: 2017-q4/txt/msg00085.txt.bz2 https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=6e5b39940ae95d098fb89cf80d48f6e03d3833bf commit 6e5b39940ae95d098fb89cf80d48f6e03d3833bf Author: Corinna Vinschen Date: Mon Dec 18 20:17:51 2017 +0100 newlib: ftello{64}: Handle appending stream without fflushing Neither upstream FreeBSD nor glibc ever call fflush from ftell and friends. In border cases it has the tendency to return wrong or unexpected values, for instance on block devices. Signed-off-by: Corinna Vinschen Diff: --- newlib/libc/stdio/ftello.c | 20 +++++++++++++------- newlib/libc/stdio64/ftello64.c | 20 +++++++++++++------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/newlib/libc/stdio/ftello.c b/newlib/libc/stdio/ftello.c index 0a9bb7b..90c4d32 100644 --- a/newlib/libc/stdio/ftello.c +++ b/newlib/libc/stdio/ftello.c @@ -102,13 +102,19 @@ _DEFUN(_ftello_r, (ptr, fp), return (_off_t) -1; } - /* Find offset of underlying I/O object, then adjust for buffered - bytes. Flush a write stream, since the offset may be altered if - the stream is appending. Do not flush a read stream, since we - must not lose the ungetc buffer. */ - if (fp->_flags & __SWR) - _fflush_r (ptr, fp); - if (fp->_flags & __SOFF) + /* Find offset of underlying I/O object, then adjust for buffered bytes. */ + if (!(fp->_flags & __SRD) && (fp->_flags & __SWR) && + fp->_p != NULL && fp->_p - fp->_bf._base > 0 && + (fp->_flags & __SAPP)) + { + pos = fp->_seek (ptr, fp->_cookie, (_fpos_t) 0, SEEK_END); + if (pos == (_fpos_t) -1) + { + _newlib_flockfile_exit (fp); + return (_off_t) -1; + } + } + else if (fp->_flags & __SOFF) pos = fp->_offset; else { diff --git a/newlib/libc/stdio64/ftello64.c b/newlib/libc/stdio64/ftello64.c index 9aca231..2441d4c 100644 --- a/newlib/libc/stdio64/ftello64.c +++ b/newlib/libc/stdio64/ftello64.c @@ -99,13 +99,19 @@ _DEFUN (_ftello64_r, (ptr, fp), return (_off64_t) -1; } - /* Find offset of underlying I/O object, then adjust for buffered - bytes. Flush a write stream, since the offset may be altered if - the stream is appending. Do not flush a read stream, since we - must not lose the ungetc buffer. */ - if (fp->_flags & __SWR) - _fflush_r (ptr, fp); - if (fp->_flags & __SOFF) + /* Find offset of underlying I/O object, then adjust for buffered bytes. */ + if (!(fp->_flags & __SRD) && (fp->_flags & __SWR) && + fp->_p != NULL && fp->_p - fp->_bf._base > 0 && + (fp->_flags & __SAPP)) + { + pos = fp->_seek64 (ptr, fp->_cookie, (_fpos64_t) 0, SEEK_END); + if (pos == (_fpos64_t) -1) + { + _newlib_flockfile_exit (fp); + return (_off64_t) -1; + } + } + else if (fp->_flags & __SOFF) pos = fp->_offset; else {