Make mingw_spawn* buildable by VS 2008
[git-cheetah/kirill.git] / exec.c
blobc0fa81944fdd551298e11804f9f28caa24fe880d
1 #include <windows.h>
2 #include <stdio.h>
3 #include <process.h>
4 #include "debug.h"
5 #include "systeminfo.h"
6 #include "exec.h"
8 #define MAX_PROCESSING_TIME (60 * 1000)
10 char *env_for_git()
12 static char *environment;
15 * if we can't find path to msys in the registry, return NULL and
16 * the CreateProcess will copy the environment for us
18 if (!environment && msys_path()) {
19 char *old = GetEnvironmentStrings();
20 size_t space = 0, path_index = -1, name_len = 0, len2;
22 while (old[space]) {
23 /* if it's PATH variable (could be Path= too!) */
24 if (!strnicmp(old + space, "PATH=", 5)) {
25 path_index = space;
26 name_len = 5;
29 while (old[space])
30 space++;
31 space++; /* skip var-terminating NULL */
34 if (path_index == -1)
35 path_index = space;
37 environment = malloc(space +
38 2 * strlen(msys_path()) + 32);
40 /* copy the block up to the equal sign of PATH var */
41 memcpy(environment, old, path_index);
42 /* insert new segments of the PATH */
43 len2 = sprintf(environment + path_index,
44 "PATH=%s\\bin;%s\\mingw\\bin%s",
45 msys_path(), msys_path(), name_len ? ";" : "");
46 /* append original value of PATH and variables after it */
47 memcpy(environment + path_index + len2,
48 old + path_index + name_len,
49 space + 1 - path_index - name_len);
51 FreeEnvironmentStrings(old);
54 return environment;
57 int exec_git(char *command, const char *wd, int mode)
59 STARTUPINFO si = { sizeof(si) };
60 PROCESS_INFORMATION pi;
61 DWORD status = -1;
62 char cmd[1024];
64 if (!msys_path()) {
65 debug_git("[ERROR] Could not find msysPath");
66 return -1;
69 sprintf(cmd, "\"%s\\bin\\git.exe\" %s", msys_path(), command);
71 si.dwFlags = STARTF_USESHOWWINDOW;
72 si.wShowWindow = SW_HIDE;
74 debug_git("Trying to spawn 'git %s' in working directory '%s'\n",
75 command, wd);
76 if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0,
77 env_for_git(), wd, &si, &pi)) {
78 debug_git("[ERROR] Could not create process (%d); "
79 "wd: %s; cmd: %s",
80 GetLastError(), wd, command);
82 return -1;
85 if (P_WAIT == mode) {
86 if (WAIT_OBJECT_0 == WaitForSingleObject(pi.hProcess,
87 MAX_PROCESSING_TIME)) {
89 if (!GetExitCodeProcess(pi.hProcess, &status))
90 debug_git("[ERROR] GetExitCode failed (%d); "
91 "wd: %s; cmd: %s",
92 GetLastError(), wd, command);
94 } else
95 debug_git("[ERROR] process timed out; "
96 "wd: %s; cmd: %s",
97 wd, command);
100 CloseHandle(pi.hProcess);
101 CloseHandle(pi.hThread);
103 return status;