]> pd.if.org Git - pdclib/blob - functions/stdio/ftell.c
Fixed safeguard calculation in ftell().
[pdclib] / functions / stdio / ftell.c
1 /* $Id$ */
2
3 /* ftell( FILE * )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <stdio.h>
10 #include <limits.h>
11
12 #ifndef REGTEST
13
14 long int ftell( struct _PDCLIB_file_t * stream )
15 {
16     /* ftell() must take into account:
17        - the actual *physical* offset of the file, i.e. the offset as recognized
18          by the operating system (and stored in stream->pos.offset); and
19        - any buffers held by PDCLib, which
20          - in case of unwritten buffers, count in *addition* to the offset; or
21          - in case of unprocessed pre-read buffers, count in *substraction* to
22            the offset. (Remember to count ungetidx into this number.)
23        Conveniently, the calculation ( ( bufend - bufidx ) + ungetidx ) results
24        in just the right number in both cases:
25          - in case of unwritten buffers, ( ( 0 - unwritten ) + 0 )
26            i.e. unwritten bytes as negative number
27          - in case of unprocessed pre-read, ( ( preread - processed ) + unget )
28            i.e. unprocessed bytes as positive number.
29        That is how the somewhat obscure return-value calculation works.
30     */
31     /*  If offset is too large for return type, report error instead of wrong
32         offset value.
33     */
34     /* TODO: Check what happens when ungetc() is called on a stream at offset 0 */
35     if ( ( stream->pos.offset - stream->bufend ) > ( LONG_MAX - ( stream->bufidx - stream->ungetidx ) ) )
36     {
37         /* integer overflow */
38         _PDCLIB_errno = _PDCLIB_ERANGE;
39         return -1;
40     }
41     return (long int)( stream->pos.offset - ( ( (int)stream->bufend - (int)stream->bufidx ) + stream->ungetidx ) );
42 }
43
44 #endif
45
46 #ifdef TEST
47 #include <_PDCLIB_test.h>
48
49 #include <stdlib.h>
50
51 int main( void )
52 {
53     /* Testing all the basic I/O functions individually would result in lots
54        of duplicated code, so I took the liberty of lumping it all together
55        here.
56     */
57     /* The following functions delegate their tests to here:
58        fgetc fflush rewind fputc ungetc fseek
59        flushbuffer seek fillbuffer prepread prepwrite
60     */
61     char * buffer = (char*)malloc( 4 );
62     FILE * fh;
63     TESTCASE( ( fh = tmpfile() ) != NULL );
64     TESTCASE( setvbuf( fh, buffer, _IOLBF, 4 ) == 0 );
65     TESTCASE( fputc( '1', fh ) == '1' );
66     TESTCASE( fputc( '2', fh ) == '2' );
67     TESTCASE( fputc( '3', fh ) == '3' );
68     /* Positions incrementing as expected? */
69     TESTCASE( ftell( fh ) == 3l );
70     TESTCASE_NOREG( fh->pos.offset == 0l );
71     TESTCASE_NOREG( fh->bufidx == 3l );
72     /* Buffer properly flushed when full? */
73     TESTCASE( fputc( '4', fh ) == '4' );
74     TESTCASE_NOREG( fh->pos.offset == 4l );
75     TESTCASE_NOREG( fh->bufidx == 0 );
76     /* fflush() resetting positions as expected? */
77     TESTCASE( fputc( '5', fh ) == '5' );
78     TESTCASE( fflush( fh ) == 0 );
79     TESTCASE( ftell( fh ) == 5l );
80     TESTCASE_NOREG( fh->pos.offset == 5l );
81     TESTCASE_NOREG( fh->bufidx == 0l );
82     /* rewind() resetting positions as expected? */
83     rewind( fh );
84     TESTCASE( ftell( fh ) == 0l );
85     TESTCASE_NOREG( fh->pos.offset == 0 );
86     TESTCASE_NOREG( fh->bufidx == 0 );
87     /* Reading back first character after rewind for basic read check */
88     TESTCASE( fgetc( fh ) == '1' );
89     /* TODO: t.b.c. */
90     TESTCASE( fclose( fh ) == 0 );
91     return TEST_RESULTS;
92 }
93
94 #endif
95