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);
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to