From 919c9dd3cfbe676bf3377a3d8f58a4fcb0ec8d42 Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 24 Apr 2023 22:13:06 +0200 Subject: [PATCH] sh: Build a more elaborate prompt using the system hostname and username --- apps/sh.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/apps/sh.cpp b/apps/sh.cpp index 42a73c63..b3ecc98e 100644 --- a/apps/sh.cpp +++ b/apps/sh.cpp @@ -6,10 +6,12 @@ #include #include +#include #include #include #include #include +#include #include #include @@ -28,6 +30,12 @@ static Result execute_command(StringView command) return os::Process::exec(args[0].view(), args.slice()); } +struct utsname g_sysinfo; + +const char* hostname = ""; +const char* username = ""; +char prompt_end = '$'; + Result luna_main(int argc, char** argv) { StringView path; @@ -55,12 +63,23 @@ Result luna_main(int argc, char** argv) input_file->set_close_on_exec(); } + if (interactive) + { + // Set up everything to form a prompt. + uname(&g_sysinfo); + hostname = g_sysinfo.nodename; + + if (getuid() == 0) prompt_end = '#'; + struct passwd* pw = getpwuid(getuid()); + if (pw) { username = pw->pw_name; } + } + while (1) { if (interactive) { auto cwd = TRY(os::FileSystem::working_directory()); - printf("sh %s%c ", cwd.chars(), getuid() == 0 ? '#' : '$'); + printf("%s@%s:%s%c ", username, hostname, cwd.chars(), prompt_end); } auto cmd = TRY(input_file->read_line());