From 3ebe87776f38e07343d79c14bc2dad3560bf5f8f Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 5 Jun 2012 14:58:32 +0100 Subject: Add --list option to errno --- errno.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 10 deletions(-) (limited to 'errno.c') 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; } -- cgit v1.2.3 From 563a442d63bab474b554453c6c3ccf10e3a5b48d Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 5 Jun 2012 15:03:26 +0100 Subject: Add --help and add --list to manpage --- errno.c | 12 ++++++++++++ errno.docbook | 15 +++++++++++++++ 2 files changed, 27 insertions(+) (limited to 'errno.c') diff --git a/errno.c b/errno.c index f1e2285..3b981ce 100644 --- a/errno.c +++ b/errno.c @@ -74,10 +74,18 @@ report_from_code(int code) static struct option options[] = { + { "help", 0, NULL, 'h' }, { "list", 0, NULL, 'l' }, }; +static void +usage(void) +{ + printf("Usage: errno [-l] [--list] [keyword]\n"); +} + + int main(int argc, char **argv) { @@ -92,6 +100,10 @@ main(int argc, char **argv) break; switch (c) { + case 'h': + usage(); + return EXIT_SUCCESS; + case 'l': mode = list_mode; break; diff --git a/errno.docbook b/errno.docbook index 0f3c241..64fd488 100644 --- a/errno.docbook +++ b/errno.docbook @@ -70,6 +70,21 @@ with this program; if not, write to the Free Software Foundation, Inc., + + OPTIONS + + + + + + + List all errno values. + + + + + + AUTHOR Lars Wirzenius -- cgit v1.2.3 From b876a119c477530b556e491c1a923212c3b0a67c Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 5 Jun 2012 15:11:37 +0100 Subject: Add --search option to errno --- errno.c | 44 ++++++++++++++++++++++++++++++++++++++++---- errno.docbook | 9 +++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) (limited to 'errno.c') diff --git a/errno.c b/errno.c index 3b981ce..64a4770 100644 --- a/errno.c +++ b/errno.c @@ -17,6 +17,7 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#define _GNU_SOURCE #include #include @@ -72,17 +73,44 @@ report_from_code(int code) } +static bool +matches(int code, int num_words, char **words) +{ + const char *text = strerror(code); + int i; + + for (i = 0; i < num_words; ++i) { + if (strcasestr(text, words[i]) == NULL) + return false; + } + return true; +} + + +static void +search(int num_words, char **words) +{ + int i; + + for (i = 0; i < num_errnos; ++i) { + if (matches(errnos[i].code, num_words, words)) + report(errnos[i].name, errnos[i].code); + } +} + + static struct option options[] = { { "help", 0, NULL, 'h' }, { "list", 0, NULL, 'l' }, + { "search", 0, NULL, 's' }, }; static void usage(void) { - printf("Usage: errno [-l] [--list] [keyword]\n"); + printf("Usage: errno [-ls] [--list] [--search] [keyword]\n"); } @@ -92,10 +120,10 @@ main(int argc, char **argv) int i; int exit_code; int index = 0; - enum { lookup_mode, list_mode } mode = lookup_mode; + enum { lookup_mode, list_mode, search_mode } mode = lookup_mode; for (;;) { - int c = getopt_long(argc, argv, "l", options, &index); + int c = getopt_long(argc, argv, "hls", options, &index); if (c == -1) break; @@ -107,6 +135,10 @@ main(int argc, char **argv) case 'l': mode = list_mode; break; + + case 's': + mode = search_mode; + break; case '?': break; @@ -121,7 +153,7 @@ main(int argc, char **argv) switch (mode) { case lookup_mode: - for (i = 1; i < argc; ++i) { + for (i = optind; i < argc; ++i) { const char *arg = argv[i]; if (toupper(arg[0]) == 'E') { if (!report_from_name(arg)) @@ -140,6 +172,10 @@ main(int argc, char **argv) for (i = 0; i < num_errnos; ++i) report(errnos[i].name, errnos[i].code); break; + + case search_mode: + search(argc - optind, argv + optind); + break; } return exit_code; diff --git a/errno.docbook b/errno.docbook index 64fd488..01a56bf 100644 --- a/errno.docbook +++ b/errno.docbook @@ -81,6 +81,15 @@ with this program; if not, write to the Free Software Foundation, Inc., List all errno values. + + + + + + Search for errors whose description contains + all the given words (case-insensitive). + + -- cgit v1.2.3 From 67f22bd1b6c2acfa5d23fa1821a0c9e895d085eb Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 5 Jun 2012 15:14:07 +0100 Subject: Make errno obey locale --- errno.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'errno.c') diff --git a/errno.c b/errno.c index 64a4770..872d11a 100644 --- a/errno.c +++ b/errno.c @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -122,6 +123,8 @@ main(int argc, char **argv) int index = 0; enum { lookup_mode, list_mode, search_mode } mode = lookup_mode; + setlocale(LC_ALL, ""); + for (;;) { int c = getopt_long(argc, argv, "hls", options, &index); if (c == -1) -- cgit v1.2.3