sh: Display username instead of shell's PID in prompt

This commit is contained in:
apio 2022-10-28 21:57:07 +02:00
parent 0b838572e1
commit e9092ab235

View File

@ -1,6 +1,7 @@
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <luna.h> #include <luna.h>
#include <pwd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -8,6 +9,7 @@
#include <unistd.h> #include <unistd.h>
static int status = 0; static int status = 0;
static char* username = NULL;
typedef struct typedef struct
{ {
@ -76,9 +78,19 @@ void shell_execvp(char* pathname, char* const argv[])
void show_prompt() void show_prompt()
{ {
if (WEXITSTATUS(status)) { printf("%d [%ld]> ", WEXITSTATUS(status), getpid()); } if (!username)
{
struct passwd* user = getpwuid(getuid());
if (!user)
{
if (errno) perror("getpwuid");
username = "??";
}
else { username = user->pw_name; }
}
if (WEXITSTATUS(status)) { printf("%d [%s]> ", WEXITSTATUS(status), username); }
else else
printf("[%ld]> ", getpid()); printf("[%s]> ", username);
} }
int command_matches(command* cmd, const char* string) int command_matches(command* cmd, const char* string)