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 <stdlib.h>
18 
19 #include "sysdeps/env.h"
20 
21 #ifdef _WIN32
22 #include <lmcons.h>
23 #include <windows.h>
24 #endif  // _WIN32
25 
26 #include <android-base/utf8.h>
27 
28 namespace adb {
29 namespace sysdeps {
30 
GetEnvironmentVariable(std::string_view var)31 std::optional<std::string> GetEnvironmentVariable(std::string_view var) {
32     if (var.empty()) {
33         return std::nullopt;
34     }
35 
36 #ifdef _WIN32
37     constexpr size_t kMaxEnvVarSize = 32767;
38     wchar_t wbuf[kMaxEnvVarSize];
39     std::wstring wvar;
40     if (!android::base::UTF8ToWide(var.data(), &wvar)) {
41         return std::nullopt;
42     }
43 
44     auto sz = ::GetEnvironmentVariableW(wvar.data(), wbuf, sizeof(wbuf));
45     if (sz == 0) {
46         return std::nullopt;
47     }
48 
49     std::string val;
50     if (!android::base::WideToUTF8(wbuf, &val)) {
51         return std::nullopt;
52     }
53 
54     return std::make_optional(val);
55 #else  // !_WIN32
56     const char* val = getenv(var.data());
57     if (val == nullptr) {
58         return std::nullopt;
59     }
60 
61     return std::make_optional(std::string(val));
62 #endif
63 }
64 
65 #ifdef _WIN32
66 constexpr char kHostNameEnvVar[] = "COMPUTERNAME";
67 constexpr char kUserNameEnvVar[] = "USERNAME";
68 #else
69 constexpr char kHostNameEnvVar[] = "HOSTNAME";
70 constexpr char kUserNameEnvVar[] = "LOGNAME";
71 #endif
72 
GetHostNameUTF8()73 std::string GetHostNameUTF8() {
74     const auto hostName = GetEnvironmentVariable(kHostNameEnvVar);
75     if (hostName && !hostName->empty()) {
76         return *hostName;
77     }
78 
79 #ifdef _WIN32
80     wchar_t wbuf[MAX_COMPUTERNAME_LENGTH + 1];
81     DWORD size = sizeof(wbuf);
82     if (!GetComputerNameW(wbuf, &size) || size == 0) {
83         return "";
84     }
85 
86     std::string name;
87     if (!android::base::WideToUTF8(wbuf, &name)) {
88         return "";
89     }
90 
91     return name;
92 #else   // !_WIN32
93     char buf[256];
94     return (gethostname(buf, sizeof(buf)) == -1) ? "" : buf;
95 #endif  // _WIN32
96 }
97 
GetLoginNameUTF8()98 std::string GetLoginNameUTF8() {
99     const auto userName = GetEnvironmentVariable(kUserNameEnvVar);
100     if (userName && !userName->empty()) {
101         return *userName;
102     }
103 
104 #ifdef _WIN32
105     wchar_t wbuf[UNLEN + 1];
106     DWORD size = sizeof(wbuf);
107     if (!GetUserNameW(wbuf, &size) || size == 0) {
108         return "";
109     }
110 
111     std::string login;
112     if (!android::base::WideToUTF8(wbuf, &login)) {
113         return "";
114     }
115 
116     return login;
117 #else   // !_WIN32
118     const char* login = getlogin();
119     return login ? login : "";
120 #endif  // _WIN32
121 }
122 
123 }  // namespace sysdeps
124 }  // namespace adb
125