changeset 12347:fe72469ccdf5

Added env_clean_except()
author Timo Sirainen <tss@iki.fi>
date Mon, 25 Oct 2010 17:31:01 +0100
parents 7cf8e3094b39
children e0a97842182f
files src/lib/env-util.c src/lib/env-util.h
diffstat 2 files changed, 36 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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;
--- 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);