xref: /third_party/musl/src/stdio/ftell.c (revision 570af302)
1#include "stdio_impl.h"
2#include <limits.h>
3#include <errno.h>
4#ifndef __LITEOS__
5#include "param_check.h"
6#endif
7
8off_t __ftello_unlocked(FILE *f)
9{
10	off_t pos = f->seek(f, 0,
11		(f->flags & F_APP) && f->wpos != f->wbase
12		? SEEK_END : SEEK_CUR);
13	if (pos < 0) return pos;
14
15	/* Adjust for data in buffer. */
16	if (f->rend)
17		pos += f->rpos - f->rend;
18	else if (f->wbase)
19		pos += f->wpos - f->wbase;
20	return pos;
21}
22
23off_t __ftello(FILE *f)
24{
25#ifndef __LITEOS__
26	PARAM_CHECK(f);
27#endif
28	off_t pos;
29	FLOCK(f);
30	pos = __ftello_unlocked(f);
31	FUNLOCK(f);
32	return pos;
33}
34
35long ftell(FILE *f)
36{
37	off_t pos = __ftello(f);
38	if (pos > LONG_MAX) {
39		errno = EOVERFLOW;
40		return -1;
41	}
42	return pos;
43}
44
45weak_alias(__ftello, ftello);
46
47weak_alias(ftello, ftello64);
48