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