]> err.no Git - moreutils/commitdiff
optimisations and fix memory leak
authorJoey Hess <joey@kitenet.net>
Tue, 6 Jul 2010 18:55:22 +0000 (14:55 -0400)
committerJoey Hess <joey@kitenet.net>
Tue, 6 Jul 2010 18:55:22 +0000 (14:55 -0400)
Fork a child before calculating its parameters.
This avoids a minor memory leak; the parameter
array was allocated in the parent and never freed.

parallel.c

index 0146a13b99beee2b2c0d45fde2039c4e58de394a..81a4184b01e0b858d1334a65c1c98e51af20e615 100644 (file)
@@ -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;