Re-arrange my funopen(3) fix to minimize differences with original stdio code,

no functional changes.

Add fp->_offset optimization in _SAPP+_SOPT case
This commit is contained in:
ache 2001-09-03 02:24:37 +00:00
parent 3b83f22254
commit eb856b3441
6 changed files with 91 additions and 83 deletions

View File

@ -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);
}

View File

@ -311,42 +311,3 @@ _fseeko(fp, offset, whence, ltest)
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);
}

View File

@ -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 {

View File

@ -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));

View File

@ -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);
}

View File

@ -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);
}