3 This file is part of the Public Domain C Library (PDCLib).
4 Permission is granted to use, modify, and / or redistribute at will.
11 #include <_PDCLIB_glue.h>
13 extern struct _PDCLIB_file_t * _PDCLIB_filelist;
15 int fclose( struct _PDCLIB_file_t * stream )
17 struct _PDCLIB_file_t * current = _PDCLIB_filelist;
18 struct _PDCLIB_file_t * previous = NULL;
19 /* Checking that the FILE handle is actually one we had opened before. */
20 while ( current != NULL )
22 if ( stream == current )
25 if ( stream->status & _PDCLIB_FWRITE )
27 if ( _PDCLIB_flushbuffer( stream ) == EOF )
29 /* Flush failed, errno already set */
34 _PDCLIB_close( stream->handle );
35 /* Remove stream from list */
36 if ( previous != NULL )
38 previous->next = stream->next;
42 _PDCLIB_filelist = stream->next;
44 /* Delete tmpfile() */
45 if ( stream->status & _PDCLIB_DELONCLOSE )
47 remove( stream->filename );
50 if ( ! ( stream->status & _PDCLIB_STATIC ) )
57 current = current->next;
59 /* See the comments on implementation-defined errno values in
62 _PDCLIB_errno = _PDCLIB_ERROR;
69 #include <_PDCLIB_test.h>
74 struct _PDCLIB_file_t * file1;
75 struct _PDCLIB_file_t * file2;
78 TESTCASE( _PDCLIB_filelist == stdin );
79 TESTCASE( ( file1 = fopen( testfile1, "w" ) ) != NULL );
80 TESTCASE( _PDCLIB_filelist == file1 );
81 TESTCASE( ( file2 = fopen( testfile2, "w" ) ) != NULL );
82 TESTCASE( _PDCLIB_filelist == file2 );
83 TESTCASE( fclose( file2 ) == 0 );
84 TESTCASE( _PDCLIB_filelist == file1 );
85 TESTCASE( ( file2 = fopen( testfile1, "w" ) ) != NULL );
86 TESTCASE( _PDCLIB_filelist == file2 );
87 TESTCASE( fclose( file1 ) == 0 );
88 TESTCASE( _PDCLIB_filelist == file2 );
89 TESTCASE( fclose( file2 ) == 0 );
90 TESTCASE( _PDCLIB_filelist == stdin );
91 TESTCASE( remove( testfile1 ) == 0 );
92 TESTCASE( remove( testfile2 ) == 0 );
94 puts( " NOTEST fclose() test driver is PDCLib-specific." );