From dc5c4ae36cdb53a2daf2949c5c844362e180c906 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sat, 27 Mar 2021 08:48:09 +0100 Subject: [PATCH] systemf(): simplify further, exited OK or was signaled Coverity highlighted a problem with logically dead code from the change prior that triggered this refactor. When system() returns we may have exited either normally or due to a signal. My reasoning behind systemf() is to allow users to treat external calls as any other syscall, abstracting away the WIFFOO() macros. This leaves us with either the return value of the external program >= 0, or -1 and errno for everything else. I.e., if fork() or alloca() fails we may get ENOMEM and if the program doesn't catch signals properly, EINTR. Signed-off-by: Joachim Wiberg --- src/systemf.c | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/src/systemf.c b/src/systemf.c index 979c634..649580e 100644 --- a/src/systemf.c +++ b/src/systemf.c @@ -25,8 +25,8 @@ int systemf(const char *fmt, ...) { va_list ap; char *cmd; - int len, status; - int rc = -1; + int len; + int rc; va_start(ap, fmt); len = vsnprintf(NULL, 0, fmt, ap); @@ -42,26 +42,17 @@ int systemf(const char *fmt, ...) vsnprintf(cmd, len, fmt, ap); va_end(ap); - status = system(cmd); - if (status == -1) + rc = system(cmd); + if (rc == -1) return -1; - if (!WIFEXITED(status)) { - if (WIFSIGNALED(status) && - (WTERMSIG(status) == SIGINT || - WTERMSIG(status) == SIGQUIT)) { - errno = EINTR; - rc = -1; - } else if (!rc) { - /* - * Alert callee that command didn't complete - * successfully. Some programs don't change - * their exit code when signaled. - */ - rc = 1; - } - } else - rc = WEXITSTATUS(status); + if (WIFEXITED(rc)) { + errno = 0; + rc = WEXITSTATUS(rc); + } else if (WIFSIGNALED(rc)) { + errno = EINTR; + rc = -1; + } return rc; }