No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

136 líneas
2.9 KiB

  1. inline proc get_cwd() -> char* {
  2. const int buf_size = 2048;
  3. char* res = (char*)malloc(buf_size * sizeof(char));
  4. #ifdef _MSC_VER
  5. _getcwd(res, buf_size);
  6. #else
  7. getcwd(res, buf_size);
  8. #endif
  9. return res;
  10. }
  11. inline proc change_cwd(char* dir) -> void {
  12. #ifdef _MSC_VER
  13. _chdir(dir);
  14. #else
  15. chdir(dir);
  16. #endif
  17. }
  18. proc get_exe_dir() -> char* {
  19. #ifdef _MSC_VER
  20. DWORD last_error;
  21. DWORD result;
  22. DWORD path_size = 1024;
  23. char* path = (char*)malloc(1024);
  24. while (true) {
  25. memset(path, 0, path_size);
  26. result = GetModuleFileName(0, path, path_size - 1);
  27. last_error = GetLastError();
  28. if (0 == result) {
  29. free(path);
  30. path = 0;
  31. break;
  32. }
  33. else if (result == path_size - 1) {
  34. free(path);
  35. /* May need to also check for ERROR_SUCCESS here if XP/2K */
  36. if (ERROR_INSUFFICIENT_BUFFER != last_error) {
  37. path = 0;
  38. break;
  39. }
  40. path_size = path_size * 2;
  41. path = (char*)malloc(path_size);
  42. }
  43. else
  44. break;
  45. }
  46. if (!path) {
  47. fprintf(stderr, "Failure: %ld\n", last_error);
  48. return nullptr;
  49. }
  50. else {
  51. // remove the exe name, so we are only left with the path
  52. int index_in_path = -1;
  53. int last_backslash = -1;
  54. char c;
  55. while ((c = path[++index_in_path]) != '\0') {
  56. if (c == '\\')
  57. last_backslash = index_in_path;
  58. }
  59. // we are assuming there are some backslashes
  60. path[last_backslash+1] = '\0';
  61. return path;
  62. }
  63. #else
  64. ssize_t size = 512, i, n;
  65. char *path, *temp;
  66. while (1) {
  67. size_t used;
  68. path = (char*)malloc(size);
  69. if (!path) {
  70. errno = ENOMEM;
  71. return NULL;
  72. }
  73. used = readlink("/proc/self/exe", path, size);
  74. if (used == -1) {
  75. const int saved_errno = errno;
  76. free(path);
  77. errno = saved_errno;
  78. return NULL;
  79. } else
  80. if (used < 1) {
  81. free(path);
  82. errno = EIO;
  83. return NULL;
  84. }
  85. if ((size_t)used >= size) {
  86. free(path);
  87. size = (size | 2047) + 2049;
  88. continue;
  89. }
  90. size = (size_t)used;
  91. break;
  92. }
  93. /* Find final slash. */
  94. n = 0;
  95. for (i = 0; i < size; i++)
  96. if (path[i] == '/')
  97. n = i;
  98. /* Optimize allocated size,
  99. ensuring there is room for
  100. a final slash and a
  101. string-terminating '\0', */
  102. temp = path;
  103. path = (char*)realloc(temp, n + 2);
  104. if (!path) {
  105. free(temp);
  106. errno = ENOMEM;
  107. return NULL;
  108. }
  109. /* and properly trim and terminate the path string. */
  110. path[n+0] = '/';
  111. path[n+1] = '\0';
  112. return path;
  113. #endif
  114. }