]> git.proxmox.com Git - wasi-libc.git/blame - libc-top-half/musl/src/stdio/fseek.c
Update to musl 1.2.3.
[wasi-libc.git] / libc-top-half / musl / src / stdio / fseek.c
CommitLineData
320054e8 1#include "stdio_impl.h"
5d8a1409 2#include <errno.h>
320054e8
DG
3
4int __fseeko_unlocked(FILE *f, off_t off, int whence)
5{
5d8a1409
DG
6 /* Fail immediately for invalid whence argument. */
7 if (whence != SEEK_CUR && whence != SEEK_SET && whence != SEEK_END) {
8 errno = EINVAL;
9 return -1;
10 }
11
320054e8
DG
12 /* Adjust relative offset for unread data in buffer, if any. */
13 if (whence == SEEK_CUR && f->rend) off -= f->rend - f->rpos;
14
15 /* Flush write buffer, and report error on failure. */
16 if (f->wpos != f->wbase) {
17 f->write(f, 0, 0);
18 if (!f->wpos) return -1;
19 }
20
21 /* Leave writing mode */
22 f->wpos = f->wbase = f->wend = 0;
23
24 /* Perform the underlying seek. */
25 if (f->seek(f, off, whence) < 0) return -1;
26
27 /* If seek succeeded, file is seekable and we discard read buffer. */
28 f->rpos = f->rend = 0;
29 f->flags &= ~F_EOF;
30
31 return 0;
32}
33
34int __fseeko(FILE *f, off_t off, int whence)
35{
36 int result;
37 FLOCK(f);
38 result = __fseeko_unlocked(f, off, whence);
39 FUNLOCK(f);
40 return result;
41}
42
43int fseek(FILE *f, long off, int whence)
44{
45 return __fseeko(f, off, whence);
46}
47
48weak_alias(__fseeko, fseeko);
49
50weak_alias(fseeko, fseeko64);