diff options
author | ache <ache@FreeBSD.org> | 2001-09-03 02:24:37 +0000 |
---|---|---|
committer | ache <ache@FreeBSD.org> | 2001-09-03 02:24:37 +0000 |
commit | cbe865cf8c86d5f4b1c19485a2cd4ff56834b78f (patch) | |
tree | 1e9ab56239da83bdafac4d6f346eacc369bb761e /lib/libc/stdio | |
parent | f1a12fefd65f6c3558da75f09d7892aa9f3cce9d (diff) | |
download | FreeBSD-src-cbe865cf8c86d5f4b1c19485a2cd4ff56834b78f.zip FreeBSD-src-cbe865cf8c86d5f4b1c19485a2cd4ff56834b78f.tar.gz |
Re-arrange my funopen(3) fix to minimize differences with original stdio code,
no functional changes.
Add fp->_offset optimization in _SAPP+_SOPT case
Diffstat (limited to 'lib/libc/stdio')
-rw-r--r-- | lib/libc/stdio/fflush.c | 8 | ||||
-rw-r--r-- | lib/libc/stdio/fseek.c | 39 | ||||
-rw-r--r-- | lib/libc/stdio/fvwrite.c | 33 | ||||
-rw-r--r-- | lib/libc/stdio/local.h | 2 | ||||
-rw-r--r-- | lib/libc/stdio/refill.c | 9 | ||||
-rw-r--r-- | lib/libc/stdio/stdio.c | 83 |
6 files changed, 91 insertions, 83 deletions
diff --git a/lib/libc/stdio/fflush.c b/lib/libc/stdio/fflush.c index 55a6a2a..9ba2ec9 100644 --- a/lib/libc/stdio/fflush.c +++ b/lib/libc/stdio/fflush.c @@ -113,15 +113,9 @@ __sflush(FILE *fp) fp->_p = p; fp->_w = t & (__SLBF|__SNBF) ? 0 : fp->_bf._size; - if (n <= 0) - return (0); - if ((fp->_flags & __SAPP) && _sseek(fp, (fpos_t)0, SEEK_END) == -1) - goto err; - fp->_flags &= ~__SOFF; /* In case FAPPEND mode is set. */ for (; n > 0; n -= t, p += t) { - t = (*fp->_write)(fp->_cookie, (char *)p, n); + t = _swrite(fp, (char *)p, n); if (t <= 0) { - err: fp->_flags |= __SERR; return (EOF); } diff --git a/lib/libc/stdio/fseek.c b/lib/libc/stdio/fseek.c index 1f7ebde..2278f31 100644 --- a/lib/libc/stdio/fseek.c +++ b/lib/libc/stdio/fseek.c @@ -311,42 +311,3 @@ dumb: fp->_flags &= ~__SEOF; return (0); } - - -fpos_t -_sseek(fp, offset, whence) - FILE *fp; - fpos_t offset; - int whence; -{ - fpos_t ret; - int serrno, errret; - - if (fp->_seek == NULL) { - errno = ESPIPE; - return (-1); - } - serrno = errno; - errno = 0; - ret = (*fp->_seek)(fp->_cookie, offset, whence); - errret = errno; - if (errno == 0) - errno = serrno; - /* - * Disallow negative seeks per POSIX. - * It is needed here to help upper level caller - * in the cases it can't detect. - */ - if (ret < 0) { - if (errret == 0) { - fp->_flags |= __SERR; - errno = EINVAL; - } - fp->_flags &= ~__SOFF; - ret = -1; - } else if (fp->_flags & __SOPT) { - fp->_flags |= __SOFF; - fp->_offset = ret; - } - return (ret); -} diff --git a/lib/libc/stdio/fvwrite.c b/lib/libc/stdio/fvwrite.c index efcfffb..9196825 100644 --- a/lib/libc/stdio/fvwrite.c +++ b/lib/libc/stdio/fvwrite.c @@ -64,7 +64,7 @@ __sfvwrite(fp, uio) register struct __siov *iov; register int w, s; char *nl; - int nlknown, nldist, firsttime; + int nlknown, nldist; if ((len = uio->uio_resid) == 0) return (0); @@ -86,22 +86,13 @@ __sfvwrite(fp, uio) len = iov->iov_len; \ iov++; \ } - firsttime = 1; if (fp->_flags & __SNBF) { /* * Unbuffered: write up to BUFSIZ bytes at a time. */ do { GETIOV(;); - if (firsttime) { - if ((fp->_flags & __SAPP) && - _sseek(fp, (fpos_t)0, SEEK_END) == -1) - goto err; - /* In case FAPPEND mode is set. */ - fp->_flags &= ~__SOFF; - firsttime = 0; - } - w = (*fp->_write)(fp->_cookie, p, MIN(len, BUFSIZ)); + w = _swrite(fp, p, MIN(len, BUFSIZ)); if (w <= 0) goto err; p += w; @@ -156,15 +147,7 @@ __sfvwrite(fp, uio) goto err; } else if (len >= (w = fp->_bf._size)) { /* write directly */ - if (firsttime) { - if ((fp->_flags & __SAPP) && - _sseek(fp, (fpos_t)0, SEEK_END) == -1) - goto err; - /* In case FAPPEND mode is set. */ - fp->_flags &= ~__SOFF; - firsttime = 0; - } - w = (*fp->_write)(fp->_cookie, p, w); + w = _swrite(fp, p, w); if (w <= 0) goto err; } else { @@ -203,15 +186,7 @@ __sfvwrite(fp, uio) if (__fflush(fp)) goto err; } else if (s >= (w = fp->_bf._size)) { - if (firsttime) { - if ((fp->_flags & __SAPP) && - _sseek(fp, (fpos_t)0, SEEK_END) == -1) - goto err; - /* In case FAPPEND mode is set. */ - fp->_flags &= ~__SOFF; - firsttime = 0; - } - w = (*fp->_write)(fp->_cookie, p, w); + w = _swrite(fp, p, w); if (w <= 0) goto err; } else { diff --git a/lib/libc/stdio/local.h b/lib/libc/stdio/local.h index e074273..c1c215c 100644 --- a/lib/libc/stdio/local.h +++ b/lib/libc/stdio/local.h @@ -46,6 +46,8 @@ * in particular, macros and private variables. */ +extern int _sread __P((FILE *, char *, int)); +extern int _swrite __P((FILE *, char const *, int)); extern fpos_t _sseek __P((FILE *, fpos_t, int)); extern int _ftello __P((FILE *, fpos_t *)); extern int _fseeko __P((FILE *, off_t, int, int)); diff --git a/lib/libc/stdio/refill.c b/lib/libc/stdio/refill.c index 19d98f9..b3e292a 100644 --- a/lib/libc/stdio/refill.c +++ b/lib/libc/stdio/refill.c @@ -44,7 +44,6 @@ static const char rcsid[] = #include "namespace.h" #include <errno.h> -#include <limits.h> #include <stdio.h> #include <stdlib.h> #include "un-namespace.h" @@ -136,7 +135,7 @@ __srefill(FILE *fp) __sflush(fp); } fp->_p = fp->_bf._base; - fp->_r = (*fp->_read)(fp->_cookie, (char *)fp->_p, fp->_bf._size); + fp->_r = _sread(fp, (char *)fp->_p, fp->_bf._size); fp->_flags &= ~__SMOD; /* buffer contents are again pristine */ if (fp->_r <= 0) { if (fp->_r == 0) @@ -144,14 +143,8 @@ __srefill(FILE *fp) else { fp->_r = 0; fp->_flags |= __SERR; - fp->_flags &= ~__SOFF; } return (EOF); - } else if (fp->_flags & __SOFF) { - if (fp->_offset > OFF_MAX - fp->_r) - fp->_flags &= ~__SOFF; - else - fp->_offset += fp->_r; } return (0); } diff --git a/lib/libc/stdio/stdio.c b/lib/libc/stdio/stdio.c index b4bf299..1a5aa72 100644 --- a/lib/libc/stdio/stdio.c +++ b/lib/libc/stdio/stdio.c @@ -45,6 +45,7 @@ static const char rcsid[] = #include "namespace.h" #include <errno.h> #include <fcntl.h> +#include <limits.h> #include <stdio.h> #include <unistd.h> #include "un-namespace.h" @@ -93,3 +94,85 @@ __sclose(cookie) return (_close(((FILE *)cookie)->_file)); } + +/* + * Higher level wrappers. + */ +int +_sread(fp, buf, n) + FILE *fp; + char *buf; + int n; +{ + int ret; + + ret = (*fp->_read)(fp->_cookie, buf, n); + if (ret > 0) { + if (fp->_flags & __SOFF) { + if (fp->_offset <= OFF_MAX - ret) + fp->_offset += ret; + else + fp->_flags &= ~__SOFF; + } + } else if (ret < 0) + fp->_flags &= ~__SOFF; + return (ret); +} + +int +_swrite(fp, buf, n) + FILE *fp; + char const *buf; + int n; +{ + int ret; + + if ((fp->_flags & __SAPP) && _sseek(fp, (fpos_t)0, SEEK_END) == -1) + return (-1); + 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) && + fp->_offset <= OFF_MAX - ret) + fp->_offset += ret; + else + fp->_flags &= ~__SOFF; + + } else if (ret < 0) + fp->_flags &= ~__SOFF; + return (ret); +} + +fpos_t +_sseek(fp, offset, whence) + FILE *fp; + fpos_t offset; + int whence; +{ + fpos_t ret; + int serrno, errret; + + serrno = errno; + errno = 0; + ret = (*fp->_seek)(fp->_cookie, offset, whence); + errret = errno; + if (errno == 0) + errno = serrno; + /* + * Disallow negative seeks per POSIX. + * It is needed here to help upper level caller + * in the cases it can't detect. + */ + if (ret < 0) { + if (errret == 0) { + fp->_flags |= __SERR; + errno = EINVAL; + } + fp->_flags &= ~__SOFF; + ret = -1; + } else if (fp->_flags & __SOPT) { + fp->_flags |= __SOFF; + fp->_offset = ret; + } + return (ret); +} |