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