sh: Build a more elaborate prompt using the system hostname and username
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-04-24 22:13:06 +02:00
parent 403b0f6b94
commit 919c9dd3cf
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -6,10 +6,12 @@
#include <os/Process.h> #include <os/Process.h>
#include <errno.h> #include <errno.h>
#include <pwd.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/utsname.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
@ -28,6 +30,12 @@ static Result<void> execute_command(StringView command)
return os::Process::exec(args[0].view(), args.slice()); return os::Process::exec(args[0].view(), args.slice());
} }
struct utsname g_sysinfo;
const char* hostname = "";
const char* username = "";
char prompt_end = '$';
Result<int> luna_main(int argc, char** argv) Result<int> luna_main(int argc, char** argv)
{ {
StringView path; StringView path;
@ -55,12 +63,23 @@ Result<int> luna_main(int argc, char** argv)
input_file->set_close_on_exec(); 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) while (1)
{ {
if (interactive) if (interactive)
{ {
auto cwd = TRY(os::FileSystem::working_directory()); 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()); auto cmd = TRY(input_file->read_line());