1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include <grpc/support/port_platform.h>
20
21 #ifdef GPR_WINDOWS_ENV
22
23 #include <windows.h>
24
25 #include <memory>
26
27 #include "src/core/util/env.h"
28 #include "src/core/util/tchar.h"
29
30 namespace grpc_core {
31
GetEnv(const char * name)32 absl::optional<std::string> GetEnv(const char* name) {
33 auto tname = CharToTchar(name);
34 DWORD ret = GetEnvironmentVariable(tname.c_str(), NULL, 0);
35 if (ret == 0) return absl::nullopt;
36 std::unique_ptr<TCHAR[]> tresult(new TCHAR[ret]);
37 ret =
38 GetEnvironmentVariable(tname.c_str(), tresult.get(), ret * sizeof(TCHAR));
39 if (ret == 0) return absl::nullopt;
40 return TcharToChar(tresult.get());
41 }
42
SetEnv(const char * name,const char * value)43 void SetEnv(const char* name, const char* value) {
44 BOOL res = SetEnvironmentVariable(CharToTchar(name).c_str(),
45 CharToTchar(value).c_str());
46 if (!res) abort();
47 }
48
UnsetEnv(const char * name)49 void UnsetEnv(const char* name) {
50 BOOL res = SetEnvironmentVariable(CharToTchar(name).c_str(), NULL);
51 if (!res) abort();
52 }
53
54 } // namespace grpc_core
55
56 #endif // GPR_WINDOWS_ENV
57