1570af302Sopenharmony_ci#include "stdio_impl.h"
2570af302Sopenharmony_ci#include <errno.h>
3570af302Sopenharmony_ci#ifndef __LITEOS__
4570af302Sopenharmony_ci#include "param_check.h"
5570af302Sopenharmony_ci#endif
6570af302Sopenharmony_ci
7570af302Sopenharmony_ciint __fseeko_unlocked(FILE *f, off_t off, int whence)
8570af302Sopenharmony_ci{
9570af302Sopenharmony_ci	/* Fail immediately for invalid whence argument. */
10570af302Sopenharmony_ci	if (whence != SEEK_CUR && whence != SEEK_SET && whence != SEEK_END) {
11570af302Sopenharmony_ci		errno = EINVAL;
12570af302Sopenharmony_ci		return -1;
13570af302Sopenharmony_ci	}
14570af302Sopenharmony_ci
15570af302Sopenharmony_ci	/* Adjust relative offset for unread data in buffer, if any. */
16570af302Sopenharmony_ci	if (whence == SEEK_CUR && f->rend) off -= f->rend - f->rpos;
17570af302Sopenharmony_ci
18570af302Sopenharmony_ci	/* Flush write buffer, and report error on failure. */
19570af302Sopenharmony_ci	if (f->wpos != f->wbase) {
20570af302Sopenharmony_ci		f->write(f, 0, 0);
21570af302Sopenharmony_ci		if (!f->wpos) return -1;
22570af302Sopenharmony_ci	}
23570af302Sopenharmony_ci
24570af302Sopenharmony_ci	/* Leave writing mode */
25570af302Sopenharmony_ci	f->wpos = f->wbase = f->wend = 0;
26570af302Sopenharmony_ci
27570af302Sopenharmony_ci	/* Perform the underlying seek. */
28570af302Sopenharmony_ci	if (f->seek(f, off, whence) < 0) return -1;
29570af302Sopenharmony_ci
30570af302Sopenharmony_ci	/* If seek succeeded, file is seekable and we discard read buffer. */
31570af302Sopenharmony_ci	f->rpos = f->rend = 0;
32570af302Sopenharmony_ci	f->flags &= ~F_EOF;
33570af302Sopenharmony_ci
34570af302Sopenharmony_ci	return 0;
35570af302Sopenharmony_ci}
36570af302Sopenharmony_ci
37570af302Sopenharmony_ciint __fseeko(FILE *f, off_t off, int whence)
38570af302Sopenharmony_ci{
39570af302Sopenharmony_ci#ifndef __LITEOS__
40570af302Sopenharmony_ci	PARAM_CHECK(f);
41570af302Sopenharmony_ci#endif
42570af302Sopenharmony_ci	int result;
43570af302Sopenharmony_ci	FLOCK(f);
44570af302Sopenharmony_ci	result = __fseeko_unlocked(f, off, whence);
45570af302Sopenharmony_ci	FUNLOCK(f);
46570af302Sopenharmony_ci	return result;
47570af302Sopenharmony_ci}
48570af302Sopenharmony_ci
49570af302Sopenharmony_ciint fseek(FILE *f, long off, int whence)
50570af302Sopenharmony_ci{
51570af302Sopenharmony_ci	return __fseeko(f, off, whence);
52570af302Sopenharmony_ci}
53570af302Sopenharmony_ci
54570af302Sopenharmony_ciweak_alias(__fseeko, fseeko);
55570af302Sopenharmony_ci
56570af302Sopenharmony_ciweak_alias(fseeko, fseeko64);
57