]> pd.if.org Git - pdclib.old/blob - functions/stdio/fseek.c
c897325509b4421e7277d8eb4f77b1dc9f5738b8
[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
13 #include <_PDCLIB_glue.h>
14
15 int fseek_unlocked( struct _PDCLIB_file_t * stream, long loffset, int whence )
16 {
17     _PDCLIB_int64_t offset = loffset;
18     if ( stream->status & _PDCLIB_FWRITE )
19     {
20         if ( _PDCLIB_flushbuffer( stream ) == EOF )
21         {
22             return EOF;
23         }
24     }
25     stream->status &= ~ _PDCLIB_EOFFLAG;
26     if ( stream->status & _PDCLIB_FRW )
27     {
28         stream->status &= ~ ( _PDCLIB_FREAD | _PDCLIB_FWRITE );
29     }
30
31     if ( whence == SEEK_CUR )
32     {
33         whence  = SEEK_SET;
34         offset += _PDCLIB_ftell64( stream );
35     }
36
37     return ( _PDCLIB_seek( stream, offset, whence ) != EOF ) ? 0 : EOF;
38 }
39
40 int fseek( struct _PDCLIB_file_t * stream, long loffset, int whence )
41 {
42     flockfile( stream );
43     int r = fseek_unlocked( stream, loffset, whence );
44     funlockfile( stream );
45     return r;
46 }
47
48 #endif
49
50 #ifdef TEST
51 #include <_PDCLIB_test.h>
52 #include <string.h>
53
54 int main( void )
55 {
56     FILE * fh;
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 );
108     return TEST_RESULTS;
109 }
110
111 #endif
112