]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Added env_clean_except()
authorTimo Sirainen <tss@iki.fi>
Mon, 25 Oct 2010 16:31:01 +0000 (17:31 +0100)
committerTimo Sirainen <tss@iki.fi>
Mon, 25 Oct 2010 16:31:01 +0000 (17:31 +0100)
src/lib/env-util.c
src/lib/env-util.h

index 74f6d5a4c7c64da5854a86299b45e317771758de..467487138d3507493cd7baa8e0005af33e36073f 100644 (file)
@@ -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 <stdlib.h>
@@ -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(&copy, 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(&copy, &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(&copy, 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;
index 44107f8ed5bab32af05a260aed5f27803e527634..def2fe91ed83b915a4161ee4a1ac19636bea0514 100644 (file)
@@ -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);