]> pd.if.org Git - pdclib.old/blob - functions/stdio/fclose.c
PDCLIB-15: fopen initializes stream lock; fclose releases
[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_glue.h>
15
16 extern struct _PDCLIB_file_t * _PDCLIB_filelist;
17
18 int fclose( struct _PDCLIB_file_t * stream )
19 {
20     struct _PDCLIB_file_t * current = _PDCLIB_filelist;
21     struct _PDCLIB_file_t * previous = NULL;
22     /* Checking that the FILE handle is actually one we had opened before. */
23     while ( current != NULL )
24     {
25         if ( stream == current )
26         {
27             /* Flush buffer */
28             if ( stream->status & _PDCLIB_FWRITE )
29             {
30                 if ( _PDCLIB_flushbuffer( stream ) == EOF )
31                 {
32                     /* Flush failed, errno already set */
33                     return EOF;
34                 }
35             }
36
37             /* Release mutex*/
38             mtx_destroy( &stream->lock );
39
40             /* Close handle */
41             _PDCLIB_close( stream->handle );
42
43             /* Remove stream from list */
44             if ( previous != NULL )
45             {
46                 previous->next = stream->next;
47             }
48             else
49             {
50                 _PDCLIB_filelist = stream->next;
51             }
52             /* Delete tmpfile() */
53             if ( stream->status & _PDCLIB_DELONCLOSE )
54             {
55                 remove( stream->filename );
56             }
57             /* Free stream */
58             if ( ! ( stream->status & _PDCLIB_STATIC ) )
59             {
60                 free( stream );
61             }
62             return 0;
63         }
64         previous = current;
65         current = current->next;
66     }
67
68     errno = EINVAL;
69     return -1;
70 }
71
72 #endif
73
74 #ifdef TEST
75 #include <_PDCLIB_test.h>
76
77 int main( void )
78 {
79 #ifndef REGTEST
80     struct _PDCLIB_file_t * file1;
81     struct _PDCLIB_file_t * file2;
82     remove( testfile1 );
83     remove( testfile2 );
84     TESTCASE( _PDCLIB_filelist == stdin );
85     TESTCASE( ( file1 = fopen( testfile1, "w" ) ) != NULL );
86     TESTCASE( _PDCLIB_filelist == file1 );
87     TESTCASE( ( file2 = fopen( testfile2, "w" ) ) != NULL );
88     TESTCASE( _PDCLIB_filelist == file2 );
89     TESTCASE( fclose( file2 ) == 0 );
90     TESTCASE( _PDCLIB_filelist == file1 );
91     TESTCASE( ( file2 = fopen( testfile2, "w" ) ) != NULL );
92     TESTCASE( _PDCLIB_filelist == file2 );
93     TESTCASE( fclose( file1 ) == 0 );
94     TESTCASE( _PDCLIB_filelist == file2 );
95     TESTCASE( fclose( file2 ) == 0 );
96     TESTCASE( _PDCLIB_filelist == stdin );
97     TESTCASE( remove( testfile1 ) == 0 );
98     TESTCASE( remove( testfile2 ) == 0 );
99 #else
100     puts( " NOTEST fclose() test driver is PDCLib-specific." );
101 #endif
102     return TEST_RESULTS;
103 }
104
105 #endif
106