-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
version.cpp
279 lines (222 loc) · 5.53 KB
/
version.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
Copyright (C) 2015 - 2018 by Iris Morelle <shadowm2006@gmail.com>
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "desktop/version.hpp"
#include "filesystem.hpp"
#include "formatter.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "serialization/unicode.hpp"
#include <cstring>
#if defined(__APPLE__)
#include "apple_version.hpp"
#elif defined(_X11)
#include <cerrno>
#include <sys/utsname.h>
#endif
#ifdef _WIN32
#ifndef UNICODE
#define UNICODE
#endif
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
static lg::log_domain log_desktop("desktop");
#define ERR_DU LOG_STREAM(err, log_desktop)
#define LOG_DU LOG_STREAM(info, log_desktop)
namespace desktop
{
namespace
{
#ifdef _WIN32
/**
* Detects whether we are running on Wine or not.
*
* This is for informational purposes only and all Windows code should assume
* we are running on the real thing instead.
*/
bool on_wine()
{
HMODULE ntdll = GetModuleHandle(L"ntdll.dll");
if(!ntdll) {
return false;
}
return GetProcAddress(ntdll, "wine_get_version") != nullptr;
}
#endif
#if defined(_X11)
/**
* Release policy for POSIX pipe streams opened with popen(3).
*/
struct posix_pipe_release_policy
{
void operator()(std::FILE* f) const { if(f != nullptr) { pclose(f); } }
};
/**
* Scoped POSIX pipe stream.
*
* The stream object type is the same as a regular file stream, but the release
* policy is different, as required by popen(3).
*/
typedef std::unique_ptr<std::FILE, posix_pipe_release_policy> scoped_posix_pipe;
/**
* Read a single line from the specified pipe.
*
* @returns An empty string if the pipe is invalid or nothing could be read.
*/
std::string read_pipe_line(scoped_posix_pipe& p)
{
if(!p.get()) {
return "";
}
std::string ver;
int c;
ver.reserve(64);
// We only want the first line.
while((c = std::fgetc(p.get())) && c != EOF && c != '\n' && c != '\r') {
ver.push_back(static_cast<char>(c));
}
return ver;
}
#endif
} // end anonymous namespace
std::string os_version()
{
#if defined(__APPLE__)
//
// Standard Mac OS X version
//
return desktop::apple::os_version();
#elif defined(_X11)
//
// Linux Standard Base version.
//
static const std::string lsb_release_bin = "/usr/bin/lsb_release";
if(filesystem::file_exists(lsb_release_bin)) {
static const std::string cmdline = lsb_release_bin + " -s -d";
scoped_posix_pipe p(popen(cmdline.c_str(), "r"));
std::string ver = read_pipe_line(p);
if(ver.length() >= 2 && ver[0] == '"' && ver[ver.length() - 1] == '"') {
ver.erase(ver.length() - 1, 1);
ver.erase(0, 1);
}
// Check this again in case we got "" above for some weird reason.
if(!ver.empty()) {
return ver;
}
}
//
// POSIX uname version fallback.
//
utsname u;
if(uname(&u) != 0) {
ERR_DU << "os_version: uname error (" << strerror(errno) << ")\n";
}
return formatter() << u.sysname << ' '
<< u.release << ' '
<< u.version << ' '
<< u.machine;
#elif defined(_WIN32)
//
// Windows version.
//
static const std::string base
= !on_wine() ? "Microsoft Windows" : "Wine/Microsoft Windows";
OSVERSIONINFOEX v { sizeof(OSVERSIONINFOEX) };
#ifdef _MSC_VER
// GetVersionEx is rather problematic, but it works for our usecase.
// See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724451(v=vs.85).aspx
// for more info.
#pragma warning(push)
#pragma warning(disable:4996)
#endif
if(!GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&v))) {
ERR_DU << "os_version: GetVersionEx error ("
<< GetLastError() << ")\n";
return base;
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif
const DWORD vnum = v.dwMajorVersion * 100 + v.dwMinorVersion;
std::string version;
switch(vnum)
{
case 500:
version = "2000";
break;
case 501:
version = "XP";
break;
case 502:
// This will misidentify XP x64 but who really cares?
version = "Server 2003";
break;
case 600:
if(v.wProductType == VER_NT_WORKSTATION) {
version = "Vista";
} else {
version = "Server 2008";
}
break;
case 601:
if(v.wProductType == VER_NT_WORKSTATION) {
version = "7";
} else {
version = "Server 2008 R2";
}
break;
case 602:
if(v.wProductType == VER_NT_WORKSTATION) {
version = "8";
} else {
version = "Server 2012";
}
break;
case 603:
if(v.wProductType == VER_NT_WORKSTATION) {
version = "8.1";
} else {
version = "Server 2012 R2";
}
break;
case 1000:
if(v.wProductType == VER_NT_WORKSTATION) {
version = "10";
break;
} // else fallback to default
default:
if(v.wProductType != VER_NT_WORKSTATION) {
version = "Server";
}
}
if(v.szCSDVersion && *v.szCSDVersion) {
version += " ";
version += unicode_cast<std::string>(std::wstring(v.szCSDVersion));
}
version += " (";
// Add internal version numbers.
version += formatter()
<< v.dwMajorVersion << '.'
<< v.dwMinorVersion << '.'
<< v.dwBuildNumber;
version += ")";
return base + " " + version;
#else
//
// "I don't know where I am" version.
//
ERR_DU << "os_version(): unsupported platform\n";
return _("operating_system^<unknown>");
#endif
}
} // end namespace desktop