]> pd.if.org Git - pdclib/blob - functions/stdio/ftell.c
Whitespace 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
46 #include "_PDCLIB_test.h"
47
48 #include <stdlib.h>
49
50 int main( void )
51 {
52     /* Testing all the basic I/O functions individually would result in lots
53        of duplicated code, so I took the liberty of lumping it all together
54        here.
55     */
56     /* The following functions delegate their tests to here:
57        fgetc fflush rewind fputc ungetc fseek
58        flushbuffer seek fillbuffer prepread prepwrite
59     */
60     char * buffer = (char*)malloc( 4 );
61     FILE * fh;
62     TESTCASE( ( fh = tmpfile() ) != NULL );
63     TESTCASE( setvbuf( fh, buffer, _IOLBF, 4 ) == 0 );
64     /* Testing ungetc() at offset 0 */
65     rewind( fh );
66     TESTCASE( ungetc( 'x', fh ) == 'x' );
67     TESTCASE( ftell( fh ) == -1l );
68     rewind( fh );
69     TESTCASE( ftell( fh ) == 0l );
70     /* Commence "normal" tests */
71     TESTCASE( fputc( '1', fh ) == '1' );
72     TESTCASE( fputc( '2', fh ) == '2' );
73     TESTCASE( fputc( '3', fh ) == '3' );
74     /* Positions incrementing as expected? */
75     TESTCASE( ftell( fh ) == 3l );
76     TESTCASE_NOREG( fh->pos.offset == 0l );
77     TESTCASE_NOREG( fh->bufidx == 3l );
78     /* Buffer properly flushed when full? */
79     TESTCASE( fputc( '4', fh ) == '4' );
80     TESTCASE_NOREG( fh->pos.offset == 4l );
81     TESTCASE_NOREG( fh->bufidx == 0 );
82     /* fflush() resetting positions as expected? */
83     TESTCASE( fputc( '5', fh ) == '5' );
84     TESTCASE( fflush( fh ) == 0 );
85     TESTCASE( ftell( fh ) == 5l );
86     TESTCASE_NOREG( fh->pos.offset == 5l );
87     TESTCASE_NOREG( fh->bufidx == 0l );
88     /* rewind() resetting positions as expected? */
89     rewind( fh );
90     TESTCASE( ftell( fh ) == 0l );
91     TESTCASE_NOREG( fh->pos.offset == 0 );
92     TESTCASE_NOREG( fh->bufidx == 0 );
93     /* Reading back first character after rewind for basic read check */
94     TESTCASE( fgetc( fh ) == '1' );
95     /* TODO: t.b.c. */
96     TESTCASE( fclose( fh ) == 0 );
97     return TEST_RESULTS;
98 }
99
100 #endif