SDL_sysfilesystem.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_EMSCRIPTEN
  20. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  21. /* System dependent filesystem routines */
  22. #include <errno.h>
  23. #include <sys/stat.h>
  24. #include <emscripten/emscripten.h>
  25. char *SDL_SYS_GetBasePath(void)
  26. {
  27. return SDL_strdup("/");
  28. }
  29. char *SDL_GetPrefPath(const char *org, const char *app)
  30. {
  31. const char *append = "/libsdl/";
  32. char *retval;
  33. char *ptr = NULL;
  34. size_t len = 0;
  35. if (!app) {
  36. SDL_InvalidParamError("app");
  37. return NULL;
  38. }
  39. if (!org) {
  40. org = "";
  41. }
  42. len = SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3;
  43. retval = (char *)SDL_malloc(len);
  44. if (!retval) {
  45. return NULL;
  46. }
  47. if (*org) {
  48. SDL_snprintf(retval, len, "%s%s/%s/", append, org, app);
  49. } else {
  50. SDL_snprintf(retval, len, "%s%s/", append, app);
  51. }
  52. for (ptr = retval + 1; *ptr; ptr++) {
  53. if (*ptr == '/') {
  54. *ptr = '\0';
  55. if (mkdir(retval, 0700) != 0 && errno != EEXIST) {
  56. goto error;
  57. }
  58. *ptr = '/';
  59. }
  60. }
  61. if (mkdir(retval, 0700) != 0 && errno != EEXIST) {
  62. error:
  63. SDL_SetError("Couldn't create directory '%s': '%s'", retval, strerror(errno));
  64. SDL_free(retval);
  65. return NULL;
  66. }
  67. return retval;
  68. }
  69. char *SDL_SYS_GetUserFolder(SDL_Folder folder)
  70. {
  71. const char *home = NULL;
  72. if (folder != SDL_FOLDER_HOME) {
  73. SDL_SetError("Emscripten only supports the home folder");
  74. return NULL;
  75. }
  76. home = SDL_getenv("HOME");
  77. if (!home) {
  78. SDL_SetError("No $HOME environment variable available");
  79. return NULL;
  80. }
  81. char *retval = SDL_malloc(SDL_strlen(home) + 2);
  82. if (!retval) {
  83. return NULL;
  84. }
  85. if (SDL_snprintf(retval, SDL_strlen(home) + 2, "%s/", home) < 0) {
  86. SDL_SetError("Couldn't snprintf home path for Emscripten: %s", home);
  87. SDL_free(retval);
  88. return NULL;
  89. }
  90. return retval;
  91. }
  92. #endif /* SDL_FILESYSTEM_EMSCRIPTEN */