xref: /openbsd-src/gnu/usr.bin/cvs/vms/waitpid.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /* Emulate waitpid on systems that just have wait.
2    Copyright (C) 1994 Free Software Foundation, Inc.
3 
4 This file is part of GNU DIFF.
5 
6 GNU DIFF is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU DIFF is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.  */
15 
16 #include "vms.h"
17 
18 #define WAITPID_CHILDREN 8
19 static pid_t waited_pid[WAITPID_CHILDREN];
20 static int waited_status[WAITPID_CHILDREN];
21 
22 pid_t
23 waitpid (pid, stat_loc, options)
24      pid_t pid;
25      int *stat_loc;
26      int options;
27 {
28   int i;
29   pid_t p;
30 
31   if (!options  &&  (0 < pid || pid == -1))
32     {
33       /* If we have already waited for this child, return it immediately.  */
34       for (i = 0;  i < WAITPID_CHILDREN;  i++)
35 	{
36 	  p = waited_pid[i];
37 	  if (p  &&  (p == pid  ||  pid == -1))
38 	    {
39 	      waited_pid[i] = 0;
40 	      goto success;
41 	    }
42 	}
43 
44       /* The child has not returned yet; wait for it, accumulating status.  */
45       for (i = 0;  i < WAITPID_CHILDREN;  i++)
46 	if (! waited_pid[i])
47 	  {
48 	    p = wait (&waited_status[i]);
49 	    if (p < 0)
50 	      return p;
51 	    if (p == pid  ||  pid == -1)
52 	      goto success;
53 	    waited_pid[i] = p;
54 	  }
55     }
56 
57   /* We cannot emulate this wait call, e.g. because of too many children.  */
58   abort ();
59 
60 success:
61   if (stat_loc)
62     *stat_loc = waited_status[i];
63   return p;
64 }
65