summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2012-06-05 14:58:32 +0100
committerLars Wirzenius <liw@liw.fi>2012-06-05 14:58:32 +0100
commit3ebe87776f38e07343d79c14bc2dad3560bf5f8f (patch)
tree3f5a2e600e85196ea677588a083f959cc0fcbd8a
parent66a2a6ace9fb99096c2a1d0144a7b12895db59e6 (diff)
Add --list option to errno
-rw-r--r--errno.c60
-rw-r--r--errno.docbook5
2 files changed, 55 insertions, 10 deletions
diff --git a/errno.c b/errno.c
index 3644c8f..f1e2285 100644
--- a/errno.c
+++ b/errno.c
@@ -24,6 +24,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
+#include <getopt.h>
static struct {
@@ -70,25 +72,63 @@ report_from_code(int code)
}
+static struct option
+options[] = {
+ { "list", 0, NULL, 'l' },
+};
+
+
int
main(int argc, char **argv)
{
int i;
int exit_code;
+ int index = 0;
+ enum { lookup_mode, list_mode } mode = lookup_mode;
+ for (;;) {
+ int c = getopt_long(argc, argv, "l", options, &index);
+ if (c == -1)
+ break;
+
+ switch (c) {
+ case 'l':
+ mode = list_mode;
+ break;
+
+ case '?':
+ break;
+
+ default:
+ fprintf(stderr, "getopt returned 0x%02x\n", c);
+ return EXIT_FAILURE;
+ }
+ }
+
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)))
+
+ switch (mode) {
+ case lookup_mode:
+ 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;
- } else {
- fprintf(stderr, "ERROR: Not understood: %s\n", arg);
- exit_code = EXIT_FAILURE;
+ }
}
+ break;
+
+ case list_mode:
+ for (i = 0; i < num_errnos; ++i)
+ report(errnos[i].name, errnos[i].code);
+ break;
}
+
return exit_code;
}
diff --git a/errno.docbook b/errno.docbook
index 0ddaba1..0f3c241 100644
--- a/errno.docbook
+++ b/errno.docbook
@@ -51,6 +51,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<command>errno</command>
<arg choice="req"><replaceable>name-or-code</replaceable></arg>
</cmdsynopsis>
+ <cmdsynopsis>
+ <command>errno</command>
+ <arg>-l</arg>
+ <arg>--list</arg>
+ </cmdsynopsis>
</refsynopsisdiv>
<refsect1>