Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 8 Nov 2015 18:00:44 +0000 (UTC)
From:      "Andrey A. Chernov" <ache@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r290549 - head/lib/libc/stdio
Message-ID:  <201511081800.tA8I0iUe047888@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ache
Date: Sun Nov  8 18:00:44 2015
New Revision: 290549
URL: https://svnweb.freebsd.org/changeset/base/290549

Log:
  Reorganize code to elimitate one _sseek() call for append modes.
  
  MFC after:      1 week

Modified:
  head/lib/libc/stdio/ftell.c

Modified: head/lib/libc/stdio/ftell.c
==============================================================================
--- head/lib/libc/stdio/ftell.c	Sun Nov  8 17:33:48 2015	(r290548)
+++ head/lib/libc/stdio/ftell.c	Sun Nov  8 18:00:44 2015	(r290549)
@@ -98,7 +98,20 @@ _ftello(FILE *fp, fpos_t *offset)
 	 * Find offset of underlying I/O object, then
 	 * adjust for buffered bytes.
 	 */
-	if (fp->_flags & __SOFF)
+	if (!(fp->_flags & __SRD) && (fp->_flags & __SWR) &&
+	    fp->_p != NULL && fp->_p - fp->_bf._base > 0 &&
+	    ((fp->_flags & __SAPP) || (fp->_flags2 & __S2OAP))) {
+		if ((pos = _sseek(fp, (fpos_t)0, SEEK_END)) == -1) {
+			if (errno == ESPIPE ||
+			    (fp->_flags & __SOPT) || __sflush(fp) ||
+			    (pos = _sseek(fp, (fpos_t)0, SEEK_CUR)) == -1)
+				return (1);
+			else {
+				*offset = pos;
+				return (0);
+			}
+		}
+	} else if (fp->_flags & __SOFF)
 		pos = fp->_offset;
 	else {
 		pos = _sseek(fp, (fpos_t)0, SEEK_CUR);
@@ -125,26 +138,6 @@ _ftello(FILE *fp, fpos_t *offset)
 		 * position to be greater than that in the
 		 * underlying object.
 		 */
-		if ((fp->_flags & __SAPP) || (fp->_flags2 & __S2OAP)) {
-			int serrno = errno;
-
-			errno = 0;
-			if ((pos = _sseek(fp, (fpos_t)0, SEEK_END)) == -1) {
-				if (errno == ESPIPE ||
-				    (fp->_flags & __SOPT) || __sflush(fp) ||
-				    (pos =
-				    _sseek(fp, (fpos_t)0, SEEK_CUR)) == -1)
-					return (1);
-				else {
-					errno = serrno;
-					*offset = pos;
-					return (0);
-				}
-			}
-			errno = serrno;
-			/* fp->_p can be changed in _sseek(), recalculate. */
-			n = fp->_p - fp->_bf._base;
-		}
 		if (pos > OFF_MAX - n) {
 			errno = EOVERFLOW;
 			return (1);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201511081800.tA8I0iUe047888>