Debian Codegraph @

src/compat/uname.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2000 Ross Combs ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 #include "common/setup_before.h"
19 #ifndef HAVE_UNAME
20 #include "uname.h"
21
22 #include <cerrno>
23 #include <cstdio>
24 #include <cstring>
25 #include <string>
26
27 #ifdef HAVE_WINDOWS_H
28 #include <Windows.h>
29 #define STATUS_SUCCESS (0x00000000)
30 #endif
31
32 #include "common/setup_after.h"
33
34
35 namespace pvpgn
36 {
37 int uname(struct utsname* buf)
38 {
39 if (!buf)
40 {
41 errno = EFAULT;
42 return -1;
43 }
44
45 #ifdef HAVE_WINDOWS_H
46 using RtlGetVersionProto = NTSTATUS(WINAPI*)(RTL_OSVERSIONINFOEXW* lpVersionInformation);
47
48 HMODULE hNtdll = nullptr;
49 if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, L"ntdll.dll", &hNtdll) != 0)
50 {
51 auto fnRtlGetVersion = reinterpret_cast<RtlGetVersionProto>(GetProcAddress(hNtdll, "RtlGetVersion"));
52 if (fnRtlGetVersion != NULL)
53 {
54 RTL_OSVERSIONINFOEXW verinfo = {};
55 verinfo.dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW);
56 if (fnRtlGetVersion(&verinfo) == STATUS_SUCCESS)
57 {
58 std::string temp;
59
60 SYSTEM_INFO sysinfo = {};
61 GetSystemInfo(&sysinfo);
62
63 switch (verinfo.dwMajorVersion)
64 {
65 case 10:
66 {
67 if (verinfo.wProductType == VER_NT_WORKSTATION)
68 {
69 temp.append("Windows 10");
70 }
71 else
72 {
73 temp.append("Windows Server 2016");
74 }
75
76 break;
77 }
78 case 6:
79 {
80 switch (verinfo.dwMinorVersion)
81 {
82 case 3:
83 {
84 if (verinfo.wProductType == VER_NT_WORKSTATION)
85 {
86 temp.append("Windows 8.1");
87 }
88 else
89 {
90 temp.append("Windows Server 2012 R2");
91 }
92
93 break;
94 }
95 case 2:
96 {
97 if (verinfo.wProductType == VER_NT_WORKSTATION)
98 {
99 temp.append("Windows 8");
100 }
101 else
102 {
103 temp.append("Windows Server 2012");
104 }
105
106 break;
107 }
108 case 1:
109 {
110 if (verinfo.wProductType == VER_NT_WORKSTATION)
111 {
112 temp.append("Windows 7");
113 }
114 else
115 {
116 temp.append("Windows Server 2008 R2");
117 }
118
119 break;
120 }
121 case 0:
122 {
123 if (verinfo.wProductType == VER_NT_WORKSTATION)
124 {
125 temp.append("Windows Vista");
126 }
127 else
128 {
129 temp.append("Windows Server 2008");
130 }
131
132 break;
133 }
134 default:
135 {
136 break;
137 }
138 }
139
140 break;
141 }
142 case 5:
143 {
144 switch (verinfo.dwMinorVersion)
145 {
146 case 2:
147 {
148 if (GetSystemMetrics(SM_SERVERR2) != 0)
149 {
150 temp.append("Windows Server 2003 R2");
151 }
152 else if (verinfo.wSuiteMask & VER_SUITE_WH_SERVER)
153 {
154 temp.append("Windows Home Server");
155 }
156 else if (GetSystemMetrics(SM_SERVERR2) == 0)
157 {
158 temp.append("Windows Server 2003");
159 }
160 else if ((verinfo.wProductType == VER_NT_WORKSTATION) && (sysinfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64))
161 {
162 temp.append("Windows XP Professional x64 Edition");
163 }
164
165 break;
166 }
167 case 1:
168 {
169 temp.append("Windows XP");
170 break;
171 }
172 case 0:
173 {
174 temp.append("Windows 2000");
175 break;
176 }
177 default:
178 {
179 break;
180 }
181 }
182
183 break;
184 }
185 default:
186 {
187 break;
188 }
189 }
190
191 if (temp.empty())
192 {
193 temp.append("Windows");
194 }
195
196 if (verinfo.wServicePackMajor != 0)
197 {
198 temp.append(" SP" + std::to_string(verinfo.wServicePackMajor));
199 if (verinfo.wServicePackMinor != 0)
200 {
201 temp.append("." + std::to_string(verinfo.wServicePackMinor));
202 }
203 }
204
205 std::snprintf(buf->sysname, sizeof buf->sysname, "%s", temp.c_str());
206
207 DWORD len = sizeof buf->nodename;
208 GetComputerNameA(buf->nodename, &len);
209
210 std::snprintf(buf->release, sizeof buf->release, "Build %lu", verinfo.dwBuildNumber);
211
212 std::snprintf(buf->version, sizeof buf->version, "%lu.%lu", verinfo.dwMajorVersion, verinfo.dwMinorVersion);
213
214 std::string arch;
215 switch (sysinfo.wProcessorArchitecture)
216 {
217 case PROCESSOR_ARCHITECTURE_AMD64:
218 {
219 arch = "x64";
220 break;
221 }
222 case PROCESSOR_ARCHITECTURE_ARM:
223 {
224 arch = "ARM";
225 break;
226 }
227 case PROCESSOR_ARCHITECTURE_IA64:
228 {
229 arch = "Intel Itanium-based";
230 break;
231 }
232 case PROCESSOR_ARCHITECTURE_INTEL:
233 {
234 arch = "x86";
235 break;
236 }
237 case PROCESSOR_ARCHITECTURE_UNKNOWN:
238 default:
239 arch = "Unknown";
240 }
241
242 std::snprintf(buf->machine, sizeof buf->machine, "%s", arch.c_str());
243
244 // leave this empty for now
245 std::snprintf(buf->domainname, sizeof buf->domainname, "");
246 }
247 }
248 }
249 #else // !HAVE_WINDOWS_H
250 std::snprintf(buf->sysname, sizeof buf->sysname, "Unknown");
251 std::snprintf(buf->nodename, sizeof buf->nodename, "Unknown");
252 std::snprintf(buf->release, sizeof buf->release, "Unknown");
253 std::snprintf(buf->version, sizeof buf->version, "Unknown");
254 std::snprintf(buf->machine, sizeof buf->machine, "Unknown");
255 std::snprintf(buf->domainname, sizeof buf->domainname, "Unknown");
256 #endif // HAVE_WINDOWS_H
257
258 return 0;
259 }
260
261 }
262 #endif