summaryrefslogtreecommitdiff
path: root/wait.c
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-03-15 12:16:24 -0400
committerJakob Kaivo <jkk@ung.org>2019-03-15 12:16:24 -0400
commit18425501da3ad57fcf78eb111cf62ca4d79c3f5f (patch)
treeec308afb62266398fe47c8ac4cb331e863757a31 /wait.c
parenta088e851fb45fe38e6a228770fa5d0c04cddb7f5 (diff)
rough out
Diffstat (limited to 'wait.c')
-rw-r--r--wait.c37
1 files changed, 33 insertions, 4 deletions
diff --git a/wait.c b/wait.c
index 16b64d7..d4a224b 100644
--- a/wait.c
+++ b/wait.c
@@ -1,8 +1,37 @@
+#define _XOPEN_SOURCE 700
#include <stdio.h>
+#include <sys/wait.h>
+#include <unistd.h>
-int
-wait_main(int argc, char **argv)
+#include "jobcontrol.h"
+
+int wait_main(int argc, char *argv[])
{
- printf("Sorry, %s isn't implemented yet.\n", argv[0]);
- return argc;
+ 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;
}