Do not modify "our" environment, because it's Explorer's one
[git-cheetah/kirill.git] / exec.c
blob2da2c08f3f715bfc5559b9c17b6533ee6bfc7bfd
1 #include <windows.h>
2 #include <stdio.h>
3 #include "debug.h"
4 #include "systeminfo.h"
5 #include "exec.h"
7 char *env_for_git()
9 static char *environment;
12 * if we can't find path to msys in the registry, return NULL and
13 * the CreateProcess will copy the environment for us
15 if (!environment && msys_path()) {
16 char *old = GetEnvironmentStrings();
17 size_t space = 0, path_index = -1, name_len = 0, len2;
19 while (old[space]) {
20 /* if it's PATH variable (could be Path= too!) */
21 if (!strnicmp(old + space, "PATH=", 5)) {
22 path_index = space;
23 name_len = 5;
26 while (old[space])
27 space++;
28 space++; /* skip var-terminating NULL */
31 if (path_index == -1)
32 path_index = space;
34 environment = malloc(space +
35 2 * strlen(msys_path()) + 32);
37 /* copy the block up to the equal sign of PATH var */
38 memcpy(environment, old, path_index);
39 /* insert new segments of the PATH */
40 len2 = sprintf(environment + path_index,
41 "PATH=%s\\bin;%s\\mingw\\bin%s",
42 msys_path(), msys_path(), name_len ? ";" : "");
43 /* append original value of PATH and variables after it */
44 memcpy(environment + path_index + len2,
45 old + path_index + name_len,
46 space + 1 - path_index - name_len);
48 FreeEnvironmentStrings(old);
51 return environment;
54 void exec_gui(char *command, const char *wd)
56 STARTUPINFO si = { sizeof(si) };
57 PROCESS_INFORMATION pi;
59 si.dwFlags = STARTF_USESHOWWINDOW;
60 si.wShowWindow = SW_HIDE;
62 debug_git("Trying to spawn '%s' in working directory '%s'\n",
63 command, wd);
64 if (CreateProcess(NULL, command, NULL, NULL, FALSE, 0,
65 env_for_git(), wd, &si, &pi))
67 CloseHandle(pi.hProcess);
68 CloseHandle(pi.hThread);
70 else
71 debug_git("[ERROR] Could not create process (%d)"
72 "wd: %s; cmd: %s",
73 GetLastError(), wd, command);