summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoey Hess <joey@kitenet.net>2010-07-06 14:55:22 -0400
committerJoey Hess <joey@kitenet.net>2010-07-06 14:55:22 -0400
commitb01a6aabcf738b3d86c0fce2164861f783ed37ca (patch)
treef39dfef2df22227fc12bca7e68311c21c4fd4f92
parentf597f10475a2e906b96e2856818632dbf66a61b5 (diff)
downloadmoreutils-b01a6aabcf738b3d86c0fce2164861f783ed37ca.tar.gz
optimisations and fix memory leak
Fork a child before calculating its parameters. This avoids a minor memory leak; the parameter array was allocated in the parent and never freed.
-rw-r--r--parallel.c44
1 files changed, 22 insertions, 22 deletions
diff --git a/parallel.c b/parallel.c
index 0146a13..81a4184 100644
--- a/parallel.c
+++ b/parallel.c
@@ -44,17 +44,22 @@ void usage() {
}
void exec_child(char **command, char **arguments, int replace_cb, int nargs) {
- char **argv;
- int argc = 0;
- int i;
-
- while (command[argc] != 0) {
- argc++;
+ if (fork() != 0) {
+ return;
}
- if (replace_cb == 0)
- argc++;
- argv = calloc(sizeof(char*), argc + nargs);
+
if (command[0]) {
+ char **argv;
+ int argc = 0;
+ int i;
+
+ while (command[argc] != 0) {
+ argc++;
+ }
+ if (replace_cb == 0)
+ argc++;
+ argv = calloc(sizeof(char*), argc + nargs);
+
for (i = 0; i < argc; i++) {
argv[i] = command[i];
if (replace_cb && (strcmp(argv[i], "{}") == 0))
@@ -62,21 +67,16 @@ void exec_child(char **command, char **arguments, int replace_cb, int nargs) {
}
if (replace_cb == 0)
memcpy(argv + i - 1, arguments, nargs * sizeof(char *));
- if (fork() == 0) {
- /* Child */
- execvp(argv[0], argv);
- exit(1);
- }
+ execvp(argv[0], argv);
+ exit(1);
}
else {
- if (fork() == 0) {
- int ret=system(arguments[0]);
- if (WIFEXITED(ret)) {
- exit(WEXITSTATUS(ret));
- }
- else {
- exit(1);
- }
+ int ret=system(arguments[0]);
+ if (WIFEXITED(ret)) {
+ exit(WEXITSTATUS(ret));
+ }
+ else {
+ exit(1);
}
}
return;