Let's try and rely less on procfs

This commit is contained in:
rdb 2010-11-06 14:22:50 +00:00
parent 2e2e0d7de4
commit 760839789e

View File

@ -36,6 +36,11 @@
#define environ (*_NSGetEnviron()) #define environ (*_NSGetEnviron())
#endif #endif
#ifdef IS_LINUX
// extern char **environ is defined here:
#include <unistd.h>
#endif
#ifdef IS_FREEBSD #ifdef IS_FREEBSD
extern char **environ; extern char **environ;
@ -387,11 +392,33 @@ get_ptr() {
void ExecutionEnvironment:: void ExecutionEnvironment::
read_environment_variables() { read_environment_variables() {
#ifdef PREREAD_ENVIRONMENT #ifdef PREREAD_ENVIRONMENT
#if defined(HAVE_PROC_SELF_ENVIRON) #if defined(IS_OSX) || defined(IS_FREEBSD) || defined(IS_LINUX)
// In Linux, and possibly in other systems, we might not be able to // In the case of Mac, we'll try reading _NSGetEnviron().
// use getenv() at static init time. However, we may be lucky and // In the case of FreeBSD and Linux, use the "environ" variable.
// have a file called /proc/self/environ that may be read to
// determine all of our environment variables. 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;
}
}
#elif defined(HAVE_PROC_SELF_ENVIRON)
// In some cases, we may have a file called /proc/self/environ
// that may be read to determine all of our environment variables.
pifstream proc("/proc/self/environ"); pifstream proc("/proc/self/environ");
if (proc.fail()) { if (proc.fail()) {
@ -422,30 +449,6 @@ read_environment_variables() {
} }
ch = proc.get(); ch = proc.get();
} }
#elif defined(IS_OSX) || defined(IS_FREEBSD)
// In the case of Mac, there's always _NSGetEnviron() which we can read.
// In the case of FreeBSD, it's the "environ" variable.
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 #else
cerr << "Warning: environment variables unavailable to dconfig.\n"; cerr << "Warning: environment variables unavailable to dconfig.\n";
#endif #endif