]> pd.if.org Git - pdclib.old/blob - functions/stdio/fgetpos.c
Namespace cleanliness: Rename all ***_unlocked functions to _PDCLIB_***_unlocked.
[pdclib.old] / functions / stdio / fgetpos.c
1 /* $Id$ */
2
3 /* fgetpos( FILE * , fpos_t * )
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_fgetpos_unlocked( FILE * _PDCLIB_restrict stream, _PDCLIB_fpos_t * _PDCLIB_restrict pos )
15 {
16     pos->offset = stream->pos.offset + stream->bufidx - stream->ungetidx;
17     pos->mbs    = stream->pos.mbs;
18     /* TODO: Add mbstate. */
19     return 0;
20 }
21
22 int fgetpos( FILE * _PDCLIB_restrict stream, _PDCLIB_fpos_t * _PDCLIB_restrict pos )
23 {
24     _PDCLIB_flockfile( stream );
25     int res = _PDCLIB_fgetpos_unlocked( stream, pos );
26     _PDCLIB_funlockfile( stream );
27     return res;
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     fpos_t pos1, pos2;
40     TESTCASE( ( fh = tmpfile() ) != NULL );
41     TESTCASE( fgetpos( fh, &pos1 ) == 0 );
42     TESTCASE( fwrite( teststring, 1, strlen( teststring ), fh ) == strlen( teststring ) );
43     TESTCASE( (size_t)ftell( fh ) == strlen( teststring ) );
44     TESTCASE( fgetpos( fh, &pos2 ) == 0 );
45     TESTCASE( fsetpos( fh, &pos1 ) == 0 );
46     TESTCASE( ftell( fh ) == 0 );
47     TESTCASE( fsetpos( fh, &pos2 ) == 0 );
48     TESTCASE( (size_t)ftell( fh ) == strlen( teststring ) );
49     TESTCASE( fclose( fh ) == 0 );
50     return TEST_RESULTS;
51 }
52
53 #endif