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.
     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 #include <sys/stat.h>
    30 #include <sys/types.h>
    31 
    32 #include "SDL_error.h"
    33 #include "SDL_stdinc.h"
    34 #include "SDL_filesystem.h"
    35 
    36 static char *
    37 readSymLink(const char *path)
    38 {
    39     char *retval = NULL;
    40     ssize_t len = 64;
    41     ssize_t rc = -1;
    42 
    43     while (1)
    44     {
    45         char *ptr = (char *) SDL_realloc(retval, (size_t) len);
    46         if (ptr == NULL) {
    47             SDL_OutOfMemory();
    48             break;
    49         }
    50 
    51         retval = ptr;
    52 
    53         rc = readlink(path, retval, len);
    54         if (rc == -1) {
    55             break;  /* not a symlink, i/o error, etc. */
    56         } else if (rc < len) {
    57             retval[rc] = '\0';  /* readlink doesn't null-terminate. */
    58             return retval;  /* we're good to go. */
    59         }
    60 
    61         len *= 2;  /* grow buffer, try again. */
    62     }
    63 
    64     if (retval != NULL) {
    65         SDL_free(retval);
    66     }
    67     return NULL;
    68 }
    69 
    70 
    71 char *
    72 SDL_GetBasePath(void)
    73 {
    74     char *retval = NULL;
    75 
    76     /* is a Linux-style /proc filesystem available? */
    77     if (access("/proc", F_OK) == 0) {
    78         retval = readSymLink("/proc/self/exe");
    79         if (retval == NULL) {
    80             /* older kernels don't have /proc/self ... try PID version... */
    81             char path[64];
    82             const int rc = (int) SDL_snprintf(path, sizeof(path),
    83                                               "/proc/%llu/exe",
    84                                               (unsigned long long) getpid());
    85             if ( (rc > 0) && (rc < sizeof(path)) ) {
    86                 retval = readSymLink(path);
    87             }
    88         }
    89     }
    90 
    91     /* If we had access to argv[0] here, we could check it for a path,
    92         or troll through $PATH looking for it, too. */
    93 
    94     if (retval != NULL) { /* chop off filename. */
    95         char *ptr = SDL_strrchr(retval, '/');
    96         if (ptr != NULL) {
    97             *(ptr+1) = '\0';
    98         } else {  /* shouldn't happen, but just in case... */
    99             SDL_free(retval);
   100             retval = NULL;
   101         }
   102     }
   103 
   104     if (retval != NULL) {
   105         /* try to shrink buffer... */
   106         char *ptr = (char *) SDL_realloc(retval, strlen(retval) + 1);
   107         if (ptr != NULL)
   108             retval = ptr;  /* oh well if it failed. */
   109     }
   110 
   111     return retval;
   112 }
   113 
   114 char *
   115 SDL_GetPrefPath(const char *org, const char *app)
   116 {
   117     /*
   118      * We use XDG's base directory spec, even if you're not on Linux.
   119      *  This isn't strictly correct, but the results are relatively sane
   120      *  in any case.
   121      *
   122      * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
   123      */
   124     const char *envr = SDL_getenv("XDG_DATA_HOME");
   125     const char *append = "/";
   126     char *retval = NULL;
   127     char *ptr = NULL;
   128     size_t len = 0;
   129 
   130     if (!envr) {
   131         /* You end up with "$HOME/.local/share/Game Name 2" */
   132         envr = SDL_getenv("HOME");
   133         if (!envr) {
   134             /* we could take heroic measures with /etc/passwd, but oh well. */
   135             SDL_SetError("neither XDG_DATA_HOME nor HOME environment is set");
   136             return NULL;
   137         }
   138         append = ".local/share/";
   139     } /* if */
   140 
   141     len = SDL_strlen(envr) + SDL_strlen(append) + SDL_strlen(app) + 2;
   142     retval = (char *) SDL_malloc(len);
   143     if (!retval) {
   144         SDL_OutOfMemory();
   145         return NULL;
   146     }
   147 
   148     SDL_snprintf(retval, len, "%s%s%s/", envr, append, app);
   149 
   150     for (ptr = retval+1; *ptr; ptr++) {
   151         if (*ptr == '/') {
   152             *ptr = '\0';
   153             mkdir(retval, 0700);
   154             *ptr = '/';
   155         }
   156     }
   157     mkdir(retval, 0700);
   158 
   159     return retval;
   160 }
   161 
   162 #endif /* SDL_FILESYSTEM_UNIX */
   163 
   164 /* vi: set ts=4 sw=4 expandtab: */