summaryrefslogtreecommitdiff
path: root/errno.c
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2012-06-04 14:14:08 +0100
committerLars Wirzenius <liw@liw.fi>2012-06-04 14:14:08 +0100
commit9433d2b803644098ce690a52e1205c541d09eba5 (patch)
treedddd9c829bec59decdc9937124a9edf8ef901a47 /errno.c
parentd6ef4a470f5ba8b7b36d39cd1cb2418d58257fc0 (diff)
downloadmoreutils-9433d2b803644098ce690a52e1205c541d09eba5.tar.gz
Add errno program
Diffstat (limited to 'errno.c')
-rw-r--r--errno.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/errno.c b/errno.c
new file mode 100644
index 0000000..aa5307b
--- /dev/null
+++ b/errno.c
@@ -0,0 +1,74 @@
+#include <ctype.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+static struct {
+ const char *name;
+ int code;
+} errnos[] = {
+ #include "errnos.h"
+};
+static const int num_errnos = sizeof(errnos) / sizeof(errnos[0]);
+
+
+static void
+report(const char *name, int code)
+{
+ printf("%s %d %s\n", name, code, strerror(code));
+}
+
+
+static bool
+report_from_name(const char *name)
+{
+ int i;
+ for (i = 0; i < num_errnos; ++i) {
+ if (strcasecmp(errnos[i].name, name) == 0) {
+ report(errnos[i].name, errnos[i].code);
+ return true;
+ }
+ }
+ return false;
+}
+
+
+static bool
+report_from_code(int code)
+{
+ int i;
+ for (i = 0; i < num_errnos; ++i) {
+ if (errnos[i].code == code) {
+ report(errnos[i].name, code);
+ return true;
+ }
+ }
+ return false;
+}
+
+
+int
+main(int argc, char **argv)
+{
+ int i;
+ int exit_code;
+
+ exit_code = EXIT_SUCCESS;
+ for (i = 1; i < argc; ++i) {
+ const char *arg = argv[i];
+ if (toupper(arg[0]) == 'E') {
+ if (!report_from_name(arg))
+ exit_code = EXIT_FAILURE;
+ } else if (isdigit(arg[0])) {
+ if (!report_from_code(atoi(arg)))
+ exit_code = EXIT_FAILURE;
+ } else {
+ fprintf(stderr, "ERROR: Not understood: %s\n", arg);
+ exit_code = EXIT_FAILURE;
+ }
+ }
+ return exit_code;
+}