1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 2011
5 * Access to the process environment variables
7 * ---------------------------------------------------------------------------*/
15 /* Windows does it differently, though arguably the most sanely.
16 * GetEnvironmentStrings() returns a pointer to a block of
17 * environment vars with a double null terminator:
22 * But because everyone else (ie POSIX) uses a vector of strings, we convert
23 * to that format. Fortunately this is just a matter of making an array of
24 * offsets into the environment block.
26 * Note that we have to call FreeEnvironmentStrings() at the end.
29 void getProgEnvv(int *out_envc, char **out_envv[]) {
35 /* For now, use the 'A'nsi not 'W'ide variant.
36 Note: corresponding Free below must use the same 'A'/'W' variant. */
37 env = GetEnvironmentStringsA();
40 for (envp = env; *envp != 0; envp += strlen(envp) + 1) {
44 envv = stgMallocBytes(sizeof(char*) * (envc+1), "getProgEnvv");
47 for (envp = env; *envp != 0; envp += strlen(envp) + 1) {
51 /* stash whole env in last+1 entry */
58 void freeProgEnvv(int envc, char *envv[]) {
59 /* we stashed the win32 env block in the last+1 entry */
60 FreeEnvironmentStringsA(envv[envc]);