|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal_stdio.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| template <typename Character>
|
| static Character const* __cdecl strip_quotes(Character const* const source) throw()
|
| {
|
|
|
|
|
| size_t quote_count = 0;
|
| size_t source_length = 0;
|
| for (Character const* it = source; *it; ++it)
|
| {
|
| if (*it == '\"')
|
| ++quote_count;
|
|
|
| ++source_length;
|
| }
|
|
|
|
|
| if (quote_count == 0)
|
| return nullptr;
|
|
|
| size_t const destination_length = source_length - quote_count + 1;
|
| __crt_unique_heap_ptr<Character> destination(_calloc_crt_t(Character, destination_length));
|
| if (destination.get() == nullptr)
|
| return nullptr;
|
|
|
|
|
| Character* destination_it = destination.get();
|
| for (Character const* source_it = source; *source_it; ++source_it)
|
| {
|
| if (*source_it == '\"')
|
| continue;
|
|
|
| *destination_it++ = *source_it;
|
| }
|
|
|
| *destination_it = '\0';
|
| return destination.detach();
|
| }
|
|
|
|
|
| template <typename Character>
|
| static Character const* __cdecl get_tmp_directory() throw()
|
| {
|
| typedef __acrt_stdio_char_traits<Character> stdio_traits;
|
|
|
| static Character const tmp_name[] = { 'T', 'M', 'P', '\0' };
|
|
|
| Character* tmp_value = nullptr;
|
| if (_ERRCHECK_EINVAL(stdio_traits::tdupenv_s_crt(&tmp_value, nullptr, tmp_name)) != 0)
|
| return nullptr;
|
|
|
| return tmp_value;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| template <typename Character>
|
| static Character const* __cdecl get_directory(
|
| Character const* const alternative,
|
| Character const** const result
|
| ) throw()
|
| {
|
| typedef __acrt_stdio_char_traits<Character> stdio_traits;
|
|
|
| __crt_unique_heap_ptr<Character const> tmp(get_tmp_directory<Character>());
|
| if (tmp.get() != nullptr)
|
| {
|
| if (stdio_traits::taccess_s(tmp.get(), 0) == 0)
|
| return *result = tmp.detach();
|
|
|
|
|
| __crt_unique_heap_ptr<Character const> unquoted_tmp(strip_quotes(tmp.get()));
|
| if (unquoted_tmp.get() != nullptr && stdio_traits::taccess_s(unquoted_tmp.get(), 0) == 0)
|
| return *result = unquoted_tmp.detach();
|
| }
|
|
|
|
|
|
|
| if (alternative != nullptr && stdio_traits::taccess_s(alternative, 0) == 0)
|
| return (*result = alternative), nullptr;
|
|
|
|
|
| static Character const root_fallback[] = { '\\', '\0' };
|
| static Character const cwd_fallback [] = { '.', '\0' };
|
|
|
| if (stdio_traits::taccess_s(root_fallback, 0) == 0)
|
| return (*result = root_fallback), nullptr;
|
|
|
| return (*result = cwd_fallback), nullptr;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|
| {
|
| typedef __acrt_stdio_char_traits<Character> stdio_traits;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| bool return_value = false;
|
|
|
| __acrt_lock(__acrt_tempnam_lock);
|
| __try
|
| {
|
| if (_old_pfxlen < prefix_length)
|
| _tempoff = 1;
|
|
|
| _old_pfxlen = static_cast<unsigned int>(prefix_length);
|
|
|
| unsigned const first = _tempoff;
|
|
|
| errno_t const saved_errno = errno;
|
| do
|
| {
|
| ++_tempoff;
|
| if (_tempoff - first > _TMP_MAX_S)
|
| {
|
| errno = saved_errno;
|
| __leave;
|
| }
|
|
|
|
|
|
|
|
|
| _ERRCHECK(stdio_traits::ultot_s(_tempoff, suffix_pointer, suffix_count, 10));
|
| errno = 0;
|
| }
|
| while (stdio_traits::taccess_s(path_buffer, 0) == 0 || errno == EACCES);
|
|
|
| errno = saved_errno;
|
| return_value = true;
|
| }
|
| __finally
|
| {
|
| __acrt_unlock(__acrt_tempnam_lock);
|
| }
|
|
|
| return return_value;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| template <typename Character>
|
| _Success_(return != 0)
|
| static Character* __cdecl common_tempnam(
|
| Character const* const alternative,
|
| Character const* const prefix,
|
| int const block_use,
|
| char const* const file_name,
|
| int const line_number
|
| ) throw()
|
| {
|
|
|
| UNREFERENCED_PARAMETER(block_use);
|
| UNREFERENCED_PARAMETER(file_name);
|
| UNREFERENCED_PARAMETER(line_number);
|
|
|
| typedef __acrt_stdio_char_traits<Character> stdio_traits;
|
|
|
| Character const* directory = nullptr;
|
| __crt_unique_heap_ptr<Character const> const directory_cleanup(get_directory(alternative, &directory));
|
|
|
| unsigned const prefix_length = prefix != nullptr
|
| ? static_cast<unsigned>(stdio_traits::tcslen(prefix))
|
| : 0;
|
|
|
|
|
|
|
| unsigned const buffer_size = static_cast<unsigned>(stdio_traits::tcslen(directory)) + prefix_length + 12;
|
|
|
| __crt_unique_heap_ptr<Character, __crt_public_free_policy> result(
|
| static_cast<Character*>(_calloc_dbg(
|
| buffer_size,
|
| sizeof(Character),
|
| block_use,
|
| file_name,
|
| line_number)));
|
|
|
| if (!result)
|
| return nullptr;
|
|
|
| *result.get() = 0;
|
| _ERRCHECK(stdio_traits::tcscat_s(result.get(), buffer_size, directory));
|
|
|
| if (__crt_stdio_path_requires_backslash(directory))
|
| {
|
| static Character const backslash[] = { '\\', '\0' };
|
| _ERRCHECK(stdio_traits::tcscat_s(result.get(), buffer_size, backslash));
|
| }
|
|
|
| if (prefix != nullptr)
|
| {
|
| _ERRCHECK(stdio_traits::tcscat_s(result.get(), buffer_size, prefix));
|
| }
|
|
|
| Character* const ptr = result.get() + stdio_traits::tcslen(result.get());
|
| size_t const ptr_size = buffer_size - (ptr - result.get());
|
|
|
| if (!compute_name(result.get(), ptr, ptr_size, prefix_length))
|
| return nullptr;
|
|
|
| return result.detach();
|
| }
|
|
|
|
|
|
|
| extern "C" char* __cdecl _tempnam(
|
| char const* const alternative,
|
| char const* const prefix
|
| )
|
| {
|
| return common_tempnam(alternative, prefix, _NORMAL_BLOCK, nullptr, 0);
|
| }
|
|
|
| extern "C" wchar_t* __cdecl _wtempnam(
|
| wchar_t const* const alternative,
|
| wchar_t const* const prefix
|
| )
|
| {
|
| return common_tempnam(alternative, prefix, _NORMAL_BLOCK, nullptr, 0);
|
| }
|
|
|
| #ifdef _DEBUG
|
|
|
| extern "C" char* __cdecl _tempnam_dbg(
|
| char const* const alternative,
|
| char const* const prefix,
|
| int const block_use,
|
| char const* const file_name,
|
| int const line_number
|
| )
|
| {
|
| return common_tempnam(alternative, prefix, block_use, file_name, line_number);
|
| }
|
|
|
| extern "C" wchar_t* __cdecl _wtempnam_dbg(
|
| wchar_t const* const alternative,
|
| wchar_t const* const prefix,
|
| int const block_use,
|
| char const* const file_name,
|
| int const line_number
|
| )
|
| {
|
| return common_tempnam(alternative, prefix, block_use, file_name, line_number);
|
| }
|
|
|
| #endif |