Skip to content

Commit

Permalink
Shell: Remember previous working dir
Browse files Browse the repository at this point in the history
...and allow switching back to it with `cd -`

Partially addresses SerenityOS#397
  • Loading branch information
seven1m authored and awesomekling committed Sep 13, 2019
1 parent ad75b61 commit 093961d
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Shell/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -635,6 +643,7 @@ int main(int argc, char** argv)
{
auto* cwd = getcwd(nullptr, 0);
g.cwd = cwd;
setenv("PWD", cwd, 1);
free(cwd);
}

Expand Down

0 comments on commit 093961d

Please sign in to comment.