From e9092ab2355222f58b5f936bca1f52aa25c4dfb8 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 28 Oct 2022 21:57:07 +0200 Subject: [PATCH] sh: Display username instead of shell's PID in prompt --- apps/src/sh.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/apps/src/sh.c b/apps/src/sh.c index e212b29c..18f5198c 100644 --- a/apps/src/sh.c +++ b/apps/src/sh.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -8,6 +9,7 @@ #include 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)