summaryrefslogtreecommitdiff
path: root/src/libstddjb
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstddjb')
-rw-r--r--src/libstddjb/waitn.c13
-rw-r--r--src/libstddjb/waitn_posix.c21
-rw-r--r--src/libstddjb/waitn_reap.c14
-rw-r--r--src/libstddjb/waitn_reap_posix.c22
4 files changed, 49 insertions, 21 deletions
diff --git a/src/libstddjb/waitn.c b/src/libstddjb/waitn.c
index 05d7f37..4d980cc 100644
--- a/src/libstddjb/waitn.c
+++ b/src/libstddjb/waitn.c
@@ -1,18 +1,11 @@
/* ISC license. */
#include <sys/wait.h>
+
#include <skalibs/djbunix.h>
int waitn (pid_t *pids, unsigned int n)
{
- while (n)
- {
- int wstat ;
- unsigned int i = 0 ;
- pid_t pid = wait_nointr(&wstat) ;
- if (pid < 0) return 0 ;
- for (; i < n ; i++) if (pid == pids[i]) break ;
- if (i < n) pids[i] = pids[--n] ;
- }
- return 1 ;
+ int dummy ;
+ return waitn_posix(pids, n, &dummy) ;
}
diff --git a/src/libstddjb/waitn_posix.c b/src/libstddjb/waitn_posix.c
new file mode 100644
index 0000000..ea5935d
--- /dev/null
+++ b/src/libstddjb/waitn_posix.c
@@ -0,0 +1,21 @@
+/* ISC license. */
+
+#include <sys/wait.h>
+
+#include <skalibs/djbunix.h>
+
+int waitn_posix (pid_t *pids, unsigned int n, int *w)
+{
+ pid_t wanted = n ? pids[n-1] : 0 ;
+ while (n)
+ {
+ int wstat ;
+ unsigned int i = 0 ;
+ pid_t pid = wait_nointr(&wstat) ;
+ if (pid < 0) return 0 ;
+ for (; i < n ; i++) if (pid == pids[i]) break ;
+ if (i < n) pids[i] = pids[--n] ;
+ if (pid == wanted) *w = wstat ;
+ }
+ return 1 ;
+}
diff --git a/src/libstddjb/waitn_reap.c b/src/libstddjb/waitn_reap.c
index 9f26b5d..2c4ebe8 100644
--- a/src/libstddjb/waitn_reap.c
+++ b/src/libstddjb/waitn_reap.c
@@ -1,19 +1,11 @@
/* ISC license. */
#include <sys/wait.h>
+
#include <skalibs/djbunix.h>
int waitn_reap (pid_t *pids, unsigned int len)
{
- unsigned int n = 0 ;
- while (len)
- {
- int w ;
- int r = wait_pids_nohang(pids, len, &w) ;
- if (r < 0) return r ;
- else if (!r) break ;
- pids[r-1] = pids[--len] ;
- n++ ;
- }
- return n ;
+ int dummy ;
+ return waitn_reap_posix(pids, len, &dummy) ;
}
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 ;
+}