ProcessUtils.cpp 20 KB

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