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(-) 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