| 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, |
| 8 | 		(f->flags & F_APP) && f->wpos != f->wbase |
| 9 | 		? SEEK_END : SEEK_CUR); |
| 10 | 	if (pos < 0) return pos; |
| 11 | |
| 12 | 	/* Adjust for data in buffer. */ |
| 13 | 	if (f->rend) |
| 14 | 		pos += f->rpos - f->rend; |
| 15 | 	else if (f->wbase) |
| 16 | 		pos += f->wpos - f->wbase; |
| 17 | 	return pos; |
| 18 | } |
| 19 | |
| 20 | off_t __ftello(FILE *f) |
| 21 | { |
| 22 | 	off_t pos; |
| 23 | 	FLOCK(f); |
| 24 | 	pos = __ftello_unlocked(f); |
| 25 | 	FUNLOCK(f); |
| 26 | 	return pos; |
| 27 | } |
| 28 | |
| 29 | long ftell(FILE *f) |
| 30 | { |
| 31 | 	off_t pos = __ftello(f); |
| 32 | 	if (pos > LONG_MAX) { |
| 33 | 		errno = EOVERFLOW; |
| 34 | 		return -1; |
| 35 | 	} |
| 36 | 	return pos; |
| 37 | } |
| 38 | |
| 39 | weak_alias(__ftello, ftello); |
| 40 | |
| 41 | weak_alias(ftello, ftello64); |