xref: /netbsd-src/external/gpl2/diffutils/dist/lib/waitpid.c (revision 75f6d617e282811cb173c2ccfbf5df0dd71f7045)
1*75f6d617Schristos /*	$NetBSD: waitpid.c,v 1.1.1.1 2016/01/13 03:15:30 christos Exp $	*/
2*75f6d617Schristos 
3*75f6d617Schristos /* Emulate waitpid on systems that just have wait.
4*75f6d617Schristos    Copyright (C) 1994, 1995, 1998, 1999 Free Software Foundation, Inc.
5*75f6d617Schristos 
6*75f6d617Schristos    This program is free software; you can redistribute it and/or modify
7*75f6d617Schristos    it under the terms of the GNU General Public License as published by
8*75f6d617Schristos    the Free Software Foundation; either version 2, or (at your option)
9*75f6d617Schristos    any later version.
10*75f6d617Schristos 
11*75f6d617Schristos    This program is distributed in the hope that it will be useful,
12*75f6d617Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*75f6d617Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*75f6d617Schristos    GNU General Public License for more details.
15*75f6d617Schristos 
16*75f6d617Schristos    You should have received a copy of the GNU General Public License
17*75f6d617Schristos    along with this program; see the file COPYING.
18*75f6d617Schristos    If not, write to the Free Software Foundation,
19*75f6d617Schristos    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20*75f6d617Schristos 
21*75f6d617Schristos #if HAVE_CONFIG_H
22*75f6d617Schristos # include <config.h>
23*75f6d617Schristos #endif
24*75f6d617Schristos 
25*75f6d617Schristos #include <errno.h>
26*75f6d617Schristos #ifndef errno
27*75f6d617Schristos extern int errno;
28*75f6d617Schristos #endif
29*75f6d617Schristos 
30*75f6d617Schristos #define WAITPID_CHILDREN 8
31*75f6d617Schristos static pid_t waited_pid[WAITPID_CHILDREN];
32*75f6d617Schristos static int waited_status[WAITPID_CHILDREN];
33*75f6d617Schristos 
34*75f6d617Schristos pid_t
waitpid(pid_t pid,int * stat_loc,int options)35*75f6d617Schristos waitpid (pid_t pid, int *stat_loc, int options)
36*75f6d617Schristos {
37*75f6d617Schristos   int i;
38*75f6d617Schristos   pid_t p;
39*75f6d617Schristos 
40*75f6d617Schristos   if (!options && (pid == -1 || 0 < pid))
41*75f6d617Schristos     {
42*75f6d617Schristos       /* If we have already waited for this child, return it immediately.  */
43*75f6d617Schristos       for (i = 0;  i < WAITPID_CHILDREN;  i++)
44*75f6d617Schristos 	{
45*75f6d617Schristos 	  p = waited_pid[i];
46*75f6d617Schristos 	  if (p && (p == pid || pid == -1))
47*75f6d617Schristos 	    {
48*75f6d617Schristos 	      waited_pid[i] = 0;
49*75f6d617Schristos 	      goto success;
50*75f6d617Schristos 	    }
51*75f6d617Schristos 	}
52*75f6d617Schristos 
53*75f6d617Schristos       /* The child has not returned yet; wait for it, accumulating status.  */
54*75f6d617Schristos       for (i = 0;  i < WAITPID_CHILDREN;  i++)
55*75f6d617Schristos 	if (! waited_pid[i])
56*75f6d617Schristos 	  {
57*75f6d617Schristos 	    p = wait (&waited_status[i]);
58*75f6d617Schristos 	    if (p < 0)
59*75f6d617Schristos 	      return p;
60*75f6d617Schristos 	    if (p == pid || pid == -1)
61*75f6d617Schristos 	      goto success;
62*75f6d617Schristos 	    waited_pid[i] = p;
63*75f6d617Schristos 	  }
64*75f6d617Schristos     }
65*75f6d617Schristos 
66*75f6d617Schristos   /* We cannot emulate this wait call, e.g. because of too many children.  */
67*75f6d617Schristos   errno = EINVAL;
68*75f6d617Schristos   return -1;
69*75f6d617Schristos 
70*75f6d617Schristos success:
71*75f6d617Schristos   if (stat_loc)
72*75f6d617Schristos     *stat_loc = waited_status[i];
73*75f6d617Schristos   return p;
74*75f6d617Schristos }
75