tests: fix unportable assumption on sys/wait.h
[pspp] / tests / test-sys_wait.c
1 /* Test of <sys/wait.h> substitute.
2    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Eric Blake <ebb9@byu.net>, 2009.  */
18
19 #include <config.h>
20
21 #include <sys/wait.h>
22
23 /* Check for existence of required types.  */
24 static pid_t a;
25
26 int
27 main (void)
28 {
29   /* Check for existence of required macros.  Note that we document
30      that these are safe only on lvalues.  */
31   int i;
32   for (i = 0; i < 0x10000; i = (i ? i << 1 : 1))
33     {
34       /* POSIX requires that for all valid process statuses, that
35          exactly one of these three macros is true.  But not all
36          possible 16-bit values map to valid process status.
37          Traditionally, 8 of the bits are for WIFEXITED, 7 of the bits
38          to tell between WIFSIGNALED and WIFSTOPPED, and either 0x80
39          or 0x8000 to flag that core was also dumped.  Since we don't
40          know which byte is WIFEXITED, we skip the both possible bits
41          that can signal core dump.  */
42       if (i == 0x80)
43         continue;
44       if (!!WIFSIGNALED (i) + !!WIFEXITED (i) + !!WIFSTOPPED (i) != 1)
45         return 1;
46     }
47   i = WEXITSTATUS (i) + WSTOPSIG (i) + WTERMSIG (i);
48
49   switch (i)
50     {
51 #if 0
52   /* Gnulib doesn't guarantee these, yet.  */
53     case WCONTINUED:
54     case WNOHANG:
55     case WUNTRACED:
56     case WEXITED:
57     case WNOWAIT:
58     case WSTOPPED:
59 #endif
60       break;
61     }
62   return a ? i : 0;
63 }