Implement setting of environment variables in spawned programs.

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
This commit is contained in:
Johannes Sixt
2007-12-05 21:39:19 +01:00
parent f6df056f6c
commit 9a930a2ecb
3 changed files with 28 additions and 16 deletions

View File

@@ -736,10 +736,9 @@ void free_env(char **env)
free(env);
}
static int lookup_env(char **env, const char *name)
static int lookup_env(char **env, const char *name, size_t nmln)
{
int i;
size_t nmln = strlen(name);
for (i = 0; env[i]; i++) {
if (0 == strncmp(env[i], name, nmln)
@@ -750,15 +749,32 @@ static int lookup_env(char **env, const char *name)
return -1;
}
void env_unsetenv(char **env, const char *name)
/*
* If name contains '=', then sets the variable, otherwise it unsets it
*/
char **env_setenv(char **env, const char *name)
{
int i = lookup_env(env, name);
if (i < 0)
return;
char *eq = strchrnul(name, '=');
int i = lookup_env(env, name, eq-name);
free(env[i]);
for (; env[i]; i++)
env[i] = env[i+1];
if (i < 0) {
if (*eq) {
for (i = 0; env[i]; i++)
;
env = xrealloc(env, (i+2)*sizeof(*env));
env[i] = xstrdup(name);
env[i+1] = NULL;
}
}
else {
free(env[i]);
if (*eq)
env[i] = xstrdup(name);
else
for (; env[i]; i++)
env[i] = env[i+1];
}
return env;
}
/* this is the first function to call into WS_32; initialize it */

View File

@@ -638,7 +638,7 @@ sig_handler_t mingw_signal(int sig, sig_handler_t handler);
char **copy_environ(void);
void free_environ(void);
void env_unsetenv(char **env, const char *name);
char **env_setenv(char **env, const char *name);
#endif /* __MINGW32__ */

View File

@@ -146,12 +146,8 @@ int start_command(struct child_process *cmd)
die("chdir in start_command() not implemented");
if (cmd->env) {
env = copy_environ();
for (; *cmd->env; cmd->env++) {
if (strchr(*cmd->env, '='))
die("setting environment in start_command() not implemented");
else
env_unsetenv(env, *cmd->env);
}
for (; *cmd->env; cmd->env++)
env = env_setenv(env, *cmd->env);
}
if (cmd->git_cmd) {