mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 13:36:13 +03:00
Shell: Remember previous working dir
...and allow switching back to it with `cd -` Partially addresses #397
This commit is contained in:
parent
ad75b61eca
commit
093961d2d9
Notes:
sideshowbarker
2024-07-19 12:08:21 +09:00
Author: https://github.com/seven1m Commit: https://github.com/SerenityOS/serenity/commit/093961d2d93 Pull-request: https://github.com/SerenityOS/serenity/pull/549
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user