|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal.h>
|
| #include <corecrt_internal_traits.h>
|
| #include <stdlib.h>
|
| #include <string.h>
|
|
|
|
|
|
|
|
|
| static bool other_environment_exists(wchar_t) throw() { return _environ_table.value() != nullptr; }
|
| static bool other_environment_exists(char) throw() { return _wenviron_table.value() != nullptr; }
|
|
|
|
|
|
|
|
|
|
|
| template <typename Character>
|
| static size_t compute_required_transform_buffer_count(
|
| Character const* const name,
|
| Character const* const value
|
| ) throw()
|
| {
|
|
|
| size_t const name_count_required = __crt_compute_required_transform_buffer_count(CP_ACP, name);
|
| _VALIDATE_RETURN_NOEXC(name_count_required != 0, EILSEQ, false);
|
|
|
| if (!value)
|
| return name_count_required;
|
|
|
| size_t const value_count_required = __crt_compute_required_transform_buffer_count(CP_ACP, value);
|
| _VALIDATE_RETURN_NOEXC(value_count_required != 0, EILSEQ, false);
|
|
|
|
|
|
|
|
|
| return name_count_required + value_count_required;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| template <typename Character>
|
| static Character* create_environment_string(
|
| Character const* const name,
|
| Character const* const value
|
| ) throw()
|
| {
|
| typedef __crt_char_traits<Character> traits;
|
|
|
| if (value)
|
| {
|
| size_t const name_length = traits::tcsnlen(name, _MAX_ENV);
|
| size_t const value_length = traits::tcsnlen(value, _MAX_ENV);
|
|
|
| _VALIDATE_RETURN(name_length < _MAX_ENV, EINVAL, nullptr);
|
| _VALIDATE_RETURN(value_length < _MAX_ENV, EINVAL, nullptr);
|
|
|
|
|
| size_t const buffer_count = name_length + 1 + value_length + 1;
|
|
|
| __crt_unique_heap_ptr<Character> buffer(_calloc_crt_t(Character, buffer_count));
|
| if (!buffer)
|
| return nullptr;
|
|
|
| traits::tcscpy_s(buffer.get(), buffer_count, name);
|
| buffer.get()[name_length] = '=';
|
| traits::tcscpy_s(buffer.get() + name_length + 1, value_length + 1, value);
|
|
|
| return buffer.detach();
|
| }
|
| else
|
| {
|
| Character const* const equal_sign_it = traits::tcschr(name, '=');
|
| if (equal_sign_it)
|
| {
|
|
|
| _VALIDATE_RETURN(equal_sign_it - name < _MAX_ENV, EINVAL, nullptr);
|
| _VALIDATE_RETURN(traits::tcsnlen(equal_sign_it + 1, _MAX_ENV) < _MAX_ENV, EINVAL, nullptr);
|
| }
|
|
|
| size_t const buffer_count = traits::tcslen(name) + 1;
|
|
|
| __crt_unique_heap_ptr<Character> buffer(_calloc_crt_t(Character, buffer_count));
|
| if (!buffer)
|
| return nullptr;
|
|
|
| traits::tcscpy_s(buffer.get(), buffer_count, name);
|
|
|
| return buffer.detach();
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
| template <typename Character>
|
| static bool __cdecl set_variable_in_other_environment(
|
| Character const* const name,
|
| Character const* const value
|
| ) throw()
|
| {
|
| typedef __crt_char_traits<Character> traits;
|
| typedef typename traits::other_char_type other_char_type;
|
| typedef __crt_char_traits<other_char_type> other_traits;
|
|
|
| size_t const buffer_count = compute_required_transform_buffer_count(name, value);
|
|
|
| __crt_unique_heap_ptr<other_char_type> buffer(_calloc_crt_t(other_char_type, buffer_count));
|
| if (!buffer)
|
| return false;
|
|
|
| size_t const name_written_count = __crt_transform_string(CP_ACP, name, buffer.get(), buffer_count);
|
| _VALIDATE_RETURN_NOEXC(name_written_count != 0, EILSEQ, false);
|
|
|
| if (value)
|
| {
|
|
|
| buffer.get()[name_written_count - 1] = '=';
|
|
|
| size_t const value_written_count = __crt_transform_string(
|
| CP_ACP,
|
| value,
|
| buffer.get() + name_written_count,
|
| buffer_count - name_written_count);
|
| _VALIDATE_RETURN_NOEXC(value_written_count != 0, EILSEQ, false);
|
| }
|
|
|
| return other_traits::set_variable_in_environment_nolock(buffer.detach(), 0) == 0;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| template <typename Character>
|
| static int __cdecl common_putenv_nolock(
|
| Character const* const name,
|
| Character const* const value
|
| ) throw()
|
| {
|
| typedef __crt_char_traits<Character> traits;
|
|
|
|
|
| if (!_environ_table.value() && !_wenviron_table.value())
|
| return -1;
|
|
|
|
|
|
|
|
|
|
|
| _VALIDATE_RETURN(name != nullptr, EINVAL, -1);
|
|
|
| __crt_unique_heap_ptr<Character> new_option(create_environment_string(name, value));
|
| if (!new_option)
|
| return -1;
|
|
|
| if (traits::set_variable_in_environment_nolock(new_option.detach(), 1) != 0)
|
| return -1;
|
|
|
|
|
|
|
| if (!other_environment_exists(Character()))
|
| return 0;
|
|
|
| if (!set_variable_in_other_environment(name, value))
|
| return -1;
|
|
|
| return 0;
|
| }
|
|
|
|
|
|
|
| template <typename Character>
|
| static int __cdecl common_putenv(
|
| Character const* const name,
|
| Character const* const value
|
| ) throw()
|
| {
|
| int status = 0;
|
|
|
| __acrt_lock(__acrt_environment_lock);
|
| __try
|
| {
|
| status = common_putenv_nolock(name, value);
|
| }
|
| __finally
|
| {
|
| __acrt_unlock(__acrt_environment_lock);
|
| }
|
|
|
| return status;
|
| }
|
|
|
|
|
|
|
| extern "C" int __cdecl _putenv(char const* const option)
|
| {
|
| return common_putenv(option, static_cast<char const*>(nullptr));
|
| }
|
|
|
| extern "C" int __cdecl _wputenv(wchar_t const* const option)
|
| {
|
| return common_putenv(option, static_cast<wchar_t const*>(nullptr));
|
| }
|
|
|
|
|
|
|
| extern "C" errno_t __cdecl _putenv_s(char const* const name, char const* const value)
|
| {
|
| _VALIDATE_RETURN_ERRCODE(value != nullptr, EINVAL);
|
| return common_putenv(name, value) == 0 ? 0 : errno;
|
| }
|
|
|
| extern "C" errno_t __cdecl _wputenv_s(wchar_t const* const name, wchar_t const* const value)
|
| {
|
| _VALIDATE_RETURN_ERRCODE(value != nullptr, EINVAL);
|
| return common_putenv(name, value) == 0 ? 0 : errno;
|
| }
|
|
|