From: Lars Wirzenius Date: Tue, 5 Jun 2012 13:58:32 +0000 (+0100) Subject: Add --list option to errno X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3ebe87776f38e07343d79c14bc2dad3560bf5f8f;p=moreutils Add --list option to errno --- diff --git a/errno.c b/errno.c index 3644c8f..f1e2285 100644 --- a/errno.c +++ b/errno.c @@ -24,6 +24,8 @@ #include #include #include +#include +#include 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., errno name-or-code + + errno + -l + --list +