summaryrefslogtreecommitdiff
path: root/Shell/main.cpp
diff options
context:
space:
mode:
authorTim Morgan <tim@timmorgan.org>2019-09-11 20:46:38 -0500
committerAndreas Kling <awesomekling@gmail.com>2019-09-13 10:48:04 +0200
commit093961d2d935b189861f86d2f805ac31c23377f9 (patch)
treee1c5eb3eab2d0cd26414dd60bcd618d5ba7dae42 /Shell/main.cpp
parentad75b61ecaba73a16b34359a22fe14b340275813 (diff)
downloadserenity-093961d2d935b189861f86d2f805ac31c23377f9.zip
Shell: Remember previous working dir
...and allow switching back to it with `cd -` Partially addresses #397
Diffstat (limited to 'Shell/main.cpp')
-rw-r--r--Shell/main.cpp13
1 files changed, 11 insertions, 2 deletions
diff --git a/Shell/main.cpp b/Shell/main.cpp
index 1891614a7f..ebf5b1c78f 100644
--- a/Shell/main.cpp
+++ b/Shell/main.cpp
@@ -83,10 +83,16 @@ static int sh_cd(int argc, char** argv)
if (argc == 1) {
strcpy(pathbuf, g.home.characters());
} else {
- if (argv[1][0] == '/')
+ if (strcmp(argv[1], "-") == 0) {
+ char* oldpwd = getenv("OLDPWD");
+ size_t len = strlen(oldpwd);
+ ASSERT(len + 1 <= PATH_MAX);
+ memcpy(pathbuf, oldpwd, len + 1);
+ } else if (argv[1][0] == '/') {
memcpy(pathbuf, argv[1], strlen(argv[1]) + 1);
- else
+ } else {
sprintf(pathbuf, "%s/%s", g.cwd.characters(), argv[1]);
+ }
}
FileSystemPath canonical_path(pathbuf);
@@ -111,7 +117,9 @@ static int sh_cd(int argc, char** argv)
printf("chdir(%s) failed: %s\n", path, strerror(errno));
return 1;
}
+ setenv("OLDPWD", g.cwd.characters(), 1);
g.cwd = canonical_path.string();
+ setenv("PWD", g.cwd.characters(), 1);
return 0;
}
@@ -635,6 +643,7 @@ int main(int argc, char** argv)
{
auto* cwd = getcwd(nullptr, 0);
g.cwd = cwd;
+ setenv("PWD", cwd, 1);
free(cwd);
}