Skip to content

Commit 19a583d

Browse files
rscharfegitster
authored andcommitted
run-command: add env_array, an optional argv_array for env
Similar to args, add a struct argv_array member to struct child_process that simplifies specifying the environment for children. It is freed automatically by finish_command() or if start_command() encounters an error. Suggested-by: Jeff King <[email protected]> Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 98349e5 commit 19a583d

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Documentation/technical/api-run-command.txt

+5
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ string pointers (NULL terminated) in .env:
169169
. If the string does not contain '=', it names an environment
170170
variable that will be removed from the child process's environment.
171171

172+
If the .env member is NULL, `start_command` will point it at the
173+
.env_array `argv_array` (so you may use one or the other, but not both).
174+
The memory in .env_array will be cleaned up automatically during
175+
`finish_command` (or during `start_command` when it is unsuccessful).
176+
172177
To specify a new initial working directory for the sub-process,
173178
specify it in the .dir member.
174179

run-command.c

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ void child_process_init(struct child_process *child)
1212
{
1313
memset(child, 0, sizeof(*child));
1414
argv_array_init(&child->args);
15+
argv_array_init(&child->env_array);
1516
}
1617

1718
struct child_to_clean {
@@ -287,6 +288,8 @@ int start_command(struct child_process *cmd)
287288

288289
if (!cmd->argv)
289290
cmd->argv = cmd->args.argv;
291+
if (!cmd->env)
292+
cmd->env = cmd->env_array.argv;
290293

291294
/*
292295
* In case of errors we must keep the promise to close FDs
@@ -338,6 +341,7 @@ int start_command(struct child_process *cmd)
338341
error("cannot create %s pipe for %s: %s",
339342
str, cmd->argv[0], strerror(failed_errno));
340343
argv_array_clear(&cmd->args);
344+
argv_array_clear(&cmd->env_array);
341345
errno = failed_errno;
342346
return -1;
343347
}
@@ -524,6 +528,7 @@ int start_command(struct child_process *cmd)
524528
else if (cmd->err)
525529
close(cmd->err);
526530
argv_array_clear(&cmd->args);
531+
argv_array_clear(&cmd->env_array);
527532
errno = failed_errno;
528533
return -1;
529534
}
@@ -550,6 +555,7 @@ int finish_command(struct child_process *cmd)
550555
{
551556
int ret = wait_or_whine(cmd->pid, cmd->argv[0]);
552557
argv_array_clear(&cmd->args);
558+
argv_array_clear(&cmd->env_array);
553559
return ret;
554560
}
555561

run-command.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
struct child_process {
1111
const char **argv;
1212
struct argv_array args;
13+
struct argv_array env_array;
1314
pid_t pid;
1415
/*
1516
* Using .in, .out, .err:
@@ -44,7 +45,7 @@ struct child_process {
4445
unsigned clean_on_exit:1;
4546
};
4647

47-
#define CHILD_PROCESS_INIT { NULL, ARGV_ARRAY_INIT }
48+
#define CHILD_PROCESS_INIT { NULL, ARGV_ARRAY_INIT, ARGV_ARRAY_INIT }
4849
void child_process_init(struct child_process *);
4950

5051
int start_command(struct child_process *);

0 commit comments

Comments
 (0)