changeset 1098:9e1d6fa397be

Experimental change to set the current working directory to the bundle path on Mac. This will only occur when the current working directory is initially / so it shouldn't affect command line tools or when run without an application bundle.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Tue, 19 Jul 2011 05:10:56 +0000
parents 8e9db23a8c31
children d1cea9be1436
files mac/dw.m
diffstat 1 files changed, 33 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mac/dw.m	Fri Jul 01 02:48:20 2011 +0000
+++ b/mac/dw.m	Tue Jul 19 05:10:56 2011 +0000
@@ -8913,6 +8913,39 @@
  */
 int API dw_init(int newthread, int argc, char *argv[])
 {
+    /* Correct the startup path if run from a bundle */
+    if(argc > 0 && argv[0])
+    {
+        char *pathcopy = strdup(argv[0]);
+        char *app = strstr(pathcopy, ".app/");
+        
+        if(app)
+        {
+            char pathbuf[PATH_MAX];
+            
+            *app = 0;
+            
+            getcwd(pathbuf, PATH_MAX);
+            
+            /* If run from a bundle the path seems to be / */
+            if(strcmp(pathbuf, "/") == 0)
+            {
+                size_t len;
+                
+                strncpy(pathbuf, pathcopy, PATH_MAX);
+                
+                len = strlen(pathbuf);
+                
+                while(len > 0 && pathbuf[len] != '/')
+                {
+                    len--;
+                }
+                pathbuf[len] = '\0';
+                chdir(pathbuf);
+            }
+        }
+        free(pathcopy);
+    }
     /* Get the operating system version */
     Gestalt(gestaltSystemVersionMajor, &DWOSMajor);
     Gestalt(gestaltSystemVersionMinor, &DWOSMinor);