tests: fix unportable assumption on sys/wait.h
[pspp] / tests / test-stdlib.c
1 /* Test of <stdlib.h> substitute.
2    Copyright (C) 2007, 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 Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include <stdlib.h>
22
23 #include "verify.h"
24
25 /* Check that EXIT_SUCCESS is 0, per POSIX.  */
26 static int exitcode = EXIT_SUCCESS;
27 #if EXIT_SUCCESS
28 "oops"
29 #endif
30
31 /* Check for GNU value (not guaranteed by POSIX, but is guaranteed by
32    gnulib).  */
33 #if EXIT_FAILURE != 1
34 "oops"
35 #endif
36
37 /* Check that NULL can be passed through varargs as a pointer type,
38    per POSIX 2008.  */
39 verify (sizeof NULL == sizeof (void *));
40
41 int
42 main (void)
43 {
44   /* Check subset of <sys/wait.h> macros that must be visible here.
45      Note that some of these macros are only portable when operating
46      on an lvalue.  */
47   int i;
48   for (i = 0; i < 0x8000; i = (i ? i << 1 : 1))
49     {
50       /* POSIX requires that for all valid process statuses, that
51          exactly one of these three macros is true.  But not all
52          possible 16-bit values map to valid process status.
53          Traditionally, 8 of the bits are for WIFEXITED, 7 of the bits
54          to tell between WIFSIGNALED and WIFSTOPPED, and either 0x80
55          or 0x8000 to flag that core was also dumped.  Since we don't
56          know which byte is WIFEXITED, we skip the both possible bits
57          that can signal core dump.  */
58       if (i == 0x80)
59         continue;
60       if (!!WIFSIGNALED (i) + !!WIFEXITED (i) + !!WIFSTOPPED (i) != 1)
61         return 1;
62     }
63   i = WEXITSTATUS (i) + WSTOPSIG (i) + WTERMSIG (i);
64
65   switch (i)
66     {
67 #if 0
68   /* Gnulib doesn't guarantee these, yet.  */
69     case WNOHANG:
70     case WUNTRACED:
71 #endif
72       break;
73     }
74   return exitcode;
75 }