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 /* TODO: 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 );
52 TESTCASE( ( fh = fopen( testfile, "w+" ) ) != NULL );
53 TESTCASE( setvbuf( fh, buffer, _IOLBF, 4 ) == 0 );
54 TESTCASE( fputc( '1', fh ) == '1' );
55 TESTCASE( fputc( '2', fh ) == '2' );
56 TESTCASE( fputc( '3', fh ) == '3' );
57 /* Positions incrementing as expected? */
58 TESTCASE( ftell( fh ) == 3l );
59 TESTCASE_NOREG( fh->pos.offset == 0l );
60 TESTCASE_NOREG( fh->bufidx == 3l );
61 /* Buffer properly flushed when full? */
62 TESTCASE( fputc( '4', fh ) == '4' );
63 TESTCASE_NOREG( fh->pos.offset == 4l );
64 TESTCASE_NOREG( fh->bufidx == 0 );
65 /* fflush() resetting positions as expected? */
66 TESTCASE( fputc( '5', fh ) == '5' );
67 TESTCASE( fflush( fh ) == 0 );
68 TESTCASE( ftell( fh ) == 5l );
69 TESTCASE_NOREG( fh->pos.offset == 5l );
70 TESTCASE_NOREG( fh->bufidx == 0l );
71 /* rewind() resetting positions as expected? */
73 TESTCASE( ftell( fh ) == 0l );
74 TESTCASE_NOREG( fh->pos.offset == 0 );
75 TESTCASE_NOREG( fh->bufidx == 0 );
76 /* Reading back first character after rewind for basic read check */
77 TESTCASE( fgetc( fh ) == '1' );
78 TESTCASE( fclose( fh ) == 0 );