]> pd.if.org Git - pdclib.old/commitdiff
PDCLIB-7: Add _PDCLIB_ftell64 to give us full precision file positioning information...
authorOwen Shepherd <owen.shepherd@e43.eu>
Sun, 26 Aug 2012 15:33:48 +0000 (16:33 +0100)
committerOwen Shepherd <owen.shepherd@e43.eu>
Sun, 26 Aug 2012 15:33:48 +0000 (16:33 +0100)
functions/stdio/_PDCLIB_ftell64.c [new file with mode: 0644]
functions/stdio/fseek.c
functions/stdio/ftell.c
includes/stdio.h

diff --git a/functions/stdio/_PDCLIB_ftell64.c b/functions/stdio/_PDCLIB_ftell64.c
new file mode 100644 (file)
index 0000000..650eda5
--- /dev/null
@@ -0,0 +1,55 @@
+/* $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
index 18c38c324ecc7dbacaa8fe734550360f28a4f0f9..8e10c2ba007f49cd599c7c5fc62275995076e729 100644 (file)
@@ -31,7 +31,7 @@ int fseek( struct _PDCLIB_file_t * stream, long loffset, int whence )
     if ( whence == SEEK_CUR )
     {
         whence  = SEEK_SET;
-        offset += stream->pos.offset;
+        offset += _PDCLIB_ftell64( stream );
     }
 
     return ( _PDCLIB_seek( stream, offset, whence ) != EOF ) ? 0 : EOF;
@@ -83,6 +83,16 @@ int main( void )
     TESTCASE( ftell( fh ) == 2 );
     TESTCASE( fseek( fh, 2, SEEK_SET ) == 0 );
     TESTCASE( fgetc( fh ) == teststring[2] );
+    /* PDCLIB-7: Check that we handle the underlying file descriptor correctly
+     *           in the SEEK_CUR case */
+    TESTCASE( fseek( fh, 10, SEEK_SET ) == 0 );
+    TESTCASE( ftell( fh ) == 10l );
+    TESTCASE( fseek( fh, 0, SEEK_CUR ) == 0 );
+    TESTCASE( ftell( fh ) == 10l );
+    TESTCASE( fseek( fh, 2, SEEK_CUR ) == 0 );
+    TESTCASE( ftell( fh ) == 12l );
+    TESTCASE( fseek( fh, -1, SEEK_CUR ) == 0 );
+    TESTCASE( ftell( fh ) == 11l );
     /* Checking error handling */
     TESTCASE( fseek( fh, -5, SEEK_SET ) == -1 );
     TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
index e82ca48155e25d22e4fcacfb2f93858e3634f6c9..d39297f688ae9bc170360f5e5902eca020e2c4e4 100644 (file)
@@ -7,39 +7,23 @@
 */
 
 #include <stdio.h>
+#include <stdint.h>
 #include <limits.h>
+#include <errno.h>
 
 #ifndef REGTEST
 
 long int ftell( struct _PDCLIB_file_t * stream )
 {
-    /* ftell() must take into account:
-       - the actual *physical* offset of the file, i.e. the offset as recognized
-         by the operating system (and stored in stream->pos.offset); and
-       - any buffers held by PDCLib, which
-         - in case of unwritten buffers, count in *addition* to the offset; or
-         - in case of unprocessed pre-read buffers, count in *substraction* to
-           the offset. (Remember to count ungetidx into this number.)
-       Conveniently, the calculation ( ( bufend - bufidx ) + ungetidx ) results
-       in just the right number in both cases:
-         - in case of unwritten buffers, ( ( 0 - unwritten ) + 0 )
-           i.e. unwritten bytes as negative number
-         - in case of unprocessed pre-read, ( ( preread - processed ) + unget )
-           i.e. unprocessed bytes as positive number.
-       That is how the somewhat obscure return-value calculation works.
-    */
-    /*  If offset is too large for return type, report error instead of wrong
-        offset value.
-    */
-    /* TODO: Check what happens when ungetc() is called on a stream at offset 0 */
-    if ( ( stream->pos.offset - stream->bufend ) > ( LONG_MAX - ( stream->bufidx - stream->ungetidx ) ) )
+    uint_fast64_t off64 = _PDCLIB_ftell64( stream );
+
+    if ( off64 > LONG_MAX )
     {
         /* integer overflow */
-        _PDCLIB_errno = _PDCLIB_ERANGE;
+        errno = ERANGE;
         return -1;
     }
-    long int res = ( stream->pos.offset - ( ( (int)stream->bufend - (int)stream->bufidx ) + stream->ungetidx ) );
-    return res;
+    return off64;
 }
 
 #endif
index b5c8abeb618a676e03cc58f0bcd413b714e62c12..74413330a0824a907630955a01f1f614ee99c3a7 100644 (file)
@@ -788,6 +788,7 @@ int fsetpos( FILE * stream, const fpos_t * pos ) _PDCLIB_nothrow;
    TODO: Implementation-defined errno setting for ftell().
 */
 long int ftell( FILE * stream ) _PDCLIB_nothrow;
+_PDCLIB_uint_fast64_t _PDCLIB_ftell64( FILE * stream ) _PDCLIB_nothrow;
 
 /* Equivalent to (void)fseek( stream, 0L, SEEK_SET ), except that the error
    indicator for the stream is also cleared.