5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
13 #include <_PDCLIB_glue.h>
15 extern struct _PDCLIB_file_t * _PDCLIB_filelist;
17 /* FIXME: Last file not removed from list. */
18 int fclose( struct _PDCLIB_file_t * stream )
20 struct _PDCLIB_file_t * current = _PDCLIB_filelist;
21 struct _PDCLIB_file_t * previous = NULL;
22 /* Checking that the FILE handle is actually one we had opened before. */
23 while ( current != NULL )
25 if ( stream == current )
27 if ( stream->status & _PDCLIB_WROTELAST ) fflush( stream );
28 if ( stream->status & _PDCLIB_LIBBUFFER ) free( stream->buffer );
29 _PDCLIB_close( stream->handle );
30 if ( previous != NULL )
32 previous->next = current->next;
36 _PDCLIB_filelist = current->next;
41 current = current->next;
49 #include <_PDCLIB_test.h>
54 struct _PDCLIB_file_t * file1;
55 struct _PDCLIB_file_t * file2;
56 TESTCASE( _PDCLIB_filelist == NULL );
57 TESTCASE( ( file1 = fopen( "testfile1", "w" ) ) != NULL );
58 TESTCASE( _PDCLIB_filelist == file1 );
59 TESTCASE( ( file2 = fopen( "testfile2", "w" ) ) != NULL );
60 TESTCASE( _PDCLIB_filelist == file2 );
61 TESTCASE( fclose( file2 ) == 0 );
62 TESTCASE( _PDCLIB_filelist == file1 );
63 TESTCASE( ( file2 = fopen( "testfile1", "w" ) ) != NULL );
64 TESTCASE( _PDCLIB_filelist == file2 );
65 TESTCASE( fclose( file1 ) == 0 );
66 TESTCASE( _PDCLIB_filelist == file2 );
67 TESTCASE( fclose( file2 ) == 0 );
68 TESTCASE( _PDCLIB_filelist == NULL );
69 system( "rm testfile1 testfile2" );
71 puts( " NOTEST fclose() test driver is PDCLib-specific." );