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