This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathsysteminfo.c
158 lines (127 loc) · 3.16 KB
/
systeminfo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "../common/git-compat-util.h"
#include "../common/strbuf.h"
#include "../common/systeminfo.h"
#include "../common/debug.h"
#include <windows.h>
#include "dll.h"
static struct strbuf gitPath = STRBUF_INIT;
static TCHAR msysPath[MAX_PATH];
static int get_msys_path_from_registry(HKEY root)
{
HKEY key;
DWORD path_len = sizeof(msysPath);
LONG result = RegOpenKeyEx(root, GIT_CHEETAH_REG_PATH,
0, KEY_QUERY_VALUE, &key);
if (ERROR_SUCCESS != result)
return 0;
result = RegQueryValueEx(key,
GIT_CHEETAH_REG_PATHTOMSYS,
NULL, NULL, (LPBYTE)msysPath, &path_len);
RegCloseKey(key);
return ERROR_SUCCESS == result;
}
static TCHAR *msys_path(void)
{
static int found_path = 0;
/* try to find user-specific settings first */
if (!found_path)
found_path = get_msys_path_from_registry(HKEY_CURRENT_USER);
/* if not found in user settings, try machine-wide */
if (!found_path)
found_path = get_msys_path_from_registry(HKEY_LOCAL_MACHINE);
if (found_path)
return msysPath;
return NULL;
}
static inline int test_msys_path(const char *postfix, char *path)
{
int n;
char stat_path[MAX_PATH];
struct stat stat_info;
n = snprintf(stat_path, MAX_PATH, "%s\\%s\\git",
msys_path(), postfix);
if (n >= MAX_PATH)
goto test_git_path_failed;
n = snprintf(path, MAX_PATH, "%s\\%s", msys_path(), postfix);
if (n >= MAX_PATH)
goto test_git_path_failed;
if (lstat(path, &stat_info) == 0) {
return 1;
}
test_git_path_failed:
*path = '\0';
return 0;
}
const char *git_path()
{
char path[MAX_PATH];
if (!msys_path())
return NULL;
if (gitPath.len)
return gitPath.buf;
if (test_msys_path("bin", path))
strbuf_addstr(&gitPath, path);
if (test_msys_path("mingw\\bin", path)) {
if (gitPath.len)
strbuf_addch(&gitPath, ';');
strbuf_addstr(&gitPath, path);
}
return gitPath.len ? gitPath.buf : NULL;
}
void message_box(const char *string)
{
MessageBox(0, string, "Hello", MB_OK|MB_ICONEXCLAMATION);
}
int is_directory(const char *path)
{
return (FILE_ATTRIBUTE_DIRECTORY & GetFileAttributes(path));
}
pid_t fork_process(const char *cmd, const char **args, const char *wd)
{
pid_t result;
char *oldpath = NULL;
const char *gitpath = git_path();
/* if gitpath is set, temporarily set PATH=<gitpath>;%PATH% */
if (gitpath) {
struct strbuf path = STRBUF_INIT;
strbuf_addstr(&path, gitpath);
oldpath = getenv("PATH");
if (oldpath) {
oldpath = xstrdup(oldpath);
strbuf_addch(&path, ';');
strbuf_addstr(&path, oldpath);
}
setenv("PATH", path.buf, 1);
strbuf_release(&path);
}
/* spawn child process */
result = mingw_spawnvpe_cwd(cmd, args, NULL, wd);
/* reset PATH to previous value */
if (gitpath) {
setenv("PATH", oldpath, 1);
free(oldpath);
}
return result;
}
int wait_for_process(pid_t pid, int max_time, int *errcode)
{
DWORD status;
*errcode = 0;
if (WAIT_OBJECT_0 == WaitForSingleObject((HANDLE)pid,
max_time)) {
if (GetExitCodeProcess((HANDLE)pid, &status)) {
*errcode = status;
debug_git("Exit code: %d", status);
}else {
/* play safe, and return total failure */
*errcode = GetLastError();
return -1;
}
return 1;
}
return 0;
}
void close_process(pid_t pid)
{
CloseHandle((HANDLE)pid);
}