qnx: Implemented SDL_GetBasePath().
authorRyan C. Gordon <icculus@icculus.org>
Thu, 17 Aug 2017 01:25:48 -0400
changeset 11309e421d01ce5bf
parent 11308 43e03b947942
child 11310 18bc7f7649d4
qnx: Implemented SDL_GetBasePath().
src/filesystem/unix/SDL_sysfilesystem.c
     1.1 --- a/src/filesystem/unix/SDL_sysfilesystem.c	Wed Aug 16 21:31:03 2017 -0400
     1.2 +++ b/src/filesystem/unix/SDL_sysfilesystem.c	Thu Aug 17 01:25:48 2017 -0400
     1.3 @@ -32,6 +32,7 @@
     1.4  #include <sys/stat.h>
     1.5  #include <sys/types.h>
     1.6  #include <limits.h>
     1.7 +#include <fcntl.h>
     1.8  
     1.9  #if defined(__FREEBSD__) || defined(__OPENBSD__)
    1.10  #include <sys/sysctl.h>
    1.11 @@ -41,6 +42,37 @@
    1.12  #include "SDL_stdinc.h"
    1.13  #include "SDL_filesystem.h"
    1.14  
    1.15 +/* QNX's /proc/self/exefile is a text file and not a symlink. */
    1.16 +#if defined(__QNXNTO__)
    1.17 +static char *
    1.18 +readWholeFile(const char *path)
    1.19 +{
    1.20 +    char *retval = (char *) SDL_malloc(PATH_MAX+1);
    1.21 +    if (retval != NULL)
    1.22 +    {
    1.23 +        const int fd = open(path, O_RDONLY);
    1.24 +        const ssize_t br = (fd == -1) ? -1 : read(fd, retval, PATH_MAX);
    1.25 +        char *ptr;
    1.26 +
    1.27 +        if (fd != -1)
    1.28 +            close(fd);
    1.29 +
    1.30 +        if ((br < 0) || (br > PATH_MAX))
    1.31 +        {
    1.32 +            free(retval);
    1.33 +            return NULL;
    1.34 +        } /* if */
    1.35 +
    1.36 +        retval[br] = '\0';
    1.37 +
    1.38 +        ptr = (char *) SDL_realloc(retval, br + 1);
    1.39 +        if (ptr != NULL)  /* just shrinking buffer; don't care if it failed. */
    1.40 +            retval = ptr;
    1.41 +    } /* else */
    1.42 +
    1.43 +    return retval;
    1.44 +}
    1.45 +#else
    1.46  static char *
    1.47  readSymLink(const char *path)
    1.48  {
    1.49 @@ -72,7 +104,7 @@
    1.50      SDL_free(retval);
    1.51      return NULL;
    1.52  }
    1.53 -
    1.54 +#endif
    1.55  
    1.56  char *
    1.57  SDL_GetBasePath(void)
    1.58 @@ -126,9 +158,10 @@
    1.59          retval = readSymLink("/proc/curproc/file");
    1.60  #elif defined(__NETBSD__)
    1.61          retval = readSymLink("/proc/curproc/exe");
    1.62 +#elif defined(__QNXNTO__)
    1.63 +        retval = readWholeFile("/proc/self/exefile");
    1.64  #else
    1.65          retval = readSymLink("/proc/self/exe");  /* linux. */
    1.66 -#endif
    1.67          if (retval == NULL) {
    1.68              /* older kernels don't have /proc/self ... try PID version... */
    1.69              char path[64];
    1.70 @@ -139,6 +172,7 @@
    1.71                  retval = readSymLink(path);
    1.72              }
    1.73          }
    1.74 +#endif
    1.75      }
    1.76  
    1.77      /* If we had access to argv[0] here, we could check it for a path,