3 /* fseek( FILE *, long offset, int )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
13 #include <_PDCLIB_glue.h>
15 int fseek( struct _PDCLIB_file_t * stream, long offset, int whence )
17 if ( stream->status & _PDCLIB_FWRITE )
19 if ( _PDCLIB_flushbuffer( stream ) == EOF )
24 stream->status &= ~ _PDCLIB_EOFFLAG;
25 if ( stream->status & _PDCLIB_FRW )
27 stream->status &= ~ ( _PDCLIB_FREAD | _PDCLIB_FWRITE );
29 return ( _PDCLIB_seek( stream, offset, whence ) != EOF ) ? 0 : EOF;
35 #include <_PDCLIB_test.h>
41 TESTCASE( ( fh = tmpfile() ) != NULL );
42 TESTCASE( fwrite( teststring, 1, strlen( teststring ), fh ) == strlen( teststring ) );
43 /* General functionality */
44 TESTCASE( fseek( fh, -1, SEEK_END ) == 0 );
45 TESTCASE( (size_t)ftell( fh ) == strlen( teststring ) - 1 );
46 TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
47 TESTCASE( (size_t)ftell( fh ) == strlen( teststring ) );
48 TESTCASE( fseek( fh, 0, SEEK_SET ) == 0 );
49 TESTCASE( ftell( fh ) == 0 );
50 TESTCASE( fseek( fh, 5, SEEK_CUR ) == 0 );
51 TESTCASE( ftell( fh ) == 5 );
52 TESTCASE( fseek( fh, -3, SEEK_CUR ) == 0 );
53 TESTCASE( ftell( fh ) == 2 );
54 /* Checking behaviour around EOF */
55 TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
56 TESTCASE( ! feof( fh ) );
57 TESTCASE( fgetc( fh ) == EOF );
58 TESTCASE( feof( fh ) );
59 TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
60 TESTCASE( ! feof( fh ) );
61 /* Checking undo of ungetc() */
62 TESTCASE( fseek( fh, 0, SEEK_SET ) == 0 );
63 TESTCASE( fgetc( fh ) == teststring[0] );
64 TESTCASE( fgetc( fh ) == teststring[1] );
65 TESTCASE( fgetc( fh ) == teststring[2] );
66 TESTCASE( ftell( fh ) == 3 );
67 TESTCASE( ungetc( teststring[2], fh ) == teststring[2] );
68 TESTCASE( ftell( fh ) == 2 );
69 TESTCASE( fgetc( fh ) == teststring[2] );
70 TESTCASE( ftell( fh ) == 3 );
71 TESTCASE( ungetc( 'x', fh ) == 'x' );
72 TESTCASE( ftell( fh ) == 2 );
73 TESTCASE( fgetc( fh ) == 'x' );
74 TESTCASE( ungetc( 'x', fh ) == 'x' );
75 TESTCASE( ftell( fh ) == 2 );
76 TESTCASE( fseek( fh, 2, SEEK_SET ) == 0 );
77 TESTCASE( fgetc( fh ) == teststring[2] );
78 /* Checking error handling */
79 TESTCASE( fseek( fh, -5, SEEK_SET ) == -1 );
80 TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
81 TESTCASE( fclose( fh ) == 0 );