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.
This commit is contained in:
parent
ca62bc5f7e
commit
7b40c6df95
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=294649
19
bin/sh/cd.c
19
bin/sh/cd.c
@ -75,7 +75,6 @@ static char *getpwd(void);
|
|||||||
static char *getpwd2(void);
|
static char *getpwd2(void);
|
||||||
|
|
||||||
static char *curdir = NULL; /* current working directory */
|
static char *curdir = NULL; /* current working directory */
|
||||||
static char *prevdir; /* previous working directory */
|
|
||||||
static char *cdcomppath;
|
static char *cdcomppath;
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -112,11 +111,10 @@ cdcmd(int argc __unused, char **argv __unused)
|
|||||||
if (*dest == '\0')
|
if (*dest == '\0')
|
||||||
dest = ".";
|
dest = ".";
|
||||||
if (dest[0] == '-' && dest[1] == '\0') {
|
if (dest[0] == '-' && dest[1] == '\0') {
|
||||||
dest = prevdir ? prevdir : curdir;
|
dest = bltinlookup("OLDPWD", 1);
|
||||||
if (dest)
|
if (dest == NULL)
|
||||||
print = 1;
|
error("OLDPWD not set");
|
||||||
else
|
print = 1;
|
||||||
dest = ".";
|
|
||||||
}
|
}
|
||||||
if (dest[0] == '/' ||
|
if (dest[0] == '/' ||
|
||||||
(dest[0] == '.' && (dest[1] == '/' || dest[1] == '\0')) ||
|
(dest[0] == '.' && (dest[1] == '/' || dest[1] == '\0')) ||
|
||||||
@ -311,14 +309,15 @@ findcwd(char *dir)
|
|||||||
static void
|
static void
|
||||||
updatepwd(char *dir)
|
updatepwd(char *dir)
|
||||||
{
|
{
|
||||||
|
char *prevdir;
|
||||||
|
|
||||||
hashcd(); /* update command hash table */
|
hashcd(); /* update command hash table */
|
||||||
|
|
||||||
if (prevdir)
|
setvar("PWD", dir, VEXPORT);
|
||||||
ckfree(prevdir);
|
setvar("OLDPWD", curdir, VEXPORT);
|
||||||
prevdir = curdir;
|
prevdir = curdir;
|
||||||
curdir = dir ? savestr(dir) : NULL;
|
curdir = dir ? savestr(dir) : NULL;
|
||||||
setvar("PWD", curdir, VEXPORT);
|
ckfree(prevdir);
|
||||||
setvar("OLDPWD", prevdir, VEXPORT);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
Loading…
Reference in New Issue
Block a user