sh: Display username instead of shell's PID in prompt
This commit is contained in:
parent
0b838572e1
commit
e9092ab235
@ -1,6 +1,7 @@
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <luna.h>
|
||||
#include <pwd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -8,6 +9,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
static int status = 0;
|
||||
static char* username = NULL;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
@ -76,9 +78,19 @@ void shell_execvp(char* pathname, char* const argv[])
|
||||
|
||||
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
|
||||
printf("[%ld]> ", getpid());
|
||||
printf("[%s]> ", username);
|
||||
}
|
||||
|
||||
int command_matches(command* cmd, const char* string)
|
||||
|
Loading…
x
Reference in New Issue
Block a user