about summary refs log tree commit diff
path: root/xe.c
diff options
context:
space:
mode:
Diffstat (limited to 'xe.c')
-rw-r--r--xe.c27
1 files changed, 25 insertions, 2 deletions
diff --git a/xe.c b/xe.c
index 7e2a468..2b6bf68 100644
--- a/xe.c
+++ b/xe.c
@@ -45,6 +45,8 @@ static size_t argslen;
 static size_t argscap;
 static size_t argsresv;
 
+static pid_t *children;
+
 static char **inargs;
 
 static char *line = 0;
@@ -85,7 +87,17 @@ mywait()
 			return 0;
 		// no other error possible?
 	}	
-	
+
+	int i;
+	for (i = 0; i < maxjobs; i++)
+		if (children[i] == pid) {
+			children[i] = 0;
+			goto my_child;
+		}
+
+	return 1;
+
+my_child:
 	if (WIFEXITED(status)) {
 		if (WEXITSTATUS(status) >= 1 && WEXITSTATUS(status) <= 125) {
 			if (Fflag) {
@@ -193,7 +205,7 @@ run()
 {
 	pid_t pid;
 
-	if (runjobs >= maxjobs)
+	while (runjobs >= maxjobs)
 		mywait();
 	runjobs++;
 	iterations++;
@@ -234,6 +246,13 @@ run()
 		trace();
 	}
 
+	int i;
+	for (i = 0; i < maxjobs; i++)
+		if (!children[i]) {
+			children[i] = pid;
+			break;
+		}
+
 	return 0;
 }
 
@@ -335,6 +354,10 @@ main(int argc, char *argv[], char *envp[])
 			exit(1);
 		}
 
+	children = calloc(sizeof (pid_t), maxjobs);
+	if (!children)
+		exit(1);
+
 	if (aflag || Aflag) {
 		input = 0;
 	} else if (!fflag || strcmp("-", fflag) == 0) {