basic/env-util: add little helper to call setenv or unsetenv
This commit is contained in:
parent
55c540d39f
commit
063f9f0da9
|
@ -747,3 +747,15 @@ int getenv_bool_secure(const char *p) {
|
||||||
|
|
||||||
return parse_boolean(e);
|
return parse_boolean(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int set_unset_env(const char *name, const char *value, bool overwrite) {
|
||||||
|
int r;
|
||||||
|
|
||||||
|
if (value)
|
||||||
|
r = setenv(name, value, overwrite);
|
||||||
|
else
|
||||||
|
r = unsetenv(name);
|
||||||
|
if (r < 0)
|
||||||
|
return -errno;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
|
@ -52,3 +52,6 @@ char *strv_env_get(char **x, const char *n) _pure_;
|
||||||
|
|
||||||
int getenv_bool(const char *p);
|
int getenv_bool(const char *p);
|
||||||
int getenv_bool_secure(const char *p);
|
int getenv_bool_secure(const char *p);
|
||||||
|
|
||||||
|
/* Like setenv, but calls unsetenv if value == NULL. */
|
||||||
|
int set_unset_env(const char *name, const char *value, bool overwrite);
|
||||||
|
|
|
@ -189,12 +189,9 @@ int pager_open(PagerFlags flags) {
|
||||||
|
|
||||||
/* We generally always set variables used by less, even if we end up using a different pager.
|
/* We generally always set variables used by less, even if we end up using a different pager.
|
||||||
* They shouldn't hurt in any case, and ideally other pagers would look at them too. */
|
* They shouldn't hurt in any case, and ideally other pagers would look at them too. */
|
||||||
if (use_secure_mode)
|
r = set_unset_env("LESSSECURE", use_secure_mode ? "1" : NULL, true);
|
||||||
r = setenv("LESSSECURE", "1", 1);
|
|
||||||
else
|
|
||||||
r = unsetenv("LESSSECURE");
|
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
log_error_errno(errno, "Failed to adjust environment variable LESSSECURE: %m");
|
log_error_errno(r, "Failed to adjust environment variable LESSSECURE: %m");
|
||||||
_exit(EXIT_FAILURE);
|
_exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -372,10 +372,7 @@ static void test_environment_gathering(void) {
|
||||||
assert_se(streq(strv_env_get(env, "PATH"), DEFAULT_PATH ":/no/such/file"));
|
assert_se(streq(strv_env_get(env, "PATH"), DEFAULT_PATH ":/no/such/file"));
|
||||||
|
|
||||||
/* reset environ PATH */
|
/* reset environ PATH */
|
||||||
if (old)
|
assert_se(set_unset_env("PATH", old, true) == 0);
|
||||||
(void) setenv("PATH", old, 1);
|
|
||||||
else
|
|
||||||
(void) unsetenv("PATH");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_error_catching(void) {
|
static void test_error_catching(void) {
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
#include "bus-locator.h"
|
#include "bus-locator.h"
|
||||||
#include "bus-map-properties.h"
|
#include "bus-map-properties.h"
|
||||||
#include "bus-print-properties.h"
|
#include "bus-print-properties.h"
|
||||||
|
#include "env-util.h"
|
||||||
#include "format-table.h"
|
#include "format-table.h"
|
||||||
#include "in-addr-util.h"
|
#include "in-addr-util.h"
|
||||||
#include "main-func.h"
|
#include "main-func.h"
|
||||||
|
@ -139,12 +140,9 @@ static int print_status_info(const StatusInfo *i) {
|
||||||
|
|
||||||
|
|
||||||
/* Restore the $TZ */
|
/* Restore the $TZ */
|
||||||
if (old_tz)
|
r = set_unset_env("TZ", old_tz, true);
|
||||||
r = setenv("TZ", old_tz, true);
|
|
||||||
else
|
|
||||||
r = unsetenv("TZ");
|
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
log_warning_errno(errno, "Failed to set TZ environment variable, ignoring: %m");
|
log_warning_errno(r, "Failed to set TZ environment variable, ignoring: %m");
|
||||||
else
|
else
|
||||||
tzset();
|
tzset();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue