aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2021-07-30 15:03:21 -0700
committerBruce Hill <bruce@bruce-hill.com>2021-07-30 15:03:21 -0700
commit3e63da180bc6cbce7c0673419a1a4a0f021591bb (patch)
tree2e0a048c193eb7faf0bb0aff32b0181cb3efc6f5
parent33a63bb8d9e56424140d4b546bad5d10a3da97aa (diff)
Cleaner checking of stdlib negative returns
-rw-r--r--bp.c35
-rw-r--r--files.c10
-rw-r--r--utils.c15
-rw-r--r--utils.h1
4 files changed, 31 insertions, 30 deletions
diff --git a/bp.c b/bp.c
index 64db5e4..925fe31 100644
--- a/bp.c
+++ b/bp.c
@@ -311,9 +311,7 @@ static int inplace_modify_file(def_t *defs, file_t *f, pat_t *pattern)
exit(EXIT_FAILURE);
// Lazy-open file for writing upon first match:
if (dest == NULL) {
- dest = fopen(f->filename, "w");
- if (!dest)
- err(EXIT_FAILURE, "Failed to open %s for modification", f->filename);
+ dest = check_nonnull(fopen(f->filename, "w"), "Failed to open %s for modification", f->filename);
backup_file = f;
modifying_file = dest;
if (options.confirm == CONFIRM_ASK && f->filename)
@@ -428,7 +426,7 @@ static int process_dir(def_t *defs, const char *dirname, pat_t *pattern)
errx(EXIT_FAILURE, "Filename is too long: %s/*", dirname);
int status = glob(globpath, 0, NULL, &globbuf);
if (status == GLOB_ABORTED || status == GLOB_NOSPACE)
- err(EXIT_FAILURE, "Failed to get directory contents: %s", dirname);
+ errx(EXIT_FAILURE, "Failed to get directory contents: %s", dirname);
if (status != GLOB_NOMATCH) {
struct stat statbuf;
for (size_t i = 0; i < globbuf.gl_pathc; i++) {
@@ -452,29 +450,21 @@ __attribute__((nonnull(2)))
static int process_git_files(def_t *defs, pat_t *pattern, int argc, char *argv[])
{
int fds[2];
- if (pipe(fds) != 0)
- err(EXIT_FAILURE, "Failed to create pipe");
- pid_t child = fork();
- if (child == -1)
- err(EXIT_FAILURE, "Failed to fork");
+ check_nonnegative(pipe(fds), "Failed to create pipe");
+ pid_t child = check_nonnegative(fork(), "Failed to fork");
if (child == 0) {
const char **git_args = new(char*[2+argc+1]);
int g = 0;
git_args[g++] = "git";
git_args[g++] = "ls-files";
while (*argv) git_args[g++] = *(argv++);
- if (dup2(fds[STDOUT_FILENO], STDOUT_FILENO) != STDOUT_FILENO)
- err(EXIT_FAILURE, "Failed to hook up pipe to stdout");
- if (close(fds[STDIN_FILENO]) != 0)
- err(EXIT_FAILURE, "Failed to close read end of pipe");
+ check_nonnegative(dup2(fds[STDOUT_FILENO], STDOUT_FILENO), "Failed to hook up pipe to stdout");
+ check_nonnegative(close(fds[STDIN_FILENO]), "Failed to close read end of pipe");
(void)execvp("git", (char**)git_args);
_exit(EXIT_FAILURE);
}
- if (close(fds[STDOUT_FILENO]) != 0)
- err(EXIT_FAILURE, "Failed to close write end of pipe");
- FILE *fp = fdopen(fds[STDIN_FILENO], "r");
- if (fp == NULL)
- err(EXIT_FAILURE, "Could not open file descriptor");
+ check_nonnegative(close(fds[STDOUT_FILENO]), "Failed to close write end of pipe");
+ FILE *fp = check_nonnull(fdopen(fds[STDIN_FILENO], "r"), "Could not open pipe file descriptor");
char *path = NULL;
size_t size = 0;
ssize_t len = 0;
@@ -484,8 +474,7 @@ static int process_git_files(def_t *defs, pat_t *pattern, int argc, char *argv[]
found += process_file(defs, path, pattern);
}
if (path) xfree(&path);
- if (fclose(fp) != 0)
- err(EXIT_FAILURE, "Failed to close read end of pipe");
+ check_nonnegative(fclose(fp), "Failed to close read end of pipe");
int status;
while (waitpid(child, &status, 0) != child) continue;
if (!((WIFEXITED(status) == 1) && (WEXITSTATUS(status) == 0)))
@@ -627,12 +616,10 @@ int main(int argc, char *argv[])
int signals[] = {SIGTERM, SIGINT, SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF, SIGSEGV, SIGTSTP};
struct sigaction sa = {.sa_handler = &sig_handler, .sa_flags = (int)(SA_NODEFER | SA_RESETHAND)};
for (size_t i = 0; i < sizeof(signals)/sizeof(signals[0]); i++)
- if (sigaction(signals[i], &sa, NULL) != 0)
- err(EXIT_FAILURE, "Failed to set signal handler");
+ check_nonnegative(sigaction(signals[i], &sa, NULL), "Failed to set signal handler");
// Handle exit() calls gracefully:
- if (atexit(&cleanup) != 0)
- err(EXIT_FAILURE, "Failed to set cleanup handler at exit");
+ check_nonnegative(atexit(&cleanup), "Failed to set cleanup handler at exit");
// User input/output is handled through /dev/tty so that normal unix pipes
// can work properly while simultaneously asking for user input.
diff --git a/files.c b/files.c
index 202c88d..223079b 100644
--- a/files.c
+++ b/files.c
@@ -118,10 +118,8 @@ file_t *load_file(file_t **files, const char *filename)
}
finished_loading:
- if (fd != STDIN_FILENO) {
- if (close(fd) != 0)
- err(EXIT_FAILURE, "Failed to close file");
- }
+ if (fd != STDIN_FILENO)
+ check_nonnegative(close(fd), "Failed to close file");
f->start = &f->memory[0];
f->end = &f->memory[length];
populate_lines(f, length);
@@ -179,8 +177,8 @@ void destroy_file(file_t **f)
if ((*f)->memory) {
if ((*f)->mmapped) {
- if (munmap((*f)->memory, (size_t)((*f)->end - (*f)->memory)) != 0)
- err(EXIT_FAILURE, "Failure to un-memory-map some memory");
+ check_nonnegative(munmap((*f)->memory, (size_t)((*f)->end - (*f)->memory)),
+ "Failure to un-memory-map some memory");
(*f)->memory = NULL;
} else {
xfree(&((*f)->memory));
diff --git a/utils.c b/utils.c
index 578b59c..60e12eb 100644
--- a/utils.c
+++ b/utils.c
@@ -140,6 +140,21 @@ void *check_nonnull(void *p, const char *err_msg, ...)
}
//
+// If the given argument is negative, print the error message and exit with
+// failure. Otherwise return the given argument.
+//
+int check_nonnegative(int i, const char *err_msg, ...)
+{
+ if (i < 0) {
+ va_list args;
+ va_start(args, err_msg);
+ verr(EXIT_FAILURE, err_msg, args);
+ va_end(args);
+ }
+ return i;
+}
+
+//
// Case-insensitive memory comparison
//
int memicmp(const void *v1, const void *v2, size_t n)
diff --git a/utils.h b/utils.h
index c029ca5..cfa0a0d 100644
--- a/utils.h
+++ b/utils.h
@@ -33,6 +33,7 @@ __attribute__((nonnull))
bool matchstr(const char **str, const char *target);
__attribute__((returns_nonnull))
void *check_nonnull(void *p, const char *err_msg, ...);
+int check_nonnegative(int i, const char *err_msg, ...);
__attribute__((nonnull))
int memicmp(const void *s1, const void *s2, size_t n);
__attribute__((nonnull))