Skip to content

Commit b8e73e7

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 b8e73e7

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

M2/Macaulay2/d/scclib.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,19 +219,25 @@ 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+
else if (WIFSIGNALED(status)) { /* whether the process died due to a signal */
226+
#ifdef WCOREDUMP
227+
return WTERMSIG(status) + (WCOREDUMP(status) ? 128 : 0); /* signal number n, plus 128 if core was dumped */
228+
#else
229+
return WTERMSIG(status);
230+
#endif
231+
}
232+
else if (WIFEXITED(status)) { /* whether the process exited */
233+
if ((WEXITSTATUS(status) & 0x80) != 0) /* whether /bin/sh indicates a signal in a command */
234+
return WEXITSTATUS(status) & 0x7f; /* the signal number */
235+
else
236+
return WEXITSTATUS(status) << 8; /* status code times 256 */
237+
}
238+
else
239+
return -2; /* still running (or stopped) */
240+
}
235241

236242
M2_arrayint system_waitNoHang(M2_arrayint pids)
237243
{

0 commit comments

Comments
 (0)