1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "sysdeps/env.h"
18 
19 #ifdef _WIN32
20 #include <lmcons.h>
21 #include <windows.h>
22 #endif  // _WIN32
23 
24 #include <android-base/utf8.h>
25 
26 namespace adb {
27 namespace sysdeps {
28 
29 std::optional<std::string> GetEnvironmentVariable(std::string_view var) {
30     if (var.empty()) {
31         return std::nullopt;
32     }
33 
34 #ifdef _WIN32
35     constexpr size_t kMaxEnvVarSize = 32767;
36     wchar_t wbuf[kMaxEnvVarSize];
37     std::wstring wvar;
38     if (!android::base::UTF8ToWide(var.data(), &wvar)) {
39         return std::nullopt;
40     }
41 
42     auto sz = ::GetEnvironmentVariableW(wvar.data(), wbuf, sizeof(wbuf));
43     if (sz == 0) {
44         return std::nullopt;
45     }
46 
47     std::string val;
48     if (!android::base::WideToUTF8(wbuf, &val)) {
49         return std::nullopt;
50     }
51 
52     return std::make_optional(val);
53 #else  // !_WIN32
54     const char* val = getenv(var.data());
55     if (val == nullptr) {
56         return std::nullopt;
57     }
58 
59     return std::make_optional(std::string(val));
60 #endif
61 }
62 
63 #ifdef _WIN32
64 constexpr char kHostNameEnvVar[] = "COMPUTERNAME";
65 constexpr char kUserNameEnvVar[] = "USERNAME";
66 #else
67 constexpr char kHostNameEnvVar[] = "HOSTNAME";
68 constexpr char kUserNameEnvVar[] = "LOGNAME";
69 #endif
70 
71 std::string GetHostNameUTF8() {
72     const auto hostName = GetEnvironmentVariable(kHostNameEnvVar);
73     if (hostName && !hostName->empty()) {
74         return *hostName;
75     }
76 
77 #ifdef _WIN32
78     wchar_t wbuf[MAX_COMPUTERNAME_LENGTH + 1];
79     DWORD size = sizeof(wbuf);
80     if (!GetComputerNameW(wbuf, &size) || size == 0) {
81         return "";
82     }
83 
84     std::string name;
85     if (!android::base::WideToUTF8(wbuf, &name)) {
86         return "";
87     }
88 
89     return name;
90 #else   // !_WIN32
91     char buf[256];
92     return (gethostname(buf, sizeof(buf)) == -1) ? "" : buf;
93 #endif  // _WIN32
94 }
95 
96 std::string GetLoginNameUTF8() {
97     const auto userName = GetEnvironmentVariable(kUserNameEnvVar);
98     if (userName && !userName->empty()) {
99         return *userName;
100     }
101 
102 #ifdef _WIN32
103     wchar_t wbuf[UNLEN + 1];
104     DWORD size = sizeof(wbuf);
105     if (!GetUserNameW(wbuf, &size) || size == 0) {
106         return "";
107     }
108 
109     std::string login;
110     if (!android::base::WideToUTF8(wbuf, &login)) {
111         return "";
112     }
113 
114     return login;
115 #else   // !_WIN32
116     const char* login = getlogin();
117     return login ? login : "";
118 #endif  // _WIN32
119 }
120 
121 }  // namespace sysdeps
122 }  // namespace adb
123