1 /* Definitions of status bits for `wait' et al.
2    Copyright (C) 1992,1994,1996,1997,2000,2004 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18 
19 #if !defined _SYS_WAIT_H && !defined _STDLIB_H
20 # error "Never include <bits/waitstatus.h> directly; use <sys/wait.h> instead."
21 #endif
22 
23 
24 /* Everything extant so far uses these same bits.  */
25 
26 
27 /* If WIFEXITED(STATUS), the low-order 8 bits of exit(N).  */
28 #define	__WEXITSTATUS(status)	(((status) & 0xff00) >> 8)
29 
30 /* If WIFSIGNALED(STATUS), the terminating signal.  */
31 #define	__WTERMSIG(status)	((status) & 0x7f)
32 
33 /* If WIFSTOPPED(STATUS), the signal that stopped the child.  */
34 #define	__WSTOPSIG(status)	__WEXITSTATUS(status)
35 
36 /* Nonzero if STATUS indicates normal termination.  */
37 #define	__WIFEXITED(status)	(__WTERMSIG(status) == 0)
38 
39 /* Nonzero if STATUS indicates termination by a signal.
40  * Note that status 0x007f is "died from signal 127", not "stopped by signal 0".
41  * This does happen on MIPS.
42  * The comparison is "< 0xff", not "< 0x7f", because WCOREDUMP bit (0x80)
43  * can be set too.
44  */
45 #define	__WIFSIGNALED(status)	(((unsigned)((status) & 0xffff) - 1U) < 0xffU)
46 
47 /* Nonzero if STATUS indicates the child is stopped.  */
48 #if !defined(__mips__)
49 #define	__WIFSTOPPED(status)	(((status) & 0xff) == 0x7f)
50 #else
51 #define	__WIFSTOPPED(status)	(((status) & 0xff) == 0x7f && ((status) & 0xff00))
52 #endif
53 
54 /* Nonzero if STATUS indicates the child continued after a stop.  We only
55    define this if <bits/waitflags.h> provides the WCONTINUED flag bit.  */
56 #ifdef WCONTINUED
57 # define __WIFCONTINUED(status)	((status) == __W_CONTINUED)
58 #endif
59 
60 /* Nonzero if STATUS indicates the child dumped core.  */
61 #define	__WCOREDUMP(status)	((status) & __WCOREFLAG)
62 
63 /* Macros for constructing status values.  */
64 #define	__W_EXITCODE(ret, sig)	((ret) << 8 | (sig))
65 #define	__W_STOPCODE(sig)	((sig) << 8 | 0x7f)
66 #define __W_CONTINUED		0xffff
67 #define	__WCOREFLAG		0x80
68 
69 
70 #ifdef	__USE_BSD
71 
72 # include <endian.h>
73 
74 union wait
75   {
76     int w_status;
77     struct
78       {
79 # if	__BYTE_ORDER == __LITTLE_ENDIAN
80 	unsigned int __w_termsig:7; /* Terminating signal.  */
81 	unsigned int __w_coredump:1; /* Set if dumped core.  */
82 	unsigned int __w_retcode:8; /* Return code if exited normally.  */
83 	unsigned int:16;
84 # endif				/* Little endian.  */
85 # if	__BYTE_ORDER == __BIG_ENDIAN
86 	unsigned int:16;
87 	unsigned int __w_retcode:8;
88 	unsigned int __w_coredump:1;
89 	unsigned int __w_termsig:7;
90 # endif				/* Big endian.  */
91       } __wait_terminated;
92     struct
93       {
94 # if	__BYTE_ORDER == __LITTLE_ENDIAN
95 	unsigned int __w_stopval:8; /* W_STOPPED if stopped.  */
96 	unsigned int __w_stopsig:8; /* Stopping signal.  */
97 	unsigned int:16;
98 # endif				/* Little endian.  */
99 # if	__BYTE_ORDER == __BIG_ENDIAN
100 	unsigned int:16;
101 	unsigned int __w_stopsig:8; /* Stopping signal.  */
102 	unsigned int __w_stopval:8; /* W_STOPPED if stopped.  */
103 # endif				/* Big endian.  */
104       } __wait_stopped;
105   };
106 
107 # define w_termsig	__wait_terminated.__w_termsig
108 # define w_coredump	__wait_terminated.__w_coredump
109 # define w_retcode	__wait_terminated.__w_retcode
110 # define w_stopsig	__wait_stopped.__w_stopsig
111 # define w_stopval	__wait_stopped.__w_stopval
112 
113 #endif	/* Use BSD.  */
114