ProcessUtils.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/ProcessUtils.h"
  24. #include "../Core/StringUtils.h"
  25. #include "../IO/FileSystem.h"
  26. #include <cstdio>
  27. #include <fcntl.h>
  28. #ifdef __APPLE__
  29. #include "TargetConditionals.h"
  30. #endif
  31. #if defined(IOS)
  32. #include <mach/mach_host.h>
  33. #elif defined(TVOS)
  34. extern "C" unsigned SDL_TVOS_GetActiveProcessorCount();
  35. #elif !defined(__linux__) && !defined(__EMSCRIPTEN__)
  36. #include <libcpuid.h>
  37. #endif
  38. #if defined(_WIN32)
  39. #include <windows.h>
  40. #include <io.h>
  41. #if defined(_MSC_VER)
  42. #include <float.h>
  43. #include <Lmcons.h> // For UNLEN.
  44. #elif defined(__MINGW32__)
  45. #include <lmcons.h> // For UNLEN. Apparently MSVC defines "<Lmcons.h>" (with an upperscore 'L' but MinGW uses an underscore 'l').
  46. #include <ntdef.h>
  47. #endif
  48. #elif defined(__linux__) && !defined(__ANDROID__)
  49. #include <pwd.h>
  50. #include <sys/sysinfo.h>
  51. #include <sys/utsname.h>
  52. #elif defined(__APPLE__)
  53. #include <sys/sysctl.h>
  54. #include <SystemConfiguration/SystemConfiguration.h> // For the detection functions inside GetLoginName().
  55. #endif
  56. #ifndef _WIN32
  57. #include <unistd.h>
  58. #endif
  59. #if defined(__EMSCRIPTEN__) && defined(__EMSCRIPTEN_PTHREADS__)
  60. #include <emscripten/threading.h>
  61. #endif
  62. #if defined(__i386__)
  63. // From http://stereopsis.com/FPU.html
  64. #define FPU_CW_PREC_MASK 0x0300
  65. #define FPU_CW_PREC_SINGLE 0x0000
  66. #define FPU_CW_PREC_DOUBLE 0x0200
  67. #define FPU_CW_PREC_EXTENDED 0x0300
  68. #define FPU_CW_ROUND_MASK 0x0c00
  69. #define FPU_CW_ROUND_NEAR 0x0000
  70. #define FPU_CW_ROUND_DOWN 0x0400
  71. #define FPU_CW_ROUND_UP 0x0800
  72. #define FPU_CW_ROUND_CHOP 0x0c00
  73. inline unsigned GetFPUState()
  74. {
  75. unsigned control = 0;
  76. __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
  77. return control;
  78. }
  79. inline void SetFPUState(unsigned control)
  80. {
  81. __asm__ __volatile__ ("fldcw %0" : : "m" (control));
  82. }
  83. #endif
  84. #ifndef MINI_URHO
  85. // ATOMIC BEGIN
  86. #include <SDL/include/SDL.h>
  87. // ATOMIC END
  88. #endif
  89. #include "../DebugNew.h"
  90. namespace Atomic
  91. {
  92. #ifdef _WIN32
  93. static bool consoleOpened = false;
  94. #endif
  95. static String currentLine;
  96. static Vector<String> arguments;
  97. static String miniDumpDir;
  98. #if defined(IOS)
  99. static void GetCPUData(host_basic_info_data_t* data)
  100. {
  101. mach_msg_type_number_t infoCount;
  102. infoCount = HOST_BASIC_INFO_COUNT;
  103. host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)data, &infoCount);
  104. }
  105. #elif defined(__linux__)
  106. struct CpuCoreCount
  107. {
  108. unsigned numPhysicalCores_;
  109. unsigned numLogicalCores_;
  110. };
  111. // This function is used by all the target triplets with Linux as the OS, such as Android, RPI, desktop Linux, etc
  112. static void GetCPUData(struct CpuCoreCount* data)
  113. {
  114. // Sanity check
  115. assert(data);
  116. // At least return 1 core
  117. data->numPhysicalCores_ = data->numLogicalCores_ = 1;
  118. FILE* fp;
  119. int res;
  120. unsigned i, j;
  121. fp = fopen("/sys/devices/system/cpu/present", "r");
  122. if (fp)
  123. {
  124. res = fscanf(fp, "%d-%d", &i, &j);
  125. fclose(fp);
  126. if (res == 2 && i == 0)
  127. {
  128. data->numPhysicalCores_ = data->numLogicalCores_ = j + 1;
  129. fp = fopen("/sys/devices/system/cpu/cpu0/topology/thread_siblings_list", "r");
  130. if (fp)
  131. {
  132. res = fscanf(fp, "%d,%d,%d,%d", &i, &j, &i, &j);
  133. fclose(fp);
  134. // Having sibling thread(s) indicates the CPU is using HT/SMT technology
  135. if (res > 1)
  136. data->numPhysicalCores_ /= res;
  137. }
  138. }
  139. }
  140. }
  141. #elif !defined(__EMSCRIPTEN__) && !defined(TVOS)
  142. static void GetCPUData(struct cpu_id_t* data)
  143. {
  144. if (cpu_identify(0, data) < 0)
  145. {
  146. data->num_logical_cpus = 1;
  147. data->num_cores = 1;
  148. }
  149. }
  150. #endif
  151. void InitFPU()
  152. {
  153. // Make sure FPU is in round-to-nearest, single precision mode
  154. // This ensures Direct3D and OpenGL behave similarly, and all threads behave similarly
  155. #if defined(_MSC_VER) && defined(_M_IX86)
  156. _controlfp(_RC_NEAR | _PC_24, _MCW_RC | _MCW_PC);
  157. #elif defined(__i386__)
  158. unsigned control = GetFPUState();
  159. control &= ~(FPU_CW_PREC_MASK | FPU_CW_ROUND_MASK);
  160. control |= (FPU_CW_PREC_SINGLE | FPU_CW_ROUND_NEAR);
  161. SetFPUState(control);
  162. #endif
  163. }
  164. void ErrorDialog(const String& title, const String& message)
  165. {
  166. #ifndef MINI_URHO
  167. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title.CString(), message.CString(), 0);
  168. #endif
  169. }
  170. void ErrorExit(const String& message, int exitCode)
  171. {
  172. if (!message.Empty())
  173. PrintLine(message, true);
  174. exit(exitCode);
  175. }
  176. void OpenConsoleWindow()
  177. {
  178. #ifdef _WIN32
  179. if (consoleOpened)
  180. return;
  181. AllocConsole();
  182. freopen("CONIN$", "r", stdin);
  183. freopen("CONOUT$", "w", stdout);
  184. consoleOpened = true;
  185. #endif
  186. }
  187. void PrintUnicode(const String& str, bool error)
  188. {
  189. #if !defined(__ANDROID__) && !defined(IOS) && !defined(TVOS)
  190. #ifdef _WIN32
  191. // If the output stream has been redirected, use fprintf instead of WriteConsoleW,
  192. // though it means that proper Unicode output will not work
  193. FILE* out = error ? stderr : stdout;
  194. if (!_isatty(_fileno(out)))
  195. fprintf(out, "%s", str.CString());
  196. else
  197. {
  198. HANDLE stream = GetStdHandle(error ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
  199. if (stream == INVALID_HANDLE_VALUE)
  200. return;
  201. WString strW(str);
  202. DWORD charsWritten;
  203. WriteConsoleW(stream, strW.CString(), strW.Length(), &charsWritten, 0);
  204. }
  205. #else
  206. fprintf(error ? stderr : stdout, "%s", str.CString());
  207. #endif
  208. #endif
  209. }
  210. void PrintUnicodeLine(const String& str, bool error)
  211. {
  212. PrintUnicode(str + "\n", error);
  213. }
  214. void PrintLine(const String& str, bool error)
  215. {
  216. #if !defined(__ANDROID__) && !defined(IOS) && !defined(TVOS)
  217. fprintf(error ? stderr : stdout, "%s\n", str.CString());
  218. #endif
  219. }
  220. const Vector<String>& ParseArguments(const String& cmdLine, bool skipFirstArgument)
  221. {
  222. arguments.Clear();
  223. unsigned cmdStart = 0, cmdEnd = 0;
  224. bool inCmd = false;
  225. bool inQuote = false;
  226. for (unsigned i = 0; i < cmdLine.Length(); ++i)
  227. {
  228. if (cmdLine[i] == '\"')
  229. inQuote = !inQuote;
  230. if (cmdLine[i] == ' ' && !inQuote)
  231. {
  232. if (inCmd)
  233. {
  234. inCmd = false;
  235. cmdEnd = i;
  236. // Do not store the first argument (executable name)
  237. if (!skipFirstArgument)
  238. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  239. skipFirstArgument = false;
  240. }
  241. }
  242. else
  243. {
  244. if (!inCmd)
  245. {
  246. inCmd = true;
  247. cmdStart = i;
  248. }
  249. }
  250. }
  251. if (inCmd)
  252. {
  253. cmdEnd = cmdLine.Length();
  254. if (!skipFirstArgument)
  255. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  256. }
  257. // Strip double quotes from the arguments
  258. for (unsigned i = 0; i < arguments.Size(); ++i)
  259. arguments[i].Replace("\"", "");
  260. return arguments;
  261. }
  262. const Vector<String>& ParseArguments(const char* cmdLine)
  263. {
  264. return ParseArguments(String(cmdLine));
  265. }
  266. const Vector<String>& ParseArguments(const WString& cmdLine)
  267. {
  268. return ParseArguments(String(cmdLine));
  269. }
  270. const Vector<String>& ParseArguments(const wchar_t* cmdLine)
  271. {
  272. return ParseArguments(String(cmdLine));
  273. }
  274. const Vector<String>& ParseArguments(int argc, char** argv)
  275. {
  276. String cmdLine;
  277. for (int i = 0; i < argc; ++i)
  278. cmdLine.AppendWithFormat("\"%s\" ", (const char*)argv[i]);
  279. return ParseArguments(cmdLine);
  280. }
  281. const Vector<String>& GetArguments()
  282. {
  283. return arguments;
  284. }
  285. String GetConsoleInput()
  286. {
  287. String ret;
  288. #ifdef ATOMIC_TESTING
  289. // When we are running automated tests, reading the console may block. Just return empty in that case
  290. return ret;
  291. #else
  292. #ifdef _WIN32
  293. HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
  294. HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
  295. if (input == INVALID_HANDLE_VALUE || output == INVALID_HANDLE_VALUE)
  296. return ret;
  297. // Use char-based input
  298. SetConsoleMode(input, ENABLE_PROCESSED_INPUT);
  299. INPUT_RECORD record;
  300. DWORD events = 0;
  301. DWORD readEvents = 0;
  302. if (!GetNumberOfConsoleInputEvents(input, &events))
  303. return ret;
  304. while (events--)
  305. {
  306. ReadConsoleInputW(input, &record, 1, &readEvents);
  307. if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
  308. {
  309. unsigned c = record.Event.KeyEvent.uChar.UnicodeChar;
  310. if (c)
  311. {
  312. if (c == '\b')
  313. {
  314. PrintUnicode("\b \b");
  315. int length = currentLine.LengthUTF8();
  316. if (length)
  317. currentLine = currentLine.SubstringUTF8(0, length - 1);
  318. }
  319. else if (c == '\r')
  320. {
  321. PrintUnicode("\n");
  322. ret = currentLine;
  323. currentLine.Clear();
  324. return ret;
  325. }
  326. else
  327. {
  328. // We have disabled echo, so echo manually
  329. wchar_t out = c;
  330. DWORD charsWritten;
  331. WriteConsoleW(output, &out, 1, &charsWritten, 0);
  332. currentLine.AppendUTF8(c);
  333. }
  334. }
  335. }
  336. }
  337. #elif !defined(__ANDROID__) && !defined(IOS) && !defined(TVOS)
  338. int flags = fcntl(STDIN_FILENO, F_GETFL);
  339. fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
  340. for (;;)
  341. {
  342. int ch = fgetc(stdin);
  343. if (ch >= 0 && ch != '\n')
  344. ret += (char)ch;
  345. else
  346. break;
  347. }
  348. #endif
  349. return ret;
  350. #endif
  351. }
  352. String GetPlatform()
  353. {
  354. #if defined(__ANDROID__)
  355. return "Android";
  356. #elif defined(IOS)
  357. return "iOS";
  358. #elif defined(TVOS)
  359. return "tvOS";
  360. #elif defined(__APPLE__)
  361. return "macOS";
  362. #elif defined(_WIN32)
  363. return "Windows";
  364. #elif defined(RPI)
  365. return "Raspberry Pi";
  366. #elif defined(__EMSCRIPTEN__)
  367. return "Web";
  368. #elif defined(__linux__)
  369. return "Linux";
  370. #else
  371. return String::EMPTY;
  372. #endif
  373. }
  374. unsigned GetNumPhysicalCPUs()
  375. {
  376. #if defined(IOS)
  377. host_basic_info_data_t data;
  378. GetCPUData(&data);
  379. #if defined(TARGET_OS_SIMULATOR)
  380. // Hardcoded to dual-core on simulator mode even if the host has more
  381. return Min(2, data.physical_cpu);
  382. #else
  383. return data.physical_cpu;
  384. #endif
  385. #elif defined(TVOS)
  386. #if defined(TARGET_OS_SIMULATOR)
  387. return Min(2, SDL_TVOS_GetActiveProcessorCount());
  388. #else
  389. return SDL_TVOS_GetActiveProcessorCount();
  390. #endif
  391. #elif defined(__linux__)
  392. struct CpuCoreCount data;
  393. GetCPUData(&data);
  394. return data.numPhysicalCores_;
  395. #elif defined(__EMSCRIPTEN__)
  396. #ifdef __EMSCRIPTEN_PTHREADS__
  397. return emscripten_num_logical_cores();
  398. #else
  399. return 1; // Targeting a single-threaded Emscripten build.
  400. #endif
  401. #else
  402. struct cpu_id_t data;
  403. GetCPUData(&data);
  404. return (unsigned)data.num_cores;
  405. #endif
  406. }
  407. unsigned GetNumLogicalCPUs()
  408. {
  409. #if defined(IOS)
  410. host_basic_info_data_t data;
  411. GetCPUData(&data);
  412. #if defined(TARGET_OS_SIMULATOR)
  413. return Min(2, data.logical_cpu);
  414. #else
  415. return data.logical_cpu;
  416. #endif
  417. #elif defined(TVOS)
  418. #if defined(TARGET_OS_SIMULATOR)
  419. return Min(2, SDL_TVOS_GetActiveProcessorCount());
  420. #else
  421. return SDL_TVOS_GetActiveProcessorCount();
  422. #endif
  423. #elif defined(__linux__)
  424. struct CpuCoreCount data;
  425. GetCPUData(&data);
  426. return data.numLogicalCores_;
  427. #elif defined(__EMSCRIPTEN__)
  428. #ifdef __EMSCRIPTEN_PTHREADS__
  429. return emscripten_num_logical_cores();
  430. #else
  431. return 1; // Targeting a single-threaded Emscripten build.
  432. #endif
  433. #else
  434. struct cpu_id_t data;
  435. GetCPUData(&data);
  436. return (unsigned)data.num_logical_cpus;
  437. #endif
  438. }
  439. void SetMiniDumpDir(const String& pathName)
  440. {
  441. miniDumpDir = AddTrailingSlash(pathName);
  442. }
  443. String GetMiniDumpDir()
  444. {
  445. #ifndef MINI_URHO
  446. if (miniDumpDir.Empty())
  447. {
  448. char* pathName = SDL_GetPrefPath("atomic", "crashdumps");
  449. if (pathName)
  450. {
  451. String ret(pathName);
  452. SDL_free(pathName);
  453. return ret;
  454. }
  455. }
  456. #endif
  457. return miniDumpDir;
  458. }
  459. // ATOMIC BEGIN
  460. void QuoteArguments(Vector<String>& args)
  461. {
  462. for (unsigned i = 0; i < args.Size(); i++)
  463. {
  464. if (args[i].Contains(' ') && !args[i].Contains('"'))
  465. {
  466. args[i] = "\"" + args[i] + "\"";
  467. }
  468. }
  469. }
  470. // ATOMIC END
  471. unsigned long long GetTotalMemory()
  472. {
  473. #if defined(__linux__) && !defined(__ANDROID__)
  474. struct sysinfo s;
  475. if (sysinfo(&s) != -1)
  476. return s.totalram;
  477. #elif defined(_WIN32)
  478. MEMORYSTATUSEX state;
  479. state.dwLength = sizeof(state);
  480. if (GlobalMemoryStatusEx(&state))
  481. return state.ullTotalPhys;
  482. #elif defined(__APPLE__)
  483. unsigned long long memSize;
  484. size_t len = sizeof(memSize);
  485. int mib[2];
  486. mib[0] = CTL_HW;
  487. mib[1] = HW_MEMSIZE;
  488. sysctl(mib, 2, &memSize, &len, NULL, 0);
  489. return memSize;
  490. #endif
  491. return 0ull;
  492. }
  493. String GetLoginName()
  494. {
  495. #if defined(__linux__) && !defined(__ANDROID__)
  496. struct passwd *p = getpwuid(getuid());
  497. if (p)
  498. return p->pw_name;
  499. #elif defined(_WIN32)
  500. char name[UNLEN + 1];
  501. DWORD len = UNLEN + 1;
  502. if (GetUserName(name, &len))
  503. return name;
  504. #elif defined(__APPLE__) && !defined(IOS) && !defined(TVOS)
  505. SCDynamicStoreRef s = SCDynamicStoreCreate(NULL, CFSTR("GetConsoleUser"), NULL, NULL);
  506. if (s != NULL)
  507. {
  508. uid_t u;
  509. CFStringRef n = SCDynamicStoreCopyConsoleUser(s, &u, NULL);
  510. CFRelease(s);
  511. if (n != NULL)
  512. {
  513. char name[256];
  514. Boolean b = CFStringGetCString(n, name, 256, kCFStringEncodingUTF8);
  515. CFRelease(n);
  516. if (b == true)
  517. return name;
  518. }
  519. }
  520. #endif
  521. return "(?)";
  522. }
  523. String GetHostName()
  524. {
  525. #if (defined(__linux__) || defined(__APPLE__)) && !defined(__ANDROID__)
  526. char buffer[256];
  527. if (gethostname(buffer, 256) == 0)
  528. return buffer;
  529. #elif defined(_WIN32)
  530. char buffer[MAX_COMPUTERNAME_LENGTH + 1];
  531. DWORD len = MAX_COMPUTERNAME_LENGTH + 1;
  532. if (GetComputerName(buffer, &len))
  533. return buffer;
  534. #endif
  535. return String::EMPTY;
  536. }
  537. // Disable Windows OS version functionality when compiling mini version for Web, see https://github.com/urho3d/Urho3D/issues/1998
  538. #if defined(_WIN32) && !defined(MINI_URHO)
  539. typedef NTSTATUS (WINAPI *RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
  540. static void GetOS(RTL_OSVERSIONINFOW *r)
  541. {
  542. HMODULE m = GetModuleHandle("ntdll.dll");
  543. if (m)
  544. {
  545. RtlGetVersionPtr fPtr = (RtlGetVersionPtr) GetProcAddress(m, "RtlGetVersion");
  546. if (r && fPtr && fPtr(r) == 0)
  547. r->dwOSVersionInfoSize = sizeof *r;
  548. }
  549. }
  550. #endif
  551. String GetOSVersion()
  552. {
  553. #if defined(__linux__) && !defined(__ANDROID__)
  554. struct utsname u;
  555. if (uname(&u) == 0)
  556. return String(u.sysname) + " " + u.release;
  557. #elif defined(_WIN32) && !defined(MINI_URHO)
  558. RTL_OSVERSIONINFOW r;
  559. GetOS(&r);
  560. // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx
  561. if (r.dwMajorVersion == 5 && r.dwMinorVersion == 0)
  562. return "Windows 2000";
  563. else if (r.dwMajorVersion == 5 && r.dwMinorVersion == 1)
  564. return "Windows XP";
  565. else if (r.dwMajorVersion == 5 && r.dwMinorVersion == 2)
  566. return "Windows XP 64-Bit Edition/Windows Server 2003/Windows Server 2003 R2";
  567. else if (r.dwMajorVersion == 6 && r.dwMinorVersion == 0)
  568. return "Windows Vista/Windows Server 2008";
  569. else if (r.dwMajorVersion == 6 && r.dwMinorVersion == 1)
  570. return "Windows 7/Windows Server 2008 R2";
  571. else if (r.dwMajorVersion == 6 && r.dwMinorVersion == 2)
  572. return "Windows 8/Windows Server 2012";
  573. else if (r.dwMajorVersion == 6 && r.dwMinorVersion == 3)
  574. return "Windows 8.1/Windows Server 2012 R2";
  575. else if (r.dwMajorVersion == 10 && r.dwMinorVersion == 0)
  576. return "Windows 10/Windows Server 2016";
  577. else
  578. return "Windows Unidentified";
  579. #elif defined(__APPLE__)
  580. char kernel_r[256];
  581. size_t size = sizeof(kernel_r);
  582. if (sysctlbyname("kern.osrelease", &kernel_r, &size, NULL, 0) != -1)
  583. {
  584. Vector<String> kernel_version = String(kernel_r).Split('.');
  585. String version = "macOS/Mac OS X ";
  586. int major = ToInt(kernel_version[0]);
  587. int minor = ToInt(kernel_version[1]);
  588. // https://en.wikipedia.org/wiki/Darwin_(operating_system)
  589. if (major == 16) // macOS Sierra
  590. {
  591. version += "Sierra ";
  592. switch(minor)
  593. {
  594. case 0: version += "10.12.0 "; break;
  595. case 1: version += "10.12.1 "; break;
  596. case 3: version += "10.12.2 "; break;
  597. }
  598. }
  599. else if (major == 15) // OS X El Capitan
  600. {
  601. version += "El Capitan ";
  602. switch(minor)
  603. {
  604. case 0: version += "10.11.0 "; break;
  605. case 6: version += "10.11.6 "; break;
  606. }
  607. }
  608. else if (major == 14) // OS X Yosemite
  609. {
  610. version += "Yosemite ";
  611. switch(minor)
  612. {
  613. case 0: version += "10.10.0 "; break;
  614. case 5: version += "10.10.5 "; break;
  615. }
  616. }
  617. else if (major == 13) // OS X Mavericks
  618. {
  619. version += "Mavericks ";
  620. switch(minor)
  621. {
  622. case 0: version += "10.9.0 "; break;
  623. case 4: version += "10.9.5 "; break;
  624. }
  625. }
  626. else if (major == 12) // OS X Mountain Lion
  627. {
  628. version += "Mountain Lion ";
  629. switch(minor)
  630. {
  631. case 0: version += "10.8.0 "; break;
  632. case 6: version += "10.8.5 "; break;
  633. }
  634. }
  635. else if (major == 11) // Mac OS X Lion
  636. {
  637. version += "Lion ";
  638. switch(minor)
  639. {
  640. case 0: version += "10.7.0 "; break;
  641. case 4: version += "10.7.5 "; break;
  642. }
  643. }
  644. else
  645. {
  646. version += "Unknown ";
  647. }
  648. return version + " (Darwin kernel " + kernel_version[0] + "." + kernel_version[1] + "." + kernel_version[2] + ")";
  649. }
  650. #endif
  651. return String::EMPTY;
  652. }
  653. }