ReactOS 0.4.16-dev-835-gd769f56
fclose.cpp
Go to the documentation of this file.
1//
2// fclose.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Defines fclose(), which closes a stdio stream.
7//
10
11
12
13// Closes a stdio stream after flushing the stream and freeing any buffer
14// associated with the stream (unless the buffer was set with setbuf). Returns
15// zero on success; EOF on failure (e.g., if the flush fails, or it is not a
16// valid file or the file is not open, etc.).
17static int __cdecl _fclose_nolock_internal(FILE* const public_stream, __crt_cached_ptd_host& ptd)
18{
19 __crt_stdio_stream const stream(public_stream);
20
22
23 int result = EOF;
24
25 if (stream.is_in_use())
26 {
27 result = __acrt_stdio_flush_nolock(stream.public_stream(), ptd);
29
30 if (_close_internal(_fileno(stream.public_stream()), ptd) < 0)
31 {
32 result = EOF;
33 }
34 else if (stream->_tmpfname != nullptr)
35 {
36 _free_crt(stream->_tmpfname);
37 stream->_tmpfname = nullptr;
38 }
39 }
40
42
43 return result;
44}
45
46extern "C" int __cdecl _fclose_nolock(FILE* const public_stream)
47{
48 __crt_cached_ptd_host ptd;
49 return _fclose_nolock_internal(public_stream, ptd);
50}
51
52static int __cdecl _fclose_internal(FILE* const public_stream, __crt_cached_ptd_host& ptd)
53{
54 __crt_stdio_stream const stream(public_stream);
55
57
58 // If the stream is backed by a string, it requires no synchronization,
59 // flushing, etc., so we can simply free it, which resets all of its
60 // data to the defaults.
61 if (stream.is_string_backed())
62 {
64 return EOF;
65 }
66
67 int return_value = 0;
68
69 _lock_file(stream.public_stream());
70 __try
71 {
72 return_value = _fclose_nolock_internal(stream.public_stream(), ptd);
73 }
75 {
76 _unlock_file(stream.public_stream());
77 }
79
80 return return_value;
81}
82
83extern "C" int __cdecl fclose(FILE* const public_stream)
84{
85 __crt_cached_ptd_host ptd;
86 return _fclose_internal(public_stream, ptd);
87}
void __cdecl __acrt_stdio_free_buffer_nolock(FILE *const public_stream)
Definition: _freebuf.cpp:15
#define EINVAL
Definition: acclib.h:90
#define __cdecl
Definition: accygwin.h:79
_Check_return_opt_ int __cdecl _close_internal(_In_ int _FileHandle, _Inout_ __crt_cached_ptd_host &_Ptd)
#define _UCRT_VALIDATE_RETURN(ptd, expr, errorcode, retexpr)
void __cdecl __acrt_stdio_free_stream(__crt_stdio_stream _Stream)
Definition: stream.cpp:97
_In_ size_t const _In_ int _In_ bool const _In_ unsigned const _In_ __acrt_rounding_mode const _Inout_ __crt_cached_ptd_host & ptd
Definition: cvt.cpp:355
int __cdecl fclose(FILE *const public_stream)
Definition: fclose.cpp:83
int __cdecl _fclose_nolock(FILE *const public_stream)
Definition: fclose.cpp:46
static int __cdecl _fclose_nolock_internal(FILE *const public_stream, __crt_cached_ptd_host &ptd)
Definition: fclose.cpp:17
static int __cdecl _fclose_internal(FILE *const public_stream, __crt_cached_ptd_host &ptd)
Definition: fclose.cpp:52
int __cdecl __acrt_stdio_flush_nolock(FILE *const public_stream, __crt_cached_ptd_host &ptd)
Definition: fflush.cpp:222
GLuint64EXT * result
Definition: glext.h:11304
_Check_return_ _CRTIMP int __cdecl _fileno(_In_ FILE *_File)
#define EOF
Definition: stdio.h:24
_CRTIMP void __cdecl _unlock_file(_Inout_ FILE *_File)
_CRTIMP void __cdecl _lock_file(_Inout_ FILE *_File)
#define _free_crt
#define __try
Definition: pseh2_64.h:172
#define __endtry
Definition: pseh2_64.h:175
#define __finally
Definition: pseh2_64.h:174
Definition: parse.h:23