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