start_command: do not clobber cmd->env on Windows code path
Previously, it would not be possible to call start_command twice for the same struct child_process that has env set. The fix is achieved by moving the loop that modifies the environment block into a helper function. This also allows us to make two other helper functions static. Signed-off-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Sixt committed
Sep 11, 2009 at 19:40 UTC
2affea4125a0ae6c528a14f14731748c1fad9a8a
3 files changed
+17
-9
compat/mingw.c
+14
-2
@@ -824,7 +824,7 @@ void mingw_execvp(const char *cmd, char *const *argv)
824
free_path_split(path);
825
}
826
827
-char **copy_environ()
827
+static char **copy_environ(void)
828
{
829
char **env;
830
int i = 0;
@@ -861,7 +861,7 @@ static int lookup_env(char **env, const char *name, size_t nmln)
861
/*
862
* If name contains '=', then sets the variable, otherwise it unsets it
863
*/
864
-char **env_setenv(char **env, const char *name)
864
+static char **env_setenv(char **env, const char *name)
865
{
866
char *eq = strchrnul(name, '=');
867
int i = lookup_env(env, name, eq-name);
@@ -886,6 +886,18 @@ char **env_setenv(char **env, const char *name)
886
return env;
887
}
888
889
+/*
890
+ * Copies global environ and adjusts variables as specified by vars.
891
+ */
892
+char **make_augmented_environ(const char *const *vars)
893
+{
894
+ char **env = copy_environ();
895
+
896
+ while (*vars)
897
+ env = env_setenv(env, *vars++);
898
+ return env;
899
+}
900
+
901
/* this is the first function to call into WS_32; initialize it */
902
#undef gethostbyname
903
struct hostent *mingw_gethostbyname(const char *host)
compat/mingw.h
+1
-2
@@ -222,9 +222,8 @@ void mingw_open_html(const char *path);
222
* helpers
223
*/
224
225
-char **copy_environ(void);
225
+char **make_augmented_environ(const char *const *vars);
226
void free_environ(char **env);
227
-char **env_setenv(char **env, const char *name);
227
228
/*
229
* A replacement of main() that ensures that argv[0] has a path
run-command.c
+2
-5
@@ -173,11 +173,8 @@ fail_pipe:
173
174
if (cmd->dir)
175
die("chdir in start_command() not implemented");
176
- if (cmd->env) {
177
- env = copy_environ();
178
- for (; *cmd->env; cmd->env++)
179
- env = env_setenv(env, *cmd->env);
180
- }
176
+ if (cmd->env)
177
+ env = make_augmented_environ(cmd->env);
178
179
if (cmd->git_cmd) {
180
cmd->argv = prepare_git_cmd(cmd->argv);