initial check-in, version 0.5.0
[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         /* If writing, flush output. */
9         if (f->wpos > f->buf && __oflow(f)) return -1;
10
11         /* Perform the underlying seek operation. */
12         if (f->seek(f, off, whence) < 0) return -1;
13
14         /* If seek succeeded, file is seekable and we discard read buffer. */
15         f->rpos = f->rend = f->rstop = 0;
16         f->flags &= ~F_EOF;
17         
18         FUNLOCK(f);     
19         return 0;
20 }
21
22 int __fseeko(FILE *f, off_t off, int whence)
23 {
24         int result;
25         FLOCK(f);
26         result = __fseeko_unlocked(f, off, whence);
27         FUNLOCK(f);
28         return result;
29 }
30
31 int fseek(FILE *f, long off, int whence)
32 {
33         return __fseeko(f, off, whence);
34 }
35
36 weak_alias(__fseeko, fseeko);
37
38 LFS64(fseeko);