--- /dev/null
+/* $Id$ */\r
+\r
+/* _PDCLIB_ftell64( FILE * )\r
+\r
+ This file is part of the Public Domain C Library (PDCLib).\r
+ Permission is granted to use, modify, and / or redistribute at will.\r
+*/\r
+\r
+#include <stdio.h>\r
+#include <stdint.h>\r
+#include <limits.h>\r
+\r
+#ifndef REGTEST\r
+\r
+uint_fast64_t _PDCLIB_ftell64( struct _PDCLIB_file_t * stream )\r
+{\r
+ /* ftell() must take into account:\r
+ - the actual *physical* offset of the file, i.e. the offset as recognized\r
+ by the operating system (and stored in stream->pos.offset); and\r
+ - any buffers held by PDCLib, which\r
+ - in case of unwritten buffers, count in *addition* to the offset; or\r
+ - in case of unprocessed pre-read buffers, count in *substraction* to\r
+ the offset. (Remember to count ungetidx into this number.)\r
+ Conveniently, the calculation ( ( bufend - bufidx ) + ungetidx ) results\r
+ in just the right number in both cases:\r
+ - in case of unwritten buffers, ( ( 0 - unwritten ) + 0 )\r
+ i.e. unwritten bytes as negative number\r
+ - in case of unprocessed pre-read, ( ( preread - processed ) + unget )\r
+ i.e. unprocessed bytes as positive number.\r
+ That is how the somewhat obscure return-value calculation works.\r
+ */\r
+\r
+ /* ungetc on a stream at offset==0 will cause an overflow to UINT64_MAX.\r
+ * C99/C11 says that the return value of ftell in this case is \r
+ * "indeterminate"\r
+ */\r
+\r
+ return ( stream->pos.offset - ( ( (int)stream->bufend - (int)stream->bufidx ) + (int)stream->ungetidx ) );\r
+}\r
+\r
+#endif\r
+\r
+#ifdef TEST\r
+#include <_PDCLIB_test.h>\r
+\r
+#include <stdlib.h>\r
+\r
+int main( void )\r
+{\r
+ /* Tested by ftell */\r
+ return TEST_RESULTS;\r
+}\r
+\r
+#endif\r
+\r