src/filesystem/unix/SDL_sysfilesystem.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 01 Jan 2017 18:33:28 -0800
changeset 10737 3406a0f8b041
parent 10061 698a7d8626b9
child 11222 083133ba8f9b
permissions -rw-r--r--
Updated copyright for 2017
icculus@7667
     1
/*
icculus@7667
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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@8093
    21
#include "../../SDL_internal.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
slouken@7715
    28
#include <errno.h>
slouken@7715
    29
#include <stdio.h>
icculus@7667
    30
#include <unistd.h>
icculus@7692
    31
#include <stdlib.h>
icculus@7668
    32
#include <sys/stat.h>
icculus@7668
    33
#include <sys/types.h>
icculus@7693
    34
#include <limits.h>
icculus@7667
    35
slouken@10061
    36
#if defined(__FREEBSD__) || defined(__OPENBSD__)
icculus@7692
    37
#include <sys/sysctl.h>
icculus@7692
    38
#endif
icculus@7692
    39
icculus@7667
    40
#include "SDL_error.h"
icculus@7667
    41
#include "SDL_stdinc.h"
icculus@7667
    42
#include "SDL_filesystem.h"
icculus@7667
    43
icculus@7668
    44
static char *
icculus@7668
    45
readSymLink(const char *path)
icculus@7667
    46
{
icculus@7667
    47
    char *retval = NULL;
icculus@7667
    48
    ssize_t len = 64;
icculus@7667
    49
    ssize_t rc = -1;
icculus@7667
    50
icculus@7667
    51
    while (1)
icculus@7667
    52
    {
icculus@7667
    53
        char *ptr = (char *) SDL_realloc(retval, (size_t) len);
icculus@7667
    54
        if (ptr == NULL) {
icculus@7667
    55
            SDL_OutOfMemory();
icculus@7667
    56
            break;
icculus@7667
    57
        }
icculus@7667
    58
icculus@7667
    59
        retval = ptr;
icculus@7667
    60
icculus@7667
    61
        rc = readlink(path, retval, len);
icculus@7667
    62
        if (rc == -1) {
icculus@7667
    63
            break;  /* not a symlink, i/o error, etc. */
icculus@7667
    64
        } else if (rc < len) {
icculus@7667
    65
            retval[rc] = '\0';  /* readlink doesn't null-terminate. */
icculus@7667
    66
            return retval;  /* we're good to go. */
icculus@7667
    67
        }
icculus@7667
    68
icculus@7667
    69
        len *= 2;  /* grow buffer, try again. */
icculus@7667
    70
    }
icculus@7667
    71
slouken@7719
    72
    SDL_free(retval);
icculus@7667
    73
    return NULL;
icculus@7667
    74
}
icculus@7667
    75
icculus@7667
    76
icculus@7667
    77
char *
icculus@7667
    78
SDL_GetBasePath(void)
icculus@7667
    79
{
icculus@7667
    80
    char *retval = NULL;
icculus@7667
    81
icculus@7692
    82
#if defined(__FREEBSD__)
icculus@7692
    83
    char fullpath[PATH_MAX];
icculus@7692
    84
    size_t buflen = sizeof (fullpath);
slouken@7717
    85
    const int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
slouken@7717
    86
    if (sysctl(mib, SDL_arraysize(mib), fullpath, &buflen, NULL, 0) != -1) {
icculus@7692
    87
        retval = SDL_strdup(fullpath);
icculus@7692
    88
        if (!retval) {
icculus@7692
    89
            SDL_OutOfMemory();
icculus@7692
    90
            return NULL;
icculus@7692
    91
        }
icculus@7692
    92
    }
slouken@10061
    93
#endif
slouken@10061
    94
#if defined(__OPENBSD__)
slouken@10061
    95
    char **retvalargs;
slouken@10061
    96
    size_t len;
slouken@10061
    97
    const int mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
slouken@10061
    98
    if (sysctl(mib, 4, NULL, &len, NULL, 0) != -1) {
slouken@10061
    99
        retvalargs = SDL_malloc(len);
slouken@10061
   100
        if (!retvalargs) {
slouken@10061
   101
            SDL_OutOfMemory();
slouken@10061
   102
            return NULL;
slouken@10061
   103
        }
slouken@10061
   104
        sysctl(mib, 4, retvalargs, &len, NULL, 0);
slouken@10061
   105
        retval = SDL_malloc(PATH_MAX + 1);
slouken@10061
   106
        if (retval)
slouken@10061
   107
            realpath(retvalargs[0], retval);
slouken@10061
   108
slouken@10061
   109
        SDL_free(retvalargs);
slouken@10061
   110
    }
slouken@10061
   111
#endif
slouken@10061
   112
#if defined(__SOLARIS__)
icculus@7692
   113
    const char *path = getexecname();
icculus@7692
   114
    if ((path != NULL) && (path[0] == '/')) { /* must be absolute path... */
icculus@7694
   115
        retval = SDL_strdup(path);
icculus@7692
   116
        if (!retval) {
icculus@7692
   117
            SDL_OutOfMemory();
icculus@7692
   118
            return NULL;
icculus@7692
   119
        }
icculus@7692
   120
    }
icculus@7692
   121
#endif
icculus@7692
   122
icculus@7667
   123
    /* is a Linux-style /proc filesystem available? */
icculus@7692
   124
    if (!retval && (access("/proc", F_OK) == 0)) {
slouken@7718
   125
#if defined(__FREEBSD__)
icculus@7692
   126
        retval = readSymLink("/proc/curproc/file");
slouken@7718
   127
#elif defined(__NETBSD__)
icculus@7692
   128
        retval = readSymLink("/proc/curproc/exe");
slouken@7718
   129
#else
icculus@7692
   130
        retval = readSymLink("/proc/self/exe");  /* linux. */
slouken@7718
   131
#endif
icculus@7667
   132
        if (retval == NULL) {
icculus@7667
   133
            /* older kernels don't have /proc/self ... try PID version... */
icculus@7667
   134
            char path[64];
icculus@7667
   135
            const int rc = (int) SDL_snprintf(path, sizeof(path),
icculus@7667
   136
                                              "/proc/%llu/exe",
icculus@7667
   137
                                              (unsigned long long) getpid());
icculus@7667
   138
            if ( (rc > 0) && (rc < sizeof(path)) ) {
icculus@7667
   139
                retval = readSymLink(path);
icculus@7667
   140
            }
icculus@7667
   141
        }
icculus@7667
   142
    }
icculus@7667
   143
icculus@7667
   144
    /* If we had access to argv[0] here, we could check it for a path,
icculus@7667
   145
        or troll through $PATH looking for it, too. */
icculus@7667
   146
icculus@7667
   147
    if (retval != NULL) { /* chop off filename. */
icculus@7667
   148
        char *ptr = SDL_strrchr(retval, '/');
icculus@7667
   149
        if (ptr != NULL) {
icculus@7667
   150
            *(ptr+1) = '\0';
icculus@7667
   151
        } else {  /* shouldn't happen, but just in case... */
icculus@7667
   152
            SDL_free(retval);
icculus@7667
   153
            retval = NULL;
icculus@7667
   154
        }
icculus@7667
   155
    }
icculus@7667
   156
icculus@7667
   157
    if (retval != NULL) {
icculus@7667
   158
        /* try to shrink buffer... */
icculus@7667
   159
        char *ptr = (char *) SDL_realloc(retval, strlen(retval) + 1);
icculus@7667
   160
        if (ptr != NULL)
icculus@7667
   161
            retval = ptr;  /* oh well if it failed. */
icculus@7667
   162
    }
icculus@7667
   163
icculus@7667
   164
    return retval;
icculus@7667
   165
}
icculus@7667
   166
icculus@7667
   167
char *
icculus@7667
   168
SDL_GetPrefPath(const char *org, const char *app)
icculus@7667
   169
{
icculus@7667
   170
    /*
icculus@7667
   171
     * We use XDG's base directory spec, even if you're not on Linux.
icculus@7667
   172
     *  This isn't strictly correct, but the results are relatively sane
icculus@7667
   173
     *  in any case.
icculus@7667
   174
     *
icculus@7667
   175
     * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
icculus@7667
   176
     */
icculus@7667
   177
    const char *envr = SDL_getenv("XDG_DATA_HOME");
slouken@7713
   178
    const char *append;
icculus@7667
   179
    char *retval = NULL;
icculus@7667
   180
    char *ptr = NULL;
icculus@7667
   181
    size_t len = 0;
icculus@7667
   182
icculus@7667
   183
    if (!envr) {
icculus@7667
   184
        /* You end up with "$HOME/.local/share/Game Name 2" */
icculus@7667
   185
        envr = SDL_getenv("HOME");
icculus@7667
   186
        if (!envr) {
icculus@7667
   187
            /* we could take heroic measures with /etc/passwd, but oh well. */
icculus@7667
   188
            SDL_SetError("neither XDG_DATA_HOME nor HOME environment is set");
icculus@7667
   189
            return NULL;
icculus@7667
   190
        }
slouken@7713
   191
        append = "/.local/share/";
urkle@7695
   192
    } else {
slouken@7713
   193
        append = "/";
slouken@7714
   194
    }
icculus@7667
   195
slouken@7713
   196
    len = SDL_strlen(envr);
slouken@7713
   197
    if (envr[len - 1] == '/')
slouken@7713
   198
        append += 1;
slouken@7713
   199
icculus@7883
   200
    len += SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3;
icculus@7667
   201
    retval = (char *) SDL_malloc(len);
icculus@7667
   202
    if (!retval) {
icculus@7667
   203
        SDL_OutOfMemory();
icculus@7667
   204
        return NULL;
icculus@7667
   205
    }
icculus@7667
   206
icculus@7883
   207
    SDL_snprintf(retval, len, "%s%s%s/%s/", envr, append, org, app);
icculus@7667
   208
icculus@7667
   209
    for (ptr = retval+1; *ptr; ptr++) {
icculus@7667
   210
        if (*ptr == '/') {
icculus@7667
   211
            *ptr = '\0';
slouken@7715
   212
            if (mkdir(retval, 0700) != 0 && errno != EEXIST)
slouken@7715
   213
                goto error;
icculus@7667
   214
            *ptr = '/';
icculus@7667
   215
        }
icculus@7667
   216
    }
slouken@7715
   217
    if (mkdir(retval, 0700) != 0 && errno != EEXIST) {
slouken@7715
   218
error:
brandon@8831
   219
        SDL_SetError("Couldn't create directory '%s': '%s'", retval, strerror(errno));
slouken@7715
   220
        SDL_free(retval);
slouken@7715
   221
        return NULL;
slouken@7715
   222
    }
icculus@7667
   223
icculus@7667
   224
    return retval;
icculus@7667
   225
}
icculus@7667
   226
icculus@7667
   227
#endif /* SDL_FILESYSTEM_UNIX */
icculus@7667
   228
icculus@7667
   229
/* vi: set ts=4 sw=4 expandtab: */