1 #include "../common/git-compat-util.h"
2 #include "../common/strbuf.h"
3 #include "../common/systeminfo.h"
4 #include "../common/debug.h"
8 static struct strbuf gitPath
= STRBUF_INIT
;
9 static TCHAR msysPath
[MAX_PATH
];
11 static int get_msys_path_from_registry(HKEY root
)
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
)
21 result
= RegQueryValueEx(key
,
22 GIT_CHEETAH_REG_PATHTOMSYS
,
23 NULL
, NULL
, (LPBYTE
)msysPath
, &path_len
);
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 */
36 found_path
= get_msys_path_from_registry(HKEY_CURRENT_USER
);
38 /* if not found in user settings, try machine-wide */
40 found_path
= get_msys_path_from_registry(HKEY_LOCAL_MACHINE
);
48 static inline int test_msys_path(const char *postfix
, char *path
)
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
);
57 goto test_git_path_failed
;
59 n
= snprintf(path
, MAX_PATH
, "%s\\%s", msys_path(), postfix
);
61 goto test_git_path_failed
;
63 if (lstat(path
, &stat_info
) == 0) {
72 const char *git_path()
82 if (test_msys_path("bin", path
))
83 strbuf_addstr(&gitPath
, path
);
85 if (test_msys_path("mingw\\bin", path
)) {
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;
122 /* if it's PATH variable (could be Path= too!) */
123 if (!strnicmp(old
+ space
, "PATH=", 5)) {
130 space
++; /* skip var-terminating NULL */
135 if (path_index
== -1)
138 environment
= malloc(sizeof(*environment
) * (total
+ 1));
140 for (i
= 0; i
< total
; i
++) {
141 if (path_index
== space
) {
142 char *path
= old
+ space
+ name_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
);
150 environment
[i
] = strdup(old
+ space
);
154 space
++; /* skip var-terminating NULL */
157 /* mark the end of the array */
160 FreeEnvironmentStrings(old
);
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
)
176 if (WAIT_OBJECT_0
== WaitForSingleObject((HANDLE
)pid
,
178 if (GetExitCodeProcess((HANDLE
)pid
, &status
)) {
180 debug_git("Exit code: %d", status
);
182 /* play safe, and return total failure */
183 *errcode
= GetLastError();