ProcessUtils.cpp 20 KB

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