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.
12 #include <_PDCLIB_io.h>
14 int _PDCLIB_fseek_unlocked( FILE * stream, long loffset, int whence )
16 _PDCLIB_int64_t offset = loffset;
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 );
30 if ( whence == SEEK_CUR )
33 offset += _PDCLIB_ftell64_unlocked( stream );
36 return ( _PDCLIB_seek( stream, offset, whence ) != EOF ) ? 0 : EOF;
39 int fseek( FILE * stream, long loffset, int whence )
41 _PDCLIB_flockfile( stream );
42 int r = _PDCLIB_fseek_unlocked( stream, loffset, whence );
43 _PDCLIB_funlockfile( stream );
50 #include <_PDCLIB_test.h>
56 TESTCASE( ( fh = tmpfile() ) != NULL );
57 TESTCASE( fwrite( teststring, 1, strlen( teststring ), fh ) == strlen( teststring ) );
58 /* General functionality */
59 TESTCASE( fseek( fh, -1, SEEK_END ) == 0 );
60 TESTCASE( (size_t)ftell( fh ) == strlen( teststring ) - 1 );
61 TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
62 TESTCASE( (size_t)ftell( fh ) == strlen( teststring ) );
63 TESTCASE( fseek( fh, 0, SEEK_SET ) == 0 );
64 TESTCASE( ftell( fh ) == 0 );
65 TESTCASE( fseek( fh, 5, SEEK_CUR ) == 0 );
66 TESTCASE( ftell( fh ) == 5 );
67 TESTCASE( fseek( fh, -3, SEEK_CUR ) == 0 );
68 TESTCASE( ftell( fh ) == 2 );
69 /* Checking behaviour around EOF */
70 TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
71 TESTCASE( ! feof( fh ) );
72 TESTCASE( fgetc( fh ) == EOF );
73 TESTCASE( feof( fh ) );
74 TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
75 TESTCASE( ! feof( fh ) );
76 /* Checking undo of ungetc() */
77 TESTCASE( fseek( fh, 0, SEEK_SET ) == 0 );
78 TESTCASE( fgetc( fh ) == teststring[0] );
79 TESTCASE( fgetc( fh ) == teststring[1] );
80 TESTCASE( fgetc( fh ) == teststring[2] );
81 TESTCASE( ftell( fh ) == 3 );
82 TESTCASE( ungetc( teststring[2], fh ) == teststring[2] );
83 TESTCASE( ftell( fh ) == 2 );
84 TESTCASE( fgetc( fh ) == teststring[2] );
85 TESTCASE( ftell( fh ) == 3 );
86 TESTCASE( ungetc( 'x', fh ) == 'x' );
87 TESTCASE( ftell( fh ) == 2 );
88 TESTCASE( fgetc( fh ) == 'x' );
89 TESTCASE( ungetc( 'x', fh ) == 'x' );
90 TESTCASE( ftell( fh ) == 2 );
91 TESTCASE( fseek( fh, 2, SEEK_SET ) == 0 );
92 TESTCASE( fgetc( fh ) == teststring[2] );
93 /* PDCLIB-7: Check that we handle the underlying file descriptor correctly
94 * in the SEEK_CUR case */
95 TESTCASE( fseek( fh, 10, SEEK_SET ) == 0 );
96 TESTCASE( ftell( fh ) == 10l );
97 TESTCASE( fseek( fh, 0, SEEK_CUR ) == 0 );
98 TESTCASE( ftell( fh ) == 10l );
99 TESTCASE( fseek( fh, 2, SEEK_CUR ) == 0 );
100 TESTCASE( ftell( fh ) == 12l );
101 TESTCASE( fseek( fh, -1, SEEK_CUR ) == 0 );
102 TESTCASE( ftell( fh ) == 11l );
103 /* Checking error handling */
104 TESTCASE( fseek( fh, -5, SEEK_SET ) == -1 );
105 TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
106 TESTCASE( fclose( fh ) == 0 );