mirror of
https://github.com/troglobit/libite.git
synced 2026-10-03 14:13:11 +07:00
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 <troglobit@gmail.com>
66 lines
1.5 KiB
C
66 lines
1.5 KiB
C
/* Formatted system() which returns actual return status of command
|
|
*
|
|
* Copyright (c) 2021 Joachim Wiberg <troglobit@gmail.com>
|
|
*
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
* copyright notice and this permission notice appear in all copies.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
|
|
#include <errno.h>
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/wait.h>
|
|
|
|
int systemf(const char *fmt, ...)
|
|
{
|
|
va_list ap;
|
|
char *cmd;
|
|
int len;
|
|
int rc;
|
|
|
|
va_start(ap, fmt);
|
|
len = vsnprintf(NULL, 0, fmt, ap);
|
|
va_end(ap);
|
|
|
|
cmd = alloca(++len);
|
|
if (!cmd) {
|
|
errno = ENOMEM;
|
|
return -1;
|
|
}
|
|
|
|
va_start(ap, fmt);
|
|
vsnprintf(cmd, len, fmt, ap);
|
|
va_end(ap);
|
|
|
|
rc = system(cmd);
|
|
if (rc == -1)
|
|
return -1;
|
|
|
|
if (WIFEXITED(rc)) {
|
|
errno = 0;
|
|
rc = WEXITSTATUS(rc);
|
|
} else if (WIFSIGNALED(rc)) {
|
|
errno = EINTR;
|
|
rc = -1;
|
|
}
|
|
|
|
return rc;
|
|
}
|
|
|
|
/**
|
|
* Local Variables:
|
|
* indent-tabs-mode: t
|
|
* c-file-style: "linux"
|
|
* End:
|
|
*/
|