Make sh(1) support \u in PS1. This removes one fork/exec on interactive
We have the LOGNAME variable that's set (from login(8)):
The login utility enters information into the environment (see environ(7)) specifying the user's home directory (HOME), command interpreter (SHELL), search path (PATH), terminal type (TERM) and user name (both LOGNAME and USER).
so maybe use that instead and fall back to this as a backup?
Can you write a regression test for this too?
I used to run a system where we had 4 different users map to the same UID, but have different home directories, so we found (and often fixed) programs that didn't grok LOGNAME properly... So it can and does happen.
There are two problems with LOGNAME - first, the user (or script) can change it, and it seems kind of wrong to display that instead of the actual login name. Second - LOGNAME seems to be unaffected by su(1), so after you su from your normal user to root, it will still return the name of the normal user and not 'root'. The latter problem also seems to apply to getlogin(2).
So, both ways seem somewhat suboptimal, in different ways. As it is now, sh(1) just calls whoami(1), and that does pretty much the same as the code added here.
Hm, we don't seem to have any tests for the prompt, do we?
My original objection to \u is no longer valid since we have accepted that the ugly \ bash prompt sequences are staying.
More "proper" way would be to getpwnam based on LOGNAME (or getlogin(2)?) and use that if the passwd's UID matches geteuid(); otherwise, use getpwuid as now.