summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2012-06-05 15:11:37 +0100
committerLars Wirzenius <liw@liw.fi>2012-06-05 15:11:37 +0100
commitb876a119c477530b556e491c1a923212c3b0a67c (patch)
treef673c7176641ff69969a4340350d025c2ab65cd9
parent563a442d63bab474b554453c6c3ccf10e3a5b48d (diff)
Add --search option to errno
-rw-r--r--errno.c44
-rw-r--r--errno.docbook9
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 <ctype.h>
#include <errno.h>
@@ -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.,
<para>List all errno values.</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><option>-s</option></term>
+ <term><option>--search</option></term>
+ <listitem>
+ <para>Search for errors whose description contains
+ all the given words (case-insensitive).</para>
+ </listitem>
+ </varlistentry>
</variablelist>
</refsect1>