Fixed bug 3639 - SDL_GetPrefPath returns a path with two consecutive slashes on Unix if org is omitted Fabian Greffrath we use SDL_GetPrefPath() in Chocolate Doom to get a reasonable directory to save and restore config files and savegames: https://github.com/chocolate-doom/chocolate-doom/blob/sdl2-branch/src/m_config.c#L2162 However, since there is no "organization" behind Chocolate Doom and there is really only one "product" called Chocolate Doom, we pass an empty string for the org parameter and the package string for app. This leads to two consecutive slashes in the path returned by SDL_GetPrefPath() like this: /home/user/.local/share//chocolate-doom/ While this is harmless, it sure looks bad. I believe that it should be possible to either pass a NULL pointer for the org parameter or at least have the function detect an empty string as a means to express "there is no origanization, just a single product". The generation of the path string to be returned by the function will have to get adapted accordingly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
diff --git a/src/filesystem/cocoa/SDL_sysfilesystem.m b/src/filesystem/cocoa/SDL_sysfilesystem.m
index ba9efe8..3e84681 100644
--- a/src/filesystem/cocoa/SDL_sysfilesystem.m
+++ b/src/filesystem/cocoa/SDL_sysfilesystem.m
@@ -71,8 +71,15 @@ char *
SDL_GetPrefPath(const char *org, const char *app)
{ @autoreleasepool
{
- char *retval = NULL;
+ if (!app) {
+ SDL_InvalidParamError("app");
+ return NULL;
+ }
+ if (!org) {
+ org = "";
+ }
+ char *retval = NULL;
NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
if ([array count] > 0) { /* we only want the first item in the list. */
@@ -85,7 +92,11 @@ SDL_GetPrefPath(const char *org, const char *app)
SDL_OutOfMemory();
} else {
char *ptr;
- SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app);
+ if (*org) {
+ SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app);
+ } else {
+ SDL_snprintf(retval, len, "%s/%s/", base, app);
+ }
for (ptr = retval+1; *ptr; ptr++) {
if (*ptr == '/') {
*ptr = '\0';
diff --git a/src/filesystem/emscripten/SDL_sysfilesystem.c b/src/filesystem/emscripten/SDL_sysfilesystem.c
index 8a2228f..14b5424 100644
--- a/src/filesystem/emscripten/SDL_sysfilesystem.c
+++ b/src/filesystem/emscripten/SDL_sysfilesystem.c
@@ -46,6 +46,14 @@ SDL_GetPrefPath(const char *org, const char *app)
char *retval;
size_t len = 0;
+ if (!app) {
+ SDL_InvalidParamError("app");
+ return NULL;
+ }
+ if (!org) {
+ org = "";
+ }
+
len = SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3;
retval = (char *) SDL_malloc(len);
if (!retval) {
@@ -53,7 +61,11 @@ SDL_GetPrefPath(const char *org, const char *app)
return NULL;
}
- SDL_snprintf(retval, len, "%s%s/%s/", append, org, app);
+ if (*org) {
+ SDL_snprintf(retval, len, "%s%s/%s/", append, org, app);
+ } else {
+ SDL_snprintf(retval, len, "%s%s/", append, app);
+ }
if (mkdir(retval, 0700) != 0 && errno != EEXIST) {
SDL_SetError("Couldn't create directory '%s': '%s'", retval, strerror(errno));
diff --git a/src/filesystem/haiku/SDL_sysfilesystem.cc b/src/filesystem/haiku/SDL_sysfilesystem.cc
index 3e94369..090c808 100644
--- a/src/filesystem/haiku/SDL_sysfilesystem.cc
+++ b/src/filesystem/haiku/SDL_sysfilesystem.cc
@@ -77,6 +77,15 @@ SDL_GetPrefPath(const char *org, const char *app)
const char *home = SDL_getenv("HOME");
const char *append = "/config/settings/";
size_t len = SDL_strlen(home);
+
+ if (!app) {
+ SDL_InvalidParamError("app");
+ return NULL;
+ }
+ if (!org) {
+ org = "";
+ }
+
if (!len || (home[len - 1] == '/')) {
++append; // home empty or ends with separator, skip the one from append
}
@@ -85,7 +94,11 @@ SDL_GetPrefPath(const char *org, const char *app)
if (!retval) {
SDL_OutOfMemory();
} else {
- SDL_snprintf(retval, len, "%s%s%s/%s/", home, append, org, app);
+ if (*org) {
+ SDL_snprintf(retval, len, "%s%s%s/%s/", home, append, org, app);
+ } else {
+ SDL_snprintf(retval, len, "%s%s%s/", home, append, app);
+ }
create_directory(retval, 0700); // Haiku api: creates missing dirs
}
diff --git a/src/filesystem/nacl/SDL_sysfilesystem.c b/src/filesystem/nacl/SDL_sysfilesystem.c
index 12e0ee3..767ef9a 100644
--- a/src/filesystem/nacl/SDL_sysfilesystem.c
+++ b/src/filesystem/nacl/SDL_sysfilesystem.c
@@ -40,3 +40,4 @@ SDL_GetPrefPath(const char *org, const char *app)
#endif /* SDL_FILESYSTEM_NACL */
+/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/filesystem/unix/SDL_sysfilesystem.c b/src/filesystem/unix/SDL_sysfilesystem.c
index 69f8feb..2db83ee 100644
--- a/src/filesystem/unix/SDL_sysfilesystem.c
+++ b/src/filesystem/unix/SDL_sysfilesystem.c
@@ -180,6 +180,14 @@ SDL_GetPrefPath(const char *org, const char *app)
char *ptr = NULL;
size_t len = 0;
+ if (!app) {
+ SDL_InvalidParamError("app");
+ return NULL;
+ }
+ if (!org) {
+ org = "";
+ }
+
if (!envr) {
/* You end up with "$HOME/.local/share/Game Name 2" */
envr = SDL_getenv("HOME");
@@ -204,7 +212,11 @@ SDL_GetPrefPath(const char *org, const char *app)
return NULL;
}
- SDL_snprintf(retval, len, "%s%s%s/%s/", envr, append, org, app);
+ if (*org) {
+ SDL_snprintf(retval, len, "%s%s%s/%s/", envr, append, org, app);
+ } else {
+ SDL_snprintf(retval, len, "%s%s%s/", envr, append, app);
+ }
for (ptr = retval+1; *ptr; ptr++) {
if (*ptr == '/') {
diff --git a/src/filesystem/windows/SDL_sysfilesystem.c b/src/filesystem/windows/SDL_sysfilesystem.c
index b74290e..1a9514a 100644
--- a/src/filesystem/windows/SDL_sysfilesystem.c
+++ b/src/filesystem/windows/SDL_sysfilesystem.c
@@ -118,6 +118,14 @@ SDL_GetPrefPath(const char *org, const char *app)
size_t new_wpath_len = 0;
BOOL api_result = FALSE;
+ if (!app) {
+ SDL_InvalidParamError("app");
+ return NULL;
+ }
+ if (!org) {
+ org = "";
+ }
+
if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, path))) {
WIN_SetError("Couldn't locate our prefpath");
return NULL;
@@ -145,8 +153,10 @@ SDL_GetPrefPath(const char *org, const char *app)
return NULL;
}
- lstrcatW(path, L"\\");
- lstrcatW(path, worg);
+ if (*worg) {
+ lstrcatW(path, L"\\");
+ lstrcatW(path, worg);
+ }
SDL_free(worg);
api_result = CreateDirectoryW(path, NULL);
diff --git a/src/filesystem/winrt/SDL_sysfilesystem.cpp b/src/filesystem/winrt/SDL_sysfilesystem.cpp
index 880fcbc..4afc96a 100644
--- a/src/filesystem/winrt/SDL_sysfilesystem.cpp
+++ b/src/filesystem/winrt/SDL_sysfilesystem.cpp
@@ -152,6 +152,14 @@ SDL_GetPrefPath(const char *org, const char *app)
size_t new_wpath_len = 0;
BOOL api_result = FALSE;
+ if (!app) {
+ SDL_InvalidParamError("app");
+ return NULL;
+ }
+ if (!org) {
+ org = "";
+ }
+
srcPath = SDL_WinRTGetFSPathUNICODE(SDL_WINRT_PATH_LOCAL_FOLDER);
if ( ! srcPath) {
SDL_SetError("Unable to find a source path");
@@ -186,9 +194,11 @@ SDL_GetPrefPath(const char *org, const char *app)
return NULL;
}
- SDL_wcslcat(path, L"\\", new_wpath_len + 1);
- SDL_wcslcat(path, worg, new_wpath_len + 1);
- SDL_free(worg);
+ if (*worg) {
+ SDL_wcslcat(path, L"\\", new_wpath_len + 1);
+ SDL_wcslcat(path, worg, new_wpath_len + 1);
+ SDL_free(worg);
+ }
api_result = CreateDirectoryW(path, NULL);
if (api_result == FALSE) {
@@ -219,3 +229,5 @@ SDL_GetPrefPath(const char *org, const char *app)
}
#endif /* __WINRT__ */
+
+/* vi: set ts=4 sw=4 expandtab: */
diff --git a/test/testfilesystem.c b/test/testfilesystem.c
index 6d6eab4..e8a3b3c 100644
--- a/test/testfilesystem.c
+++ b/test/testfilesystem.c
@@ -46,6 +46,15 @@ main(int argc, char *argv[])
SDL_Log("pref path: '%s'\n", pref_path);
SDL_free(pref_path);
+ pref_path = SDL_GetPrefPath(NULL, "testfilesystem");
+ if(pref_path == NULL){
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find pref path without organization: %s\n",
+ SDL_GetError());
+ return 1;
+ }
+ SDL_Log("pref path: '%s'\n", pref_path);
+ SDL_free(pref_path);
+
SDL_Quit();
return 0;
}