summaryrefslogtreecommitdiff
path: root/src/libstddjb/waitn_reap_posix.c
blob: 4a6994f11f52ce7fa827623812009738c4d555a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 ;
}