Page MenuHomeFreeBSD

Make sh(1) support \u in PS1.
Needs ReviewPublic

Authored by trasz on Wed, Jan 9, 12:47 PM.


Group Reviewers

Make sh(1) support \u in PS1. This removes one fork/exec on interactive
shell startup.

Diff Detail

rS FreeBSD src repository
Lint OK
No Unit Test Coverage
Build Status
Buildable 22061
Build 21289: arc lint + arc unit

Event Timeline

trasz created this revision.Wed, Jan 9, 12:47 PM
0mp accepted this revision.Wed, Jan 9, 12:56 PM
0mp added a subscriber: 0mp.

OK from manpages.


Remember to bump the date!

This revision is now accepted and ready to land.Wed, Jan 9, 12:56 PM
trasz updated this revision to Diff 52689.Wed, Jan 9, 12:59 PM

Rework a bit.

This revision now requires review to proceed.Wed, Jan 9, 12:59 PM
trasz added inline comments.Wed, Jan 9, 12:59 PM

I do remember; it's just that I never know when something will get committed.

imp added inline comments.Wed, Jan 9, 4:01 PM

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?

imp added inline comments.Wed, Jan 9, 4:53 PM

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.

trasz added inline comments.Wed, Jan 9, 5:33 PM

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?

trasz updated this revision to Diff 52704.Wed, Jan 9, 5:39 PM

Check EUID, not UID; don't overuse snprintf(3).

trasz added a comment.Wed, Jan 9, 5:57 PM

One more data point: csh(1) does the same, ie it uses getuid/getpwuid, not getlogin.

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.

trasz updated this revision to Diff 53044.Sun, Jan 20, 11:30 PM

Use algorithm suggested by jilles@.