platform/unix.c
changeset 23 bd6ba9c8717c
parent 20 efdde0d21521
child 28 529214f57d1b
equal deleted inserted replaced
22:49f6101707b4 23:bd6ba9c8717c
    55 
    55 
    56     return(retval);
    56     return(retval);
    57 } /* __PHYSFS_detectAvailableCDs */
    57 } /* __PHYSFS_detectAvailableCDs */
    58 
    58 
    59 
    59 
    60 char *__PHYSFS_platformCalcBaseDir(char *argv0)
       
    61 {
       
    62     return(NULL);  /* default PhysicsFS behaviour is acceptable. */
       
    63 } /* __PHYSFS_platformCalcBaseDir */
       
    64 
       
    65 
       
    66 static char *copyEnvironmentVariable(const char *varname)
    60 static char *copyEnvironmentVariable(const char *varname)
    67 {
    61 {
    68     const char *envr = getenv(varname);
    62     const char *envr = getenv(varname);
    69     char *retval = NULL;
    63     char *retval = NULL;
    70 
    64 
    75             strcpy(retval, envr);
    69             strcpy(retval, envr);
    76     } /* if */
    70     } /* if */
    77 
    71 
    78     return(retval);
    72     return(retval);
    79 } /* copyEnvironmentVariable */
    73 } /* copyEnvironmentVariable */
       
    74 
       
    75 
       
    76 /* !!! this is ugly. */
       
    77 char *__PHYSFS_platformCalcBaseDir(const char *argv0)
       
    78 {
       
    79     /* If there isn't a path on argv0, then look through the $PATH for it. */
       
    80 
       
    81     char *retval = NULL;
       
    82     char *envr;
       
    83     char *start;
       
    84     char *ptr;
       
    85     char *exe;
       
    86 
       
    87     if (strchr(argv0, '/') != NULL)   /* default behaviour can handle this. */
       
    88         return(NULL);
       
    89 
       
    90     envr = copyEnvironmentVariable("PATH");
       
    91     BAIL_IF_MACRO(!envr, NULL, NULL);
       
    92 
       
    93     start = envr;
       
    94     do
       
    95     {
       
    96         ptr = strchr(start, ':');
       
    97         if (ptr)
       
    98             *ptr = '\0';
       
    99 
       
   100         exe = (char *) malloc(strlen(start) + strlen(argv0) + 2);
       
   101         if (!exe)
       
   102         {
       
   103             free(envr);
       
   104             BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
       
   105         } /* if */
       
   106         strcpy(exe, start);
       
   107         if (start[strlen(start) - 1] != '/')
       
   108             strcat(start, "/");
       
   109         strcat(start, argv0);
       
   110         if (access(exe, X_OK) != 0)
       
   111             free(exe);
       
   112         else
       
   113         {
       
   114             retval = exe;
       
   115             strcpy(retval, start);  /* i'm lazy. piss off. */
       
   116             break;
       
   117         } /* else */
       
   118 
       
   119         start = ptr + 1;
       
   120     } while (ptr != NULL);
       
   121 
       
   122     free(envr);
       
   123     return(retval);
       
   124 } /* __PHYSFS_platformCalcBaseDir */
    80 
   125 
    81 
   126 
    82 static char *getUserNameByUID(void)
   127 static char *getUserNameByUID(void)
    83 {
   128 {
    84     uid_t uid = getuid();
   129     uid_t uid = getuid();