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