Page MenuHomeFreeBSD

Make sh(1) support \u in PS1.
ClosedPublic

Authored by trasz on Jan 9 2019, 12:47 PM.
Tags
None
Referenced Files
Unknown Object (File)
Feb 23 2024, 4:43 PM
Unknown Object (File)
Jan 8 2024, 6:19 PM
Unknown Object (File)
Dec 28 2023, 7:44 PM
Unknown Object (File)
Dec 20 2023, 4:36 AM
Unknown Object (File)
Dec 4 2023, 12:05 AM
Unknown Object (File)
Dec 4 2023, 12:05 AM
Unknown Object (File)
Dec 4 2023, 12:05 AM
Unknown Object (File)
Dec 4 2023, 12:05 AM
Subscribers

Details

Summary

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

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

0mp added a subscriber: 0mp.

OK from manpages.

bin/sh/sh.1
1406 ↗(On Diff #52688)

Remember to bump the date!

This revision is now accepted and ready to land.Jan 9 2019, 12:56 PM
This revision now requires review to proceed.Jan 9 2019, 12:59 PM
bin/sh/sh.1
1406 ↗(On Diff #52688)

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

bin/sh/parser.c
1983 ↗(On Diff #52689)

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?

2059 ↗(On Diff #52689)

Can you write a regression test for this too?

bin/sh/parser.c
1983 ↗(On Diff #52689)

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.

bin/sh/parser.c
1983 ↗(On Diff #52689)

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.

2059 ↗(On Diff #52689)

Hm, we don't seem to have any tests for the prompt, do we?

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

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.

bin/sh/parser.c
1983 ↗(On Diff #52689)

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.

Use algorithm suggested by jilles@.

This revision is now accepted and ready to land.Jan 23 2019, 10:41 PM
This revision was automatically updated to reflect the committed changes.