about summary refs log tree commit diff
path: root/Src
diff options
context:
space:
mode:
authorPeter Stephenson <pws@users.sourceforge.net>2009-03-24 16:13:12 +0000
committerPeter Stephenson <pws@users.sourceforge.net>2009-03-24 16:13:12 +0000
commit89d979e1bc39feaff8062ff0fa2242ad277b4080 (patch)
tree7fcaa2cb5074f20a27f3e79867f5ff7ce68452a5 /Src
parent9defc9850d0d73742856165f595899f0d8a87c6d (diff)
downloadzsh-89d979e1bc39feaff8062ff0fa2242ad277b4080.tar.gz
zsh-89d979e1bc39feaff8062ff0fa2242ad277b4080.tar.xz
zsh-89d979e1bc39feaff8062ff0fa2242ad277b4080.zip
26774: cd to $HOME on failure in preference to /
Diffstat (limited to 'Src')
-rw-r--r--Src/utils.c22
1 files changed, 18 insertions, 4 deletions
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