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_unlocked( struct _PDCLIB_file_t * stream, long loffset, int whence )
17 _PDCLIB_int64_t offset = loffset;
18 if ( stream->status & _PDCLIB_FWRITE )
20 if ( _PDCLIB_flushbuffer( stream ) == EOF )
25 stream->status &= ~ _PDCLIB_EOFFLAG;
26 if ( stream->status & _PDCLIB_FRW )
28 stream->status &= ~ ( _PDCLIB_FREAD | _PDCLIB_FWRITE );
31 if ( whence == SEEK_CUR )
34 offset += _PDCLIB_ftell64( stream );
37 return ( _PDCLIB_seek( stream, offset, whence ) != EOF ) ? 0 : EOF;
40 int fseek( struct _PDCLIB_file_t * stream, long loffset, int whence )
43 int r = fseek_unlocked( stream, loffset, whence );
44 funlockfile( stream );
51 #include <_PDCLIB_test.h>
57 TESTCASE( ( fh = tmpfile() ) != NULL );
58 TESTCASE( fwrite( teststring, 1, strlen( teststring ), fh ) == strlen( teststring ) );
59 /* General functionality */
60 TESTCASE( fseek( fh, -1, SEEK_END ) == 0 );
61 TESTCASE( (size_t)ftell( fh ) == strlen( teststring ) - 1 );
62 TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
63 TESTCASE( (size_t)ftell( fh ) == strlen( teststring ) );
64 TESTCASE( fseek( fh, 0, SEEK_SET ) == 0 );
65 TESTCASE( ftell( fh ) == 0 );
66 TESTCASE( fseek( fh, 5, SEEK_CUR ) == 0 );
67 TESTCASE( ftell( fh ) == 5 );
68 TESTCASE( fseek( fh, -3, SEEK_CUR ) == 0 );
69 TESTCASE( ftell( fh ) == 2 );
70 /* Checking behaviour around EOF */
71 TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
72 TESTCASE( ! feof( fh ) );
73 TESTCASE( fgetc( fh ) == EOF );
74 TESTCASE( feof( fh ) );
75 TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
76 TESTCASE( ! feof( fh ) );
77 /* Checking undo of ungetc() */
78 TESTCASE( fseek( fh, 0, SEEK_SET ) == 0 );
79 TESTCASE( fgetc( fh ) == teststring[0] );
80 TESTCASE( fgetc( fh ) == teststring[1] );
81 TESTCASE( fgetc( fh ) == teststring[2] );
82 TESTCASE( ftell( fh ) == 3 );
83 TESTCASE( ungetc( teststring[2], fh ) == teststring[2] );
84 TESTCASE( ftell( fh ) == 2 );
85 TESTCASE( fgetc( fh ) == teststring[2] );
86 TESTCASE( ftell( fh ) == 3 );
87 TESTCASE( ungetc( 'x', fh ) == 'x' );
88 TESTCASE( ftell( fh ) == 2 );
89 TESTCASE( fgetc( fh ) == 'x' );
90 TESTCASE( ungetc( 'x', fh ) == 'x' );
91 TESTCASE( ftell( fh ) == 2 );
92 TESTCASE( fseek( fh, 2, SEEK_SET ) == 0 );
93 TESTCASE( fgetc( fh ) == teststring[2] );
94 /* PDCLIB-7: Check that we handle the underlying file descriptor correctly
95 * in the SEEK_CUR case */
96 TESTCASE( fseek( fh, 10, SEEK_SET ) == 0 );
97 TESTCASE( ftell( fh ) == 10l );
98 TESTCASE( fseek( fh, 0, SEEK_CUR ) == 0 );
99 TESTCASE( ftell( fh ) == 10l );
100 TESTCASE( fseek( fh, 2, SEEK_CUR ) == 0 );
101 TESTCASE( ftell( fh ) == 12l );
102 TESTCASE( fseek( fh, -1, SEEK_CUR ) == 0 );
103 TESTCASE( ftell( fh ) == 11l );
104 /* Checking error handling */
105 TESTCASE( fseek( fh, -5, SEEK_SET ) == -1 );
106 TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
107 TESTCASE( fclose( fh ) == 0 );