sh: Build a more elaborate prompt using the system hostname and username
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
403b0f6b94
commit
919c9dd3cf
21
apps/sh.cpp
21
apps/sh.cpp
@ -6,10 +6,12 @@
|
||||
#include <os/Process.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <pwd.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@ -28,6 +30,12 @@ static Result<void> 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<int> luna_main(int argc, char** argv)
|
||||
{
|
||||
StringView path;
|
||||
@ -55,12 +63,23 @@ Result<int> 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());
|
||||
|
Loading…
Reference in New Issue
Block a user