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