2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
21 #include "../../SDL_internal.h"
23 #ifdef SDL_FILESYSTEM_UNIX
25 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 /* System dependent filesystem routines */
33 #include <sys/types.h>
37 #include <sys/sysctl.h>
40 #include "SDL_error.h"
41 #include "SDL_stdinc.h"
42 #include "SDL_filesystem.h"
45 readSymLink(const char *path)
53 char *ptr = (char *) SDL_realloc(retval, (size_t) len);
61 rc = readlink(path, retval, len);
63 break; /* not a symlink, i/o error, etc. */
64 } else if (rc < len) {
65 retval[rc] = '\0'; /* readlink doesn't null-terminate. */
66 return retval; /* we're good to go. */
69 len *= 2; /* grow buffer, try again. */
82 #if defined(__FREEBSD__)
83 char fullpath[PATH_MAX];
84 size_t buflen = sizeof (fullpath);
85 const int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
86 if (sysctl(mib, SDL_arraysize(mib), fullpath, &buflen, NULL, 0) != -1) {
87 retval = SDL_strdup(fullpath);
93 #elif defined(__SOLARIS__)
94 const char *path = getexecname();
95 if ((path != NULL) && (path[0] == '/')) { /* must be absolute path... */
96 retval = SDL_strdup(path);
104 /* is a Linux-style /proc filesystem available? */
105 if (!retval && (access("/proc", F_OK) == 0)) {
106 #if defined(__FREEBSD__)
107 retval = readSymLink("/proc/curproc/file");
108 #elif defined(__NETBSD__)
109 retval = readSymLink("/proc/curproc/exe");
111 retval = readSymLink("/proc/self/exe"); /* linux. */
113 if (retval == NULL) {
114 /* older kernels don't have /proc/self ... try PID version... */
116 const int rc = (int) SDL_snprintf(path, sizeof(path),
118 (unsigned long long) getpid());
119 if ( (rc > 0) && (rc < sizeof(path)) ) {
120 retval = readSymLink(path);
125 /* If we had access to argv[0] here, we could check it for a path,
126 or troll through $PATH looking for it, too. */
128 if (retval != NULL) { /* chop off filename. */
129 char *ptr = SDL_strrchr(retval, '/');
132 } else { /* shouldn't happen, but just in case... */
138 if (retval != NULL) {
139 /* try to shrink buffer... */
140 char *ptr = (char *) SDL_realloc(retval, strlen(retval) + 1);
142 retval = ptr; /* oh well if it failed. */
149 SDL_GetPrefPath(const char *org, const char *app)
152 * We use XDG's base directory spec, even if you're not on Linux.
153 * This isn't strictly correct, but the results are relatively sane
156 * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
158 const char *envr = SDL_getenv("XDG_DATA_HOME");
165 /* You end up with "$HOME/.local/share/Game Name 2" */
166 envr = SDL_getenv("HOME");
168 /* we could take heroic measures with /etc/passwd, but oh well. */
169 SDL_SetError("neither XDG_DATA_HOME nor HOME environment is set");
172 append = "/.local/share/";
177 len = SDL_strlen(envr);
178 if (envr[len - 1] == '/')
181 len += SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3;
182 retval = (char *) SDL_malloc(len);
188 SDL_snprintf(retval, len, "%s%s%s/%s/", envr, append, org, app);
190 for (ptr = retval+1; *ptr; ptr++) {
193 if (mkdir(retval, 0700) != 0 && errno != EEXIST)
198 if (mkdir(retval, 0700) != 0 && errno != EEXIST) {
200 SDL_SetError("Couldn't create directory '%s': '%s'", retval, strerror(errno));
208 #endif /* SDL_FILESYSTEM_UNIX */
210 /* vi: set ts=4 sw=4 expandtab: */