src/filesystem/unix/SDL_sysfilesystem.c
changeset 7667 be1cc6f55840
child 7668 66da4242ce53
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/filesystem/unix/SDL_sysfilesystem.c	Tue Aug 20 19:57:11 2013 -0400
     1.3 @@ -0,0 +1,161 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +#include "SDL_config.h"
    1.25 +
    1.26 +#ifdef SDL_FILESYSTEM_UNIX
    1.27 +
    1.28 +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    1.29 +/* System dependent filesystem routines                                */
    1.30 +
    1.31 +#include <unistd.h>
    1.32 +
    1.33 +#include "SDL_error.h"
    1.34 +#include "SDL_stdinc.h"
    1.35 +#include "SDL_filesystem.h"
    1.36 +
    1.37 +static char *readSymLink(const char *path)
    1.38 +{
    1.39 +    char *retval = NULL;
    1.40 +    ssize_t len = 64;
    1.41 +    ssize_t rc = -1;
    1.42 +
    1.43 +    while (1)
    1.44 +    {
    1.45 +        char *ptr = (char *) SDL_realloc(retval, (size_t) len);
    1.46 +        if (ptr == NULL) {
    1.47 +            SDL_OutOfMemory();
    1.48 +            break;
    1.49 +        }
    1.50 +
    1.51 +        retval = ptr;
    1.52 +
    1.53 +        rc = readlink(path, retval, len);
    1.54 +        if (rc == -1) {
    1.55 +            break;  /* not a symlink, i/o error, etc. */
    1.56 +        } else if (rc < len) {
    1.57 +            retval[rc] = '\0';  /* readlink doesn't null-terminate. */
    1.58 +            return retval;  /* we're good to go. */
    1.59 +        }
    1.60 +
    1.61 +        len *= 2;  /* grow buffer, try again. */
    1.62 +    }
    1.63 +
    1.64 +    if (retval != NULL) {
    1.65 +        SDL_free(retval);
    1.66 +    }
    1.67 +    return NULL;
    1.68 +}
    1.69 +
    1.70 +
    1.71 +char *
    1.72 +SDL_GetBasePath(void)
    1.73 +{
    1.74 +    char *retval = NULL;
    1.75 +
    1.76 +    /* is a Linux-style /proc filesystem available? */
    1.77 +    if (access("/proc", F_OK) {
    1.78 +        retval = readSymLink("/proc/self/exe");
    1.79 +        if (retval == NULL) {
    1.80 +            /* older kernels don't have /proc/self ... try PID version... */
    1.81 +            char path[64];
    1.82 +            const int rc = (int) SDL_snprintf(path, sizeof(path),
    1.83 +                                              "/proc/%llu/exe",
    1.84 +                                              (unsigned long long) getpid());
    1.85 +            if ( (rc > 0) && (rc < sizeof(path)) ) {
    1.86 +                retval = readSymLink(path);
    1.87 +            }
    1.88 +        }
    1.89 +    }
    1.90 +
    1.91 +    /* If we had access to argv[0] here, we could check it for a path,
    1.92 +        or troll through $PATH looking for it, too. */
    1.93 +
    1.94 +    if (retval != NULL) { /* chop off filename. */
    1.95 +        char *ptr = SDL_strrchr(retval, '/');
    1.96 +        if (ptr != NULL) {
    1.97 +            *(ptr+1) = '\0';
    1.98 +        } else {  /* shouldn't happen, but just in case... */
    1.99 +            SDL_free(retval);
   1.100 +            retval = NULL;
   1.101 +        }
   1.102 +    }
   1.103 +
   1.104 +    if (retval != NULL) {
   1.105 +        /* try to shrink buffer... */
   1.106 +        char *ptr = (char *) SDL_realloc(retval, strlen(retval) + 1);
   1.107 +        if (ptr != NULL)
   1.108 +            retval = ptr;  /* oh well if it failed. */
   1.109 +    }
   1.110 +
   1.111 +    return retval;
   1.112 +}
   1.113 +
   1.114 +char *
   1.115 +SDL_GetPrefPath(const char *org, const char *app)
   1.116 +{
   1.117 +    /*
   1.118 +     * We use XDG's base directory spec, even if you're not on Linux.
   1.119 +     *  This isn't strictly correct, but the results are relatively sane
   1.120 +     *  in any case.
   1.121 +     *
   1.122 +     * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
   1.123 +     */
   1.124 +    const char *envr = SDL_getenv("XDG_DATA_HOME");
   1.125 +    const char *append = "/";
   1.126 +    char *retval = NULL;
   1.127 +    char *ptr = NULL;
   1.128 +    size_t len = 0;
   1.129 +
   1.130 +    if (!envr) {
   1.131 +        /* You end up with "$HOME/.local/share/Game Name 2" */
   1.132 +        envr = SDL_getenv("HOME");
   1.133 +        if (!envr) {
   1.134 +            /* we could take heroic measures with /etc/passwd, but oh well. */
   1.135 +            SDL_SetError("neither XDG_DATA_HOME nor HOME environment is set");
   1.136 +            return NULL;
   1.137 +        }
   1.138 +        append = ".local/share/";
   1.139 +    } /* if */
   1.140 +
   1.141 +    len = SDL_strlen(envr) + SDL_strlen(append) + SDL_strlen(app) + 2;
   1.142 +    retval = (char *) SDL_malloc(len);
   1.143 +    if (!retval) {
   1.144 +        SDL_OutOfMemory();
   1.145 +        return NULL;
   1.146 +    }
   1.147 +
   1.148 +    SDL_snprintf(retval, len, "%s%s%s/", envr, append, app);
   1.149 +
   1.150 +    for (ptr = retval+1; *ptr; ptr++) {
   1.151 +        if (*ptr == '/') {
   1.152 +            *ptr = '\0';
   1.153 +            mkdir(retval, 0700);
   1.154 +            *ptr = '/';
   1.155 +        }
   1.156 +    }
   1.157 +    mkdir(retval, 0700);
   1.158 +
   1.159 +    return retval;
   1.160 +}
   1.161 +
   1.162 +#endif /* SDL_FILESYSTEM_UNIX */
   1.163 +
   1.164 +/* vi: set ts=4 sw=4 expandtab: */