From a948b7f5466e79b0d4f22cf36a6f5fa764029eab Mon Sep 17 00:00:00 2001 From: Timo Hirvonen Date: Sat, 26 Apr 2014 20:12:09 +0300 Subject: [PATCH] Bash-like "cd -" --- Documentation/dex.txt | 3 ++- commands.c | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Documentation/dex.txt b/Documentation/dex.txt index 320328e..e173c47 100644 --- a/Documentation/dex.txt +++ b/Documentation/dex.txt @@ -112,7 +112,8 @@ case [-lu] -u upper case cd - Change directory. Updates $PWD and $OLDPWD. + Change directory. Updates $PWD and $OLDPWD. "cd -" changes to + previous directory ($OLDPWD). center-view Center view to cursor. diff --git a/commands.c b/commands.c index 011f765..e0d9480 100644 --- a/commands.c +++ b/commands.c @@ -87,12 +87,20 @@ static void cmd_case(const char *pf, char **args) static void cmd_cd(const char *pf, char **args) { + char *dir = args[0]; char cwd[8192]; char *cwdp = NULL; bool got_cwd = !!getcwd(cwd, sizeof(cwd)); int i; - if (chdir(args[0])) { + if (streq(dir, "-")) { + dir = getenv("OLDPWD"); + if (dir == NULL || dir[0] == 0) { + error_msg("cd: OLDPWD not set"); + return; + } + } + if (chdir(dir)) { error_msg("cd: %s", strerror(errno)); return; }