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