/* 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. */
+ /* Leave writing mode */
+ f->wpos = f->wbase = f->wend = 0;
+
+ /* Perform the underlying seek. */
if (f->seek(f, off, whence) < 0) 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;
}