From 831e2fb1cd4fc12dbd3e9ac761af7c87223a85b9 Mon Sep 17 00:00:00 2001 From: ache Date: Thu, 30 Aug 2001 20:49:47 +0000 Subject: Try to discard some ungetc data in saved internal buffer checks too, if offset tends to be negative. --- lib/libc/stdio/fseek.c | 20 ++++++++++++++++---- lib/libc/stdio/ftell.c | 12 +++++++++--- 2 files changed, 25 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/libc/stdio/fseek.c b/lib/libc/stdio/fseek.c index aa886d2..8af60ba 100644 --- a/lib/libc/stdio/fseek.c +++ b/lib/libc/stdio/fseek.c @@ -151,8 +151,14 @@ _fseeko(fp, offset, whence, ltest) if (HASUB(fp)) { curoff -= fp->_ur; if (curoff < 0) { - errno = EBADF; - return (-1); + if (-curoff <= fp->_r) { + fp->_p -= curoff; + fp->_r += curoff; + curoff = 0; + } else { + errno = EBADF; + return (-1); + } } } } else if ((fp->_flags & __SWR) && fp->_p != NULL) { @@ -261,8 +267,14 @@ _fseeko(fp, offset, whence, ltest) } if (HASUB(fp)) { curoff -= fp->_ur; - if (curoff < 0) - goto dumb; + if (curoff < 0) { + if (-curoff <= fp->_r) { + fp->_p -= curoff; + fp->_r += curoff; + curoff = 0; + } else + goto dumb; + } } } diff --git a/lib/libc/stdio/ftell.c b/lib/libc/stdio/ftell.c index 38a164e..46a309c 100644 --- a/lib/libc/stdio/ftell.c +++ b/lib/libc/stdio/ftell.c @@ -118,9 +118,15 @@ ftello(fp) if (HASUB(fp)) { pos -= fp->_ur; if (pos < 0) { - errno = EBADF; - FUNLOCKFILE(fp); - return (-1); + if (-pos <= fp->_r) { + fp->_p -= pos; + fp->_r += pos; + pos = 0; + } else { + errno = EBADF; + FUNLOCKFILE(fp); + return (-1); + } } } } else if ((fp->_flags & __SWR) && fp->_p != NULL) { -- cgit v1.1