major stdio overhaul, using readv/writev, plus other changes
[musl] / src / stdio / fseek.c
1 #include "stdio_impl.h"
2
3 int __fseeko_unlocked(FILE *f, off_t off, int whence)
4 {
5         /* Adjust relative offset for unread data in buffer, if any. */
6         if (whence == SEEK_CUR) off -= f->rend - f->rpos;
7
8         /* Flush write buffer, and report error on failure. */
9         if (f->wpos > f->wbase) {
10                 f->write(f, 0, 0);
11                 if (!f->wpos) return -1;
12         }
13
14         /* Leave writing mode */
15         f->wpos = f->wbase = f->wend = 0;
16
17         /* Perform the underlying seek. */
18         if (f->seek(f, off, whence) < 0) {
19                 f->flags |= F_ERR;
20                 return -1;
21         }
22
23         /* If seek succeeded, file is seekable and we discard read buffer. */
24         f->rpos = f->rend = 0;
25         f->flags &= ~F_EOF;
26         
27         return 0;
28 }
29
30 int __fseeko(FILE *f, off_t off, int whence)
31 {
32         int result;
33         FLOCK(f);
34         result = __fseeko_unlocked(f, off, whence);
35         FUNLOCK(f);
36         return result;
37 }
38
39 int fseek(FILE *f, long off, int whence)
40 {
41         return __fseeko(f, off, whence);
42 }
43
44 weak_alias(__fseeko, fseeko);
45
46 LFS64(fseeko);