fix syntax error for macro substituting a function call
[git-cheetah/kirill.git] / explorer / systeminfo.c
blob9f03df2c2e13457cc582416c57c51d1724812365
1 #include "../common/git-compat-util.h"
2 #include "../common/strbuf.h"
3 #include "../common/systeminfo.h"
4 #include "../common/debug.h"
5 #include <windows.h>
6 #include "dll.h"
8 static struct strbuf gitPath = STRBUF_INIT;
9 static TCHAR msysPath[MAX_PATH];
11 static int get_msys_path_from_registry(HKEY root)
13 HKEY key;
14 DWORD path_len = sizeof(msysPath);
16 LONG result = RegOpenKeyEx(root, GIT_CHEETAH_REG_PATH,
17 0, KEY_QUERY_VALUE, &key);
18 if (ERROR_SUCCESS != result)
19 return 0;
21 result = RegQueryValueEx(key,
22 GIT_CHEETAH_REG_PATHTOMSYS,
23 NULL, NULL, (LPBYTE)msysPath, &path_len);
25 RegCloseKey(key);
27 return ERROR_SUCCESS == result;
30 static TCHAR *msys_path(void)
32 static int found_path = 0;
34 /* try to find user-specific settings first */
35 if (!found_path)
36 found_path = get_msys_path_from_registry(HKEY_CURRENT_USER);
38 /* if not found in user settings, try machine-wide */
39 if (!found_path)
40 found_path = get_msys_path_from_registry(HKEY_LOCAL_MACHINE);
42 if (found_path)
43 return msysPath;
45 return NULL;
48 static inline int test_msys_path(const char *postfix, char *path)
50 int n;
51 char stat_path[MAX_PATH];
52 struct stat stat_info;
54 n = snprintf(stat_path, MAX_PATH, "%s\\%s\\git",
55 msys_path(), postfix);
56 if (n >= MAX_PATH)
57 goto test_git_path_failed;
59 n = snprintf(path, MAX_PATH, "%s\\%s", msys_path(), postfix);
60 if (n >= MAX_PATH)
61 goto test_git_path_failed;
63 if (lstat(path, &stat_info) == 0) {
64 return 1;
67 test_git_path_failed:
68 *path = '\0';
69 return 0;
72 const char *git_path()
74 char path[MAX_PATH];
76 if (!msys_path())
77 return NULL;
79 if (gitPath.len)
80 return gitPath.buf;
82 if (test_msys_path("bin", path))
83 strbuf_addstr(&gitPath, path);
85 if (test_msys_path("mingw\\bin", path)) {
86 if (gitPath.len)
87 strbuf_addch(&gitPath, ';');
88 strbuf_addstr(&gitPath, path);
91 return gitPath.len ? gitPath.buf : NULL;
94 void message_box(const char *string)
96 MessageBox(0, string, "Hello", MB_OK|MB_ICONEXCLAMATION);
99 int is_directory(const char *path)
101 return (FILE_ATTRIBUTE_DIRECTORY & GetFileAttributes(path));
105 * builds an array of environment variables,
106 * as expected by mingw_spawnvpe
108 static char **env_for_git()
110 static char **environment;
113 * if we can't find path to msys in the registry, return NULL and
114 * the CreateProcess will copy the environment for us
116 if (!environment && git_path()) {
117 char *old = GetEnvironmentStrings();
118 size_t space = 0, path_index = -1, name_len = 0;
119 int total = 0, i;
121 while (old[space]) {
122 /* if it's PATH variable (could be Path= too!) */
123 if (!strnicmp(old + space, "PATH=", 5)) {
124 path_index = space;
125 name_len = 5;
128 while (old[space])
129 space++;
130 space++; /* skip var-terminating NULL */
132 total++;
135 if (path_index == -1)
136 path_index = space;
138 environment = malloc(sizeof(*environment) * (total + 1));
139 space = 0;
140 for (i = 0; i < total; i++) {
141 if (path_index == space) {
142 char *path = old + space + name_len;
143 size_t len;
144 environment[i] = malloc(strlen(path) + 1 +
145 2 * strlen(git_path()) + 32);
146 len = sprintf(environment[i],
147 "PATH=%s%s%s", git_path(),
148 *path ? ";" : "", path);
149 } else
150 environment[i] = strdup(old + space);
152 while (old[space])
153 space++;
154 space++; /* skip var-terminating NULL */
157 /* mark the end of the array */
158 environment[i] = 0;
160 FreeEnvironmentStrings(old);
163 return environment;
166 pid_t fork_process(const char *cmd, const char **args, const char *wd)
168 return mingw_spawnvpe_cwd(cmd, args, env_for_git(), wd);
171 int wait_for_process(pid_t pid, int max_time, int *errcode)
173 DWORD status;
174 *errcode = 0;
176 if (WAIT_OBJECT_0 == WaitForSingleObject((HANDLE)pid,
177 max_time)) {
178 if (GetExitCodeProcess((HANDLE)pid, &status)) {
179 *errcode = status;
180 debug_git("Exit code: %d", status);
181 }else {
182 /* play safe, and return total failure */
183 *errcode = GetLastError();
184 return -1;
186 return 1;
188 return 0;
191 void close_process(pid_t pid)
193 CloseHandle((HANDLE)pid);