]> pd.if.org Git - pdclib/blob - functions/stdio/fclose.c
Synced fclose().
[pdclib] / functions / stdio / fclose.c
1 /* fclose( FILE * )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #ifndef REGTEST
11
12 #include "_PDCLIB_glue.h"
13
14 extern struct _PDCLIB_file_t * _PDCLIB_filelist;
15
16 int fclose( struct _PDCLIB_file_t * stream )
17 {
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 )
22     {
23         if ( stream == current )
24         {
25             /* Flush buffer */
26             if ( stream->status & _PDCLIB_FWRITE )
27             {
28                 if ( _PDCLIB_flushbuffer( stream ) == EOF )
29                 {
30                     /* Flush failed, errno already set */
31                     return EOF;
32                 }
33             }
34             /* Close handle */
35             _PDCLIB_close( stream->handle );
36             /* Remove stream from list */
37             if ( previous != NULL )
38             {
39                 previous->next = stream->next;
40             }
41             else
42             {
43                 _PDCLIB_filelist = stream->next;
44             }
45             /* Delete tmpfile() */
46             if ( stream->status & _PDCLIB_DELONCLOSE )
47             {
48                 remove( stream->filename );
49             }
50             /* Free user buffer (SetVBuf allocated) */
51             if ( stream->status & _PDCLIB_FREEBUFFER )
52             {
53                 free( stream->buffer );
54             }
55             /* Free stream */
56             if ( ! ( stream->status & _PDCLIB_STATIC ) )
57             {
58                 free( stream );
59             }
60             return 0;
61         }
62         previous = current;
63         current = current->next;
64     }
65     /* See the comments on implementation-defined errno values in
66        <_PDCLIB_config.h>.
67     */
68     _PDCLIB_errno = _PDCLIB_ERROR;
69     return -1;
70 }
71
72 #endif
73
74 #ifdef TEST
75
76 #include "_PDCLIB_test.h"
77
78 int main( void )
79 {
80 #ifndef REGTEST
81     struct _PDCLIB_file_t * file1;
82     struct _PDCLIB_file_t * 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