]> pd.if.org Git - pdclib/blob - functions/stdio/fseek.c
PDCLib includes with quotes, not <>.
[pdclib] / functions / stdio / fseek.c
1 /* fseek( FILE *, long, int )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <stdio.h>
8
9 #ifndef REGTEST
10 #include "_PDCLIB_io.h"
11
12 int _PDCLIB_fseek_unlocked( FILE * stream, long loffset, int whence )
13 {
14     _PDCLIB_int64_t offset = loffset;
15     if ( stream->status & _PDCLIB_FWRITE )
16     {
17         if ( _PDCLIB_flushbuffer( stream ) == EOF )
18         {
19             return EOF;
20         }
21     }
22     stream->status &= ~ _PDCLIB_EOFFLAG;
23     if ( stream->status & _PDCLIB_FRW )
24     {
25         stream->status &= ~ ( _PDCLIB_FREAD | _PDCLIB_FWRITE );
26     }
27
28     if ( whence == SEEK_CUR )
29     {
30         whence  = SEEK_SET;
31         offset += _PDCLIB_ftell64_unlocked( stream );
32     }
33
34     return ( _PDCLIB_seek( stream, offset, whence ) != EOF ) ? 0 : EOF;
35 }
36
37 int fseek( FILE * stream, long loffset, int whence )
38 {
39     _PDCLIB_flockfile( stream );
40     int r = _PDCLIB_fseek_unlocked( stream, loffset, whence );
41     _PDCLIB_funlockfile( stream );
42     return r;
43 }
44
45 #endif
46
47 #ifdef TEST
48 #include "_PDCLIB_test.h"
49 #include <string.h>
50
51 int main( void )
52 {
53     FILE * fh;
54     TESTCASE( ( fh = tmpfile() ) != NULL );
55     TESTCASE( fwrite( teststring, 1, strlen( teststring ), fh ) == strlen( teststring ) );
56     /* General functionality */
57     TESTCASE( fseek( fh, -1, SEEK_END ) == 0  );
58     TESTCASE( (size_t)ftell( fh ) == strlen( teststring ) - 1 );
59     TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
60     TESTCASE( (size_t)ftell( fh ) == strlen( teststring ) );
61     TESTCASE( fseek( fh, 0, SEEK_SET ) == 0 );
62     TESTCASE( ftell( fh ) == 0 );
63     TESTCASE( fseek( fh, 5, SEEK_CUR ) == 0 );
64     TESTCASE( ftell( fh ) == 5 );
65     TESTCASE( fseek( fh, -3, SEEK_CUR ) == 0 );
66     TESTCASE( ftell( fh ) == 2 );
67     /* Checking behaviour around EOF */
68     TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
69     TESTCASE( ! feof( fh ) );
70     TESTCASE( fgetc( fh ) == EOF );
71     TESTCASE( feof( fh ) );
72     TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
73     TESTCASE( ! feof( fh ) );
74     /* Checking undo of ungetc() */
75     TESTCASE( fseek( fh, 0, SEEK_SET ) == 0 );
76     TESTCASE( fgetc( fh ) == teststring[0] );
77     TESTCASE( fgetc( fh ) == teststring[1] );
78     TESTCASE( fgetc( fh ) == teststring[2] );
79     TESTCASE( ftell( fh ) == 3 );
80     TESTCASE( ungetc( teststring[2], fh ) == teststring[2] );
81     TESTCASE( ftell( fh ) == 2 );
82     TESTCASE( fgetc( fh ) == teststring[2] );
83     TESTCASE( ftell( fh ) == 3 );
84     TESTCASE( ungetc( 'x', fh ) == 'x' );
85     TESTCASE( ftell( fh ) == 2 );
86     TESTCASE( fgetc( fh ) == 'x' );
87     TESTCASE( ungetc( 'x', fh ) == 'x' );
88     TESTCASE( ftell( fh ) == 2 );
89     TESTCASE( fseek( fh, 2, SEEK_SET ) == 0 );
90     TESTCASE( fgetc( fh ) == teststring[2] );
91     /* PDCLIB-7: Check that we handle the underlying file descriptor correctly
92      *           in the SEEK_CUR case */
93     TESTCASE( fseek( fh, 10, SEEK_SET ) == 0 );
94     TESTCASE( ftell( fh ) == 10l );
95     TESTCASE( fseek( fh, 0, SEEK_CUR ) == 0 );
96     TESTCASE( ftell( fh ) == 10l );
97     TESTCASE( fseek( fh, 2, SEEK_CUR ) == 0 );
98     TESTCASE( ftell( fh ) == 12l );
99     TESTCASE( fseek( fh, -1, SEEK_CUR ) == 0 );
100     TESTCASE( ftell( fh ) == 11l );
101     /* Checking error handling */
102     TESTCASE( fseek( fh, -5, SEEK_SET ) == -1 );
103     TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
104     TESTCASE( fclose( fh ) == 0 );
105     return TEST_RESULTS;
106 }
107
108 #endif
109