From 96d4b701e626c735cea447b5b0664d1b420a00fb Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 25 Apr 2019 23:09:21 +0200 Subject: [PATCH] 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. --- Userland/sh.cpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Userland/sh.cpp b/Userland/sh.cpp index cd00a8dd06..6813e97b83 100644 --- a/Userland/sh.cpp +++ b/Userland/sh.cpp @@ -18,6 +18,7 @@ struct GlobalState { String cwd; String username; + String home; char ttyname[32]; char hostname[32]; pid_t sid; @@ -66,16 +67,16 @@ static int sh_exit(int, char**) static int sh_cd(int argc, char** argv) { - if (argc == 1) { - printf("usage: cd \n"); - return 0; - } + char pathbuf[PATH_MAX]; - char pathbuf[128]; - if (argv[1][0] == '/') - memcpy(pathbuf, argv[1], strlen(argv[1]) + 1); - else - sprintf(pathbuf, "%s/%s", g->cwd.characters(), argv[1]); + if (argc == 1) { + strcpy(pathbuf, g->home.characters()); + } else { + if (argv[1][0] == '/') + memcpy(pathbuf, argv[1], strlen(argv[1]) + 1); + else + sprintf(pathbuf, "%s/%s", g->cwd.characters(), argv[1]); + } FileSystemPath canonical_path(pathbuf); if (!canonical_path.is_valid()) { @@ -520,8 +521,11 @@ int main(int argc, char** argv) { auto* pw = getpwuid(getuid()); - if (pw) + if (pw) { g->username = pw->pw_name; + g->home = pw->pw_dir; + putenv(const_cast(String::format("HOME=%s", pw->pw_dir).characters())); + } endpwent(); }