]> pd.if.org Git - pdclib/blob - functions/stdio/fclose.c
Comment cleanups.
[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 #include <_PDCLIB_glue.h>
12
13 extern struct _PDCLIB_file_t * _PDCLIB_filelist;
14
15 int fclose( struct _PDCLIB_file_t * stream )
16 {
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 )
21     {
22         if ( stream == current )
23         {
24             /* Flush buffer */
25             if ( stream->status & _PDCLIB_FWRITE )
26             {
27                 if ( _PDCLIB_flushbuffer( stream ) == EOF )
28                 {
29                     /* Flush failed, errno already set */
30                     return EOF;
31                 }
32             }
33             /* Close handle */
34             _PDCLIB_close( stream->handle );
35             /* Remove stream from list */
36             if ( previous != NULL )
37             {
38                 previous->next = stream->next;
39             }
40             else
41             {
42                 _PDCLIB_filelist = stream->next;
43             }
44             /* Delete tmpfile() */
45             if ( stream->status & _PDCLIB_DELONCLOSE )
46             {
47                 remove( stream->filename );
48             }
49             /* Free stream */
50             if ( ! ( stream->status & _PDCLIB_STATIC ) )
51             {
52                 free( stream );
53             }
54             return 0;
55         }
56         previous = current;
57         current = current->next;
58     }
59     /* See the comments on implementation-defined errno values in
60        <_PDCLIB_config.h>.
61     */
62     _PDCLIB_errno = _PDCLIB_ERROR;
63     return -1;
64 }
65
66 #endif
67
68 #ifdef TEST
69 #include <_PDCLIB_test.h>
70
71 int main( void )
72 {
73 #ifndef REGTEST
74     struct _PDCLIB_file_t * file1;
75     struct _PDCLIB_file_t * file2;
76     remove( testfile1 );
77     remove( testfile2 );
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 );
93 #else
94     puts( " NOTEST fclose() test driver is PDCLib-specific." );
95 #endif
96     return TEST_RESULTS;
97 }
98
99 #endif
100