From: Timo Sirainen Date: Tue, 9 Nov 2010 21:42:58 +0000 (+0000) Subject: Added env_get_environ_p() as more portable way of accessing environ variable. X-Git-Tag: 2.0.8~99 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a857fb61f1cc77a81d18adee6a95ae04c27a5ffb;p=thirdparty%2Fdovecot%2Fcore.git Added env_get_environ_p() as more portable way of accessing environ variable. Implemented with OS X using _NSGetEnviron(). --- diff --git a/src/lib-settings/settings-parser.c b/src/lib-settings/settings-parser.c index 55f0359857..bfdd57986d 100644 --- a/src/lib-settings/settings-parser.c +++ b/src/lib-settings/settings-parser.c @@ -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 @@ static int environ_cmp(char *const *s1, char *const *s2) 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 --git a/src/lib/env-util.c b/src/lib/env-util.c index 467487138d..12836b135c 100644 --- a/src/lib/env-util.c +++ b/src/lib/env-util.c @@ -5,6 +5,9 @@ #include "env-util.h" #include +#ifdef __APPLE__ +# include +#endif struct env_backup { pool_t pool; @@ -106,8 +109,8 @@ void env_clean_except(const char *const preserve_envs[]) 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 @@ void env_backup_free(struct env_backup **_env) 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 --git a/src/lib/env-util.h b/src/lib/env-util.h index def2fe91ed..f793da8bd4 100644 --- a/src/lib/env-util.h +++ b/src/lib/env-util.h @@ -18,6 +18,10 @@ void env_backup_restore(struct env_backup *env); /* 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 --git a/src/lib/process-title.c b/src/lib/process-title.c index e7bfa1390e..5ccf34bfb5 100644 --- a/src/lib/process-title.c +++ b/src/lib/process-title.c @@ -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 /* NetBSD, OpenBSD */ @@ -115,12 +116,12 @@ static void proctitle_hack_set(const char *title) 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];