public inbox for newlib-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: newlib-cvs@sourceware.org
Subject: [newlib-cygwin] Revert "newlib: fix fseek optimization with SEEK_CUR"
Date: Wed, 29 Jan 2020 17:59:00 -0000	[thread overview]
Message-ID: <20200129175943.91989.qmail@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=2607f00423ead393f24b81b9fe5a39a37e036af8

commit 2607f00423ead393f24b81b9fe5a39a37e036af8
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Wed Jan 29 18:47:33 2020 +0100

    Revert "newlib: fix fseek optimization with SEEK_CUR"
    
    This reverts commit 59362c80e3a02c011fd0ef3d7f07a20098d2a9d5.
    
    This breaks gnulib's autoconf test for POSIX compatibility of
    fflush/fseek.  After fflush/fseek, ftello and lseek are out of
    sync, with lseek having the wrong offset.  This breaks backward
    compatibility with Cygwin applications.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 newlib/libc/stdio/fseeko.c     | 31 +++++++++++++++++++++++++------
 newlib/libc/stdio64/fseeko64.c | 31 +++++++++++++++++++++++++------
 2 files changed, 50 insertions(+), 12 deletions(-)

diff --git a/newlib/libc/stdio/fseeko.c b/newlib/libc/stdio/fseeko.c
index bbf1af4..3e0f9e9 100644
--- a/newlib/libc/stdio/fseeko.c
+++ b/newlib/libc/stdio/fseeko.c
@@ -141,12 +141,31 @@ _fseeko_r (struct _reent *ptr,
   switch (whence)
     {
     case SEEK_CUR:
-      curoff = _ftello_r(ptr, fp);
-      if (curoff == -1L)
-        {
-          _newlib_flockfile_exit (fp);
-          return EOF;
-        }
+      /*
+       * In order to seek relative to the current stream offset,
+       * we have to first find the current stream offset a la
+       * ftell (see ftell for details).
+       */
+      _fflush_r (ptr, fp);   /* may adjust seek offset on append stream */
+      if (fp->_flags & __SOFF)
+	curoff = fp->_offset;
+      else
+	{
+	  curoff = seekfn (ptr, fp->_cookie, (_fpos_t) 0, SEEK_CUR);
+	  if (curoff == -1L)
+	    {
+	      _newlib_flockfile_exit (fp);
+	      return EOF;
+	    }
+	}
+      if (fp->_flags & __SRD)
+	{
+	  curoff -= fp->_r;
+	  if (HASUB (fp))
+	    curoff -= fp->_ur;
+	}
+      else if (fp->_flags & __SWR && fp->_p != NULL)
+	curoff += fp->_p - fp->_bf._base;
 
       offset += curoff;
       whence = SEEK_SET;
diff --git a/newlib/libc/stdio64/fseeko64.c b/newlib/libc/stdio64/fseeko64.c
index f380055..0672086 100644
--- a/newlib/libc/stdio64/fseeko64.c
+++ b/newlib/libc/stdio64/fseeko64.c
@@ -142,12 +142,31 @@ _fseeko64_r (struct _reent *ptr,
   switch (whence)
     {
     case SEEK_CUR:
-      curoff = _ftello64_r(ptr, fp);
-      if (curoff == -1L)
-        {
-          _newlib_flockfile_exit (fp);
-          return EOF;
-        }
+      /*
+       * In order to seek relative to the current stream offset,
+       * we have to first find the current stream offset a la
+       * ftell (see ftell for details).
+       */
+      _fflush_r (ptr, fp);   /* may adjust seek offset on append stream */
+      if (fp->_flags & __SOFF)
+	curoff = fp->_offset;
+      else
+	{
+	  curoff = seekfn (ptr, fp->_cookie, (_fpos64_t) 0, SEEK_CUR);
+	  if (curoff == -1L)
+	    {
+	      _newlib_flockfile_exit(fp);
+	      return EOF;
+	    }
+	}
+      if (fp->_flags & __SRD)
+	{
+	  curoff -= fp->_r;
+	  if (HASUB (fp))
+	    curoff -= fp->_ur;
+	}
+      else if (fp->_flags & __SWR && fp->_p != NULL)
+	curoff += fp->_p - fp->_bf._base;
 
       offset += curoff;
       whence = SEEK_SET;


                 reply	other threads:[~2020-01-29 17:59 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200129175943.91989.qmail@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=newlib-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).