src/filesystem/unix/SDL_sysfilesystem.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 20 Aug 2013 20:15:15 -0400
changeset 7668 66da4242ce53
parent 7667 be1cc6f55840
child 7692 e1036c58fac7
permissions -rw-r--r--
Patched new filesystem code to compile.
icculus@7667
     1
/*
icculus@7667
     2
  Simple DirectMedia Layer
icculus@7667
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
icculus@7667
     4
icculus@7667
     5
  This software is provided 'as-is', without any express or implied
icculus@7667
     6
  warranty.  In no event will the authors be held liable for any damages
icculus@7667
     7
  arising from the use of this software.
icculus@7667
     8
icculus@7667
     9
  Permission is granted to anyone to use this software for any purpose,
icculus@7667
    10
  including commercial applications, and to alter it and redistribute it
icculus@7667
    11
  freely, subject to the following restrictions:
icculus@7667
    12
icculus@7667
    13
  1. The origin of this software must not be misrepresented; you must not
icculus@7667
    14
     claim that you wrote the original software. If you use this software
icculus@7667
    15
     in a product, an acknowledgment in the product documentation would be
icculus@7667
    16
     appreciated but is not required.
icculus@7667
    17
  2. Altered source versions must be plainly marked as such, and must not be
icculus@7667
    18
     misrepresented as being the original software.
icculus@7667
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@7667
    20
*/
icculus@7667
    21
#include "SDL_config.h"
icculus@7667
    22
icculus@7667
    23
#ifdef SDL_FILESYSTEM_UNIX
icculus@7667
    24
icculus@7667
    25
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
icculus@7667
    26
/* System dependent filesystem routines                                */
icculus@7667
    27
icculus@7667
    28
#include <unistd.h>
icculus@7668
    29
#include <sys/stat.h>
icculus@7668
    30
#include <sys/types.h>
icculus@7667
    31
icculus@7667
    32
#include "SDL_error.h"
icculus@7667
    33
#include "SDL_stdinc.h"
icculus@7667
    34
#include "SDL_filesystem.h"
icculus@7667
    35
icculus@7668
    36
static char *
icculus@7668
    37
readSymLink(const char *path)
icculus@7667
    38
{
icculus@7667
    39
    char *retval = NULL;
icculus@7667
    40
    ssize_t len = 64;
icculus@7667
    41
    ssize_t rc = -1;
icculus@7667
    42
icculus@7667
    43
    while (1)
icculus@7667
    44
    {
icculus@7667
    45
        char *ptr = (char *) SDL_realloc(retval, (size_t) len);
icculus@7667
    46
        if (ptr == NULL) {
icculus@7667
    47
            SDL_OutOfMemory();
icculus@7667
    48
            break;
icculus@7667
    49
        }
icculus@7667
    50
icculus@7667
    51
        retval = ptr;
icculus@7667
    52
icculus@7667
    53
        rc = readlink(path, retval, len);
icculus@7667
    54
        if (rc == -1) {
icculus@7667
    55
            break;  /* not a symlink, i/o error, etc. */
icculus@7667
    56
        } else if (rc < len) {
icculus@7667
    57
            retval[rc] = '\0';  /* readlink doesn't null-terminate. */
icculus@7667
    58
            return retval;  /* we're good to go. */
icculus@7667
    59
        }
icculus@7667
    60
icculus@7667
    61
        len *= 2;  /* grow buffer, try again. */
icculus@7667
    62
    }
icculus@7667
    63
icculus@7667
    64
    if (retval != NULL) {
icculus@7667
    65
        SDL_free(retval);
icculus@7667
    66
    }
icculus@7667
    67
    return NULL;
icculus@7667
    68
}
icculus@7667
    69
icculus@7667
    70
icculus@7667
    71
char *
icculus@7667
    72
SDL_GetBasePath(void)
icculus@7667
    73
{
icculus@7667
    74
    char *retval = NULL;
icculus@7667
    75
icculus@7667
    76
    /* is a Linux-style /proc filesystem available? */
icculus@7668
    77
    if (access("/proc", F_OK) == 0) {
icculus@7667
    78
        retval = readSymLink("/proc/self/exe");
icculus@7667
    79
        if (retval == NULL) {
icculus@7667
    80
            /* older kernels don't have /proc/self ... try PID version... */
icculus@7667
    81
            char path[64];
icculus@7667
    82
            const int rc = (int) SDL_snprintf(path, sizeof(path),
icculus@7667
    83
                                              "/proc/%llu/exe",
icculus@7667
    84
                                              (unsigned long long) getpid());
icculus@7667
    85
            if ( (rc > 0) && (rc < sizeof(path)) ) {
icculus@7667
    86
                retval = readSymLink(path);
icculus@7667
    87
            }
icculus@7667
    88
        }
icculus@7667
    89
    }
icculus@7667
    90
icculus@7667
    91
    /* If we had access to argv[0] here, we could check it for a path,
icculus@7667
    92
        or troll through $PATH looking for it, too. */
icculus@7667
    93
icculus@7667
    94
    if (retval != NULL) { /* chop off filename. */
icculus@7667
    95
        char *ptr = SDL_strrchr(retval, '/');
icculus@7667
    96
        if (ptr != NULL) {
icculus@7667
    97
            *(ptr+1) = '\0';
icculus@7667
    98
        } else {  /* shouldn't happen, but just in case... */
icculus@7667
    99
            SDL_free(retval);
icculus@7667
   100
            retval = NULL;
icculus@7667
   101
        }
icculus@7667
   102
    }
icculus@7667
   103
icculus@7667
   104
    if (retval != NULL) {
icculus@7667
   105
        /* try to shrink buffer... */
icculus@7667
   106
        char *ptr = (char *) SDL_realloc(retval, strlen(retval) + 1);
icculus@7667
   107
        if (ptr != NULL)
icculus@7667
   108
            retval = ptr;  /* oh well if it failed. */
icculus@7667
   109
    }
icculus@7667
   110
icculus@7667
   111
    return retval;
icculus@7667
   112
}
icculus@7667
   113
icculus@7667
   114
char *
icculus@7667
   115
SDL_GetPrefPath(const char *org, const char *app)
icculus@7667
   116
{
icculus@7667
   117
    /*
icculus@7667
   118
     * We use XDG's base directory spec, even if you're not on Linux.
icculus@7667
   119
     *  This isn't strictly correct, but the results are relatively sane
icculus@7667
   120
     *  in any case.
icculus@7667
   121
     *
icculus@7667
   122
     * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
icculus@7667
   123
     */
icculus@7667
   124
    const char *envr = SDL_getenv("XDG_DATA_HOME");
icculus@7667
   125
    const char *append = "/";
icculus@7667
   126
    char *retval = NULL;
icculus@7667
   127
    char *ptr = NULL;
icculus@7667
   128
    size_t len = 0;
icculus@7667
   129
icculus@7667
   130
    if (!envr) {
icculus@7667
   131
        /* You end up with "$HOME/.local/share/Game Name 2" */
icculus@7667
   132
        envr = SDL_getenv("HOME");
icculus@7667
   133
        if (!envr) {
icculus@7667
   134
            /* we could take heroic measures with /etc/passwd, but oh well. */
icculus@7667
   135
            SDL_SetError("neither XDG_DATA_HOME nor HOME environment is set");
icculus@7667
   136
            return NULL;
icculus@7667
   137
        }
icculus@7667
   138
        append = ".local/share/";
icculus@7667
   139
    } /* if */
icculus@7667
   140
icculus@7667
   141
    len = SDL_strlen(envr) + SDL_strlen(append) + SDL_strlen(app) + 2;
icculus@7667
   142
    retval = (char *) SDL_malloc(len);
icculus@7667
   143
    if (!retval) {
icculus@7667
   144
        SDL_OutOfMemory();
icculus@7667
   145
        return NULL;
icculus@7667
   146
    }
icculus@7667
   147
icculus@7667
   148
    SDL_snprintf(retval, len, "%s%s%s/", envr, append, app);
icculus@7667
   149
icculus@7667
   150
    for (ptr = retval+1; *ptr; ptr++) {
icculus@7667
   151
        if (*ptr == '/') {
icculus@7667
   152
            *ptr = '\0';
icculus@7667
   153
            mkdir(retval, 0700);
icculus@7667
   154
            *ptr = '/';
icculus@7667
   155
        }
icculus@7667
   156
    }
icculus@7667
   157
    mkdir(retval, 0700);
icculus@7667
   158
icculus@7667
   159
    return retval;
icculus@7667
   160
}
icculus@7667
   161
icculus@7667
   162
#endif /* SDL_FILESYSTEM_UNIX */
icculus@7667
   163
icculus@7667
   164
/* vi: set ts=4 sw=4 expandtab: */