From 8a3e4a6db71e690b2b8bf5f3372f4f99a506fdd7 Mon Sep 17 00:00:00 2001 From: ache Date: Sun, 8 Nov 2015 13:37:16 +0000 Subject: MFC: r289863,r289931,r290110,r290230,r290231,r290232 r290232: Microoptimize. r290231: Addition to prev. commit. In some edge cases fp->_p can be changed in _sseek(), recalculate. r290230: Don't seek to the end if write buffer is empty (in append modes). PR: 204156 r290110: Add _flags2 per jhb@ suggestion since no room left in _flags. Rewrite O_APPEND flag checking using new __S2OAP flag. r289931: According to POSIX, a write operation shall start at the current size of the stream (if mode had 'a' as the first character). r289863: Since no room left in the _flags, reuse __SALC for O_APPEND. It helps to remove _fcntl() call from _ftello() and optimize seek position calculation in _swrite(). --- lib/libc/stdio/fdopen.c | 4 +++- lib/libc/stdio/findfp.c | 1 + lib/libc/stdio/fmemopen.c | 3 +++ lib/libc/stdio/fopen.c | 4 +++- lib/libc/stdio/freopen.c | 5 ++++- lib/libc/stdio/ftell.c | 41 ++++++++++++++++++++++------------------- lib/libc/stdio/stdio.c | 2 +- 7 files changed, 37 insertions(+), 23 deletions(-) (limited to 'lib/libc/stdio') diff --git a/lib/libc/stdio/fdopen.c b/lib/libc/stdio/fdopen.c index 2e19b9f..e5d167a 100644 --- a/lib/libc/stdio/fdopen.c +++ b/lib/libc/stdio/fdopen.c @@ -90,7 +90,9 @@ fdopen(int fd, const char *mode) * O_APPEND bit set, assert __SAPP so that __swrite() caller * will _sseek() to the end before write. */ - if ((oflags & O_APPEND) && !(fdflags & O_APPEND)) + if (fdflags & O_APPEND) + fp->_flags2 |= __S2OAP; + else if (oflags & O_APPEND) fp->_flags |= __SAPP; fp->_file = fd; fp->_cookie = fp; diff --git a/lib/libc/stdio/findfp.c b/lib/libc/stdio/findfp.c index be196b7..12d3659 100644 --- a/lib/libc/stdio/findfp.c +++ b/lib/libc/stdio/findfp.c @@ -155,6 +155,7 @@ found: /* fp->_fl_mutex = NULL; */ /* once set always set (reused) */ fp->_orientation = 0; memset(&fp->_mbstate, 0, sizeof(mbstate_t)); + fp->_flags2 = 0; return (fp); } diff --git a/lib/libc/stdio/fmemopen.c b/lib/libc/stdio/fmemopen.c index ebd3596..bcf187d 100644 --- a/lib/libc/stdio/fmemopen.c +++ b/lib/libc/stdio/fmemopen.c @@ -149,6 +149,9 @@ fmemopen(void * __restrict buf, size_t size, const char * __restrict mode) return (NULL); } + if (mode[0] == 'a') + f->_flags |= __SAPP; + /* * Turn off buffering, so a write past the end of the buffer * correctly returns a short object count. diff --git a/lib/libc/stdio/fopen.c b/lib/libc/stdio/fopen.c index b08e336..9ab61ba 100644 --- a/lib/libc/stdio/fopen.c +++ b/lib/libc/stdio/fopen.c @@ -91,7 +91,9 @@ fopen(const char * __restrict file, const char * __restrict mode) * we can do about this. (We could set __SAPP and check in * fseek and ftell.) */ - if (oflags & O_APPEND) + if (oflags & O_APPEND) { + fp->_flags2 |= __S2OAP; (void)_sseek(fp, (fpos_t)0, SEEK_END); + } return (fp); } diff --git a/lib/libc/stdio/freopen.c b/lib/libc/stdio/freopen.c index bf3f030..58227fa 100644 --- a/lib/libc/stdio/freopen.c +++ b/lib/libc/stdio/freopen.c @@ -186,6 +186,7 @@ finish: fp->_lb._size = 0; fp->_orientation = 0; memset(&fp->_mbstate, 0, sizeof(mbstate_t)); + fp->_flags2 = 0; if (f < 0) { /* did not get it after all */ if (isopen) @@ -239,8 +240,10 @@ finish: * we can do about this. (We could set __SAPP and check in * fseek and ftell.) */ - if (oflags & O_APPEND) + if (oflags & O_APPEND) { + fp->_flags2 |= __S2OAP; (void) _sseek(fp, (fpos_t)0, SEEK_END); + } FUNLOCKFILE(fp); return (fp); } diff --git a/lib/libc/stdio/ftell.c b/lib/libc/stdio/ftell.c index 745d500..152617f 100644 --- a/lib/libc/stdio/ftell.c +++ b/lib/libc/stdio/ftell.c @@ -88,7 +88,6 @@ _ftello(FILE *fp, fpos_t *offset) { fpos_t pos; size_t n; - int dflags; if (fp->_seek == NULL) { errno = ESPIPE; /* historic practice */ @@ -119,29 +118,33 @@ _ftello(FILE *fp, fpos_t *offset) } if (HASUB(fp)) pos -= fp->_r; /* Can be negative at this point. */ - } else if ((fp->_flags & __SWR) && fp->_p != NULL) { - dflags = 0; - if (fp->_flags & __SAPP) - dflags = O_APPEND; - else if (fp->_file != -1 && - (dflags = _fcntl(fp->_file, F_GETFL)) < 0) - return (1); - if ((dflags & O_APPEND) && - (pos = _sseek(fp, (fpos_t)0, SEEK_END)) == -1) { - if ((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 & __SWR) && fp->_p != NULL && + (n = fp->_p - fp->_bf._base) > 0) { /* * Writing. Any buffered characters cause the * position to be greater than that in the * underlying object. */ - n = fp->_p - fp->_bf._base; + 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); diff --git a/lib/libc/stdio/stdio.c b/lib/libc/stdio/stdio.c index 44ee0ab..5d6fb9a 100644 --- a/lib/libc/stdio/stdio.c +++ b/lib/libc/stdio/stdio.c @@ -117,7 +117,7 @@ _swrite(FILE *fp, char const *buf, int n) ret = (*fp->_write)(fp->_cookie, buf, n); /* __SOFF removed even on success in case O_APPEND mode is set. */ if (ret >= 0) { - if ((fp->_flags & (__SAPP|__SOFF)) == (__SAPP|__SOFF) && + if ((fp->_flags & __SOFF) && !(fp->_flags2 & __S2OAP) && fp->_offset <= OFF_MAX - ret) fp->_offset += ret; else -- cgit v1.1