From 5aaddefb351e87ea1e97db92924a75ff8aa50181 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Mon, 25 Oct 2010 17:31:01 +0100 Subject: [PATCH] Added env_clean_except() --- src/lib/env-util.c | 34 ++++++++++++++++++++++++++++++++++ src/lib/env-util.h | 2 ++ 2 files changed, 36 insertions(+) diff --git a/src/lib/env-util.c b/src/lib/env-util.c index 74f6d5a4c7..467487138d 100644 --- a/src/lib/env-util.c +++ b/src/lib/env-util.c @@ -1,6 +1,7 @@ /* Copyright (c) 2002-2010 Dovecot authors, see the included COPYING file */ #include "lib.h" +#include "array.h" #include "env-util.h" #include @@ -70,6 +71,39 @@ void env_clean(void) p_clear(env_pool); } +static void env_clean_except_real(const char *const preserve_envs[]) +{ + ARRAY_TYPE(const_string) copy; + const char *value, *const *envp; + unsigned int i; + + t_array_init(©, 16); + for (i = 0; preserve_envs[i] != NULL; i++) { + const char *key = preserve_envs[i]; + + value = getenv(key); + if (value != NULL) { + value = t_strconcat(key, "=", value, NULL); + array_append(©, &value, 1); + } + } + + /* Note that if the original environment was set with env_put(), the + environment strings will be invalid after env_clean(). That's why + we t_strconcat() them above. */ + env_clean(); + + array_foreach(©, envp) + env_put(*envp); +} + +void env_clean_except(const char *const preserve_envs[]) +{ + T_BEGIN { + env_clean_except_real(preserve_envs); + } T_END; +} + struct env_backup *env_backup_save(void) { struct env_backup *env; diff --git a/src/lib/env-util.h b/src/lib/env-util.h index 44107f8ed5..def2fe91ed 100644 --- a/src/lib/env-util.h +++ b/src/lib/env-util.h @@ -8,6 +8,8 @@ void env_put(const char *env); void env_remove(const char *name); /* Clear all environment variables. */ void env_clean(void); +/* Clear all environment variables except what's listed in preserve_envs[] */ +void env_clean_except(const char *const preserve_envs[]); /* Save a copy of the current environment. */ struct env_backup *env_backup_save(void); -- 2.47.3