--- a/contrib/chg/util.c Thu Jan 24 11:35:40 2019 -0500
+++ b/contrib/chg/util.c Thu Jan 24 10:21:59 2019 -0500
@@ -25,8 +25,9 @@
static inline void fsetcolor(FILE *fp, const char *code)
{
- if (!colorenabled)
+ if (!colorenabled) {
return;
+ }
fprintf(fp, "\033[%sm", code);
}
@@ -35,8 +36,9 @@
fsetcolor(stderr, "1;31");
fputs("chg: abort: ", stderr);
vfprintf(stderr, fmt, args);
- if (no != 0)
+ if (no != 0) {
fprintf(stderr, " (errno = %d, %s)", no, strerror(no));
+ }
fsetcolor(stderr, "");
fputc('\n', stderr);
exit(255);
@@ -82,8 +84,9 @@
void debugmsg(const char *fmt, ...)
{
- if (!debugmsgenabled)
+ if (!debugmsgenabled) {
return;
+ }
va_list args;
va_start(args, fmt);
@@ -98,32 +101,37 @@
void fchdirx(int dirfd)
{
int r = fchdir(dirfd);
- if (r == -1)
+ if (r == -1) {
abortmsgerrno("failed to fchdir");
+ }
}
void fsetcloexec(int fd)
{
int flags = fcntl(fd, F_GETFD);
- if (flags < 0)
+ if (flags < 0) {
abortmsgerrno("cannot get flags of fd %d", fd);
- if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
+ }
+ if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
abortmsgerrno("cannot set flags of fd %d", fd);
+ }
}
void *mallocx(size_t size)
{
void *result = malloc(size);
- if (!result)
+ if (!result) {
abortmsg("failed to malloc");
+ }
return result;
}
void *reallocx(void *ptr, size_t size)
{
void *result = realloc(ptr, size);
- if (!result)
+ if (!result) {
abortmsg("failed to realloc");
+ }
return result;
}
@@ -144,30 +152,37 @@
memset(&newsa, 0, sizeof(newsa));
newsa.sa_handler = SIG_IGN;
newsa.sa_flags = 0;
- if (sigemptyset(&newsa.sa_mask) < 0)
+ if (sigemptyset(&newsa.sa_mask) < 0) {
goto done;
- if (sigaction(SIGINT, &newsa, &oldsaint) < 0)
+ }
+ if (sigaction(SIGINT, &newsa, &oldsaint) < 0) {
goto done;
+ }
doneflags |= F_SIGINT;
- if (sigaction(SIGQUIT, &newsa, &oldsaquit) < 0)
+ if (sigaction(SIGQUIT, &newsa, &oldsaquit) < 0) {
goto done;
+ }
doneflags |= F_SIGQUIT;
- if (sigaddset(&newsa.sa_mask, SIGCHLD) < 0)
+ if (sigaddset(&newsa.sa_mask, SIGCHLD) < 0) {
goto done;
- if (sigprocmask(SIG_BLOCK, &newsa.sa_mask, &oldmask) < 0)
+ }
+ if (sigprocmask(SIG_BLOCK, &newsa.sa_mask, &oldmask) < 0) {
goto done;
+ }
doneflags |= F_SIGMASK;
pid_t pid = fork();
- if (pid < 0)
+ if (pid < 0) {
goto done;
+ }
if (pid == 0) {
sigaction(SIGINT, &oldsaint, NULL);
sigaction(SIGQUIT, &oldsaquit, NULL);
sigprocmask(SIG_SETMASK, &oldmask, NULL);
- if (cwd && chdir(cwd) < 0)
+ if (cwd && chdir(cwd) < 0) {
_exit(127);
+ }
const char *argv[] = {"sh", "-c", cmd, NULL};
if (envp) {
execve("/bin/sh", (char **)argv, (char **)envp);
@@ -176,25 +191,32 @@
}
_exit(127);
} else {
- if (waitpid(pid, &status, 0) < 0)
+ if (waitpid(pid, &status, 0) < 0) {
goto done;
+ }
doneflags |= F_WAITPID;
}
done:
- if (doneflags & F_SIGINT)
+ if (doneflags & F_SIGINT) {
sigaction(SIGINT, &oldsaint, NULL);
- if (doneflags & F_SIGQUIT)
+ }
+ if (doneflags & F_SIGQUIT) {
sigaction(SIGQUIT, &oldsaquit, NULL);
- if (doneflags & F_SIGMASK)
+ }
+ if (doneflags & F_SIGMASK) {
sigprocmask(SIG_SETMASK, &oldmask, NULL);
+ }
/* no way to report other errors, use 127 (= shell termination) */
- if (!(doneflags & F_WAITPID))
+ if (!(doneflags & F_WAITPID)) {
return 127;
- if (WIFEXITED(status))
+ }
+ if (WIFEXITED(status)) {
return WEXITSTATUS(status);
- if (WIFSIGNALED(status))
+ }
+ if (WIFSIGNALED(status)) {
return -WTERMSIG(status);
+ }
return 127;
}