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 int fclose( struct _PDCLIB_file_t * stream )
19 struct _PDCLIB_file_t * current = _PDCLIB_filelist;
20 struct _PDCLIB_file_t * previous = NULL;
21 /* Checking that the FILE handle is actually one we had opened before. */
22 while ( current != NULL )
24 if ( stream == current )
27 if ( stream->status & _PDCLIB_FWRITE )
29 if ( _PDCLIB_flushbuffer( stream ) == EOF )
31 /* Flush failed, errno already set */
36 if ( stream->status & _PDCLIB_LIBBUFFER )
38 free( stream->buffer );
41 _PDCLIB_close( stream->handle );
42 /* Remove stream from list */
43 if ( previous != NULL )
45 previous->next = stream->next;
49 _PDCLIB_filelist = stream->next;
56 current = current->next;
58 _PDCLIB_errno = _PDCLIB_EIO;
65 #include <_PDCLIB_test.h>
70 struct _PDCLIB_file_t * file1;
71 struct _PDCLIB_file_t * file2;
72 remove( "testfile1" );
73 remove( "testfile2" );
74 TESTCASE( _PDCLIB_filelist == stdin );
75 TESTCASE( ( file1 = fopen( "testfile1", "w" ) ) != NULL );
76 TESTCASE( _PDCLIB_filelist == file1 );
77 TESTCASE( ( file2 = fopen( "testfile2", "w" ) ) != NULL );
78 TESTCASE( _PDCLIB_filelist == file2 );
79 TESTCASE( fclose( file2 ) == 0 );
80 TESTCASE( _PDCLIB_filelist == file1 );
81 TESTCASE( ( file2 = fopen( "testfile1", "w" ) ) != NULL );
82 TESTCASE( _PDCLIB_filelist == file2 );
83 TESTCASE( fclose( file1 ) == 0 );
84 TESTCASE( _PDCLIB_filelist == file2 );
85 TESTCASE( fclose( file2 ) == 0 );
86 TESTCASE( _PDCLIB_filelist == stdin );
87 remove( "testfile1" );
88 remove( "testfile2" );
90 puts( " NOTEST fclose() test driver is PDCLib-specific." );