Reactos
at master 45 lines 1.1 kB view raw
1// 2// closeall.cpp 3// 4// Copyright (c) Microsoft Corporation. All rights reserved. 5// 6// Defines _fcloseall(), which closes all opened files. 7// 8#include <corecrt_internal_stdio.h> 9 10 11 12// Closes all streams currently open except for stdin, stdout, and stderr. All 13// tmpfile()-created streams are closed as well. Returns EOF on failure; returns 14// the number of closed streams on success. 15extern "C" int __cdecl _fcloseall() 16{ 17 int count = 0; 18 19 __acrt_lock(__acrt_stdio_index_lock); 20 __try 21 { 22 for (int i = _IOB_ENTRIES; i != _nstream; ++i) 23 { 24 if (__piob[i] == nullptr) 25 continue; 26 27 // If the stream is in use, close it: 28 if (__crt_stdio_stream(__piob[i]).is_in_use() && fclose(&__piob[i]->_public_file) != EOF) 29 { 30 ++count; 31 } 32 33 DeleteCriticalSection(&__piob[i]->_lock); 34 _free_crt(__piob[i]); 35 __piob[i] = nullptr; 36 } 37 } 38 __finally 39 { 40 __acrt_unlock(__acrt_stdio_index_lock); 41 } 42 __endtry 43 44 return count; 45}