diff options
author | ache <ache@FreeBSD.org> | 2015-10-24 02:23:15 +0000 |
---|---|---|
committer | ache <ache@FreeBSD.org> | 2015-10-24 02:23:15 +0000 |
commit | d6852ac6f1dfc1396dee130993f8686cf53f5d2c (patch) | |
tree | c8f96d268fae59791f07df0c9f770053554f1f6a /lib/libc | |
parent | 9901e2ee18d8dffc84891f9ec0e7de37518bb95f (diff) | |
download | FreeBSD-src-d6852ac6f1dfc1396dee130993f8686cf53f5d2c.zip FreeBSD-src-d6852ac6f1dfc1396dee130993f8686cf53f5d2c.tar.gz |
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().
MFC after: 3 weeks
Diffstat (limited to 'lib/libc')
-rw-r--r-- | lib/libc/stdio/fdopen.c | 5 | ||||
-rw-r--r-- | lib/libc/stdio/fopen.c | 5 | ||||
-rw-r--r-- | lib/libc/stdio/freopen.c | 5 | ||||
-rw-r--r-- | lib/libc/stdio/ftell.c | 32 | ||||
-rw-r--r-- | lib/libc/stdio/stdio.c | 3 |
5 files changed, 31 insertions, 19 deletions
diff --git a/lib/libc/stdio/fdopen.c b/lib/libc/stdio/fdopen.c index b936998..8bd9c2d 100644 --- a/lib/libc/stdio/fdopen.c +++ b/lib/libc/stdio/fdopen.c @@ -91,7 +91,10 @@ 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)) + /* XXX: Reuse __SALC for O_APPEND. */ + if (fdflags & O_APPEND) + fp->_flags |= __SALC; + else if (oflags & O_APPEND) fp->_flags |= __SAPP; fp->_file = fd; fp->_cookie = fp; diff --git a/lib/libc/stdio/fopen.c b/lib/libc/stdio/fopen.c index b08e336..b26f637 100644 --- a/lib/libc/stdio/fopen.c +++ b/lib/libc/stdio/fopen.c @@ -91,7 +91,10 @@ 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) { + /* XXX: Reuse __SALC for O_APPEND. */ + fp->_flags |= __SALC; (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 4dcd50f..8b68bac 100644 --- a/lib/libc/stdio/freopen.c +++ b/lib/libc/stdio/freopen.c @@ -240,8 +240,11 @@ finish: * we can do about this. (We could set __SAPP and check in * fseek and ftell.) */ - if (oflags & O_APPEND) + if (oflags & O_APPEND) { + /* XXX: Reuse __SALC for O_APPEND. */ + fp->_flags |= __SALC; (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..0d2222b 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 */ @@ -120,21 +119,24 @@ _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); + /* XXX: Reuse __SALC for O_APPEND. */ + if (fp->_flags & (__SAPP|__SALC)) { + 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; } /* * Writing. Any buffered characters cause the diff --git a/lib/libc/stdio/stdio.c b/lib/libc/stdio/stdio.c index 44ee0ab..fc2e74b 100644 --- a/lib/libc/stdio/stdio.c +++ b/lib/libc/stdio/stdio.c @@ -117,7 +117,8 @@ _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) && + /* XXX: Reuse __SALC for O_APPEND. */ + if ((fp->_flags & __SOFF) && !(fp->_flags & __SALC) && fp->_offset <= OFF_MAX - ret) fp->_offset += ret; else |