]> err.no Git - moreutils/commitdiff
Add --list option to errno
authorLars Wirzenius <liw@liw.fi>
Tue, 5 Jun 2012 13:58:32 +0000 (14:58 +0100)
committerLars Wirzenius <liw@liw.fi>
Tue, 5 Jun 2012 13:58:32 +0000 (14:58 +0100)
errno.c
errno.docbook

diff --git a/errno.c b/errno.c
index 3644c8fb8e3ea09eb979fb9536dc168d43d79699..f1e2285fd56378bc1898117e6c22994c9a708c48 100644 (file)
--- 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;
 }
index 0ddaba1328a4d1e9bef7dd59888b031bd287f2ff..0f3c241c5c8af0030b2dc7cc160f99f2aa5346f4 100644 (file)
@@ -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>