Fix fish startup behavior in presence of unset $USER

As reported in fish-shell/fish-shell#5180, when the USER environment
variable is not set and fish is started, `get_runtime_path()` returns a
blank string. At some point in the past, this was called after
`setup_user()` in env.cpp, but this is no longer the case.

This commit removes the reliance on the $USER environment variable
entirely, and instead uses `getpwuid(geteuid()).pw_name` to retrieve the
current username.

Closes #5180.
This commit is contained in:
Mahmoud Al-Qudsi 2018-09-18 21:03:04 -05:00
parent 0e82fcd999
commit dc250e0c29

View File

@ -140,12 +140,9 @@ static wcstring get_runtime_path() {
if (dir != NULL && access(dir, mode) == 0 && check_runtime_path(dir) == 0) {
result = str2wcstring(dir);
} else {
const char *uname = getenv("USER");
// $USER should already have been set (in setup_path()).
// If it still isn't, there's something wrong.
if (!uname) {
return result;
}
// Don't rely on $USER being set, as setup_user() has not yet been called.
// See https://github.com/fish-shell/fish-shell/issues/5180
const char *uname = getpwuid(geteuid()).pw_name;
// /tmp/fish.user
std::string tmpdir = "/tmp/fish.";
tmpdir.append(uname);