summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Braun <rbraun@sceen.net>2018-01-04 00:32:47 +0100
committerRichard Braun <rbraun@sceen.net>2018-01-04 00:32:47 +0100
commitc1ebfc97887a8efbd02b25379cc40d1086d0ad46 (patch)
treeee2d5ab7e529d90ee35940cb748b4ec270a19476
parenta0568115cdc9e6aa14820c5553f45f6ec701fb0c (diff)
error: replace error_die with error_check
-rw-r--r--error.c13
-rw-r--r--error.h10
-rw-r--r--test/test_shell.c7
3 files changed, 16 insertions, 14 deletions
diff --git a/error.c b/error.c
index 997f5c9..8da0005 100644
--- a/error.c
+++ b/error.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2009-2015 Richard Braun.
+ * Copyright (c) 2009-2018 Richard Braun.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -86,8 +86,15 @@ error_from_errno(int errno_code)
}
void
-error_die(unsigned int error)
+error_check(int error, const char *prefix)
{
- fprintf(stderr, "process terminating, reason: %s\n", error_str(error));
+ if (!error) {
+ return;
+ }
+
+ fprintf(stderr, "%s%s%s\n",
+ (prefix == NULL) ? "" : prefix,
+ (prefix == NULL) ? "" : ": ",
+ error_str(error));
abort();
}
diff --git a/error.h b/error.h
index 63812be..7579eea 100644
--- a/error.h
+++ b/error.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2009-2015 Richard Braun.
+ * Copyright (c) 2009-2018 Richard Braun.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -74,11 +74,9 @@ const char * error_str(unsigned int error);
unsigned int error_from_errno(int errno_code);
/*
- * Exit the current process, reporting an error.
- *
- * This function will report the given error and make the process exit,
- * using the error code as the exit() parameter.
+ * If error denotes an actual error (i.e. is not 0), abort, using the given
+ * string as a prefix for the error message. A NULL prefix is allowed.
*/
-noreturn void error_die(unsigned int error);
+void error_check(int error, const char *prefix);
#endif /* _ERROR_H */
diff --git a/test/test_shell.c b/test/test_shell.c
index 0b63ac0..ee81b20 100644
--- a/test/test_shell.c
+++ b/test/test_shell.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2015 Richard Braun.
+ * Copyright (c) 2015-2018 Richard Braun.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -95,10 +95,7 @@ main(int argc, char *argv[])
for (i = 0; i < ARRAY_SIZE(test_shell_cmds); i++) {
ret = shell_cmd_register(&test_shell_cmds[i]);
-
- if (ret) {
- error_die(ret);
- }
+ error_check(ret, "shell_cmd_register");
}
setbuf(stdin, NULL);