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