xref: /reactos/sdk/lib/ucrt/stdio/rmtmp.cpp (revision e3e520d1)
1 //
2 // rmtmp.cpp
3 //
4 //      Copyright (c) Microsoft Corporation.  All rights reserved.
5 //
6 // Defines _rmtmp(), which closes and removes all temporary files created by
7 // tmpfile()
8 //
9 #include <corecrt_internal_stdio.h>
10 
11 
12 
13 _CRT_LINKER_FORCE_INCLUDE(__acrt_tmpfile_terminator);
14 
15 
16 
17 // These definitions will cause this object to be linked in whenever the
18 // termination code requires it.
19 #ifndef CRTDLL
20     extern "C" { unsigned __acrt_tmpfile_used = 1; }
21 #endif
22 
23 extern "C" { unsigned _tempoff = 1; }
24 extern "C" { unsigned _old_pfxlen = 0; }
25 
26 
27 
28 // Closes and removes all temporary files created by tmpfile().  Returns the
29 // number of streams that were closed.
30 extern "C" int __cdecl _rmtmp()
31 {
32     int count = 0;
33 
34     __acrt_lock(__acrt_stdio_index_lock);
35     __try
36     {
37         for (int i = 0; i != _nstream; ++i)
38         {
39             __crt_stdio_stream stream(__piob[i]);
40 
41             if (!stream.valid())
42                 continue;
43 
44             _lock_file(stream.public_stream());
45             __try
46             {
47                 if (!stream.is_in_use())
48                     __leave;
49 
50                 if (stream->_tmpfname == nullptr)
51                     __leave;
52 
53                 // The stream is still in use.  Close it:
54                 _fclose_nolock(stream.public_stream());
55                 ++count;
56             }
57             __finally
58             {
59                 _unlock_file(stream.public_stream());
60             }
61             __endtry
62         }
63     }
64     __finally
65     {
66         __acrt_unlock(__acrt_stdio_index_lock);
67     }
68     __endtry
69 
70     return count;
71 }
72 
73 extern "C" void __cdecl __acrt_uninitialize_tmpfile()
74 {
75     __acrt_stdio_free_tmpfile_name_buffers_nolock();
76     _rmtmp();
77 }
78