aboutsummaryrefslogtreecommitdiffhomepage
path: root/env.cpp
diff options
context:
space:
mode:
authorGravatar ridiculousfish <corydoras@ridiculousfish.com>2014-07-25 10:42:22 -0700
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2014-07-25 10:49:13 -0700
commit30ae473d85be7528a360979a6cf30fd3aae5ddf1 (patch)
treed3c68127123bfc6a9fe1a40270f9a0c0785c454e /env.cpp
parente9f870e25af2fb8bf2dfbf5c92faaff667fd4cc1 (diff)
Clean up default environment variables.
This stops unconditionally setting values for HOME and USER, if we find those values in the environment. It also saves about 16KB on OS X, which getpwuid allocates.
Diffstat (limited to 'env.cpp')
-rw-r--r--env.cpp62
1 files changed, 23 insertions, 39 deletions
diff --git a/env.cpp b/env.cpp
index ec0fea30..8f43d888 100644
--- a/env.cpp
+++ b/env.cpp
@@ -415,39 +415,6 @@ wcstring env_get_pwd_slash(void)
return pwd;
}
-/**
- Set up default values for various variables if not defined.
- */
-static void env_set_defaults()
-{
-
- if (env_get_string(L"USER").missing())
- {
- struct passwd *pw = getpwuid(getuid());
- if (pw->pw_name != NULL)
- {
- const wcstring wide_name = str2wcstring(pw->pw_name);
- env_set(L"USER", wide_name.c_str(), ENV_GLOBAL);
- }
- }
-
- if (env_get_string(L"HOME").missing())
- {
- const env_var_t unam = env_get_string(L"USER");
- char *unam_narrow = wcs2str(unam.c_str());
- struct passwd *pw = getpwnam(unam_narrow);
- if (pw->pw_dir != NULL)
- {
- const wcstring dir = str2wcstring(pw->pw_dir);
- env_set(L"HOME", dir.c_str(), ENV_GLOBAL);
- }
- free(unam_narrow);
- }
-
- env_set_pwd();
-
-}
-
// Some variables should not be arrays. This used to be handled by a startup script, but we'd like to get down to 0 forks for startup, so handle it here.
static bool variable_can_be_array(const wcstring &key)
{
@@ -546,11 +513,14 @@ void env_init(const struct config_paths_t *paths /* or NULL */)
/*
Set up the USER variable
*/
- const struct passwd *pw = getpwuid(getuid());
- if (pw && pw->pw_name)
+ if (env_get_string(L"USER").missing_or_empty())
{
- const wcstring uname = str2wcstring(pw->pw_name);
- env_set(L"USER", uname.c_str(), ENV_GLOBAL | ENV_EXPORT);
+ const struct passwd *pw = getpwuid(getuid());
+ if (pw && pw->pw_name)
+ {
+ const wcstring uname = str2wcstring(pw->pw_name);
+ env_set(L"USER", uname.c_str(), ENV_GLOBAL | ENV_EXPORT);
+ }
}
/*
@@ -580,8 +550,22 @@ void env_init(const struct config_paths_t *paths /* or NULL */)
}
env_set(L"SHLVL", nshlvl_str.c_str(), ENV_GLOBAL | ENV_EXPORT);
- /* Set correct defaults for e.g. USER and HOME variables */
- env_set_defaults();
+ /* Set up the HOME variable */
+ if (env_get_string(L"HOME").missing_or_empty())
+ {
+ const env_var_t unam = env_get_string(L"USER");
+ char *unam_narrow = wcs2str(unam.c_str());
+ struct passwd *pw = getpwnam(unam_narrow);
+ if (pw->pw_dir != NULL)
+ {
+ const wcstring dir = str2wcstring(pw->pw_dir);
+ env_set(L"HOME", dir.c_str(), ENV_GLOBAL);
+ }
+ free(unam_narrow);
+ }
+
+ /* Set PWD */
+ env_set_pwd();
/* Set g_log_forks */
env_var_t log_forks = env_get_string(L"fish_log_forks");