changeset 9054:89de2b91a655 HEAD

Added env_backup_*() for saving/restoring environment.
author Timo Sirainen <tss@iki.fi>
date Sun, 17 May 2009 21:03:39 -0400
parents b52f165fccd8
children 249554df412f
files src/lib/env-util.c src/lib/env-util.h
diffstat 2 files changed, 47 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/env-util.c	Sun May 17 21:35:07 2009 -0400
+++ b/src/lib/env-util.c	Sun May 17 21:03:39 2009 -0400
@@ -5,6 +5,11 @@
 
 #include <stdlib.h>
 
+struct env_backup {
+	pool_t pool;
+	const char **strings;
+};
+
 static pool_t env_pool = NULL;
 
 void env_put(const char *env)
@@ -59,3 +64,38 @@
 	if (env_pool != NULL)
 		p_clear(env_pool);
 }
+
+struct env_backup *env_backup_save(void)
+{
+	struct env_backup *env;
+	extern char **environ;
+	unsigned int i, count;
+	pool_t pool;
+
+	for (count = 0; environ[count] != NULL; count++) ;
+
+	pool = pool_alloconly_create("saved environment", 4096);
+	env = p_new(pool, struct env_backup, 1);
+	env->pool = pool;
+	env->strings = p_new(pool, const char *, count + 1);
+	for (i = 0; i < count; i++)
+		env->strings[i] = p_strdup(pool, environ[i]);
+	return env;
+}
+
+void env_backup_restore(struct env_backup *env)
+{
+	unsigned int i;
+
+	env_clean();
+	for (i = 0; env->strings[i] != NULL; i++)
+		env_put(env->strings[i]);
+}
+
+void env_backup_free(struct env_backup **_env)
+{
+	struct env_backup *env = *_env;
+
+	*_env = NULL;
+	pool_unref(&env->pool);
+}
--- a/src/lib/env-util.h	Sun May 17 21:35:07 2009 -0400
+++ b/src/lib/env-util.h	Sun May 17 21:03:39 2009 -0400
@@ -9,4 +9,11 @@
 /* Clear all environment variables. */
 void env_clean(void);
 
+/* Save a copy of the current environment. */
+struct env_backup *env_backup_save(void);
+/* Clear the current environment and restore the backup. */
+void env_backup_restore(struct env_backup *env);
+/* Free the memory used by environment backup. */
+void env_backup_free(struct env_backup **env);
+
 #endif