summaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authorjilles <jilles@FreeBSD.org>2016-01-23 23:00:38 +0000
committerjilles <jilles@FreeBSD.org>2016-01-23 23:00:38 +0000
commitc917a39008740eaf29dc9d7f60f03e1b3984fee9 (patch)
treed4f63f37586bc1731736e89e674aaf47d5d79fe4 /bin
parentf519aed619251f16640a39b7d3a9f04fba3641d3 (diff)
downloadFreeBSD-src-c917a39008740eaf29dc9d7f60f03e1b3984fee9.zip
FreeBSD-src-c917a39008740eaf29dc9d7f60f03e1b3984fee9.tar.gz
sh: Use OLDPWD shell variable for 'cd -'.
Per POSIX, 'cd -' should use the OLDPWD shell variable, not internal state. This variable is normally exported. Also, if OLDPWD is not set, fail 'cd -' instead of changing to the current directory.
Diffstat (limited to 'bin')
-rw-r--r--bin/sh/cd.c19
1 files changed, 9 insertions, 10 deletions
diff --git a/bin/sh/cd.c b/bin/sh/cd.c
index 88f03f5..aa2549f 100644
--- a/bin/sh/cd.c
+++ b/bin/sh/cd.c
@@ -75,7 +75,6 @@ static char *getpwd(void);
static char *getpwd2(void);
static char *curdir = NULL; /* current working directory */
-static char *prevdir; /* previous working directory */
static char *cdcomppath;
int
@@ -112,11 +111,10 @@ cdcmd(int argc __unused, char **argv __unused)
if (*dest == '\0')
dest = ".";
if (dest[0] == '-' && dest[1] == '\0') {
- dest = prevdir ? prevdir : curdir;
- if (dest)
- print = 1;
- else
- dest = ".";
+ dest = bltinlookup("OLDPWD", 1);
+ if (dest == NULL)
+ error("OLDPWD not set");
+ print = 1;
}
if (dest[0] == '/' ||
(dest[0] == '.' && (dest[1] == '/' || dest[1] == '\0')) ||
@@ -311,14 +309,15 @@ findcwd(char *dir)
static void
updatepwd(char *dir)
{
+ char *prevdir;
+
hashcd(); /* update command hash table */
- if (prevdir)
- ckfree(prevdir);
+ setvar("PWD", dir, VEXPORT);
+ setvar("OLDPWD", curdir, VEXPORT);
prevdir = curdir;
curdir = dir ? savestr(dir) : NULL;
- setvar("PWD", curdir, VEXPORT);
- setvar("OLDPWD", prevdir, VEXPORT);
+ ckfree(prevdir);
}
int
OpenPOWER on IntegriCloud