blob: bb62897a4c7cc710295fd62aae8bb727b116a4d5 [file] [log] [blame]
Rich Felker0b44a032011-02-12 00:22:29 -05001#include "stdio_impl.h"
Rich Felker835f9f92012-11-08 16:39:41 -05002#include <limits.h>
3#include <errno.h>
Rich Felker0b44a032011-02-12 00:22:29 -05004
5off_t __ftello_unlocked(FILE *f)
6{
Rich Felker3af2ede2014-02-07 00:57:50 -05007 off_t pos = f->seek(f, 0,
8 (f->flags & F_APP) && f->wpos > f->wbase
9 ? SEEK_END : SEEK_CUR);
Rich Felkerdba68bf2011-07-30 08:02:14 -040010 if (pos < 0) return pos;
11
Rich Felker0b44a032011-02-12 00:22:29 -050012 /* Adjust for data in buffer. */
13 return pos - (f->rend - f->rpos) + (f->wpos - f->wbase);
14}
15
16off_t __ftello(FILE *f)
17{
18 off_t pos;
19 FLOCK(f);
20 pos = __ftello_unlocked(f);
21 FUNLOCK(f);
22 return pos;
23}
24
25long ftell(FILE *f)
26{
27 off_t pos = __ftello(f);
28 if (pos > LONG_MAX) {
29 errno = EOVERFLOW;
30 return -1;
31 }
32 return pos;
33}
34
35weak_alias(__ftello, ftello);
36
37LFS64(ftello);