5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
14 long int ftell( struct _PDCLIB_file_t * stream )
16 /* FIXME: A bit too fuzzy in the head now. stream->ungetidx should be in here
19 if ( stream->pos.offset > ( LONG_MAX - stream->bufidx ) )
21 /* integer overflow */
22 _PDCLIB_errno = _PDCLIB_EINVAL;
25 /* Position of start-of-buffer, plus:
26 - buffered, unwritten content (for output streams), or
27 - already-parsed content from buffer (for input streams)
29 return (long int)( stream->pos.offset + stream->bufidx - stream->ungetidx );
35 #include <_PDCLIB_test.h>
41 /* Testing all the basic I/O functions individually would result in lots
42 of duplicated code, so I took the liberty of lumping it all together
45 /* The following functions delegate their tests to here:
46 fgetc fflush rewind fputc ungetc fseek
47 flushbuffer seek fillbuffer prepread prepwrite
49 char * buffer = (char*)malloc( 4 );
51 TESTCASE( ( fh = tmpfile() ) != NULL );
52 TESTCASE( setvbuf( fh, buffer, _IOLBF, 4 ) == 0 );
53 TESTCASE( fputc( '1', fh ) == '1' );
54 TESTCASE( fputc( '2', fh ) == '2' );
55 TESTCASE( fputc( '3', fh ) == '3' );
56 /* Positions incrementing as expected? */
57 TESTCASE( ftell( fh ) == 3l );
58 TESTCASE_NOREG( fh->pos.offset == 0l );
59 TESTCASE_NOREG( fh->bufidx == 3l );
60 /* Buffer properly flushed when full? */
61 TESTCASE( fputc( '4', fh ) == '4' );
62 TESTCASE_NOREG( fh->pos.offset == 4l );
63 TESTCASE_NOREG( fh->bufidx == 0 );
64 /* fflush() resetting positions as expected? */
65 TESTCASE( fputc( '5', fh ) == '5' );
66 TESTCASE( fflush( fh ) == 0 );
67 TESTCASE( ftell( fh ) == 5l );
68 TESTCASE_NOREG( fh->pos.offset == 5l );
69 TESTCASE_NOREG( fh->bufidx == 0l );
70 /* rewind() resetting positions as expected? */
72 TESTCASE( ftell( fh ) == 0l );
73 TESTCASE_NOREG( fh->pos.offset == 0 );
74 TESTCASE_NOREG( fh->bufidx == 0 );
75 /* Reading back first character after rewind for basic read check */
76 TESTCASE( fgetc( fh ) == '1' );
78 TESTCASE( fclose( fh ) == 0 );