1 #include "stdio_impl.h"
2 #include <errno.h>
3 #ifndef __LITEOS__
4 #include "param_check.h"
5 #endif
6 
__fseeko_unlocked(FILE *f, off_t off, int whence)7 int __fseeko_unlocked(FILE *f, off_t off, int whence)
8 {
9 	/* Fail immediately for invalid whence argument. */
10 	if (whence != SEEK_CUR && whence != SEEK_SET && whence != SEEK_END) {
11 		errno = EINVAL;
12 		return -1;
13 	}
14 
15 	/* Adjust relative offset for unread data in buffer, if any. */
16 	if (whence == SEEK_CUR && f->rend) off -= f->rend - f->rpos;
17 
18 	/* Flush write buffer, and report error on failure. */
19 	if (f->wpos != f->wbase) {
20 		f->write(f, 0, 0);
21 		if (!f->wpos) return -1;
22 	}
23 
24 	/* Leave writing mode */
25 	f->wpos = f->wbase = f->wend = 0;
26 
27 	/* Perform the underlying seek. */
28 	if (f->seek(f, off, whence) < 0) return -1;
29 
30 	/* If seek succeeded, file is seekable and we discard read buffer. */
31 	f->rpos = f->rend = 0;
32 	f->flags &= ~F_EOF;
33 
34 	return 0;
35 }
36 
__fseeko(FILE *f, off_t off, int whence)37 int __fseeko(FILE *f, off_t off, int whence)
38 {
39 #ifndef __LITEOS__
40 	PARAM_CHECK(f);
41 #endif
42 	int result;
43 	FLOCK(f);
44 	result = __fseeko_unlocked(f, off, whence);
45 	FUNLOCK(f);
46 	return result;
47 }
48 
fseek(FILE *f, long off, int whence)49 int fseek(FILE *f, long off, int whence)
50 {
51 	return __fseeko(f, off, whence);
52 }
53 
54 weak_alias(__fseeko, fseeko);
55 
56 weak_alias(fseeko, fseeko64);
57