about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog4
-rw-r--r--Src/utils.c22
2 files changed, 21 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index e1f014314..020716bc6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,7 @@
 2009-03-24  Peter Stephenson  <pws@csr.com>
 
+	* 26774: Src/utils.c: cd to $HOME on failure in preference to /.
+
 	* 26772: Src/glob.c, Src/utils.c, Src/zsh.h, Src/Modules/files.c:
 	more failed cd handling: fix possible runaway series of chdirs;
 	better error handling.
@@ -11480,5 +11482,5 @@
 
 *****************************************************
 * This is used by the shell to define $ZSH_PATCHLEVEL
-* $Revision: 1.4634 $
+* $Revision: 1.4635 $
 *****************************************************
diff --git a/Src/utils.c b/Src/utils.c
index 9bf7878e0..cf375821d 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -5493,16 +5493,30 @@ lchdir(char const *path, struct dirsav *d, int hard)
     }
     if (restoredir(d)) {
 	int restoreerr = errno;
+	int i;
 	/*
 	 * Failed to restore the directory.
 	 * Just be definite, cd to root and report the result.
 	 */
-	zsfree(pwd);
-	pwd = ztrdup("/");
-	if (chdir(pwd) < 0)
+	for (i = 0; i < 2; i++) {
+	    const char *cdest;
+	    if (i)
+		cdest = "/";
+	    else {
+		if (!home)
+		    continue;
+		cdest = home;
+	    }
+	    zsfree(pwd);
+	    pwd = ztrdup(cdest);
+	    if (chdir(pwd) == 0)
+		break;
+	}
+	if (i == 2)
 	    zerr("lost current directory, failed to cd to /: %e", errno);
 	else
-	    zerr("lost current directory: %e: changed to /", restoreerr);
+	    zerr("lost current directory: %e: changed to `%s'", restoreerr,
+		pwd);
 	if (d == &ds)
 	    zsfree(ds.dirname);
 #ifdef HAVE_FCHDIR