diff options
author | Joey Hess <joey@kitenet.net> | 2010-07-06 14:55:22 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2010-07-06 14:55:22 -0400 |
commit | b01a6aabcf738b3d86c0fce2164861f783ed37ca (patch) | |
tree | f39dfef2df22227fc12bca7e68311c21c4fd4f92 | |
parent | f597f10475a2e906b96e2856818632dbf66a61b5 (diff) | |
download | moreutils-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.c | 44 |
1 files changed, 22 insertions, 22 deletions
@@ -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; |