summaryrefslogtreecommitdiffstats
path: root/lib/libc/stdio/fseek.c
diff options
context:
space:
mode:
authorache <ache@FreeBSD.org>2001-08-31 18:23:29 +0000
committerache <ache@FreeBSD.org>2001-08-31 18:23:29 +0000
commit2362fc36bc1d2beae0027a778eeca43c5f18bc8d (patch)
treeef0edbd2131d5fbfeb3556a582d2491b8a11f007 /lib/libc/stdio/fseek.c
parent85477cf711c667771f03f0ccd26176800e8e751d (diff)
downloadFreeBSD-src-2362fc36bc1d2beae0027a778eeca43c5f18bc8d.zip
FreeBSD-src-2362fc36bc1d2beae0027a778eeca43c5f18bc8d.tar.gz
The same big piece of ftell code repeated in 3 places. Simplify things moving
it into one subfunction instead. Try to use real offset in strange cases.
Diffstat (limited to 'lib/libc/stdio/fseek.c')
-rw-r--r--lib/libc/stdio/fseek.c91
1 files changed, 7 insertions, 84 deletions
diff --git a/lib/libc/stdio/fseek.c b/lib/libc/stdio/fseek.c
index 4e3d5cc..7cf117a 100644
--- a/lib/libc/stdio/fseek.c
+++ b/lib/libc/stdio/fseek.c
@@ -104,7 +104,7 @@ _fseeko(fp, offset, whence, ltest)
int ltest;
{
register fpos_t (*seekfn) __P((void *, fpos_t, int));
- fpos_t target, curoff, spos;
+ fpos_t target, curoff;
size_t n;
struct stat st;
int havepos;
@@ -126,53 +126,11 @@ _fseeko(fp, offset, whence, ltest)
case SEEK_CUR:
/*
* In order to seek relative to the current stream offset,
- * we have to first find the current stream offset a la
+ * we have to first find the current stream offset via
* ftell (see ftell for details).
*/
- if (fp->_flags & __SOFF)
- curoff = fp->_offset;
- else {
- curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
- if (curoff == -1)
- return (-1);
- }
- if (fp->_flags & __SRD) {
- spos = curoff;
- curoff -= fp->_r;
- if (curoff < 0) {
- if (HASUB(fp)) {
- fp->_p -= curoff;
- fp->_r += curoff;
- curoff = 0;
- } else {
- fp->_p = fp->_bf._base;
- fp->_r = 0;
- curoff = spos;
- }
- }
- if (HASUB(fp)) {
- curoff -= fp->_ur;
- if (curoff < 0) {
- if (-curoff <= fp->_r) {
- fp->_p -= curoff;
- fp->_r += curoff;
- curoff = 0;
- } else {
- fp->_p = fp->_bf._base;
- fp->_r = 0;
- FREEUB(fp);
- curoff = spos;
- }
- }
- }
- } else if ((fp->_flags & __SWR) && fp->_p != NULL) {
- n = fp->_p - fp->_bf._base;
- if (curoff > OFF_MAX - n) {
- errno = EOVERFLOW;
- return (-1);
- }
- curoff += n;
- }
+ if ((curoff = _ftello(fp)) == -1)
+ return (-1);
if (offset > 0 && curoff > OFF_MAX - offset) {
errno = EOVERFLOW;
return (-1);
@@ -252,43 +210,8 @@ _fseeko(fp, offset, whence, ltest)
}
}
- if (!havepos) {
- if (fp->_flags & __SOFF)
- curoff = fp->_offset;
- else {
- curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
- if (curoff == POS_ERR)
- goto dumb;
- }
- spos = curoff;
- curoff -= fp->_r;
- if (curoff < 0) {
- if (HASUB(fp)) {
- fp->_p -= curoff;
- fp->_r += curoff;
- curoff = 0;
- } else {
- fp->_p = fp->_bf._base;
- fp->_r = 0;
- curoff = spos;
- }
- }
- if (HASUB(fp)) {
- curoff -= fp->_ur;
- if (curoff < 0) {
- if (-curoff <= fp->_r) {
- fp->_p -= curoff;
- fp->_r += curoff;
- curoff = 0;
- } else {
- fp->_p = fp->_bf._base;
- fp->_r = 0;
- FREEUB(fp);
- curoff = spos;
- }
- }
- }
- }
+ if (!havepos && (curoff = _ftello(fp)) == -1)
+ goto dumb;
/*
* Compute the number of bytes in the input buffer (pretending
@@ -346,7 +269,6 @@ abspos:
fp->_p = fp->_bf._base;
if (HASUB(fp))
FREEUB(fp);
- fp->_flags &= ~__SEOF;
n = target - curoff;
if (n) {
if (__srefill(fp) || fp->_r < n)
@@ -354,6 +276,7 @@ abspos:
fp->_p += n;
fp->_r -= n;
}
+ fp->_flags &= ~__SEOF;
return (0);
/*
OpenPOWER on IntegriCloud