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 )
28 if ( stream->status & _PDCLIB_FWRITE )
30 if ( _PDCLIB_flushbuffer( stream ) == EOF )
32 /* Flush failed, errno already set */
37 if ( stream->status & _PDCLIB_LIBBUFFER )
39 free( stream->buffer );
42 _PDCLIB_close( stream->handle );
43 /* Remove stream from list */
44 if ( previous != NULL )
46 previous->next = stream->next;
50 _PDCLIB_filelist = stream->next;
57 current = current->next;
59 _PDCLIB_errno = _PDCLIB_EIO;
66 #include <_PDCLIB_test.h>
71 struct _PDCLIB_file_t * file1;
72 struct _PDCLIB_file_t * file2;
73 remove( "testfile1" );
74 remove( "testfile2" );
75 TESTCASE( _PDCLIB_filelist == stdin );
76 TESTCASE( ( file1 = fopen( "testfile1", "w" ) ) != NULL );
77 TESTCASE( _PDCLIB_filelist == file1 );
78 TESTCASE( ( file2 = fopen( "testfile2", "w" ) ) != NULL );
79 TESTCASE( _PDCLIB_filelist == file2 );
80 TESTCASE( fclose( file2 ) == 0 );
81 TESTCASE( _PDCLIB_filelist == file1 );
82 TESTCASE( ( file2 = fopen( "testfile1", "w" ) ) != NULL );
83 TESTCASE( _PDCLIB_filelist == file2 );
84 TESTCASE( fclose( file1 ) == 0 );
85 TESTCASE( _PDCLIB_filelist == file2 );
86 TESTCASE( fclose( file2 ) == 0 );
87 TESTCASE( _PDCLIB_filelist == stdin );
88 remove( "testfile1" );
89 remove( "testfile2" );
91 puts( " NOTEST fclose() test driver is PDCLib-specific." );