Environment.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "il2cpp-config.h"
  2. #if !IL2CPP_USE_GENERIC_ENVIRONMENT && IL2CPP_TARGET_POSIX && !IL2CPP_USE_PLATFORM_SPECIFIC_ENVIRON
  3. #include "il2cpp-class-internals.h"
  4. #include "os/Environment.h"
  5. #include "il2cpp-api.h"
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <sys/utsname.h>
  9. #if defined(__APPLE__) && !defined(__arm__)
  10. // Apple defines this in crt_externs.h but doesn't provide that header for
  11. // arm-apple-darwin9. We'll manually define the symbol on Apple as it does
  12. // in fact exist on all implementations (so far)
  13. extern "C" char*** _NSGetEnviron(void);
  14. #define environ (*_NSGetEnviron())
  15. #else
  16. extern char **environ; // GNU C library
  17. #endif
  18. namespace il2cpp
  19. {
  20. namespace os
  21. {
  22. int32_t Environment::GetProcessorCount()
  23. {
  24. int count = 1;
  25. #ifdef _SC_NPROCESSORS_ONLN
  26. count = (int)sysconf(_SC_NPROCESSORS_ONLN);
  27. if (count > 0)
  28. return count;
  29. #endif
  30. #ifdef USE_SYSCTL
  31. {
  32. int mib[2];
  33. size_t len = sizeof(int);
  34. mib[0] = CTL_HW;
  35. mib[1] = HW_NCPU;
  36. if (sysctl(mib, 2, &count, &len, NULL, 0) == 0)
  37. return count;
  38. }
  39. #endif
  40. return count;
  41. }
  42. #if !RUNTIME_TINY
  43. std::string Environment::GetMachineName()
  44. {
  45. const int n = 512;
  46. char buf[n];
  47. if (gethostname(buf, sizeof(buf)) == 0)
  48. {
  49. buf[n - 1] = 0;
  50. int i;
  51. // try truncating the string at the first dot
  52. for (i = 0; i < n; i++)
  53. {
  54. if (buf[i] == '.')
  55. {
  56. buf[i] = 0;
  57. break;
  58. }
  59. }
  60. return buf;
  61. }
  62. return NULL;
  63. }
  64. std::string Environment::GetOsVersionString()
  65. {
  66. struct utsname name;
  67. if (uname(&name) >= 0)
  68. return name.release;
  69. return "0.0.0.0";
  70. }
  71. std::string Environment::GetOsUserName()
  72. {
  73. const std::string username(GetEnvironmentVariable("USER"));
  74. return username.empty() ? "Unknown" : username;
  75. }
  76. std::string Environment::GetEnvironmentVariable(const std::string& name)
  77. {
  78. const char* variable = getenv(name.c_str());
  79. return variable ? std::string(variable) : std::string();
  80. }
  81. void Environment::SetEnvironmentVariable(const std::string& name, const std::string& value)
  82. {
  83. if (value.empty())
  84. {
  85. unsetenv(name.c_str());
  86. }
  87. else
  88. {
  89. setenv(name.c_str(), value.c_str(), 1); // 1 means overwrite
  90. }
  91. }
  92. std::vector<std::string> Environment::GetEnvironmentVariableNames()
  93. {
  94. std::vector<std::string> result;
  95. for (char **envvar = environ; *envvar != NULL; ++envvar)
  96. {
  97. const char* equalAddress = strchr(*envvar, '=');
  98. if (equalAddress != NULL)
  99. result.push_back(std::string(*envvar, size_t(equalAddress - *envvar)));
  100. }
  101. return result;
  102. }
  103. std::string Environment::GetHomeDirectory()
  104. {
  105. static std::string homeDirectory;
  106. if (!homeDirectory.empty())
  107. return homeDirectory;
  108. homeDirectory = GetEnvironmentVariable("HOME");
  109. return homeDirectory.empty() ? "/" : homeDirectory;
  110. }
  111. std::vector<std::string> Environment::GetLogicalDrives()
  112. {
  113. std::vector<std::string> result;
  114. // This implementation is not correct according to the definition of this icall, but this is
  115. // the only "logical drive" that the Mono version in Unity returns for OSX.
  116. result.push_back("/");
  117. // TODO: Implement additional logic for Linux
  118. return result;
  119. }
  120. void Environment::Exit(int result)
  121. {
  122. exit(result);
  123. }
  124. #endif // !RUNTIME_TINY
  125. NORETURN void Environment::Abort()
  126. {
  127. abort();
  128. }
  129. #if !RUNTIME_TINY
  130. utils::Expected<std::string> Environment::GetWindowsFolderPath(int folder)
  131. {
  132. // This should only be called on Windows.
  133. return std::string();
  134. }
  135. utils::Expected<bool> Environment::Is64BitOs()
  136. {
  137. struct utsname name;
  138. if (uname(&name) >= 0)
  139. {
  140. return strcmp(name.machine, "x86_64") == 0 || strncmp(name.machine, "aarch64", 7) == 0 || strncmp(name.machine, "ppc64", 5) == 0;
  141. }
  142. return false;
  143. }
  144. #endif // !RUNTIME_TINY
  145. }
  146. }
  147. #endif