Patches from David Hedbor to handle missing current working directories. release-0.1.4
authorRyan C. Gordon <icculus@icculus.org>
Fri, 14 Sep 2001 22:59:53 +0000
changeset 100 d2f04ab4b412
parent 99 5a5a0d60cbf1
child 101 bd18e17ad6ad
Patches from David Hedbor to handle missing current working directories. "The attached patch makes PhysFS handle the case when the current directory (as returned by getcwd()) has been deleted. Fix 1, platform/unix.c: Only retry getcwd if the error is ERANGE. Before it retried till the malloc failed in the case of the directory not existing. Fix 2: physfs.c: If __PHYSFS_platformCurrentDir() return's NULL, use the root as the base dir. Bad solution but the best I can think of (alternative : use /tmp or Windows TEMP dir?)."
physfs.c
platform/unix.c
--- a/physfs.c	Fri Sep 14 22:58:04 2001 +0000
+++ b/physfs.c	Fri Sep 14 22:59:53 2001 +0000
@@ -340,7 +340,19 @@
     /*
      * Last ditch effort: it's the current working directory. (*shrug*)
      */
-    return(__PHYSFS_platformCurrentDir());
+    retval = __PHYSFS_platformCurrentDir();
+    if(retval != NULL) {
+	return(retval);
+    }
+
+    /*
+     * Ok, current directory doesn't exist, use the root directory.
+     * Not a good alternative, but it only happens if the current
+     * directory was deleted from under the program.
+     */
+    retval = (char *) malloc(strlen(dirsep) + 1);
+    strcpy(retval, dirsep);
+    return(retval);
 } /* calculateBaseDir */
 
 
--- a/platform/unix.c	Fri Sep 14 22:58:04 2001 +0000
+++ b/platform/unix.c	Fri Sep 14 22:59:53 2001 +0000
@@ -436,11 +436,18 @@
                 free(retval);
             BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
         } /* if */
-
+	
         retval = ptr;
         ptr = getcwd(retval, allocSize);
-    } while (ptr == NULL);
-
+    } while (ptr == NULL && errno == ERANGE);
+    if(ptr == NULL && errno) {
+	/* getcwd() failed for some reason, for example current
+	 * directory not existing.
+	 */
+	if (retval != NULL)
+	    free(retval);
+	BAIL_IF_MACRO(1, ERR_NO_SUCH_FILE, NULL);
+    }
     return(retval);
 } /* __PHYSFS_platformCurrentDir */