Path: blob/master/waterbox/libc/functions/stdio/_PDCLIB_ftell64.c
2 views
/* _PDCLIB_ftell64( FILE * )12This file is part of the Public Domain C Library (PDCLib).3Permission is granted to use, modify, and / or redistribute at will.4*/56#include <stdio.h>7#include <stdint.h>8#include <limits.h>910#ifndef REGTEST11#include "_PDCLIB_io.h"1213uint_fast64_t _PDCLIB_ftell64_unlocked( FILE * stream )14{15/* ftell() must take into account:16- the actual *physical* offset of the file, i.e. the offset as recognized17by the operating system (and stored in stream->pos.offset); and18- any buffers held by PDCLib, which19- in case of unwritten buffers, count in *addition* to the offset; or20- in case of unprocessed pre-read buffers, count in *substraction* to21the offset. (Remember to count ungetidx into this number.)22Conveniently, the calculation ( ( bufend - bufidx ) + ungetidx ) results23in just the right number in both cases:24- in case of unwritten buffers, ( ( 0 - unwritten ) + 0 )25i.e. unwritten bytes as negative number26- in case of unprocessed pre-read, ( ( preread - processed ) + unget )27i.e. unprocessed bytes as positive number.28That is how the somewhat obscure return-value calculation works.29*/3031/* ungetc on a stream at offset==0 will cause an overflow to UINT64_MAX.32* C99/C11 says that the return value of ftell in this case is33* "indeterminate"34*/3536return ( stream->pos.offset - ( ( (int)stream->bufend - (int)stream->bufidx ) + (int)stream->ungetidx ) );37}3839uint_fast64_t _PDCLIB_ftell64( FILE * stream )40{41_PDCLIB_flockfile( stream );42uint_fast64_t pos = _PDCLIB_ftell64_unlocked( stream );43_PDCLIB_funlockfile( stream );44return pos;45}4647#endif4849#ifdef TEST50#include "_PDCLIB_test.h"5152#include <stdlib.h>5354int main( void )55{56/* Tested by ftell */57return TEST_RESULTS;58}5960#endif61626364