mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:07:35 +00:00
sh: Make "cd" jump to the home directory. And also let's set $HOME.
Nobody else is setting $HOME right now, so we might as well do it here. This should eventually be done by some sort of "login" program.
This commit is contained in:
parent
89e9aafbb5
commit
96d4b701e6
1 changed files with 14 additions and 10 deletions
|
@ -18,6 +18,7 @@
|
||||||
struct GlobalState {
|
struct GlobalState {
|
||||||
String cwd;
|
String cwd;
|
||||||
String username;
|
String username;
|
||||||
|
String home;
|
||||||
char ttyname[32];
|
char ttyname[32];
|
||||||
char hostname[32];
|
char hostname[32];
|
||||||
pid_t sid;
|
pid_t sid;
|
||||||
|
@ -66,16 +67,16 @@ static int sh_exit(int, char**)
|
||||||
|
|
||||||
static int sh_cd(int argc, char** argv)
|
static int sh_cd(int argc, char** argv)
|
||||||
{
|
{
|
||||||
if (argc == 1) {
|
char pathbuf[PATH_MAX];
|
||||||
printf("usage: cd <path>\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
char pathbuf[128];
|
if (argc == 1) {
|
||||||
if (argv[1][0] == '/')
|
strcpy(pathbuf, g->home.characters());
|
||||||
memcpy(pathbuf, argv[1], strlen(argv[1]) + 1);
|
} else {
|
||||||
else
|
if (argv[1][0] == '/')
|
||||||
sprintf(pathbuf, "%s/%s", g->cwd.characters(), argv[1]);
|
memcpy(pathbuf, argv[1], strlen(argv[1]) + 1);
|
||||||
|
else
|
||||||
|
sprintf(pathbuf, "%s/%s", g->cwd.characters(), argv[1]);
|
||||||
|
}
|
||||||
|
|
||||||
FileSystemPath canonical_path(pathbuf);
|
FileSystemPath canonical_path(pathbuf);
|
||||||
if (!canonical_path.is_valid()) {
|
if (!canonical_path.is_valid()) {
|
||||||
|
@ -520,8 +521,11 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
{
|
{
|
||||||
auto* pw = getpwuid(getuid());
|
auto* pw = getpwuid(getuid());
|
||||||
if (pw)
|
if (pw) {
|
||||||
g->username = pw->pw_name;
|
g->username = pw->pw_name;
|
||||||
|
g->home = pw->pw_dir;
|
||||||
|
putenv(const_cast<char*>(String::format("HOME=%s", pw->pw_dir).characters()));
|
||||||
|
}
|
||||||
endpwent();
|
endpwent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue