3904a1d871dd9229b59ba0c97d13923dfc8d5c14
[musl] / src / stdio / ftell.c
1 #include "stdio_impl.h"
2
3 off_t __ftello_unlocked(FILE *f)
4 {
5         off_t pos = f->seek(f, 0, SEEK_CUR);
6         if (pos < 0) return pos;
7
8         /* Adjust for data in buffer. */
9         return pos - (f->rend - f->rpos) + (f->wpos - f->wbase);
10 }
11
12 off_t __ftello(FILE *f)
13 {
14         off_t pos;
15         FLOCK(f);
16         pos = __ftello_unlocked(f);
17         FUNLOCK(f);
18         return pos;
19 }
20
21 long ftell(FILE *f)
22 {
23         off_t pos = __ftello(f);
24         if (pos > LONG_MAX) {
25                 errno = EOVERFLOW;
26                 return -1;
27         }
28         return pos;
29 }
30
31 weak_alias(__ftello, ftello);
32
33 LFS64(ftello);