diff --git a/dtool/src/dtoolutil/executionEnvironment.cxx b/dtool/src/dtoolutil/executionEnvironment.cxx index b61a582a49..0c435a65e3 100644 --- a/dtool/src/dtoolutil/executionEnvironment.cxx +++ b/dtool/src/dtoolutil/executionEnvironment.cxx @@ -26,6 +26,13 @@ #include #endif +#ifdef __APPLE__ +// This is for _NSGetExecutablePath() and _NSGetEnviron(). +#include +#include +#define environ (*_NSGetEnviron()) +#endif + // We define the symbol PREREAD_ENVIRONMENT if we cannot rely on // getenv() to read environment variables at static init time. In // this case, we must read all of the environment variables directly @@ -373,6 +380,29 @@ read_environment_variables() { } ch = proc.get(); } +#elif defined(IS_OSX) + // In the case of Mac, there's always _NSGetEnviron() which we can read. + + char **envp; + for (envp = environ; envp && *envp; envp++) { + string variable; + string value; + + char *envc; + for (envc = *envp; envc && *envc && strncmp(envc, "=", 1) != 0; envc++) { + variable += (char) *envc; + } + + if (strncmp(envc, "=", 1) == 0) { + for (envc++; envc && *envc; envc++) { + value += (char) *envc; + } + } + + if (!variable.empty()) { + _variables[variable] = value; + } + } #else cerr << "Warning: environment variables unavailable to dconfig.\n"; #endif @@ -442,6 +472,18 @@ read_args() { } #endif +#if defined(__APPLE__) + // And on Mac, we have _NSGetExecutablePath. + if (_binary_name.empty()) { + char *pathbuf = new char[PATH_MAX]; + uint32_t *bufsize; + if (_NSGetExecutablePath(pathbuf, bufsize) == 0) { + _binary_name = pathbuf; + } + delete[] pathbuf; + } +#endif + #if defined(HAVE_GLOBAL_ARGV) int argc = GLOBAL_ARGC;