diff --git a/physfs.c b/physfs.c index a0bb6f2..fa3d4ce 100644 --- a/physfs.c +++ b/physfs.c @@ -340,7 +340,19 @@ static char *calculateBaseDir(const char *argv0) /* * 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 */ diff --git a/platform/unix.c b/platform/unix.c index 3d34533..1f07f93 100644 --- a/platform/unix.c +++ b/platform/unix.c @@ -436,11 +436,18 @@ char *__PHYSFS_platformCurrentDir(void) 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 */