src/filesystem/unix/SDL_sysfilesystem.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 29 Aug 2013 08:24:43 -0700
changeset 7713 6074c53b0cda
parent 7697 678f71df2c6b
child 7714 1c227c5f5d8d
permissions -rw-r--r--
Christoph Mallon: Simplify avoidance of duplicate / in SDL_GetPrefPath()
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);
7697
678f71df2c6b Minor FreeBSD code cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 7695
diff changeset
    85
    const int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
7692
e1036c58fac7 Add support for some BSDs and Solaris to SDL_GetBasePath().
Ryan C. Gordon <icculus@icculus.org>
parents: 7668
diff changeset
    86
    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
    87
        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
    88
        if (!retval) {
e1036c58fac7 Add support for some BSDs and Solaris to SDL_GetBasePath().
Ryan C. Gordon <icculus@icculus.org>
parents: 7668
diff changeset
    89
            SDL_OutOfMemory();
e1036c58fac7 Add support for some BSDs and Solaris to SDL_GetBasePath().
Ryan C. Gordon <icculus@icculus.org>
parents: 7668
diff changeset
    90
            return NULL;
e1036c58fac7 Add support for some BSDs and Solaris to SDL_GetBasePath().
Ryan C. Gordon <icculus@icculus.org>
parents: 7668
diff changeset
    91
        }
e1036c58fac7 Add support for some BSDs and Solaris to SDL_GetBasePath().
Ryan C. Gordon <icculus@icculus.org>
parents: 7668
diff changeset
    92
    }
e1036c58fac7 Add support for some BSDs and Solaris to SDL_GetBasePath().
Ryan C. Gordon <icculus@icculus.org>
parents: 7668
diff changeset
    93
#elif defined(__SOLARIS__)
e1036c58fac7 Add support for some BSDs and Solaris to SDL_GetBasePath().
Ryan C. Gordon <icculus@icculus.org>
parents: 7668
diff changeset
    94
    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
    95
    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
    96
        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
    97
        if (!retval) {
e1036c58fac7 Add support for some BSDs and Solaris to SDL_GetBasePath().
Ryan C. Gordon <icculus@icculus.org>
parents: 7668
diff changeset
    98
            SDL_OutOfMemory();
e1036c58fac7 Add support for some BSDs and Solaris to SDL_GetBasePath().
Ryan C. Gordon <icculus@icculus.org>
parents: 7668
diff changeset
    99
            return NULL;
e1036c58fac7 Add support for some BSDs and Solaris to SDL_GetBasePath().
Ryan C. Gordon <icculus@icculus.org>
parents: 7668
diff changeset
   100
        }
e1036c58fac7 Add support for some BSDs and Solaris to SDL_GetBasePath().
Ryan C. Gordon <icculus@icculus.org>
parents: 7668
diff changeset
   101
    }
e1036c58fac7 Add support for some BSDs and Solaris to SDL_GetBasePath().
Ryan C. Gordon <icculus@icculus.org>
parents: 7668
diff changeset
   102
#endif
e1036c58fac7 Add support for some BSDs and Solaris to SDL_GetBasePath().
Ryan C. Gordon <icculus@icculus.org>
parents: 7668
diff changeset
   103
7667
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
    /* 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
   105
    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
   106
        #if defined(__FREEBSD__)
e1036c58fac7 Add support for some BSDs and Solaris to SDL_GetBasePath().
Ryan C. Gordon <icculus@icculus.org>
parents: 7668
diff changeset
   107
        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
   108
        #elif defined(__NETBSD__)
e1036c58fac7 Add support for some BSDs and Solaris to SDL_GetBasePath().
Ryan C. Gordon <icculus@icculus.org>
parents: 7668
diff changeset
   109
        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
   110
        #else
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/self/exe");  /* linux. */
e1036c58fac7 Add support for some BSDs and Solaris to SDL_GetBasePath().
Ryan C. Gordon <icculus@icculus.org>
parents: 7668
diff changeset
   112
        #endif
7667
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
        if (retval == NULL) {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
            /* 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
   115
            char path[64];
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
            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
   117
                                              "/proc/%llu/exe",
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
                                              (unsigned long long) getpid());
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
            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
   120
                retval = readSymLink(path);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
            }
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
        }
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
    }
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
    /* 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
   126
        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
   127
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
    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
   129
        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
   130
        if (ptr != NULL) {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
            *(ptr+1) = '\0';
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
        } 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
   133
            SDL_free(retval);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
            retval = NULL;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
        }
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
    }
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
    if (retval != NULL) {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
        /* try to shrink buffer... */
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
        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
   141
        if (ptr != NULL)
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
            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
   143
    }
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
    return retval;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
}
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
char *
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
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
   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
     * 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
   153
     *  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
   154
     *  in any case.
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
     * 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
   157
     */
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    const char *envr = SDL_getenv("XDG_DATA_HOME");
7713
6074c53b0cda Christoph Mallon: Simplify avoidance of duplicate / in SDL_GetPrefPath()
Sam Lantinga <slouken@libsdl.org>
parents: 7697
diff changeset
   159
    const char *append;
7667
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
    char *retval = NULL;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    char *ptr = NULL;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
    size_t len = 0;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
    if (!envr) {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
        /* 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
   166
        envr = SDL_getenv("HOME");
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
        if (!envr) {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
            /* 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
   169
            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
   170
            return NULL;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
        }
7713
6074c53b0cda Christoph Mallon: Simplify avoidance of duplicate / in SDL_GetPrefPath()
Sam Lantinga <slouken@libsdl.org>
parents: 7697
diff changeset
   172
        append = "/.local/share/";
7695
47d12c248988 Fix #2062 Be more diligent about validating trailing "/" existence in HOME and XDG_DATA_HOME env vars
Edward Rudd <urkle@outoforder.cc>
parents: 7694
diff changeset
   173
    } else {
7713
6074c53b0cda Christoph Mallon: Simplify avoidance of duplicate / in SDL_GetPrefPath()
Sam Lantinga <slouken@libsdl.org>
parents: 7697
diff changeset
   174
        append = "/";
7667
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
    } /* if */
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
7713
6074c53b0cda Christoph Mallon: Simplify avoidance of duplicate / in SDL_GetPrefPath()
Sam Lantinga <slouken@libsdl.org>
parents: 7697
diff changeset
   177
    len = SDL_strlen(envr);
6074c53b0cda Christoph Mallon: Simplify avoidance of duplicate / in SDL_GetPrefPath()
Sam Lantinga <slouken@libsdl.org>
parents: 7697
diff changeset
   178
    if (envr[len - 1] == '/')
6074c53b0cda Christoph Mallon: Simplify avoidance of duplicate / in SDL_GetPrefPath()
Sam Lantinga <slouken@libsdl.org>
parents: 7697
diff changeset
   179
        append += 1;
6074c53b0cda Christoph Mallon: Simplify avoidance of duplicate / in SDL_GetPrefPath()
Sam Lantinga <slouken@libsdl.org>
parents: 7697
diff changeset
   180
6074c53b0cda Christoph Mallon: Simplify avoidance of duplicate / in SDL_GetPrefPath()
Sam Lantinga <slouken@libsdl.org>
parents: 7697
diff changeset
   181
    len += SDL_strlen(append) + SDL_strlen(app) + 2;
7667
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
    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
   183
    if (!retval) {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
        SDL_OutOfMemory();
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
        return NULL;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
    }
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
    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
   189
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
    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
   191
        if (*ptr == '/') {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
            *ptr = '\0';
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
            mkdir(retval, 0700);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
            *ptr = '/';
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
        }
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
    mkdir(retval, 0700);
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
    return retval;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
}
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
#endif /* SDL_FILESYSTEM_UNIX */
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
/* vi: set ts=4 sw=4 expandtab: */