summaryrefslogtreecommitdiff
path: root/src/stdio/fseek.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/stdio/fseek.c')
-rw-r--r--src/stdio/fseek.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/src/stdio/fseek.c b/src/stdio/fseek.c
index bfaad375..8d9da440 100644
--- a/src/stdio/fseek.c
+++ b/src/stdio/fseek.c
@@ -5,17 +5,25 @@ int __fseeko_unlocked(FILE *f, off_t off, int whence)
/* Adjust relative offset for unread data in buffer, if any. */
if (whence == SEEK_CUR) off -= f->rend - f->rpos;
- /* If writing, flush output. */
- if (f->wpos > f->buf && __oflow(f)) return -1;
+ /* Flush write buffer, and report error on failure. */
+ if (f->wpos > f->wbase) {
+ f->write(f, 0, 0);
+ if (!f->wpos) return -1;
+ }
- /* Perform the underlying seek operation. */
- if (f->seek(f, off, whence) < 0) return -1;
+ /* Leave writing mode */
+ f->wpos = f->wbase = f->wend = 0;
+
+ /* Perform the underlying seek. */
+ if (f->seek(f, off, whence) < 0) {
+ f->flags |= F_ERR;
+ return -1;
+ }
/* If seek succeeded, file is seekable and we discard read buffer. */
- f->rpos = f->rend = f->rstop = 0;
+ f->rpos = f->rend = 0;
f->flags &= ~F_EOF;
- FUNLOCK(f);
return 0;
}