ReactOS 0.4.16-dev-736-g28b802b
tempnam.cpp File Reference
Include dependency graph for tempnam.cpp:

Go to the source code of this file.

Functions

template<typename Character >
static Character const *__cdecl strip_quotes (Character const *const source) throw ()
 
template<typename Character >
static Character const *__cdecl get_tmp_directory () throw ()
 
template<typename Character >
static Character const *__cdecl get_directory (Character const *const alternative, Character const **const result) throw ()
 
template<typename Character >
static bool __cdecl compute_name (Character const *const path_buffer, Character *const suffix_pointer, size_t const suffix_count, size_t const prefix_length) throw ()
 
template<typename Character >
 _Success_ (return !=0) static Character *__cdecl common_tempnam(Character const *const alternative
 
Character const *const int const char const *const int const line_number throw ()
 
char *__cdecl _tempnam (char const *const alternative, char const *const prefix)
 
wchar_t *__cdecl _wtempnam (wchar_t const *const alternative, wchar_t const *const prefix)
 

Variables

Character const *const prefix
 
Character const *const int const block_use
 
Character const *const int const char const *const file_name
 

Function Documentation

◆ _Success_()

template<typename Character >
_Success_ ( return = 0) const

◆ _tempnam()

char *__cdecl _tempnam ( char const *const  alternative,
char const *const  prefix 
)

Definition at line 255 of file tempnam.cpp.

259{
260 return common_tempnam(alternative, prefix, _NORMAL_BLOCK, nullptr, 0);
261}
#define _NORMAL_BLOCK
Definition: crtdbg.h:67
Character const *const prefix
Definition: tempnam.cpp:195

◆ _wtempnam()

wchar_t *__cdecl _wtempnam ( wchar_t const *const  alternative,
wchar_t const *const  prefix 
)

Definition at line 263 of file tempnam.cpp.

267{
268 return common_tempnam(alternative, prefix, _NORMAL_BLOCK, nullptr, 0);
269}

◆ compute_name()

template<typename Character >
static bool __cdecl compute_name ( Character const *const  path_buffer,
Character *const  suffix_pointer,
size_t const  suffix_count,
size_t const  prefix_length 
)
throw (
)
static

Definition at line 120 of file tempnam.cpp.

126{
127 typedef __acrt_stdio_char_traits<Character> stdio_traits;
128
129 // Re-initialize _tempoff if necessary. If we don't re-init _tempoff, we
130 // can get into an infinate loop (e.g., (a) _tempoff is a big number on
131 // entry, (b) prefix is a long string (e.g., 8 chars) and all tempfiles
132 // with that prefix exist, (c) _tempoff will never equal first and we'll
133 // loop forever).
134
135 // [NOTE: To avoid a conflict that causes the same bug as that discussed
136 // above, _tempnam() uses _tempoff; tmpnam() uses _tmpoff]
137
138 bool return_value = false;
139
141 __try
142 {
143 if (_old_pfxlen < prefix_length)
144 _tempoff = 1;
145
146 _old_pfxlen = static_cast<unsigned int>(prefix_length);
147
148 unsigned const first = _tempoff;
149
150 errno_t const saved_errno = errno;
151 do
152 {
153 ++_tempoff;
154 if (_tempoff - first > _TMP_MAX_S)
155 {
156 errno = saved_errno;
157 __leave;
158 }
159
160 // The maximum length string returned by the conversion is ten
161 // characters, assuming a 32-bit unsigned integer, so there is
162 // sufficient room in the result buffer for it.
163 _ERRCHECK(stdio_traits::ultot_s(_tempoff, suffix_pointer, suffix_count, 10));
164 errno = 0;
165 }
166 while (stdio_traits::taccess_s(path_buffer, 0) == 0 || errno == EACCES);
167
168 errno = saved_errno;
169 return_value = true;
170 }
172 {
174 }
176
177 return return_value;
178}
#define EACCES
Definition: acclib.h:85
void __cdecl __acrt_unlock(_In_ __acrt_lock_id lock)
Definition: locks.cpp:57
@ __acrt_tempnam_lock
#define _ERRCHECK(e)
__acrt_lock(__acrt_heap_lock)
const GLint * first
Definition: glext.h:5794
#define __try
Definition: pseh2_64.h:172
#define __leave
Definition: pseh2_64.h:176
#define __endtry
Definition: pseh2_64.h:175
#define __finally
Definition: pseh2_64.h:174
unsigned _tempoff
Definition: rmtmp.cpp:23
unsigned _old_pfxlen
Definition: rmtmp.cpp:24
#define errno
Definition: errno.h:18
int errno_t
Definition: corecrt.h:615

Referenced by throw().

◆ get_directory()

template<typename Character >
static Character const *__cdecl get_directory ( Character const *const  alternative,
Character const **const  result 
)
throw (
)
static

Definition at line 77 of file tempnam.cpp.

81{
82 typedef __acrt_stdio_char_traits<Character> stdio_traits;
83
84 __crt_unique_heap_ptr<Character const> tmp(get_tmp_directory<Character>());
85 if (tmp.get() != nullptr)
86 {
87 if (stdio_traits::taccess_s(tmp.get(), 0) == 0)
88 return *result = tmp.detach();
89
90 // Otherwise, try stripping quotes out of the TMP path and check again:
91 __crt_unique_heap_ptr<Character const> unquoted_tmp(strip_quotes(tmp.get()));
92 if (unquoted_tmp.get() != nullptr && stdio_traits::taccess_s(unquoted_tmp.get(), 0) == 0)
93 return *result = unquoted_tmp.detach();
94 }
95
96 // Otherwise, the TMP path is not usable; use the alternative path if one
97 // was provided and is accessible:
98 if (alternative != nullptr && stdio_traits::taccess_s(alternative, 0) == 0)
99 return (*result = alternative), nullptr;
100
101 // Otherwise, fall back to \ or .:
102 static Character const root_fallback[] = { '\\', '\0' };
103 static Character const cwd_fallback [] = { '.', '\0' };
104
105 if (stdio_traits::taccess_s(root_fallback, 0) == 0)
106 return (*result = root_fallback), nullptr;
107
108 return (*result = cwd_fallback), nullptr;
109}
GLuint64EXT * result
Definition: glext.h:11304
static Character const *__cdecl strip_quotes(Character const *const source)
Definition: tempnam.cpp:17

Referenced by throw().

◆ get_tmp_directory()

template<typename Character >
static Character const *__cdecl get_tmp_directory ( )
throw (
)
static

Definition at line 56 of file tempnam.cpp.

57{
58 typedef __acrt_stdio_char_traits<Character> stdio_traits;
59
60 static Character const tmp_name[] = { 'T', 'M', 'P', '\0' };
61
62 Character* tmp_value = nullptr;
63 if (_ERRCHECK_EINVAL(stdio_traits::tdupenv_s_crt(&tmp_value, nullptr, tmp_name)) != 0)
64 return nullptr;
65
66 return tmp_value;
67}
#define _ERRCHECK_EINVAL(e)
static char * tmp_name
Definition: cache.c:24

◆ strip_quotes()

template<typename Character >
static Character const *__cdecl strip_quotes ( Character const *const  source)
throw (
)
static

Definition at line 17 of file tempnam.cpp.

18{
19 // Count the number of quotation marks in the string, and compute the length
20 // of the string, in case we need to allocate a new string:
21 size_t quote_count = 0;
22 size_t source_length = 0;
23 for (Character const* it = source; *it; ++it)
24 {
25 if (*it == '\"')
26 ++quote_count;
27
28 ++source_length;
29 }
30
31 // No quotes? No problem!
32 if (quote_count == 0)
33 return nullptr;
34
35 size_t const destination_length = source_length - quote_count + 1;
36 __crt_unique_heap_ptr<Character> destination(_calloc_crt_t(Character, destination_length));
37 if (destination.get() == nullptr)
38 return nullptr;
39
40 // Copy the string, stripping quotation marks:
41 Character* destination_it = destination.get();
42 for (Character const* source_it = source; *source_it; ++source_it)
43 {
44 if (*source_it == '\"')
45 continue;
46
47 *destination_it++ = *source_it;
48 }
49
50 *destination_it = '\0';
51 return destination.detach();
52}

Referenced by get_directory().

◆ throw()

Definition at line 199 of file tempnam.cpp.

200{
201 // These are referenced only in the Debug CRT build
205
206 typedef __acrt_stdio_char_traits<Character> stdio_traits;
207
208 Character const* directory = nullptr;
209 __crt_unique_heap_ptr<Character const> const directory_cleanup(get_directory(alternative, &directory));
210
211 unsigned const prefix_length = prefix != nullptr
212 ? static_cast<unsigned>(stdio_traits::tcslen(prefix))
213 : 0;
214
215 // The 12 allows for a backslash, a ten character temporary string, and a
216 // null terminator.
217 unsigned const buffer_size = static_cast<unsigned>(stdio_traits::tcslen(directory)) + prefix_length + 12;
218
219 __crt_unique_heap_ptr<Character, __crt_public_free_policy> result(
220 static_cast<Character*>(_calloc_dbg(
222 sizeof(Character),
223 block_use,
224 file_name,
225 line_number)));
226
227 if (!result)
228 return nullptr;
229
230 *result.get() = 0;
231 _ERRCHECK(stdio_traits::tcscat_s(result.get(), buffer_size, directory));
232
234 {
235 static Character const backslash[] = { '\\', '\0' };
236 _ERRCHECK(stdio_traits::tcscat_s(result.get(), buffer_size, backslash));
237 }
238
239 if (prefix != nullptr)
240 {
241 _ERRCHECK(stdio_traits::tcscat_s(result.get(), buffer_size, prefix));
242 }
243
244 Character* const ptr = result.get() + stdio_traits::tcslen(result.get());
245 size_t const ptr_size = buffer_size - (ptr - result.get());
246
247 if (!compute_name(result.get(), ptr, ptr_size, prefix_length))
248 return nullptr;
249
250 return result.detach();
251}
bool __cdecl __crt_stdio_path_requires_backslash(char const *const first)
#define _calloc_dbg(c, s, t, f, l)
Definition: crtdbg.h:205
int const char const *const int const line_number
Definition: debug_heap.cpp:499
static PVOID ptr
Definition: dispmode.c:27
static const DWORD ptr_size
Definition: registry.c:42
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:325
wchar_t const *const size_t const buffer_size
Definition: stat.cpp:95
static bool __cdecl compute_name(Character const *const path_buffer, Character *const suffix_pointer, size_t const suffix_count, size_t const prefix_length)
Definition: tempnam.cpp:120
Character const *const int const block_use
Definition: tempnam.cpp:196
static Character const *__cdecl get_directory(Character const *const alternative, Character const **const result)
Definition: tempnam.cpp:77
Character const *const int const char const *const file_name
Definition: tempnam.cpp:197

Variable Documentation

◆ block_use

Character const* const int const block_use

Definition at line 196 of file tempnam.cpp.

Referenced by throw().

◆ file_name

Character const* const int const char const* const file_name

Definition at line 197 of file tempnam.cpp.

Referenced by throw().

◆ prefix

Character const* const prefix

Definition at line 195 of file tempnam.cpp.

Referenced by _CEnumZipContents_CreateInstance(), _tempnam(), _wtempnam(), acpi_util_eval_error(), alloc_element(), append_string(), build_file_mask(), CertNameToStrA(), check_colorvalue_(), check_floats_(), check_vertex_buffer_(), chm_enumerate_dir(), compat_catpath(), contentHandler_endPrefixMapping(), contentHandler_startPrefixMapping(), create_storagefile(), CRYPT_AddPrefixA(), CRYPT_AddPrefixAToW(), CRYPT_AddPrefixW(), debugclient_SetOutputLinePrefix(), debugclient_SetOutputLinePrefixWide(), DECLARE_INTERFACE_(), declare_prefix(), DumpGroup(), DumpOwner(), endElementNsDebug(), enum_gac_assemblies(), ext4_extract_xattr_name(), ext4_get_xattr_name_prefix(), find(), format_float(), format_namespace(), get_declared_prefix_idx(), get_declared_prefix_uri(), get_dest_dir(), get_key_name_for_target(), get_temp_ini_path(), get_temp_path(), get_uri_from_prefix(), gif_compress(), init_paths(), CEnumZipContents::Initialize(), is_prefix_wa(), lzw_dict_add(), lzw_dict_lookup(), msi_get_temp_file_name(), nameNsPush(), namespacemanager_declarePrefix(), namespacemanager_getDeclaredPrefix(), namespacemanager_getPrefix(), namespacemanager_getURI(), ncacn_pipe_name(), ncalrpc_pipe_name(), CZipEnumerator::next_unique(), nsPush(), print_wait_status(), priv_script_write_params(), process_long_option(), processFile(), reader_add_attr(), reader_lookup_ns(), reader_parse_attribute(), reader_parse_element(), reader_parse_endtag(), reader_parse_qname(), reader_parse_stag(), reader_push_element(), reader_push_ns(), SAXContentHandler_endPrefixMapping(), SAXContentHandler_startPrefixMapping(), set_builder_component(), shader_generate_glsl_declarations(), shader_glsl_bufinfo(), shader_glsl_gather4(), shader_glsl_generate_pshader(), shader_glsl_generate_sm4_output_setup(), shader_glsl_get_register_name(), shader_glsl_init_uniform_block_bindings(), shader_glsl_ld_raw_structured(), shader_glsl_load_icb(), shader_glsl_load_images(), shader_glsl_load_samplers(), shader_glsl_load_samplers_range(), shader_glsl_scalar_op(), shader_glsl_store_raw_structured(), shader_glsl_uav_counter(), sources_entry_create(), startElementNsDebug(), __crt_stdio_output::output_processor< Character, OutputAdapter, ProcessorBase >::state_case_type(), str_startswith(), streamout(), test_common_stub_data(), test_communication(), test_CopyFile2(), test_CopyFileA(), test_CopyFileEx(), test_CopyFileW(), test_CreateFile2(), test_CreateFileA(), test_CreateFileW(), test_createNode(), test_filesourcefilter(), test_FindFirstChangeNotification(), test_GetAdaptersAddresses(), test_GetFinalPathNameByHandleA(), test_GetFinalPathNameByHandleW(), test_GetLongPathNameW(), test_iocp_callback(), test_MoveFileA(), test_MoveFileW(), test_read_element(), test_read_write(), test_ReplaceFileA(), test_ReplaceFileW(), test_SHCreateStreamOnFileEx_CopyTo(), test_WriteAttributeString(), test_WriteElementString(), test_WriteStartElement(), TEXT_SkipChars(), throw(), unpack_avi_file(), vbnamespacemanager_declarePrefix(), vbnamespacemanager_getURI(), vbnamespacemanager_getURIFromNode(), VBSAXContentHandler_endPrefixMapping(), VBSAXContentHandler_startPrefixMapping(), VfdOpenImage(), wined3d_debug_location(), WNetEnumCachedPasswords(), write_attribute_string(), write_client_call_routine(), write_element_string(), write_endpoints(), write_function_proto(), write_output_attribute(), write_output_qname(), write_raw_resources(), write_start_element(), writer_find_ns(), writer_find_ns_current(), writer_push_ns(), xmlAddDefAttrs(), xmlDictAddQString(), xmlDictComputeBigQKey(), xmlDictComputeFastQKey(), xmlDictQLookup(), xmlErrAttributeDup(), xmlGetDtdAttrDesc(), xmlGetDtdElementDesc(), xmlGetDtdElementDesc2(), xmlGetDtdQAttrDesc(), xmlGetDtdQElementDesc(), xmlGetNamespace(), xmlHashComputeQKey(), xmlHashQLookup(), xmlHashQLookup2(), xmlHashQLookup3(), xmlParseAttribute2(), xmlParseElementStart(), xmlParseQName(), xmlParseQNameAndCompare(), xmlParseStartTag2(), xmlreader_GetNamespaceUri(), xmlreader_GetPrefix(), xmlSAX2AttributeDecl(), xmlSAX2AttributeNs(), xmlSAX2StartElementNs(), xmlSplitQName(), xmlwriter_WriteAttributeString(), xmlwriter_WriteElementString(), xmlwriter_WriteStartElement(), xsltApplyAttributeSet(), xsltAttribute(), xsltAttributeComp(), xsltCompileStepPattern(), xsltElement(), xsltElementAvailableFunction(), xsltElementComp(), xsltFormatNumberConversion(), xsltFormatNumberFunction(), xsltFunctionAvailableFunction(), xsltKeyFunction(), xsltNewExtDef(), xsltParseStylesheetAttributeSet(), xsltParseStylesheetExcludePrefix(), xsltParseStylesheetExtPrefix(), xsltProcessUserParamInternal(), xsltRegisterExtPrefix(), xsltSplitQName(), xsltSystemPropertyFunction(), ZSTD_CCtx_refPrefix(), ZSTD_CCtx_refPrefix_advanced(), ZSTD_DCtx_refPrefix(), and ZSTD_DCtx_refPrefix_advanced().