]> pd.if.org Git - pdclib.old/blob - functions/stdio/fseek.c
Namespace cleanliness: Rename all ***_unlocked functions to _PDCLIB_***_unlocked.
[pdclib.old] / functions / stdio / fseek.c
1 /* $Id$ */
2
3 /* fseek( FILE *, long offset, int )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <stdio.h>
10
11 #ifndef REGTEST
12 #include <_PDCLIB_io.h>
13
14 int _PDCLIB_fseek_unlocked( FILE * stream, long loffset, int whence )
15 {
16     _PDCLIB_int64_t offset = loffset;
17     if ( stream->status & _PDCLIB_FWRITE )
18     {
19         if ( _PDCLIB_flushbuffer( stream ) == EOF )
20         {
21             return EOF;
22         }
23     }
24     stream->status &= ~ _PDCLIB_EOFFLAG;
25     if ( stream->status & _PDCLIB_FRW )
26     {
27         stream->status &= ~ ( _PDCLIB_FREAD | _PDCLIB_FWRITE );
28     }
29
30     if ( whence == SEEK_CUR )
31     {
32         whence  = SEEK_SET;
33         offset += _PDCLIB_ftell64_unlocked( stream );
34     }
35
36     return ( _PDCLIB_seek( stream, offset, whence ) != EOF ) ? 0 : EOF;
37 }
38
39 int fseek( FILE * stream, long loffset, int whence )
40 {
41     _PDCLIB_flockfile( stream );
42     int r = _PDCLIB_fseek_unlocked( stream, loffset, whence );
43     _PDCLIB_funlockfile( stream );
44     return r;
45 }
46
47 #endif
48
49 #ifdef TEST
50 #include <_PDCLIB_test.h>
51 #include <string.h>
52
53 int main( void )
54 {
55     FILE * fh;
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 );
107     return TEST_RESULTS;
108 }
109
110 #endif
111