SDL_sysfilesystem.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2017 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #ifdef SDL_FILESYSTEM_UNIX
  20. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  21. /* System dependent filesystem routines */
  22. #include <errno.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <sys/stat.h>
  27. #include <sys/types.h>
  28. #include <limits.h>
  29. #include <fcntl.h>
  30. #if defined(__FREEBSD__) || defined(__OPENBSD__)
  31. #include <sys/sysctl.h>
  32. #endif
  33. #include "SDL_error.h"
  34. #include "SDL_stdinc.h"
  35. #include "SDL_filesystem.h"
  36. /* QNX's /proc/self/exefile is a text file and not a symlink. */
  37. #if !defined(__QNXNTO__)
  38. static char *
  39. readSymLink(const char *path)
  40. {
  41. char *retval = NULL;
  42. ssize_t len = 64;
  43. ssize_t rc = -1;
  44. while (1)
  45. {
  46. char *ptr = (char *) SDL_realloc(retval, (size_t) len);
  47. if (ptr == NULL) {
  48. SDL_OutOfMemory();
  49. break;
  50. }
  51. retval = ptr;
  52. rc = readlink(path, retval, len);
  53. if (rc == -1) {
  54. break; /* not a symlink, i/o error, etc. */
  55. } else if (rc < len) {
  56. retval[rc] = '\0'; /* readlink doesn't null-terminate. */
  57. return retval; /* we're good to go. */
  58. }
  59. len *= 2; /* grow buffer, try again. */
  60. }
  61. SDL_free(retval);
  62. return NULL;
  63. }
  64. #endif
  65. char *
  66. SDL_GetBasePath(void)
  67. {
  68. char *retval = NULL;
  69. #if defined(__FREEBSD__)
  70. char fullpath[PATH_MAX];
  71. size_t buflen = sizeof (fullpath);
  72. const int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
  73. if (sysctl(mib, SDL_arraysize(mib), fullpath, &buflen, NULL, 0) != -1) {
  74. retval = SDL_strdup(fullpath);
  75. if (!retval) {
  76. SDL_OutOfMemory();
  77. return NULL;
  78. }
  79. }
  80. #endif
  81. #if defined(__OPENBSD__)
  82. char **retvalargs;
  83. size_t len;
  84. const int mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
  85. if (sysctl(mib, 4, NULL, &len, NULL, 0) != -1) {
  86. retvalargs = SDL_malloc(len);
  87. if (!retvalargs) {
  88. SDL_OutOfMemory();
  89. return NULL;
  90. }
  91. sysctl(mib, 4, retvalargs, &len, NULL, 0);
  92. retval = SDL_malloc(PATH_MAX + 1);
  93. if (retval)
  94. realpath(retvalargs[0], retval);
  95. SDL_free(retvalargs);
  96. }
  97. #endif
  98. #if defined(__SOLARIS__)
  99. const char *path = getexecname();
  100. if ((path != NULL) && (path[0] == '/')) { /* must be absolute path... */
  101. retval = SDL_strdup(path);
  102. if (!retval) {
  103. SDL_OutOfMemory();
  104. return NULL;
  105. }
  106. }
  107. #endif
  108. /* is a Linux-style /proc filesystem available? */
  109. if (!retval && (access("/proc", F_OK) == 0)) {
  110. #if defined(__FREEBSD__)
  111. retval = readSymLink("/proc/curproc/file");
  112. #elif defined(__NETBSD__)
  113. retval = readSymLink("/proc/curproc/exe");
  114. #elif defined(__QNXNTO__)
  115. retval = SDL_LoadFile("/proc/self/exefile", NULL);
  116. #else
  117. retval = readSymLink("/proc/self/exe"); /* linux. */
  118. if (retval == NULL) {
  119. /* older kernels don't have /proc/self ... try PID version... */
  120. char path[64];
  121. const int rc = (int) SDL_snprintf(path, sizeof(path),
  122. "/proc/%llu/exe",
  123. (unsigned long long) getpid());
  124. if ( (rc > 0) && (rc < sizeof(path)) ) {
  125. retval = readSymLink(path);
  126. }
  127. }
  128. #endif
  129. }
  130. /* If we had access to argv[0] here, we could check it for a path,
  131. or troll through $PATH looking for it, too. */
  132. if (retval != NULL) { /* chop off filename. */
  133. char *ptr = SDL_strrchr(retval, '/');
  134. if (ptr != NULL) {
  135. *(ptr+1) = '\0';
  136. } else { /* shouldn't happen, but just in case... */
  137. SDL_free(retval);
  138. retval = NULL;
  139. }
  140. }
  141. if (retval != NULL) {
  142. /* try to shrink buffer... */
  143. char *ptr = (char *) SDL_realloc(retval, strlen(retval) + 1);
  144. if (ptr != NULL)
  145. retval = ptr; /* oh well if it failed. */
  146. }
  147. return retval;
  148. }
  149. char *
  150. SDL_GetPrefPath(const char *org, const char *app)
  151. {
  152. /*
  153. * We use XDG's base directory spec, even if you're not on Linux.
  154. * This isn't strictly correct, but the results are relatively sane
  155. * in any case.
  156. *
  157. * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
  158. */
  159. const char *envr = SDL_getenv("XDG_DATA_HOME");
  160. const char *append;
  161. char *retval = NULL;
  162. char *ptr = NULL;
  163. size_t len = 0;
  164. if (!app) {
  165. SDL_InvalidParamError("app");
  166. return NULL;
  167. }
  168. if (!org) {
  169. org = "";
  170. }
  171. if (!envr) {
  172. /* You end up with "$HOME/.local/share/Game Name 2" */
  173. envr = SDL_getenv("HOME");
  174. if (!envr) {
  175. /* we could take heroic measures with /etc/passwd, but oh well. */
  176. SDL_SetError("neither XDG_DATA_HOME nor HOME environment is set");
  177. return NULL;
  178. }
  179. append = "/.local/share/";
  180. } else {
  181. append = "/";
  182. }
  183. len = SDL_strlen(envr);
  184. if (envr[len - 1] == '/')
  185. append += 1;
  186. len += SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3;
  187. retval = (char *) SDL_malloc(len);
  188. if (!retval) {
  189. SDL_OutOfMemory();
  190. return NULL;
  191. }
  192. if (*org) {
  193. SDL_snprintf(retval, len, "%s%s%s/%s/", envr, append, org, app);
  194. } else {
  195. SDL_snprintf(retval, len, "%s%s%s/", envr, append, app);
  196. }
  197. for (ptr = retval+1; *ptr; ptr++) {
  198. if (*ptr == '/') {
  199. *ptr = '\0';
  200. if (mkdir(retval, 0700) != 0 && errno != EEXIST)
  201. goto error;
  202. *ptr = '/';
  203. }
  204. }
  205. if (mkdir(retval, 0700) != 0 && errno != EEXIST) {
  206. error:
  207. SDL_SetError("Couldn't create directory '%s': '%s'", retval, strerror(errno));
  208. SDL_free(retval);
  209. return NULL;
  210. }
  211. return retval;
  212. }
  213. #endif /* SDL_FILESYSTEM_UNIX */
  214. /* vi: set ts=4 sw=4 expandtab: */