3 This file is part of the Public Domain C Library (PDCLib).
4 Permission is granted to use, modify, and / or redistribute at will.
12 #include "_PDCLIB_glue.h"
14 extern struct _PDCLIB_file_t * _PDCLIB_filelist;
16 int fclose( struct _PDCLIB_file_t * stream )
18 struct _PDCLIB_file_t * current = _PDCLIB_filelist;
19 struct _PDCLIB_file_t * previous = NULL;
20 /* Checking that the FILE handle is actually one we had opened before. */
21 while ( current != NULL )
23 if ( stream == current )
26 if ( stream->status & _PDCLIB_FWRITE )
28 if ( _PDCLIB_flushbuffer( stream ) == EOF )
30 /* Flush failed, errno already set */
35 _PDCLIB_close( stream->handle );
36 /* Remove stream from list */
37 if ( previous != NULL )
39 previous->next = stream->next;
43 _PDCLIB_filelist = stream->next;
45 /* Delete tmpfile() */
46 if ( stream->status & _PDCLIB_DELONCLOSE )
48 remove( stream->filename );
51 if ( ! ( stream->status & _PDCLIB_STATIC ) )
58 current = current->next;
60 /* See the comments on implementation-defined errno values in
63 _PDCLIB_errno = _PDCLIB_ERROR;
71 #include "_PDCLIB_test.h"
76 struct _PDCLIB_file_t * file1;
77 struct _PDCLIB_file_t * file2;
80 TESTCASE( _PDCLIB_filelist == stdin );
81 TESTCASE( ( file1 = fopen( testfile1, "w" ) ) != NULL );
82 TESTCASE( _PDCLIB_filelist == file1 );
83 TESTCASE( ( file2 = fopen( testfile2, "w" ) ) != NULL );
84 TESTCASE( _PDCLIB_filelist == file2 );
85 TESTCASE( fclose( file2 ) == 0 );
86 TESTCASE( _PDCLIB_filelist == file1 );
87 TESTCASE( ( file2 = fopen( testfile1, "w" ) ) != NULL );
88 TESTCASE( _PDCLIB_filelist == file2 );
89 TESTCASE( fclose( file1 ) == 0 );
90 TESTCASE( _PDCLIB_filelist == file2 );
91 TESTCASE( fclose( file2 ) == 0 );
92 TESTCASE( _PDCLIB_filelist == stdin );
93 TESTCASE( remove( testfile1 ) == 0 );
94 TESTCASE( remove( testfile2 ) == 0 );
96 puts( " NOTEST fclose() test driver is PDCLib-specific." );