SDL_sysfilesystem.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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_OS2
  20. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  21. /* System dependent filesystem routines */
  22. #include "../../core/os2/SDL_os2.h"
  23. #include "SDL_error.h"
  24. #include "SDL_filesystem.h"
  25. #define INCL_DOSFILEMGR
  26. #define INCL_DOSPROCESS
  27. #define INCL_DOSMODULEMGR
  28. #define INCL_DOSERRORS
  29. #include <os2.h>
  30. char *
  31. SDL_GetBasePath(void)
  32. {
  33. PTIB tib;
  34. PPIB pib;
  35. ULONG ulRC = DosGetInfoBlocks(&tib, &pib);
  36. PCHAR pcEnd;
  37. CHAR acBuf[CCHMAXPATH];
  38. if (ulRC != NO_ERROR) {
  39. SDL_SetError("Can't get process information block (E%lu)", ulRC);
  40. return NULL;
  41. }
  42. ulRC = DosQueryModuleName(pib->pib_hmte, sizeof(acBuf), acBuf);
  43. if (ulRC != NO_ERROR) {
  44. SDL_SetError("Can't query the module name (E%lu)", ulRC);
  45. return NULL;
  46. }
  47. pcEnd = SDL_strrchr(acBuf, '\\');
  48. if (pcEnd != NULL)
  49. pcEnd[1] = '\0';
  50. else {
  51. if (acBuf[1] == ':') /* e.g. "C:FOO" */
  52. acBuf[2] = '\0';
  53. else {
  54. SDL_SetError("No path in module name");
  55. return NULL;
  56. }
  57. }
  58. return OS2_SysToUTF8(acBuf);
  59. }
  60. char *
  61. SDL_GetPrefPath(const char *org, const char *app)
  62. {
  63. PSZ pszPath;
  64. CHAR acBuf[CCHMAXPATH];
  65. int lPosApp, lPosOrg;
  66. PSZ pszApp, pszOrg;
  67. if (!app) {
  68. SDL_InvalidParamError("app");
  69. return NULL;
  70. }
  71. pszPath = SDL_getenv("HOME");
  72. if (!pszPath) {
  73. pszPath = SDL_getenv("ETC");
  74. if (!pszPath) {
  75. SDL_SetError("HOME or ETC environment not set");
  76. return NULL;
  77. }
  78. }
  79. if (!org) {
  80. lPosApp = SDL_snprintf(acBuf, sizeof(acBuf) - 1, "%s", pszPath);
  81. } else {
  82. pszOrg = OS2_UTF8ToSys(org);
  83. if (!pszOrg) {
  84. SDL_OutOfMemory();
  85. return NULL;
  86. }
  87. lPosApp = SDL_snprintf(acBuf, sizeof(acBuf) - 1, "%s\\%s", pszPath, pszOrg);
  88. SDL_free(pszOrg);
  89. }
  90. if (lPosApp < 0)
  91. return NULL;
  92. DosCreateDir(acBuf, NULL);
  93. pszApp = OS2_UTF8ToSys(app);
  94. if (!pszApp) {
  95. SDL_OutOfMemory();
  96. return NULL;
  97. }
  98. lPosOrg = SDL_snprintf(&acBuf[lPosApp], sizeof(acBuf) - lPosApp - 1, "\\%s", pszApp);
  99. SDL_free(pszApp);
  100. if (lPosOrg < 0)
  101. return NULL;
  102. DosCreateDir(acBuf, NULL);
  103. *((PUSHORT)&acBuf[lPosApp + lPosOrg]) = (USHORT)'\0\\';
  104. return OS2_SysToUTF8(acBuf);
  105. }
  106. #endif /* SDL_FILESYSTEM_OS2 */
  107. /* vi: set ts=4 sw=4 expandtab: */