summaryrefslogtreecommitdiff
path: root/rts/win32/GetEnv.c
diff options
context:
space:
mode:
authorDuncan Coutts <duncan@well-typed.com>2011-05-26 18:44:41 +0100
committerDuncan Coutts <duncan@well-typed.com>2011-05-26 18:47:38 +0100
commite8832eb9f05ca46d9315250c3baf7010eb0630a4 (patch)
treebe08e88284b463362f14673175aedca35bcfcb8c /rts/win32/GetEnv.c
parent43c7d555c8d7eea6ba0d76bce33be8d25a01c6fd (diff)
downloadhaskell-e8832eb9f05ca46d9315250c3baf7010eb0630a4.tar.gz
Emit various bits of OS process info into the eventlog
The process ID, parent process ID, rts name and version The program arguments and environment.
Diffstat (limited to 'rts/win32/GetEnv.c')
-rw-r--r--rts/win32/GetEnv.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/rts/win32/GetEnv.c b/rts/win32/GetEnv.c
new file mode 100644
index 0000000000..b8a43951a9
--- /dev/null
+++ b/rts/win32/GetEnv.c
@@ -0,0 +1,61 @@
+/* -----------------------------------------------------------------------------
+ *
+ * (c) The GHC Team 2011
+ *
+ * Access to the process environment variables
+ *
+ * ---------------------------------------------------------------------------*/
+
+#include "Rts.h"
+#include "GetEnv.h"
+
+#include <windows.h>
+
+/* Windows does it differently, though arguably the most sanely.
+ * GetEnvironmentStrings() returns a pointer to a block of
+ * environment vars with a double null terminator:
+ * Var1=Value1\0
+ * Var2=Value2\0
+ * ...
+ * VarN=ValueN\0\0
+ * But because everyone else (ie POSIX) uses a vector of strings, we convert
+ * to that format. Fortunately this is just a matter of making an array of
+ * offsets into the environment block.
+ *
+ * Note that we have to call FreeEnvironmentStrings() at the end.
+ *
+ */
+void getProgEnvv(int *out_envc, char **out_envv[]) {
+ int envc, i;
+ char *env;
+ char *envp;
+ char **envv;
+
+ /* For now, use the 'A'nsi not 'W'ide variant.
+ Note: corresponding Free below must use the same 'A'/'W' variant. */
+ env = GetEnvironmentStringsA();
+
+ envc = 0;
+ for (envp = env; *envp != 0; envp += strlen(envp) + 1) {
+ envc++;
+ }
+
+ envv = stgMallocBytes(sizeof(char*) * (envc+1));
+
+ i = 0;
+ for (envp = env; *envp != NULL; envp += strlen(envp) + 1) {
+ envv[i] = envp;
+ i++;
+ }
+ /* stash whole env in last+1 entry */
+ envv[envc] = env;
+
+ *out_envc = envc;
+ *out_envv = envv;
+}
+
+void freeProgEnvv(int envc, char *envv[]) {
+ /* we stashed the win32 env block in the last+1 entry */
+ FreeEnvironmentStringsA(envv[envc]);
+ stgFree(envv);
+}