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