-
Notifications
You must be signed in to change notification settings - Fork 47
/
putenv.cpp
80 lines (65 loc) · 1.6 KB
/
putenv.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdlib.h>
#include <internal_shared.h>
static __forceinline size_t __cdecl _tcslen(
_In_z_ char const* _Str
)
{
return strlen(_Str);
}
static __forceinline size_t __cdecl _tcslen(
_In_z_ wchar_t const* _String
)
{
return wcslen(_String);
}
__forceinline int __cdecl _tputenv(
_In_z_ wchar_t const* _EnvString
)
{
return _wputenv(_EnvString);
}
__forceinline int __cdecl _tputenv(
_In_z_ char const* _EnvString
)
{
return _putenv(_EnvString);
}
template<typename TCAHR> __forceinline errno_t __cdecl common_putenv_s(
_In_z_ TCAHR const* _Name,
_In_z_ TCAHR const* _Value
)
{
_VALIDATE_RETURN_ERRCODE(_Value != nullptr, EINVAL);
_VALIDATE_RETURN_ERRCODE(_Name != nullptr, EINVAL);
auto cName = _tcslen(_Name);
auto cValue = _tcslen(_Value);
auto _EnvString = (TCAHR*)_malloca(cName + cValue + 2 * sizeof(TCAHR));
if (!_EnvString)
return errno;
memcpy(_EnvString, _Name, cName * sizeof(TCAHR));
_EnvString[cName] = '=';
memcpy(_EnvString + cName + 1, _Value, (cValue + 1) * sizeof(TCAHR));
auto result = _tputenv(_EnvString) == 0 ? 0 : errno;
_freea(_EnvString);
return result;
}
#if WindowsTargetPlatformMinVersion < WindowsTargetPlatformWindows6
extern "C" errno_t __cdecl _putenv_s(
_In_z_ char const* _Name,
_In_z_ char const* _Value
)
{
return common_putenv_s(_Name, _Value);
}
_LCRT_DEFINE_IAT_SYMBOL(_putenv_s);
#endif
#if WindowsTargetPlatformMinVersion < WindowsTargetPlatformWindows6
extern "C" errno_t __cdecl _wputenv_s(
_In_z_ wchar_t const* _Name,
_In_z_ wchar_t const* _Value
)
{
return common_putenv_s(_Name, _Value);
}
_LCRT_DEFINE_IAT_SYMBOL(_wputenv_s);
#endif