blob: 9a1388d520233756dfb23f5ede9bc50b308b29c0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
/* fclose( FILE * )
This file is part of the Public Domain C Library (PDCLib).
Permission is granted to use, modify, and / or redistribute at will.
*/
#include <stdio.h>
#include <stdlib.h>
#ifndef REGTEST
#include "pdclib/_PDCLIB_glue.h"
extern struct _PDCLIB_file_t * _PDCLIB_filelist;
int fclose( struct _PDCLIB_file_t * stream )
{
struct _PDCLIB_file_t * current = _PDCLIB_filelist;
struct _PDCLIB_file_t * previous = NULL;
/* Checking that the FILE handle is actually one we had opened before. */
while ( current != NULL )
{
if ( stream == current )
{
/* Flush buffer */
if ( stream->status & _PDCLIB_FWRITE )
{
if ( _PDCLIB_flushbuffer( stream ) == EOF )
{
/* Flush failed, errno already set */
return EOF;
}
}
/* Close handle */
_PDCLIB_close( stream->handle );
/* Remove stream from list */
if ( previous != NULL )
{
previous->next = stream->next;
}
else
{
_PDCLIB_filelist = stream->next;
}
/* Delete tmpfile() */
if ( stream->status & _PDCLIB_DELONCLOSE )
{
remove( stream->filename );
}
/* Free user buffer (SetVBuf allocated) */
if ( stream->status & _PDCLIB_FREEBUFFER )
{
free( stream->buffer );
}
/* Free stream */
if ( ! ( stream->status & _PDCLIB_STATIC ) )
{
free( stream );
}
return 0;
}
previous = current;
current = current->next;
}
/* See the comments on implementation-defined errno values in
<_PDCLIB_config.h>.
*/
_PDCLIB_errno = _PDCLIB_ERROR;
return -1;
}
#endif
#ifdef TEST
#include "_PDCLIB_test.h"
int main( void )
{
#ifndef REGTEST
struct _PDCLIB_file_t * file1;
struct _PDCLIB_file_t * file2;
remove( testfile1 );
remove( testfile2 );
TESTCASE( _PDCLIB_filelist == stdin );
TESTCASE( ( file1 = fopen( testfile1, "w" ) ) != NULL );
TESTCASE( _PDCLIB_filelist == file1 );
TESTCASE( ( file2 = fopen( testfile2, "w" ) ) != NULL );
TESTCASE( _PDCLIB_filelist == file2 );
TESTCASE( fclose( file2 ) == 0 );
TESTCASE( _PDCLIB_filelist == file1 );
TESTCASE( ( file2 = fopen( testfile2, "w" ) ) != NULL );
TESTCASE( _PDCLIB_filelist == file2 );
TESTCASE( fclose( file1 ) == 0 );
TESTCASE( _PDCLIB_filelist == file2 );
TESTCASE( fclose( file2 ) == 0 );
TESTCASE( _PDCLIB_filelist == stdin );
TESTCASE( remove( testfile1 ) == 0 );
TESTCASE( remove( testfile2 ) == 0 );
#else
puts( " NOTEST fclose() test driver is PDCLib-specific." );
#endif
return TEST_RESULTS;
}
#endif
|