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