summaryrefslogtreecommitdiff
path: root/wait.c
blob: d4a224b1e79b25f88990a80f6c65d2fafd0e0de2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>

#include "jobcontrol.h"

int wait_main(int argc, char *argv[])
{
	int c;
	while ((c = getopt(argc, argv, "")) != -1) {
		return 1;
	}

	if (optind >= argc) {
		/* wait for all children */
		return 0;
	}

	int ret = 0;
	do {
		pid_t pid = pidfrom(argv[optind++]);
		pid = waitpid(pid, &ret, 0);

		if (pid == -1) {
			ret = 127;
		} else if (WIFEXITED(ret)) {
			ret = WEXITSTATUS(ret);
		} else if (WIFSIGNALED(ret)) {
			ret = 128; /* + something */
		} else {
			ret = 126;
		}
	} while (argv[optind]);

	return 0;
}