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