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