Shell: '\u' in PS1 is replaced with username corresponding to euid

This commit is contained in:
Bananymous 2023-08-14 14:55:23 +03:00
parent 81d79cca61
commit db2eca697e
1 changed files with 11 additions and 1 deletions

View File

@ -5,6 +5,7 @@
#include <ctype.h> #include <ctype.h>
#include <inttypes.h> #include <inttypes.h>
#include <pwd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -469,6 +470,15 @@ BAN::String get_prompt()
break; break;
} }
case 'u':
{
auto* passwd = getpwuid(geteuid());
if (passwd == nullptr)
break;
MUST(prompt.append(passwd->pw_name));
endpwent();
break;
}
case '\0': case '\0':
MUST(prompt.push_back('\\')); MUST(prompt.push_back('\\'));
break; break;
@ -529,7 +539,7 @@ int main(int argc, char** argv)
if (argc >= 1) if (argc >= 1)
setenv("SHELL", argv[0], true); setenv("SHELL", argv[0], true);
setenv("PS1", "\e[32muser@host\e[m:\e[34m\\~\e[m$ ", false); setenv("PS1", "\e[32m\\u@host\e[m:\e[34m\\~\e[m$ ", false);
tcgetattr(0, &old_termios); tcgetattr(0, &old_termios);