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