ProcessUtils.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. //
  2. // Copyright (c) 2008-2018 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); // NOLINT(cert-err34-c)
  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); // NOLINT(cert-err34-c)
  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(nullptr, 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(), nullptr);
  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, nullptr);
  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. PrintLine(str.CString(), error);
  215. }
  216. void PrintLine(const char* str, bool error)
  217. {
  218. #if !defined(__ANDROID__) && !defined(IOS) && !defined(TVOS)
  219. fprintf(error ? stderr : stdout, "%s\n", str);
  220. #endif
  221. }
  222. const Vector<String>& ParseArguments(const String& cmdLine, bool skipFirstArgument)
  223. {
  224. arguments.Clear();
  225. unsigned cmdStart = 0, cmdEnd = 0;
  226. bool inCmd = false;
  227. bool inQuote = false;
  228. for (unsigned i = 0; i < cmdLine.Length(); ++i)
  229. {
  230. if (cmdLine[i] == '\"')
  231. inQuote = !inQuote;
  232. if (cmdLine[i] == ' ' && !inQuote)
  233. {
  234. if (inCmd)
  235. {
  236. inCmd = false;
  237. cmdEnd = i;
  238. // Do not store the first argument (executable name)
  239. if (!skipFirstArgument)
  240. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  241. skipFirstArgument = false;
  242. }
  243. }
  244. else
  245. {
  246. if (!inCmd)
  247. {
  248. inCmd = true;
  249. cmdStart = i;
  250. }
  251. }
  252. }
  253. if (inCmd)
  254. {
  255. cmdEnd = cmdLine.Length();
  256. if (!skipFirstArgument)
  257. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  258. }
  259. // Strip double quotes from the arguments
  260. for (unsigned i = 0; i < arguments.Size(); ++i)
  261. arguments[i].Replace("\"", "");
  262. return arguments;
  263. }
  264. const Vector<String>& ParseArguments(const char* cmdLine)
  265. {
  266. return ParseArguments(String(cmdLine));
  267. }
  268. const Vector<String>& ParseArguments(const WString& cmdLine)
  269. {
  270. return ParseArguments(String(cmdLine));
  271. }
  272. const Vector<String>& ParseArguments(const wchar_t* cmdLine)
  273. {
  274. return ParseArguments(String(cmdLine));
  275. }
  276. const Vector<String>& ParseArguments(int argc, char** argv)
  277. {
  278. String cmdLine;
  279. for (int i = 0; i < argc; ++i)
  280. cmdLine.AppendWithFormat("\"%s\" ", (const char*)argv[i]);
  281. return ParseArguments(cmdLine);
  282. }
  283. const Vector<String>& GetArguments()
  284. {
  285. return arguments;
  286. }
  287. String GetConsoleInput()
  288. {
  289. String ret;
  290. #ifdef URHO3D_TESTING
  291. // When we are running automated tests, reading the console may block. Just return empty in that case
  292. return ret;
  293. #else
  294. #ifdef _WIN32
  295. HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
  296. HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
  297. if (input == INVALID_HANDLE_VALUE || output == INVALID_HANDLE_VALUE)
  298. return ret;
  299. // Use char-based input
  300. SetConsoleMode(input, ENABLE_PROCESSED_INPUT);
  301. INPUT_RECORD record;
  302. DWORD events = 0;
  303. DWORD readEvents = 0;
  304. if (!GetNumberOfConsoleInputEvents(input, &events))
  305. return ret;
  306. while (events--)
  307. {
  308. ReadConsoleInputW(input, &record, 1, &readEvents);
  309. if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
  310. {
  311. unsigned c = record.Event.KeyEvent.uChar.UnicodeChar;
  312. if (c)
  313. {
  314. if (c == '\b')
  315. {
  316. PrintUnicode("\b \b");
  317. int length = currentLine.LengthUTF8();
  318. if (length)
  319. currentLine = currentLine.SubstringUTF8(0, length - 1);
  320. }
  321. else if (c == '\r')
  322. {
  323. PrintUnicode("\n");
  324. ret = currentLine;
  325. currentLine.Clear();
  326. return ret;
  327. }
  328. else
  329. {
  330. // We have disabled echo, so echo manually
  331. wchar_t out = c;
  332. DWORD charsWritten;
  333. WriteConsoleW(output, &out, 1, &charsWritten, nullptr);
  334. currentLine.AppendUTF8(c);
  335. }
  336. }
  337. }
  338. }
  339. #elif !defined(__ANDROID__) && !defined(IOS) && !defined(TVOS)
  340. int flags = fcntl(STDIN_FILENO, F_GETFL);
  341. fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
  342. for (;;)
  343. {
  344. int ch = fgetc(stdin);
  345. if (ch >= 0 && ch != '\n')
  346. ret += (char)ch;
  347. else
  348. break;
  349. }
  350. #endif
  351. return ret;
  352. #endif
  353. }
  354. String GetPlatform()
  355. {
  356. #if defined(__ANDROID__)
  357. return "Android";
  358. #elif defined(IOS)
  359. return "iOS";
  360. #elif defined(TVOS)
  361. return "tvOS";
  362. #elif defined(__APPLE__)
  363. return "macOS";
  364. #elif defined(_WIN32)
  365. return "Windows";
  366. #elif defined(RPI)
  367. return "Raspberry Pi";
  368. #elif defined(__EMSCRIPTEN__)
  369. return "Web";
  370. #elif defined(__linux__)
  371. return "Linux";
  372. #else
  373. return "(?)";
  374. #endif
  375. }
  376. unsigned GetNumPhysicalCPUs()
  377. {
  378. #if defined(IOS)
  379. host_basic_info_data_t data;
  380. GetCPUData(&data);
  381. #if defined(TARGET_OS_SIMULATOR)
  382. // Hardcoded to dual-core on simulator mode even if the host has more
  383. return Min(2, data.physical_cpu);
  384. #else
  385. return data.physical_cpu;
  386. #endif
  387. #elif defined(TVOS)
  388. #if defined(TARGET_OS_SIMULATOR)
  389. return Min(2, SDL_TVOS_GetActiveProcessorCount());
  390. #else
  391. return SDL_TVOS_GetActiveProcessorCount();
  392. #endif
  393. #elif defined(__linux__)
  394. struct CpuCoreCount data{};
  395. GetCPUData(&data);
  396. return data.numPhysicalCores_;
  397. #elif defined(__EMSCRIPTEN__)
  398. #ifdef __EMSCRIPTEN_PTHREADS__
  399. return emscripten_num_logical_cores();
  400. #else
  401. return 1; // Targeting a single-threaded Emscripten build.
  402. #endif
  403. #else
  404. struct cpu_id_t data;
  405. GetCPUData(&data);
  406. return (unsigned)data.num_cores;
  407. #endif
  408. }
  409. unsigned GetNumLogicalCPUs()
  410. {
  411. #if defined(IOS)
  412. host_basic_info_data_t data;
  413. GetCPUData(&data);
  414. #if defined(TARGET_OS_SIMULATOR)
  415. return Min(2, data.logical_cpu);
  416. #else
  417. return data.logical_cpu;
  418. #endif
  419. #elif defined(TVOS)
  420. #if defined(TARGET_OS_SIMULATOR)
  421. return Min(2, SDL_TVOS_GetActiveProcessorCount());
  422. #else
  423. return SDL_TVOS_GetActiveProcessorCount();
  424. #endif
  425. #elif defined(__linux__)
  426. struct CpuCoreCount data{};
  427. GetCPUData(&data);
  428. return data.numLogicalCores_;
  429. #elif defined(__EMSCRIPTEN__)
  430. #ifdef __EMSCRIPTEN_PTHREADS__
  431. return emscripten_num_logical_cores();
  432. #else
  433. return 1; // Targeting a single-threaded Emscripten build.
  434. #endif
  435. #else
  436. struct cpu_id_t data;
  437. GetCPUData(&data);
  438. return (unsigned)data.num_logical_cpus;
  439. #endif
  440. }
  441. void SetMiniDumpDir(const String& pathName)
  442. {
  443. miniDumpDir = AddTrailingSlash(pathName);
  444. }
  445. String GetMiniDumpDir()
  446. {
  447. #ifndef MINI_URHO
  448. if (miniDumpDir.Empty())
  449. {
  450. char* pathName = SDL_GetPrefPath("urho3d", "crashdumps");
  451. if (pathName)
  452. {
  453. String ret(pathName);
  454. SDL_free(pathName);
  455. return ret;
  456. }
  457. }
  458. #endif
  459. return miniDumpDir;
  460. }
  461. unsigned long long GetTotalMemory()
  462. {
  463. #if defined(__linux__) && !defined(__ANDROID__)
  464. struct sysinfo s{};
  465. if (sysinfo(&s) != -1)
  466. return s.totalram;
  467. #elif defined(_WIN32)
  468. MEMORYSTATUSEX state;
  469. state.dwLength = sizeof(state);
  470. if (GlobalMemoryStatusEx(&state))
  471. return state.ullTotalPhys;
  472. #elif defined(__APPLE__)
  473. unsigned long long memSize;
  474. size_t len = sizeof(memSize);
  475. int mib[2];
  476. mib[0] = CTL_HW;
  477. mib[1] = HW_MEMSIZE;
  478. sysctl(mib, 2, &memSize, &len, NULL, 0);
  479. return memSize;
  480. #endif
  481. return 0ull;
  482. }
  483. String GetLoginName()
  484. {
  485. #if defined(__linux__) && !defined(__ANDROID__)
  486. struct passwd *p = getpwuid(getuid());
  487. if (p != nullptr)
  488. return p->pw_name;
  489. #elif defined(_WIN32)
  490. char name[UNLEN + 1];
  491. DWORD len = UNLEN + 1;
  492. if (GetUserName(name, &len))
  493. return name;
  494. #elif defined(__APPLE__) && !defined(IOS) && !defined(TVOS)
  495. SCDynamicStoreRef s = SCDynamicStoreCreate(NULL, CFSTR("GetConsoleUser"), NULL, NULL);
  496. if (s != NULL)
  497. {
  498. uid_t u;
  499. CFStringRef n = SCDynamicStoreCopyConsoleUser(s, &u, NULL);
  500. CFRelease(s);
  501. if (n != NULL)
  502. {
  503. char name[256];
  504. Boolean b = CFStringGetCString(n, name, 256, kCFStringEncodingUTF8);
  505. CFRelease(n);
  506. if (b == true)
  507. return name;
  508. }
  509. }
  510. #endif
  511. return "(?)";
  512. }
  513. String GetHostName()
  514. {
  515. #if (defined(__linux__) || defined(__APPLE__)) && !defined(__ANDROID__)
  516. char buffer[256];
  517. if (gethostname(buffer, 256) == 0)
  518. return buffer;
  519. #elif defined(_WIN32)
  520. char buffer[MAX_COMPUTERNAME_LENGTH + 1];
  521. DWORD len = MAX_COMPUTERNAME_LENGTH + 1;
  522. if (GetComputerName(buffer, &len))
  523. return buffer;
  524. #endif
  525. return "(?)";
  526. }
  527. // Disable Windows OS version functionality when compiling mini version for Web, see https://github.com/urho3d/Urho3D/issues/1998
  528. #if defined(_WIN32) && defined(HAVE_RTL_OSVERSIONINFOW) && !defined(MINI_URHO)
  529. using RtlGetVersionPtr = NTSTATUS (WINAPI *)(PRTL_OSVERSIONINFOW);
  530. static void GetOS(RTL_OSVERSIONINFOW *r)
  531. {
  532. HMODULE m = GetModuleHandle("ntdll.dll");
  533. if (m)
  534. {
  535. RtlGetVersionPtr fPtr = (RtlGetVersionPtr) GetProcAddress(m, "RtlGetVersion");
  536. if (r && fPtr && fPtr(r) == 0)
  537. r->dwOSVersionInfoSize = sizeof *r;
  538. }
  539. }
  540. #endif
  541. String GetOSVersion()
  542. {
  543. #if defined(__linux__) && !defined(__ANDROID__)
  544. struct utsname u{};
  545. if (uname(&u) == 0)
  546. return String(u.sysname) + " " + u.release;
  547. #elif defined(_WIN32) && defined(HAVE_RTL_OSVERSIONINFOW) && !defined(MINI_URHO)
  548. RTL_OSVERSIONINFOW r;
  549. GetOS(&r);
  550. // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx
  551. if (r.dwMajorVersion == 5 && r.dwMinorVersion == 0)
  552. return "Windows 2000";
  553. else if (r.dwMajorVersion == 5 && r.dwMinorVersion == 1)
  554. return "Windows XP";
  555. else if (r.dwMajorVersion == 5 && r.dwMinorVersion == 2)
  556. return "Windows XP 64-Bit Edition/Windows Server 2003/Windows Server 2003 R2";
  557. else if (r.dwMajorVersion == 6 && r.dwMinorVersion == 0)
  558. return "Windows Vista/Windows Server 2008";
  559. else if (r.dwMajorVersion == 6 && r.dwMinorVersion == 1)
  560. return "Windows 7/Windows Server 2008 R2";
  561. else if (r.dwMajorVersion == 6 && r.dwMinorVersion == 2)
  562. return "Windows 8/Windows Server 2012";
  563. else if (r.dwMajorVersion == 6 && r.dwMinorVersion == 3)
  564. return "Windows 8.1/Windows Server 2012 R2";
  565. else if (r.dwMajorVersion == 10 && r.dwMinorVersion == 0)
  566. return "Windows 10/Windows Server 2016";
  567. else
  568. return "Windows Unknown";
  569. #elif defined(__APPLE__)
  570. char kernel_r[256];
  571. size_t size = sizeof(kernel_r);
  572. if (sysctlbyname("kern.osrelease", &kernel_r, &size, NULL, 0) != -1)
  573. {
  574. Vector<String> kernel_version = String(kernel_r).Split('.');
  575. String version = "macOS/Mac OS X ";
  576. int major = ToInt(kernel_version[0]);
  577. int minor = ToInt(kernel_version[1]);
  578. // https://en.wikipedia.org/wiki/Darwin_(operating_system)
  579. if (major == 16) // macOS Sierra
  580. {
  581. version += "Sierra ";
  582. switch(minor)
  583. {
  584. case 0: version += "10.12.0 "; break;
  585. case 1: version += "10.12.1 "; break;
  586. case 3: version += "10.12.2 "; break;
  587. }
  588. }
  589. else if (major == 15) // OS X El Capitan
  590. {
  591. version += "El Capitan ";
  592. switch(minor)
  593. {
  594. case 0: version += "10.11.0 "; break;
  595. case 6: version += "10.11.6 "; break;
  596. }
  597. }
  598. else if (major == 14) // OS X Yosemite
  599. {
  600. version += "Yosemite ";
  601. switch(minor)
  602. {
  603. case 0: version += "10.10.0 "; break;
  604. case 5: version += "10.10.5 "; break;
  605. }
  606. }
  607. else if (major == 13) // OS X Mavericks
  608. {
  609. version += "Mavericks ";
  610. switch(minor)
  611. {
  612. case 0: version += "10.9.0 "; break;
  613. case 4: version += "10.9.5 "; break;
  614. }
  615. }
  616. else if (major == 12) // OS X Mountain Lion
  617. {
  618. version += "Mountain Lion ";
  619. switch(minor)
  620. {
  621. case 0: version += "10.8.0 "; break;
  622. case 6: version += "10.8.5 "; break;
  623. }
  624. }
  625. else if (major == 11) // Mac OS X Lion
  626. {
  627. version += "Lion ";
  628. switch(minor)
  629. {
  630. case 0: version += "10.7.0 "; break;
  631. case 4: version += "10.7.5 "; break;
  632. }
  633. }
  634. else
  635. {
  636. version += "Unknown ";
  637. }
  638. return version + " (Darwin kernel " + kernel_version[0] + "." + kernel_version[1] + "." + kernel_version[2] + ")";
  639. }
  640. #endif
  641. return "(?)";
  642. }
  643. }