]> pd.if.org Git - pdclib.old/blob - functions/stdio/fclose.c
fclose.c: don't try to open testfile2 while already open; [win32] implement rename
[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             /* Close handle */
37             _PDCLIB_close( stream->handle );
38             /* Remove stream from list */
39             if ( previous != NULL )
40             {
41                 previous->next = stream->next;
42             }
43             else
44             {
45                 _PDCLIB_filelist = stream->next;
46             }
47             /* Delete tmpfile() */
48             if ( stream->status & _PDCLIB_DELONCLOSE )
49             {
50                 remove( stream->filename );
51             }
52             /* Free stream */
53             if ( ! ( stream->status & _PDCLIB_STATIC ) )
54             {
55                 free( stream );
56             }
57             return 0;
58         }
59         previous = current;
60         current = current->next;
61     }
62
63     errno = EINVAL;
64     return -1;
65 }
66
67 #endif
68
69 #ifdef TEST
70 #include <_PDCLIB_test.h>
71
72 int main( void )
73 {
74 #ifndef REGTEST
75     struct _PDCLIB_file_t * file1;
76     struct _PDCLIB_file_t * file2;
77     remove( testfile1 );
78     remove( testfile2 );
79     TESTCASE( _PDCLIB_filelist == stdin );
80     TESTCASE( ( file1 = fopen( testfile1, "w" ) ) != NULL );
81     TESTCASE( _PDCLIB_filelist == file1 );
82     TESTCASE( ( file2 = fopen( testfile2, "w" ) ) != NULL );
83     TESTCASE( _PDCLIB_filelist == file2 );
84     TESTCASE( fclose( file2 ) == 0 );
85     TESTCASE( _PDCLIB_filelist == file1 );
86     TESTCASE( ( file2 = fopen( testfile2, "w" ) ) != NULL );
87     TESTCASE( _PDCLIB_filelist == file2 );
88     TESTCASE( fclose( file1 ) == 0 );
89     TESTCASE( _PDCLIB_filelist == file2 );
90     TESTCASE( fclose( file2 ) == 0 );
91     TESTCASE( _PDCLIB_filelist == stdin );
92     TESTCASE( remove( testfile1 ) == 0 );
93     TESTCASE( remove( testfile2 ) == 0 );
94 #else
95     puts( " NOTEST fclose() test driver is PDCLib-specific." );
96 #endif
97     return TEST_RESULTS;
98 }
99
100 #endif
101