dovecot-2.0: Added env_get_environ_p() as more portable way of a...

dovecot at dovecot.org dovecot at dovecot.org
Tue Nov 9 23:43:02 EET 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/e60061623012
changeset: 12412:e60061623012
user:      Timo Sirainen <tss at iki.fi>
date:      Tue Nov 09 21:42:58 2010 +0000
description:
Added env_get_environ_p() as more portable way of accessing environ variable.
Implemented with OS X using _NSGetEnviron().

diffstat:

 src/lib-settings/settings-parser.c |   3 ++-
 src/lib/env-util.c                 |  16 +++++++++++++++-
 src/lib/env-util.h                 |   4 ++++
 src/lib/process-title.c            |   7 ++++---
 4 files changed, 25 insertions(+), 5 deletions(-)

diffs (102 lines):

diff -r f89b4de6688d -r e60061623012 src/lib-settings/settings-parser.c
--- a/src/lib-settings/settings-parser.c	Tue Nov 09 21:17:15 2010 +0000
+++ b/src/lib-settings/settings-parser.c	Tue Nov 09 21:42:58 2010 +0000
@@ -5,6 +5,7 @@
 #include "hash.h"
 #include "network.h"
 #include "istream.h"
+#include "env-util.h"
 #include "execv-const.h"
 #include "str.h"
 #include "strescape.h"
@@ -942,7 +943,7 @@
 
 int settings_parse_environ(struct setting_parser_context *ctx)
 {
-	extern char **environ;
+	char **environ = *env_get_environ_p();
 	ARRAY_TYPE(string) sorted_envs_arr;
 	const char *key, *value;
 	char *const *sorted_envs;
diff -r f89b4de6688d -r e60061623012 src/lib/env-util.c
--- a/src/lib/env-util.c	Tue Nov 09 21:17:15 2010 +0000
+++ b/src/lib/env-util.c	Tue Nov 09 21:42:58 2010 +0000
@@ -5,6 +5,9 @@
 #include "env-util.h"
 
 #include <stdlib.h>
+#ifdef __APPLE__
+#  include <crt_externs.h>
+#endif
 
 struct env_backup {
 	pool_t pool;
@@ -106,8 +109,8 @@
 
 struct env_backup *env_backup_save(void)
 {
+	char **environ = *env_get_environ_p();
 	struct env_backup *env;
-	extern char **environ;
 	unsigned int i, count;
 	pool_t pool;
 
@@ -141,6 +144,17 @@
 	pool_unref(&env->pool);
 }
 
+char ***env_get_environ_p(void)
+{
+#ifdef __APPLE__
+	return _NSGetEnviron();
+#else
+	extern char **environ;
+
+	return &environ;
+#endif
+}
+
 void env_deinit(void)
 {
 	if (env_pool != NULL)
diff -r f89b4de6688d -r e60061623012 src/lib/env-util.h
--- a/src/lib/env-util.h	Tue Nov 09 21:17:15 2010 +0000
+++ b/src/lib/env-util.h	Tue Nov 09 21:42:58 2010 +0000
@@ -18,6 +18,10 @@
 /* Free the memory used by environment backup. */
 void env_backup_free(struct env_backup **env);
 
+/* Returns the value of "&environ". This is more portable than using it
+   directly. */
+char ***env_get_environ_p(void);
+
 /* Free all memory used by env_put() function. Environment must not be
    accessed afterwards. */
 void env_deinit(void);
diff -r f89b4de6688d -r e60061623012 src/lib/process-title.c
--- a/src/lib/process-title.c	Tue Nov 09 21:17:15 2010 +0000
+++ b/src/lib/process-title.c	Tue Nov 09 21:42:58 2010 +0000
@@ -1,6 +1,7 @@
 /* Copyright (c) 2002-2010 Dovecot authors, see the included COPYING file */
 
 #include "lib.h"
+#include "env-util.h"
 #include "process-title.h"
 
 #include <stdlib.h> /* NetBSD, OpenBSD */
@@ -115,12 +116,12 @@
 void process_title_init(char **argv[])
 {
 #ifdef PROCTITLE_HACK
-	extern char **environ;
+	char ***environ_p = env_get_environ_p();
 	char **orig_argv = *argv;
-	char **orig_environ = environ;
+	char **orig_environ = *environ_p;
 
 	*argv = argv_dup(orig_argv, &argv_memblock);
-	environ = argv_dup(orig_environ, &environ_memblock);
+	*environ_p = argv_dup(orig_environ, &environ_memblock);
 	proctitle_hack_init(orig_argv, orig_environ);
 #endif
 	process_name = (*argv)[0];


More information about the dovecot-cvs mailing list