Path.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_POSIX && !RUNTIME_TINY && !IL2CPP_USE_PLATFORM_SPECIFIC_PATH
  3. #include "os/Environment.h"
  4. #include "os/Path.h"
  5. #include "utils/PathUtils.h"
  6. #include <string>
  7. #if defined(__APPLE__)
  8. #include "mach-o/dyld.h"
  9. #elif IL2CPP_TARGET_LINUX || IL2CPP_TARGET_ANDROID
  10. #include <linux/limits.h>
  11. #include <sys/stat.h>
  12. #include <sys/types.h>
  13. #include <unistd.h>
  14. #include <stdio.h>
  15. #elif IL2CPP_TARGET_QNX
  16. #include <unistd.h>
  17. #endif
  18. namespace il2cpp
  19. {
  20. namespace os
  21. {
  22. std::string Path::GetExecutablePath()
  23. {
  24. #if defined(__APPLE__)
  25. char path[1024];
  26. uint32_t size = sizeof(path);
  27. if (_NSGetExecutablePath(path, &size) == 0)
  28. return path;
  29. std::string result;
  30. result.resize(size + 1);
  31. _NSGetExecutablePath(&result[0], &size);
  32. return result;
  33. #elif IL2CPP_TARGET_LINUX || IL2CPP_TARGET_ANDROID
  34. char path[PATH_MAX];
  35. char dest[PATH_MAX + 1];
  36. //readlink does not null terminate
  37. memset(dest, 0, PATH_MAX + 1);
  38. pid_t pid = getpid();
  39. sprintf(path, "/proc/%d/exe", pid);
  40. if (readlink(path, dest, PATH_MAX) == -1)
  41. return std::string();
  42. return dest;
  43. #elif IL2CPP_TARGET_QNX
  44. char path[PATH_MAX];
  45. char dest[PATH_MAX + 1];
  46. pid_t pid = getpid();
  47. sprintf(path, "/proc/%d/exefile", pid);
  48. auto* fh = fopen(path, "r");
  49. if (fh)
  50. {
  51. const auto read = fread(dest, 1, sizeof(dest), fh);
  52. const auto errorFlag = ferror(fh);
  53. fclose(fh);
  54. if (errorFlag == 0)
  55. {
  56. return dest;
  57. }
  58. }
  59. return "";
  60. #else
  61. return std::string();
  62. #endif
  63. }
  64. std::string Path::GetApplicationFolder()
  65. {
  66. return utils::PathUtils::DirectoryName(GetExecutablePath());
  67. }
  68. std::string Path::GetTempPath()
  69. {
  70. static const char* tmpdirs[] = { "TMPDIR", "TMP", "TEMP", NULL};
  71. for (size_t i = 0; tmpdirs[i] != NULL; ++i)
  72. {
  73. std::string tmpdir = Environment::GetEnvironmentVariable(tmpdirs[i]);
  74. if (!tmpdir.empty())
  75. return tmpdir;
  76. }
  77. #if IL2CPP_TARGET_ANDROID
  78. return std::string("/data/local/tmp");
  79. #else
  80. return std::string("/tmp");
  81. #endif
  82. }
  83. bool Path::IsAbsolute(const std::string& path)
  84. {
  85. return path[0] == '/';
  86. }
  87. }
  88. }
  89. #endif