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 _PDCLIB_close( stream->handle );
37 /* Remove stream from list */
38 if ( previous != NULL )
40 previous->next = stream->next;
44 _PDCLIB_filelist = stream->next;
51 current = current->next;
53 _PDCLIB_errno = _PDCLIB_EIO;
60 #include <_PDCLIB_test.h>
65 struct _PDCLIB_file_t * file1;
66 struct _PDCLIB_file_t * file2;
67 remove( "testfile1" );
68 remove( "testfile2" );
69 TESTCASE( _PDCLIB_filelist == stdin );
70 TESTCASE( ( file1 = fopen( "testfile1", "w" ) ) != NULL );
71 TESTCASE( _PDCLIB_filelist == file1 );
72 TESTCASE( ( file2 = fopen( "testfile2", "w" ) ) != NULL );
73 TESTCASE( _PDCLIB_filelist == file2 );
74 TESTCASE( fclose( file2 ) == 0 );
75 TESTCASE( _PDCLIB_filelist == file1 );
76 TESTCASE( ( file2 = fopen( "testfile1", "w" ) ) != NULL );
77 TESTCASE( _PDCLIB_filelist == file2 );
78 TESTCASE( fclose( file1 ) == 0 );
79 TESTCASE( _PDCLIB_filelist == file2 );
80 TESTCASE( fclose( file2 ) == 0 );
81 TESTCASE( _PDCLIB_filelist == stdin );
82 remove( "testfile1" );
83 remove( "testfile2" );
85 puts( " NOTEST fclose() test driver is PDCLib-specific." );