]> pd.if.org Git - pdclib.old/blob - functions/stdio/fclose.c
PDCLIB-27 #resolve fixed
[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 user buffer (SetVBuf allocated) */
59             if ( stream->status & _PDCLIB_FREEBUFFER )
60             {
61                 free( stream->buffer );
62             }
63             /* Free stream */
64             if ( ! ( stream->status & _PDCLIB_STATIC ) )
65             {
66                 free( stream );
67             }
68             return 0;
69         }
70         previous = current;
71         current = current->next;
72     }
73
74     errno = EINVAL;
75     return -1;
76 }
77
78 #endif
79
80 #ifdef TEST
81 #include <_PDCLIB_test.h>
82
83 int main( void )
84 {
85 #ifndef REGTEST
86     FILE * file1;
87     FILE * file2;
88     remove( testfile1 );
89     remove( testfile2 );
90     TESTCASE( _PDCLIB_filelist == stdin );
91     TESTCASE( ( file1 = fopen( testfile1, "w" ) ) != NULL );
92     TESTCASE( _PDCLIB_filelist == file1 );
93     TESTCASE( ( file2 = fopen( testfile2, "w" ) ) != NULL );
94     TESTCASE( _PDCLIB_filelist == file2 );
95     TESTCASE( fclose( file2 ) == 0 );
96     TESTCASE( _PDCLIB_filelist == file1 );
97     TESTCASE( ( file2 = fopen( testfile2, "w" ) ) != NULL );
98     TESTCASE( _PDCLIB_filelist == file2 );
99     TESTCASE( fclose( file1 ) == 0 );
100     TESTCASE( _PDCLIB_filelist == file2 );
101     TESTCASE( fclose( file2 ) == 0 );
102     TESTCASE( _PDCLIB_filelist == stdin );
103     TESTCASE( remove( testfile1 ) == 0 );
104     TESTCASE( remove( testfile2 ) == 0 );
105 #else
106     puts( " NOTEST fclose() test driver is PDCLib-specific." );
107 #endif
108     return TEST_RESULTS;
109 }
110
111 #endif
112