dovecot-1.2: Added env_backup_*() for saving/restoring environment.

dovecot at dovecot.org dovecot at dovecot.org
Mon May 18 04:36:24 EEST 2009


details:   http://hg.dovecot.org/dovecot-1.2/rev/89de2b91a655
changeset: 9054:89de2b91a655
user:      Timo Sirainen <tss at iki.fi>
date:      Sun May 17 21:03:39 2009 -0400
description:
Added env_backup_*() for saving/restoring environment.

diffstat:

2 files changed, 47 insertions(+)
src/lib/env-util.c |   40 ++++++++++++++++++++++++++++++++++++++++
src/lib/env-util.h |    7 +++++++

diffs (69 lines):

diff -r b52f165fccd8 -r 89de2b91a655 src/lib/env-util.c
--- 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
@@ -4,6 +4,11 @@
 #include "env-util.h"
 
 #include <stdlib.h>
+
+struct env_backup {
+	pool_t pool;
+	const char **strings;
+};
 
 static pool_t env_pool = NULL;
 
@@ -59,3 +64,38 @@ void env_clean(void)
 	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);
+}
diff -r b52f165fccd8 -r 89de2b91a655 src/lib/env-util.h
--- 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 @@ void env_remove(const char *name);
 /* 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


More information about the dovecot-cvs mailing list