Backport from default branch: fixed basedir detection on Mac OS X.

This commit is contained in:
Ryan C. Gordon 2011-07-09 23:11:56 -07:00
parent 647867b6e4
commit cdcb169bcc
1 changed files with 37 additions and 14 deletions

View File

@ -16,6 +16,7 @@
#include <IOKit/storage/IOCDMedia.h> #include <IOKit/storage/IOCDMedia.h>
#include <IOKit/storage/IODVDMedia.h> #include <IOKit/storage/IODVDMedia.h>
#include <sys/mount.h> #include <sys/mount.h>
#include <sys/stat.h>
/* Seems to get defined in some system header... */ /* Seems to get defined in some system header... */
#ifdef Free #ifdef Free
@ -219,12 +220,15 @@ static char *convertCFString(CFStringRef cfstr)
char *__PHYSFS_platformCalcBaseDir(const char *argv0) char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{ {
ProcessSerialNumber psn = { 0, kCurrentProcess }; ProcessSerialNumber psn = { 0, kCurrentProcess };
struct stat statbuf;
FSRef fsref; FSRef fsref;
CFRange cfrange; CFRange cfrange;
CFURLRef cfurl = NULL; CFURLRef cfurl = NULL;
CFStringRef cfstr = NULL; CFStringRef cfstr = NULL;
CFMutableStringRef cfmutstr = NULL; CFMutableStringRef cfmutstr = NULL;
char *retval = NULL; char *retval = NULL;
char *cstr = NULL;
int rc = 0;
BAIL_IF_MACRO(GetProcessBundleLocation(&psn, &fsref) != noErr, NULL, NULL); BAIL_IF_MACRO(GetProcessBundleLocation(&psn, &fsref) != noErr, NULL, NULL);
cfurl = CFURLCreateFromFSRef(cfallocator, &fsref); cfurl = CFURLCreateFromFSRef(cfallocator, &fsref);
@ -236,7 +240,25 @@ char *__PHYSFS_platformCalcBaseDir(const char *argv0)
CFRelease(cfstr); CFRelease(cfstr);
BAIL_IF_MACRO(cfmutstr == NULL, NULL, NULL); BAIL_IF_MACRO(cfmutstr == NULL, NULL, NULL);
/* Find last dirsep so we can chop the binary's filename from the path. */ /* we have to decide if we got a binary's path, or the .app dir... */
cstr = convertCFString(cfmutstr);
if (cstr == NULL)
{
CFRelease(cfmutstr);
return(NULL);
} /* if */
rc = stat(cstr, &statbuf);
allocator.Free(cstr); /* done with this. */
if (rc == -1)
{
CFRelease(cfmutstr);
return(NULL); /* maybe default behaviour will work? */
} /* if */
if (S_ISREG(statbuf.st_mode))
{
/* Find last dirsep so we can chop the filename from the path. */
cfrange = CFStringFind(cfmutstr, CFSTR("/"), kCFCompareBackwards); cfrange = CFStringFind(cfmutstr, CFSTR("/"), kCFCompareBackwards);
if (cfrange.location == kCFNotFound) if (cfrange.location == kCFNotFound)
{ {
@ -257,6 +279,7 @@ char *__PHYSFS_platformCalcBaseDir(const char *argv0)
if (cfrange.location != kCFNotFound) if (cfrange.location != kCFNotFound)
CFStringDelete(cfmutstr, cfrange); /* chop that, too. */ CFStringDelete(cfmutstr, cfrange); /* chop that, too. */
} /* if */
retval = convertCFString(cfmutstr); retval = convertCFString(cfmutstr);
CFRelease(cfmutstr); CFRelease(cfmutstr);