dovecot-2.0: Added env_clean_except()

dovecot at dovecot.org dovecot at dovecot.org
Mon Oct 25 19:34:15 EEST 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/fe72469ccdf5
changeset: 12347:fe72469ccdf5
user:      Timo Sirainen <tss at iki.fi>
date:      Mon Oct 25 17:31:01 2010 +0100
description:
Added env_clean_except()

diffstat:

 src/lib/env-util.c |  34 ++++++++++++++++++++++++++++++++++
 src/lib/env-util.h |   2 ++
 2 files changed, 36 insertions(+), 0 deletions(-)

diffs (63 lines):

diff -r 7cf8e3094b39 -r fe72469ccdf5 src/lib/env-util.c
--- a/src/lib/env-util.c	Mon Oct 25 17:22:40 2010 +0100
+++ b/src/lib/env-util.c	Mon Oct 25 17:31:01 2010 +0100
@@ -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 @@
 		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;
diff -r 7cf8e3094b39 -r fe72469ccdf5 src/lib/env-util.h
--- a/src/lib/env-util.h	Mon Oct 25 17:22:40 2010 +0100
+++ b/src/lib/env-util.h	Mon Oct 25 17:31:01 2010 +0100
@@ -8,6 +8,8 @@
 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);


More information about the dovecot-cvs mailing list