Merge branch 'pkahle'
[git-cheetah.git] / explorer / systeminfo.c
blob2471887d23892f9220d8286963beb93d7f3f1200
1 #include "../common/git-compat-util.h"
2 #include "../common/strbuf.h"
3 #include "../common/systeminfo.h"
4 #include <windows.h>
5 #include "dll.h"
7 static struct strbuf gitPath = STRBUF_INIT;
8 static TCHAR msysPath[MAX_PATH];
10 static int get_msys_path_from_registry(HKEY root)
12 HKEY key;
13 DWORD path_len = sizeof(msysPath);
15 LONG result = RegOpenKeyEx(root, GIT_CHEETAH_REG_PATH,
16 0, KEY_QUERY_VALUE, &key);
17 if (ERROR_SUCCESS != result)
18 return 0;
20 result = RegQueryValueEx(key,
21 GIT_CHEETAH_REG_PATHTOMSYS,
22 NULL, NULL, (LPBYTE)msysPath, &path_len);
24 RegCloseKey(key);
26 return ERROR_SUCCESS == result;
29 static TCHAR *msys_path(void)
31 static int found_path = 0;
33 /* try to find user-specific settings first */
34 if (!found_path)
35 found_path = get_msys_path_from_registry(HKEY_CURRENT_USER);
37 /* if not found in user settings, try machine-wide */
38 if (!found_path)
39 found_path = get_msys_path_from_registry(HKEY_LOCAL_MACHINE);
41 if (found_path)
42 return msysPath;
44 return NULL;
47 static inline int test_msys_path(const char *postfix, char *path)
49 int n;
50 char stat_path[MAX_PATH];
51 struct stat stat_info;
53 n = snprintf(stat_path, MAX_PATH, "%s\\%s\\git",
54 msys_path(), postfix);
55 if (n >= MAX_PATH)
56 goto test_git_path_failed;
58 n = snprintf(path, MAX_PATH, "%s\\%s", msys_path(), postfix);
59 if (n >= MAX_PATH)
60 goto test_git_path_failed;
62 if (lstat(path, &stat_info) == 0) {
63 return 1;
66 test_git_path_failed:
67 *path = '\0';
68 return 0;
71 const char *git_path()
73 char path[MAX_PATH];
75 if (!msys_path())
76 return NULL;
78 if (gitPath.len)
79 return gitPath.buf;
81 if (test_msys_path("bin", path))
82 strbuf_addstr(&gitPath, path);
84 if (test_msys_path("mingw\\bin", path)) {
85 if (gitPath.len)
86 strbuf_addch(&gitPath, ';');
87 strbuf_addstr(&gitPath, path);
90 return gitPath.len ? gitPath.buf : NULL;
93 void message_box(const char *string)
95 MessageBox(0, string, "Hello", MB_OK|MB_ICONEXCLAMATION);
98 int is_directory(const char *path)
100 return (FILE_ATTRIBUTE_DIRECTORY & GetFileAttributes(path));
104 * builds an array of environment variables,
105 * as expected by mingw_spawnvpe
107 static char **env_for_git()
109 static char **environment;
112 * if we can't find path to msys in the registry, return NULL and
113 * the CreateProcess will copy the environment for us
115 if (!environment && git_path()) {
116 char *old = GetEnvironmentStrings();
117 size_t space = 0, path_index = -1, name_len = 0;
118 int total = 0, i;
120 while (old[space]) {
121 /* if it's PATH variable (could be Path= too!) */
122 if (!strnicmp(old + space, "PATH=", 5)) {
123 path_index = space;
124 name_len = 5;
127 while (old[space])
128 space++;
129 space++; /* skip var-terminating NULL */
131 total++;
134 if (path_index == -1)
135 path_index = space;
137 environment = malloc(sizeof(*environment) * (total + 1));
138 space = 0;
139 for (i = 0; i < total; i++) {
140 if (path_index == space) {
141 char *path = old + space + name_len;
142 size_t len;
143 environment[i] = malloc(strlen(path) + 1 +
144 2 * strlen(git_path()) + 32);
145 len = sprintf(environment[i],
146 "PATH=%s%s%s", git_path(),
147 *path ? ";" : "", path);
148 } else
149 environment[i] = strdup(old + space);
151 while (old[space])
152 space++;
153 space++; /* skip var-terminating NULL */
156 /* mark the end of the array */
157 environment[i] = 0;
159 FreeEnvironmentStrings(old);
162 return environment;
165 pid_t fork_process(const char *cmd, const char **args, const char *wd)
167 return mingw_spawnvpe_cwd(cmd, args, env_for_git(), wd);
170 int wait_for_process(pid_t pid, int max_time, int *errcode)
172 DWORD status;
173 *errcode = 0;
175 if (WAIT_OBJECT_0 == WaitForSingleObject((HANDLE)pid,
176 max_time)) {
177 if (GetExitCodeProcess((HANDLE)pid, &status)) {
178 *errcode = status;
179 debug_git("Exit code: %d", status);
180 }else {
181 /* play safe, and return total failure */
182 *errcode = GetLastError();
183 return -1;
185 return 1;
187 return 0;