summaryrefslogtreecommitdiff
path: root/src/libstddjb/waitn_reap_posix.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstddjb/waitn_reap_posix.c')
-rw-r--r--src/libstddjb/waitn_reap_posix.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/libstddjb/waitn_reap_posix.c b/src/libstddjb/waitn_reap_posix.c
new file mode 100644
index 0000000..4a6994f
--- /dev/null
+++ b/src/libstddjb/waitn_reap_posix.c
@@ -0,0 +1,22 @@
+/* ISC license. */
+
+#include <sys/wait.h>
+
+#include <skalibs/djbunix.h>
+
+int waitn_reap_posix (pid_t *pids, unsigned int len, int *w)
+{
+ pid_t wanted = len ? pids[len-1] : 0 ;
+ unsigned int n = 0 ;
+ while (len)
+ {
+ int wstat ;
+ int r = wait_pids_nohang(pids, len, &wstat) ;
+ if (r < 0) return r ;
+ else if (!r) break ;
+ if (pids[r-1] == wanted) *w = wstat ;
+ pids[r-1] = pids[--len] ;
+ n++ ;
+ }
+ return n ;
+}