diff mac/dw.m @ 1381:3ba4853d5b78

Initial attempt at dw_app_dir() for OS/2, Windows and Mac... This function will return the best attempt at determining the application data directory for the current platform. The executable directory on OS/2 or Windows. The application bundle directory on Mac. The "share" folder at the same level as the binary (or /usr/local/share if that can't be determined) plus the binary name on Unix.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Fri, 25 Nov 2011 17:36:59 +0000
parents a54720b10821
children ead81c3d9e4e
line wrap: on
line diff
--- a/mac/dw.m	Thu Nov 24 06:07:40 2011 +0000
+++ b/mac/dw.m	Fri Nov 25 17:36:59 2011 +0000
@@ -121,6 +121,7 @@
 pthread_key_t _dw_fg_color_key;
 pthread_key_t _dw_bg_color_key;
 SInt32 DWOSMajor, DWOSMinor, DWOSBuild;
+static char _dw_bundle_path[PATH_MAX+1] = { 0 };
 
 /* Create a default colors for a thread */
 void _init_colors(void)
@@ -3032,14 +3033,14 @@
  */
 char *dw_user_dir(void)
 {
-    static char _user_dir[1024] = "";
+    static char _user_dir[PATH_MAX+1] = { 0 };
 
     if(!_user_dir[0])
     {
         char *home = getenv("HOME");
 
         if(home)
-            strcpy(_user_dir, home);
+            strncpy(_user_dir, home, PATH_MAX);
         else
             strcpy(_user_dir, "/");
     }
@@ -3047,6 +3048,15 @@
 }
 
 /*
+ * Returns a pointer to a static buffer which containes the
+ * private application data directory. 
+ */
+char *dw_app_dir(void)
+{
+    return _dw_bundle_path;
+}
+
+/*
  * Displays a debug message on the console...
  * Parameters:
  *           format: printf style format string.
@@ -9604,8 +9614,11 @@
 
         if(app)
         {
-            char pathbuf[PATH_MAX];
-
+            char pathbuf[PATH_MAX+1] = { 0 };
+            size_t len = (size_t)(app - pathcopy);
+            
+            if(len > 0)
+                strncpy(_dw_bundle_path, pathcopy, len + 5);
             *app = 0;
 
             getcwd(pathbuf, PATH_MAX);
@@ -9613,22 +9626,23 @@
             /* 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] != '/')
+                char *pos = strrchr(pathcopy, '/');
+
+                if(pos)
                 {
-                    len--;
+                    strncpy(pathbuf, pathcopy, (size_t)(pos - pathcopy));
+                    chdir(pathbuf);
                 }
-                pathbuf[len] = '\0';
-                chdir(pathbuf);
             }
         }
+    }
+    if(pathcopy)
         free(pathcopy);
-    }
+        
+    /* Just in case we can't obtain a path */
+    if(!_dw_bundle_path[0])
+        getcwd(_dw_bundle_path, PATH_MAX);
+    
     /* Get the operating system version */
     Gestalt(gestaltSystemVersionMajor, &DWOSMajor);
     Gestalt(gestaltSystemVersionMinor, &DWOSMinor);