4 #include "systeminfo.h"
7 #define MAX_PROCESSING_TIME (60 * 1000)
11 * builds an array of environment variables,
12 * as expected by mingw_spawnvpe
14 static char **env_for_git()
16 static char **environment
;
19 * if we can't find path to msys in the registry, return NULL and
20 * the CreateProcess will copy the environment for us
22 if (!environment
&& msys_path()) {
23 char *old
= GetEnvironmentStrings();
24 size_t space
= 0, path_index
= -1, name_len
= 0;
28 /* if it's PATH variable (could be Path= too!) */
29 if (!strnicmp(old
+ space
, "PATH=", 5)) {
36 space
++; /* skip var-terminating NULL */
44 environment
= malloc(sizeof(*environment
) * (total
+ 1));
46 for (i
= 0; i
< total
; i
++) {
47 if (path_index
== space
) {
48 char *path
= old
+ space
+ name_len
;
50 environment
[i
] = malloc(strlen(path
) + 1 +
51 2 * strlen(msys_path()) + 32);
52 len
= sprintf(environment
[i
],
53 "PATH=%s\\bin;%s\\mingw\\bin%s%s",
54 msys_path(), msys_path(),
55 *path
? ";" : "", path
);
57 environment
[i
] = strdup(old
+ space
);
61 space
++; /* skip var-terminating NULL */
64 /* mark the end of the array */
67 FreeEnvironmentStrings(old
);
73 /* copy from run-command.c */
74 static inline void close_pair(int fd
[2])
80 int exec_program(const char *working_directory
,
81 struct strbuf
*output
, struct strbuf
*error_output
,
84 int fdout
[2], fderr
[2];
85 int s0
= -1, s1
= -1, s2
= -1; /* backups of stdin, stdout, stderr */
88 const char *argv
[MAX_ARGS
];
95 reporter
*debug
= QUIETMODE
& flags
? debug_git
: debug_git_mbox
;
98 debug("[ERROR] Could not find msysPath");
103 if (pipe(fdout
) < 0) {
104 return -ERR_RUN_COMMAND_PIPE
;
113 if (pipe(fderr
) < 0) {
116 return -ERR_RUN_COMMAND_PIPE
;
124 va_start(params
, flags
);
126 arg
= va_arg(params
, char*);
128 } while (argc
< MAX_ARGS
&& arg
);
131 pid
= mingw_spawnvpe_cwd(argv
[0], argv
, env_for_git(),
135 dup2(s1
, 1), close(s1
);
137 dup2(s2
, 2), close(s2
);
144 return -ERR_RUN_COMMAND_FORK
;
152 if (WAITMODE
& flags
) {
153 if (WAIT_OBJECT_0
== WaitForSingleObject((HANDLE
)pid
,
154 MAX_PROCESSING_TIME
)) {
155 if (GetExitCodeProcess((HANDLE
)pid
, &status
))
156 debug_git("Exit code: %d", status
);
158 /* play safe, and return total failure */
160 debug_git("[ERROR] GetExitCode failed (%d); "
168 strbuf_read(output
, fdout
[0], 0);
169 debug_git("STDOUT:\r\n%s\r\n*** end of STDOUT ***\r\n", output
->buf
);
173 strbuf_read(error_output
, fderr
[0], 0);
174 debug_git("STDERR:\r\n%s\r\n*** end of STDERR ***\r\n", error_output
->buf
);
177 status
= -ERR_RUN_COMMAND_WAITPID_NOEXIT
;
178 debug_git("[ERROR] process timed out; "
180 working_directory
, argv
[0]);