Skip to content

Commit 1a6cfd7

Browse files
committed
Rewrite fix_status for readability
So many ternary conditional operators... Also put #ifdef's around the non-POSIX WCOREDUMP as recommended in the waitpid manpage.
1 parent d6d3c59 commit 1a6cfd7

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

M2/Macaulay2/d/scclib.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,19 +219,32 @@ int system_strnumcmp(M2_string s,M2_string t) {
219219
}
220220

221221
int fix_status(int status) {
222-
/* We can't handle status codes bigger than 127 if the shell intervenes. */
223-
return
224-
status == ERROR ? ERROR :
225-
WIFSIGNALED(status) ? /* whether the process died due to a signal */
226-
WTERMSIG(status) + (WCOREDUMP(status) ? 128 : 0) : /* signal number n, plus 128 if core was dumped */
227-
WIFEXITED(status) ? /* whether the process exited */
228-
(
229-
((WEXITSTATUS(status) & 0x80) != 0) ? /* whether /bin/sh indicates a signal in a command */
230-
(WEXITSTATUS(status) & 0x7f) : /* the signal number */
231-
(WEXITSTATUS(status) << 8) /* status code times 256 */
232-
) :
233-
-2; /* still running (or stopped) */
234-
}
222+
/* We can't handle status codes bigger than 127 if the shell intervenes. */
223+
if (status == ERROR)
224+
return ERROR;
225+
/* whether the process died due to a signal */
226+
else if (WIFSIGNALED(status)) {
227+
#ifdef WCOREDUMP
228+
/* signal number n, plus 128 if core was dumped */
229+
return WTERMSIG(status) + (WCOREDUMP(status) ? 128 : 0);
230+
#else
231+
return WTERMSIG(status);
232+
#endif
233+
}
234+
/* whether the process exited */
235+
else if (WIFEXITED(status)) {
236+
/* whether /bin/sh indicates a signal in a command */
237+
if ((WEXITSTATUS(status) & 0x80) != 0)
238+
/* the signal number */
239+
return WEXITSTATUS(status) & 0x7f;
240+
else
241+
/* status code times 256 */
242+
return WEXITSTATUS(status) << 8;
243+
}
244+
else
245+
/* still running (or stopped) */
246+
return -2;
247+
}
235248

236249
M2_arrayint system_waitNoHang(M2_arrayint pids)
237250
{

0 commit comments

Comments
 (0)