]> pd.if.org Git - pdclib/blob - functions/stdio/fseek.c
Comment cleanups.
[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
11 #include <_PDCLIB_glue.h>
12
13 int fseek( struct _PDCLIB_file_t * stream, long offset, int whence )
14 {
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     return ( _PDCLIB_seek( stream, offset, whence ) != EOF ) ? 0 : EOF;
28 }
29
30 #endif
31
32 #ifdef TEST
33 #include <_PDCLIB_test.h>
34 #include <string.h>
35
36 int main( void )
37 {
38     FILE * fh;
39     TESTCASE( ( fh = tmpfile() ) != NULL );
40     TESTCASE( fwrite( teststring, 1, strlen( teststring ), fh ) == strlen( teststring ) );
41     /* General functionality */
42     TESTCASE( fseek( fh, -1, SEEK_END ) == 0  );
43     TESTCASE( (size_t)ftell( fh ) == strlen( teststring ) - 1 );
44     TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
45     TESTCASE( (size_t)ftell( fh ) == strlen( teststring ) );
46     TESTCASE( fseek( fh, 0, SEEK_SET ) == 0 );
47     TESTCASE( ftell( fh ) == 0 );
48     TESTCASE( fseek( fh, 5, SEEK_CUR ) == 0 );
49     TESTCASE( ftell( fh ) == 5 );
50     TESTCASE( fseek( fh, -3, SEEK_CUR ) == 0 );
51     TESTCASE( ftell( fh ) == 2 );
52     /* Checking behaviour around EOF */
53     TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
54     TESTCASE( ! feof( fh ) );
55     TESTCASE( fgetc( fh ) == EOF );
56     TESTCASE( feof( fh ) );
57     TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
58     TESTCASE( ! feof( fh ) );
59     /* Checking undo of ungetc() */
60     TESTCASE( fseek( fh, 0, SEEK_SET ) == 0 );
61     TESTCASE( fgetc( fh ) == teststring[0] );
62     TESTCASE( fgetc( fh ) == teststring[1] );
63     TESTCASE( fgetc( fh ) == teststring[2] );
64     TESTCASE( ftell( fh ) == 3 );
65     TESTCASE( ungetc( teststring[2], fh ) == teststring[2] );
66     TESTCASE( ftell( fh ) == 2 );
67     TESTCASE( fgetc( fh ) == teststring[2] );
68     TESTCASE( ftell( fh ) == 3 );
69     TESTCASE( ungetc( 'x', fh ) == 'x' );
70     TESTCASE( ftell( fh ) == 2 );
71     TESTCASE( fgetc( fh ) == 'x' );
72     TESTCASE( ungetc( 'x', fh ) == 'x' );
73     TESTCASE( ftell( fh ) == 2 );
74     TESTCASE( fseek( fh, 2, SEEK_SET ) == 0 );
75     TESTCASE( fgetc( fh ) == teststring[2] );
76     /* Checking error handling */
77     TESTCASE( fseek( fh, -5, SEEK_SET ) == -1 );
78     TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
79     TESTCASE( fclose( fh ) == 0 );
80     return TEST_RESULTS;
81 }
82
83 #endif
84