1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
4 
5   This software is provided 'as-is', without any express or implied
6   warranty.  In no event will the authors be held liable for any damages
7   arising from the use of this software.
8 
9   Permission is granted to anyone to use this software for any purpose,
10   including commercial applications, and to alter it and redistribute it
11   freely, subject to the following restrictions:
12 
13   1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17   2. Altered source versions must be plainly marked as such, and must not be
18      misrepresented as being the original software.
19   3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22 
23 #ifdef SDL_FILESYSTEM_OS2
24 
25 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 /* System dependent filesystem routines                                */
27 
28 #include "SDL_error.h"
29 #include "SDL_filesystem.h"
30 #include "../../core/os2/SDL_os2.h"
31 
32 #define INCL_DOSFILEMGR
33 #define INCL_DOSPROCESS
34 #define INCL_DOSERRORS
35 #include <os2.h>
36 
37 
38 char *
SDL_GetBasePath(void)39 SDL_GetBasePath(void)
40 {
41     PTIB    tib;
42     PPIB    pib;
43     ULONG   ulRC = DosGetInfoBlocks(&tib, &pib);
44     PCHAR   pcEnd;
45     ULONG   cbResult;
46     CHAR    acBuf[_MAX_PATH];
47 
48     if (ulRC != NO_ERROR) {
49         debug_os2("DosGetInfoBlocks() failed, rc = %u", ulRC);
50         return NULL;
51     }
52 
53     pcEnd = SDL_strrchr(pib->pib_pchcmd, '\\');
54     if (pcEnd != NULL)
55         pcEnd++;
56     else {
57         if (pib->pib_pchcmd[1] == ':')
58             pcEnd = &pib->pib_pchcmd[2];
59         else {
60             SDL_SetError("No path in pib->pib_pchcmd");
61             return NULL;
62         }
63     }
64 
65     cbResult = pcEnd - pib->pib_pchcmd;
66     SDL_memcpy(acBuf, pib->pib_pchcmd, cbResult);
67     acBuf[cbResult] = '\0';
68 
69     return OS2_SysToUTF8(acBuf);
70 }
71 
72 char *
SDL_GetPrefPath(const char * org,const char * app)73 SDL_GetPrefPath(const char *org, const char *app)
74 {
75     PSZ     pszPath;
76     CHAR    acBuf[_MAX_PATH];
77     int     lPosApp, lPosOrg;
78     PSZ     pszApp, pszOrg;
79 
80     if (!app) {
81         SDL_InvalidParamError("app");
82         return NULL;
83     }
84 
85     pszPath = SDL_getenv("HOME");
86     if (!pszPath) {
87         pszPath = SDL_getenv("ETC");
88         if (!pszPath) {
89             SDL_SetError("HOME or ETC environment not set");
90             return NULL;
91         }
92     }
93 
94     if (!org) {
95         lPosApp = SDL_snprintf(acBuf, sizeof(acBuf) - 1, "%s", pszPath);
96     } else {
97         pszOrg = OS2_UTF8ToSys(org);
98         if (!pszOrg) {
99             SDL_OutOfMemory();
100             return NULL;
101         }
102         lPosApp = SDL_snprintf(acBuf, sizeof(acBuf) - 1, "%s\\%s", pszPath, pszOrg);
103         SDL_free(pszOrg);
104     }
105     if (lPosApp < 0)
106         return NULL;
107 
108     DosCreateDir(acBuf, NULL);
109 
110     pszApp = OS2_UTF8ToSys(app);
111     if (!pszApp) {
112         SDL_OutOfMemory();
113         return NULL;
114     }
115 
116     lPosOrg = SDL_snprintf(&acBuf[lPosApp], sizeof(acBuf) - lPosApp - 1, "\\%s", pszApp);
117     SDL_free(pszApp);
118     if (lPosOrg < 0)
119         return NULL;
120 
121     DosCreateDir(acBuf, NULL);
122     *((PUSHORT)&acBuf[lPosApp + lPosOrg]) = (USHORT)'\0\\';
123 
124     return OS2_SysToUTF8(acBuf);
125 }
126 
127 #endif /* SDL_FILESYSTEM_OS2 */
128 
129 /* vi: set ts=4 sw=4 expandtab: */
130