]> pd.if.org Git - pdclib.old/blob - functions/stdio/fclose.c
945241318afe09c8012d812412621ba50e13338c
[pdclib.old] / functions / stdio / fclose.c
1 /* $Id$ */
2
3 /* fclose( FILE * )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <errno.h>
12
13 #ifndef REGTEST
14 #include <_PDCLIB_io.h>
15 #include <threads.h>
16
17 extern FILE * _PDCLIB_filelist;
18
19 int fclose( FILE * stream )
20 {
21     FILE * current = _PDCLIB_filelist;
22     FILE * previous = NULL;
23     /* Checking that the FILE handle is actually one we had opened before. */
24     while ( current != NULL )
25     {
26         if ( stream == current )
27         {
28             /* Flush buffer */
29             if ( stream->status & _PDCLIB_FWRITE )
30             {
31                 if ( _PDCLIB_flushbuffer( stream ) == EOF )
32                 {
33                     /* Flush failed, errno already set */
34                     return EOF;
35                 }
36             }
37
38             /* Release mutex*/
39             mtx_destroy( &stream->lock );
40
41             /* Close handle */
42             stream->ops->close(stream->handle);
43
44             /* Remove stream from list */
45             if ( previous != NULL )
46             {
47                 previous->next = stream->next;
48             }
49             else
50             {
51                 _PDCLIB_filelist = stream->next;
52             }
53             /* Delete tmpfile() */
54             if ( stream->status & _PDCLIB_DELONCLOSE )
55             {
56                 remove( stream->filename );
57             }
58             /* Free stream */
59             if ( ! ( stream->status & _PDCLIB_STATIC ) )
60             {
61                 free( stream );
62             }
63             return 0;
64         }
65         previous = current;
66         current = current->next;
67     }
68
69     errno = EINVAL;
70     return -1;
71 }
72
73 #endif
74
75 #ifdef TEST
76 #include <_PDCLIB_test.h>
77
78 int main( void )
79 {
80 #ifndef REGTEST
81     FILE * file1;
82     FILE * file2;
83     remove( testfile1 );
84     remove( testfile2 );
85     TESTCASE( _PDCLIB_filelist == stdin );
86     TESTCASE( ( file1 = fopen( testfile1, "w" ) ) != NULL );
87     TESTCASE( _PDCLIB_filelist == file1 );
88     TESTCASE( ( file2 = fopen( testfile2, "w" ) ) != NULL );
89     TESTCASE( _PDCLIB_filelist == file2 );
90     TESTCASE( fclose( file2 ) == 0 );
91     TESTCASE( _PDCLIB_filelist == file1 );
92     TESTCASE( ( file2 = fopen( testfile2, "w" ) ) != NULL );
93     TESTCASE( _PDCLIB_filelist == file2 );
94     TESTCASE( fclose( file1 ) == 0 );
95     TESTCASE( _PDCLIB_filelist == file2 );
96     TESTCASE( fclose( file2 ) == 0 );
97     TESTCASE( _PDCLIB_filelist == stdin );
98     TESTCASE( remove( testfile1 ) == 0 );
99     TESTCASE( remove( testfile2 ) == 0 );
100 #else
101     puts( " NOTEST fclose() test driver is PDCLib-specific." );
102 #endif
103     return TEST_RESULTS;
104 }
105
106 #endif
107