SDL_sysfilesystem.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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 <stdio.h>
  23. #include <sys/stat.h>
  24. #include <sys/types.h>
  25. #include <dirent.h>
  26. #include <errno.h>
  27. #include <fcntl.h>
  28. #include <limits.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #if defined(SDL_PLATFORM_FREEBSD) || defined(SDL_PLATFORM_OPENBSD)
  33. #include <sys/sysctl.h>
  34. #endif
  35. static char *readSymLink(const char *path)
  36. {
  37. char *retval = NULL;
  38. ssize_t len = 64;
  39. ssize_t rc = -1;
  40. while (1) {
  41. char *ptr = (char *)SDL_realloc(retval, (size_t)len);
  42. if (!ptr) {
  43. break;
  44. }
  45. retval = ptr;
  46. rc = readlink(path, retval, len);
  47. if (rc == -1) {
  48. break; /* not a symlink, i/o error, etc. */
  49. } else if (rc < len) {
  50. retval[rc] = '\0'; /* readlink doesn't null-terminate. */
  51. return retval; /* we're good to go. */
  52. }
  53. len *= 2; /* grow buffer, try again. */
  54. }
  55. SDL_free(retval);
  56. return NULL;
  57. }
  58. #ifdef SDL_PLATFORM_OPENBSD
  59. static char *search_path_for_binary(const char *bin)
  60. {
  61. char *envr = SDL_getenv("PATH");
  62. size_t alloc_size;
  63. char *exe = NULL;
  64. char *start = envr;
  65. char *ptr;
  66. if (!envr) {
  67. SDL_SetError("No $PATH set");
  68. return NULL;
  69. }
  70. envr = SDL_strdup(envr);
  71. if (!envr) {
  72. return NULL;
  73. }
  74. SDL_assert(bin != NULL);
  75. alloc_size = SDL_strlen(bin) + SDL_strlen(envr) + 2;
  76. exe = (char *)SDL_malloc(alloc_size);
  77. do {
  78. ptr = SDL_strchr(start, ':'); /* find next $PATH separator. */
  79. if (ptr != start) {
  80. if (ptr) {
  81. *ptr = '\0';
  82. }
  83. /* build full binary path... */
  84. SDL_snprintf(exe, alloc_size, "%s%s%s", start, (ptr && (ptr[-1] == '/')) ? "" : "/", bin);
  85. if (access(exe, X_OK) == 0) { /* Exists as executable? We're done. */
  86. SDL_free(envr);
  87. return exe;
  88. }
  89. }
  90. start = ptr + 1; /* start points to beginning of next element. */
  91. } while (ptr);
  92. SDL_free(envr);
  93. SDL_free(exe);
  94. SDL_SetError("Process not found in $PATH");
  95. return NULL; /* doesn't exist in path. */
  96. }
  97. #endif
  98. char *SDL_SYS_GetBasePath(void)
  99. {
  100. char *retval = NULL;
  101. #ifdef SDL_PLATFORM_FREEBSD
  102. char fullpath[PATH_MAX];
  103. size_t buflen = sizeof(fullpath);
  104. const int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
  105. if (sysctl(mib, SDL_arraysize(mib), fullpath, &buflen, NULL, 0) != -1) {
  106. retval = SDL_strdup(fullpath);
  107. if (!retval) {
  108. return NULL;
  109. }
  110. }
  111. #endif
  112. #ifdef SDL_PLATFORM_OPENBSD
  113. /* Please note that this will fail if the process was launched with a relative path and $PWD + the cwd have changed, or argv is altered. So don't do that. Or add a new sysctl to OpenBSD. */
  114. char **cmdline;
  115. size_t len;
  116. const int mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
  117. if (sysctl(mib, 4, NULL, &len, NULL, 0) != -1) {
  118. char *exe, *pwddst;
  119. char *realpathbuf = (char *)SDL_malloc(PATH_MAX + 1);
  120. if (!realpathbuf) {
  121. return NULL;
  122. }
  123. cmdline = SDL_malloc(len);
  124. if (!cmdline) {
  125. SDL_free(realpathbuf);
  126. return NULL;
  127. }
  128. sysctl(mib, 4, cmdline, &len, NULL, 0);
  129. exe = cmdline[0];
  130. pwddst = NULL;
  131. if (SDL_strchr(exe, '/') == NULL) { /* not a relative or absolute path, check $PATH for it */
  132. exe = search_path_for_binary(cmdline[0]);
  133. } else {
  134. if (exe && *exe == '.') {
  135. const char *pwd = SDL_getenv("PWD");
  136. if (pwd && *pwd) {
  137. SDL_asprintf(&pwddst, "%s/%s", pwd, exe);
  138. }
  139. }
  140. }
  141. if (exe) {
  142. if (!pwddst) {
  143. if (realpath(exe, realpathbuf) != NULL) {
  144. retval = realpathbuf;
  145. }
  146. } else {
  147. if (realpath(pwddst, realpathbuf) != NULL) {
  148. retval = realpathbuf;
  149. }
  150. SDL_free(pwddst);
  151. }
  152. if (exe != cmdline[0]) {
  153. SDL_free(exe);
  154. }
  155. }
  156. if (!retval) {
  157. SDL_free(realpathbuf);
  158. }
  159. SDL_free(cmdline);
  160. }
  161. #endif
  162. /* is a Linux-style /proc filesystem available? */
  163. if (!retval && (access("/proc", F_OK) == 0)) {
  164. /* !!! FIXME: after 2.0.6 ships, let's delete this code and just
  165. use the /proc/%llu version. There's no reason to have
  166. two copies of this plus all the #ifdefs. --ryan. */
  167. #ifdef SDL_PLATFORM_FREEBSD
  168. retval = readSymLink("/proc/curproc/file");
  169. #elif defined(SDL_PLATFORM_NETBSD)
  170. retval = readSymLink("/proc/curproc/exe");
  171. #elif defined(SDL_PLATFORM_SOLARIS)
  172. retval = readSymLink("/proc/self/path/a.out");
  173. #else
  174. retval = readSymLink("/proc/self/exe"); /* linux. */
  175. if (!retval) {
  176. /* older kernels don't have /proc/self ... try PID version... */
  177. char path[64];
  178. const int rc = SDL_snprintf(path, sizeof(path),
  179. "/proc/%llu/exe",
  180. (unsigned long long)getpid());
  181. if ((rc > 0) && (rc < sizeof(path))) {
  182. retval = readSymLink(path);
  183. }
  184. }
  185. #endif
  186. }
  187. #ifdef SDL_PLATFORM_SOLARIS /* try this as a fallback if /proc didn't pan out */
  188. if (!retval) {
  189. const char *path = getexecname();
  190. if ((path) && (path[0] == '/')) { /* must be absolute path... */
  191. retval = SDL_strdup(path);
  192. if (!retval) {
  193. return NULL;
  194. }
  195. }
  196. }
  197. #endif
  198. /* If we had access to argv[0] here, we could check it for a path,
  199. or troll through $PATH looking for it, too. */
  200. if (retval) { /* chop off filename. */
  201. char *ptr = SDL_strrchr(retval, '/');
  202. if (ptr) {
  203. *(ptr + 1) = '\0';
  204. } else { /* shouldn't happen, but just in case... */
  205. SDL_free(retval);
  206. retval = NULL;
  207. }
  208. }
  209. if (retval) {
  210. /* try to shrink buffer... */
  211. char *ptr = (char *)SDL_realloc(retval, SDL_strlen(retval) + 1);
  212. if (ptr) {
  213. retval = ptr; /* oh well if it failed. */
  214. }
  215. }
  216. return retval;
  217. }
  218. char *SDL_SYS_GetPrefPath(const char *org, const char *app)
  219. {
  220. /*
  221. * We use XDG's base directory spec, even if you're not on Linux.
  222. * This isn't strictly correct, but the results are relatively sane
  223. * in any case.
  224. *
  225. * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
  226. */
  227. const char *envr = SDL_getenv("XDG_DATA_HOME");
  228. const char *append;
  229. char *retval = NULL;
  230. char *ptr = NULL;
  231. size_t len = 0;
  232. if (!app) {
  233. SDL_InvalidParamError("app");
  234. return NULL;
  235. }
  236. if (!org) {
  237. org = "";
  238. }
  239. if (!envr) {
  240. /* You end up with "$HOME/.local/share/Game Name 2" */
  241. envr = SDL_getenv("HOME");
  242. if (!envr) {
  243. /* we could take heroic measures with /etc/passwd, but oh well. */
  244. SDL_SetError("neither XDG_DATA_HOME nor HOME environment is set");
  245. return NULL;
  246. }
  247. append = "/.local/share/";
  248. } else {
  249. append = "/";
  250. }
  251. len = SDL_strlen(envr);
  252. if (envr[len - 1] == '/') {
  253. append += 1;
  254. }
  255. len += SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3;
  256. retval = (char *)SDL_malloc(len);
  257. if (!retval) {
  258. return NULL;
  259. }
  260. if (*org) {
  261. (void)SDL_snprintf(retval, len, "%s%s%s/%s/", envr, append, org, app);
  262. } else {
  263. (void)SDL_snprintf(retval, len, "%s%s%s/", envr, append, app);
  264. }
  265. for (ptr = retval + 1; *ptr; ptr++) {
  266. if (*ptr == '/') {
  267. *ptr = '\0';
  268. if (mkdir(retval, 0700) != 0 && errno != EEXIST) {
  269. goto error;
  270. }
  271. *ptr = '/';
  272. }
  273. }
  274. if (mkdir(retval, 0700) != 0 && errno != EEXIST) {
  275. error:
  276. SDL_SetError("Couldn't create directory '%s': '%s'", retval, strerror(errno));
  277. SDL_free(retval);
  278. return NULL;
  279. }
  280. return retval;
  281. }
  282. /*
  283. The two functions below (prefixed with `xdg_`) have been copied from:
  284. https://gitlab.freedesktop.org/xdg/xdg-user-dirs/-/blob/master/xdg-user-dir-lookup.c
  285. and have been adapted to work with SDL. They are licensed under the following
  286. terms:
  287. Copyright (c) 2007 Red Hat, Inc.
  288. Permission is hereby granted, free of charge, to any person
  289. obtaining a copy of this software and associated documentation files
  290. (the "Software"), to deal in the Software without restriction,
  291. including without limitation the rights to use, copy, modify, merge,
  292. publish, distribute, sublicense, and/or sell copies of the Software,
  293. and to permit persons to whom the Software is furnished to do so,
  294. subject to the following conditions:
  295. The above copyright notice and this permission notice shall be
  296. included in all copies or substantial portions of the Software.
  297. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  298. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  299. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  300. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  301. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  302. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  303. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  304. SOFTWARE.
  305. */
  306. static char *xdg_user_dir_lookup_with_fallback (const char *type, const char *fallback)
  307. {
  308. FILE *file;
  309. char *home_dir, *config_home, *config_file;
  310. char buffer[512];
  311. char *user_dir;
  312. char *p, *d;
  313. int len;
  314. int relative;
  315. size_t l;
  316. home_dir = SDL_getenv ("HOME");
  317. if (!home_dir)
  318. goto error;
  319. config_home = SDL_getenv ("XDG_CONFIG_HOME");
  320. if (!config_home || config_home[0] == 0)
  321. {
  322. l = SDL_strlen (home_dir) + SDL_strlen ("/.config/user-dirs.dirs") + 1;
  323. config_file = (char*) SDL_malloc (l);
  324. if (!config_file)
  325. goto error;
  326. SDL_strlcpy (config_file, home_dir, l);
  327. SDL_strlcat (config_file, "/.config/user-dirs.dirs", l);
  328. }
  329. else
  330. {
  331. l = SDL_strlen (config_home) + SDL_strlen ("/user-dirs.dirs") + 1;
  332. config_file = (char*) SDL_malloc (l);
  333. if (!config_file)
  334. goto error;
  335. SDL_strlcpy (config_file, config_home, l);
  336. SDL_strlcat (config_file, "/user-dirs.dirs", l);
  337. }
  338. file = fopen (config_file, "r");
  339. SDL_free (config_file);
  340. if (!file)
  341. goto error;
  342. user_dir = NULL;
  343. while (fgets (buffer, sizeof (buffer), file))
  344. {
  345. /* Remove newline at end */
  346. len = SDL_strlen (buffer);
  347. if (len > 0 && buffer[len-1] == '\n')
  348. buffer[len-1] = 0;
  349. p = buffer;
  350. while (*p == ' ' || *p == '\t')
  351. p++;
  352. if (SDL_strncmp (p, "XDG_", 4) != 0)
  353. continue;
  354. p += 4;
  355. if (SDL_strncmp (p, type, SDL_strlen (type)) != 0)
  356. continue;
  357. p += SDL_strlen (type);
  358. if (SDL_strncmp (p, "_DIR", 4) != 0)
  359. continue;
  360. p += 4;
  361. while (*p == ' ' || *p == '\t')
  362. p++;
  363. if (*p != '=')
  364. continue;
  365. p++;
  366. while (*p == ' ' || *p == '\t')
  367. p++;
  368. if (*p != '"')
  369. continue;
  370. p++;
  371. relative = 0;
  372. if (SDL_strncmp (p, "$HOME/", 6) == 0)
  373. {
  374. p += 6;
  375. relative = 1;
  376. }
  377. else if (*p != '/')
  378. continue;
  379. SDL_free (user_dir);
  380. if (relative)
  381. {
  382. l = SDL_strlen (home_dir) + 1 + SDL_strlen (p) + 1;
  383. user_dir = (char*) SDL_malloc (l);
  384. if (!user_dir)
  385. goto error2;
  386. SDL_strlcpy (user_dir, home_dir, l);
  387. SDL_strlcat (user_dir, "/", l);
  388. }
  389. else
  390. {
  391. user_dir = (char*) SDL_malloc (SDL_strlen (p) + 1);
  392. if (!user_dir)
  393. goto error2;
  394. *user_dir = 0;
  395. }
  396. d = user_dir + SDL_strlen (user_dir);
  397. while (*p && *p != '"')
  398. {
  399. if ((*p == '\\') && (*(p+1) != 0))
  400. p++;
  401. *d++ = *p++;
  402. }
  403. *d = 0;
  404. }
  405. error2:
  406. fclose (file);
  407. if (user_dir)
  408. return user_dir;
  409. error:
  410. if (fallback)
  411. return SDL_strdup (fallback);
  412. return NULL;
  413. }
  414. static char *xdg_user_dir_lookup (const char *type)
  415. {
  416. char *dir, *home_dir, *user_dir;
  417. dir = xdg_user_dir_lookup_with_fallback(type, NULL);
  418. if (dir)
  419. return dir;
  420. home_dir = SDL_getenv("HOME");
  421. if (!home_dir)
  422. return NULL;
  423. /* Special case desktop for historical compatibility */
  424. if (SDL_strcmp(type, "DESKTOP") == 0) {
  425. size_t length = SDL_strlen(home_dir) + SDL_strlen("/Desktop") + 1;
  426. user_dir = (char*) SDL_malloc(length);
  427. if (!user_dir)
  428. return NULL;
  429. SDL_strlcpy(user_dir, home_dir, length);
  430. SDL_strlcat(user_dir, "/Desktop", length);
  431. return user_dir;
  432. }
  433. return NULL;
  434. }
  435. char *SDL_SYS_GetUserFolder(SDL_Folder folder)
  436. {
  437. const char *param = NULL;
  438. char *retval;
  439. char *newretval;
  440. /* According to `man xdg-user-dir`, the possible values are:
  441. DESKTOP
  442. DOWNLOAD
  443. TEMPLATES
  444. PUBLICSHARE
  445. DOCUMENTS
  446. MUSIC
  447. PICTURES
  448. VIDEOS
  449. */
  450. switch(folder) {
  451. case SDL_FOLDER_HOME:
  452. param = SDL_getenv("HOME");
  453. if (!param) {
  454. SDL_SetError("No $HOME environment variable available");
  455. return NULL;
  456. }
  457. retval = SDL_strdup(param);
  458. goto append_slash;
  459. case SDL_FOLDER_DESKTOP:
  460. param = "DESKTOP";
  461. break;
  462. case SDL_FOLDER_DOCUMENTS:
  463. param = "DOCUMENTS";
  464. break;
  465. case SDL_FOLDER_DOWNLOADS:
  466. param = "DOWNLOAD";
  467. break;
  468. case SDL_FOLDER_MUSIC:
  469. param = "MUSIC";
  470. break;
  471. case SDL_FOLDER_PICTURES:
  472. param = "PICTURES";
  473. break;
  474. case SDL_FOLDER_PUBLICSHARE:
  475. param = "PUBLICSHARE";
  476. break;
  477. case SDL_FOLDER_SAVEDGAMES:
  478. SDL_SetError("Saved Games folder unavailable on XDG");
  479. return NULL;
  480. case SDL_FOLDER_SCREENSHOTS:
  481. SDL_SetError("Screenshots folder unavailable on XDG");
  482. return NULL;
  483. case SDL_FOLDER_TEMPLATES:
  484. param = "TEMPLATES";
  485. break;
  486. case SDL_FOLDER_VIDEOS:
  487. param = "VIDEOS";
  488. break;
  489. default:
  490. SDL_SetError("Invalid SDL_Folder: %d", (int) folder);
  491. return NULL;
  492. }
  493. /* param *should* to be set to something at this point, but just in case */
  494. if (!param) {
  495. SDL_SetError("No corresponding XDG user directory");
  496. return NULL;
  497. }
  498. retval = xdg_user_dir_lookup(param);
  499. if (!retval) {
  500. SDL_SetError("XDG directory not available");
  501. return NULL;
  502. }
  503. append_slash:
  504. newretval = (char *) SDL_realloc(retval, SDL_strlen(retval) + 2);
  505. if (!newretval) {
  506. SDL_free(retval);
  507. return NULL;
  508. }
  509. retval = newretval;
  510. SDL_strlcat(retval, "/", SDL_strlen(retval) + 2);
  511. return retval;
  512. }
  513. #endif /* SDL_FILESYSTEM_UNIX */