src/platform_posix.c
changeset 1266 135cb99a06a0
parent 1249 1a5ef6c908f0
child 1290 a47c773cc5ee
--- a/src/platform_posix.c	Fri Mar 23 04:54:20 2012 -0400
+++ b/src/platform_posix.c	Sat Mar 24 00:26:04 2012 -0400
@@ -61,23 +61,6 @@
 } /* errcodeFromErrno */
 
 
-
-char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname)
-{
-    const char *envr = getenv(varname);
-    char *retval = NULL;
-
-    if (envr != NULL)
-    {
-        retval = (char *) allocator.Malloc(strlen(envr) + 1);
-        BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
-        strcpy(retval, envr);
-    } /* if */
-
-    return retval;
-} /* __PHYSFS_platformCopyEnvironmentVariable */
-
-
 static char *getUserDirByUID(void)
 {
     uid_t uid = getuid();
@@ -85,11 +68,20 @@
     char *retval = NULL;
 
     pw = getpwuid(uid);
-    if ((pw != NULL) && (pw->pw_dir != NULL))
+    if ((pw != NULL) && (pw->pw_dir != NULL) && (*pw->pw_dir != '\0'))
     {
-        retval = (char *) allocator.Malloc(strlen(pw->pw_dir) + 1);
+        const size_t dlen = strlen(pw->pw_dir);
+        const size_t add_dirsep = (pw->pw_dir[dlen-1] != '/') ? 1 : 0;
+        retval = (char *) allocator.Malloc(dlen + 1 + add_dirsep);
         if (retval != NULL)
+        {
             strcpy(retval, pw->pw_dir);
+            if (add_dirsep)
+            {
+                retval[dlen] = '/';
+                retval[dlen+1] = '\0';
+            } /* if */
+        } /* if */
     } /* if */
     
     return retval;
@@ -98,16 +90,27 @@
 
 char *__PHYSFS_platformCalcUserDir(void)
 {
-    char *retval = __PHYSFS_platformCopyEnvironmentVariable("HOME");
+    char *retval = NULL;
+    char *envr = getenv("HOME");
 
     /* if the environment variable was set, make sure it's really a dir. */
-    if (retval != NULL)
+    if (envr != NULL)
     {
         struct stat statbuf;
-        if ((stat(retval, &statbuf) == -1) || (S_ISDIR(statbuf.st_mode) == 0))
+        if ((stat(envr, &statbuf) != -1) && (S_ISDIR(statbuf.st_mode)))
         {
-            allocator.Free(retval);
-            retval = NULL;
+            const size_t envrlen = strlen(envr);
+            const size_t add_dirsep = (envr[envrlen-1] != '/') ? 1 : 0;
+            retval = allocator.Malloc(envrlen + 1 + add_dirsep);
+            if (retval)
+            {
+                strcpy(retval, envr);
+                if (add_dirsep)
+                {
+                    retval[envrlen] = '/';
+                    retval[envrlen+1] = '\0';
+                } /* if */
+            } /* if */
         } /* if */
     } /* if */