2 * errno.c -- look up errno names and descriptions
3 * Copyright 2012 Lars Wirzenius (liw@iki.fi)
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 * Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 static const int num_errnos
= sizeof(errnos
) / sizeof(errnos
[0]);
43 report(const char *name
, int code
)
45 printf("%s %d %s\n", name
, code
, strerror(code
));
50 report_from_name(const char *name
)
53 for (i
= 0; i
< num_errnos
; ++i
) {
54 if (strcasecmp(errnos
[i
].name
, name
) == 0) {
55 report(errnos
[i
].name
, errnos
[i
].code
);
64 report_from_code(int code
)
67 for (i
= 0; i
< num_errnos
; ++i
) {
68 if (errnos
[i
].code
== code
) {
69 report(errnos
[i
].name
, code
);
78 matches(int code
, int num_words
, char **words
)
80 const char *text
= strerror(code
);
83 for (i
= 0; i
< num_words
; ++i
) {
84 if (strcasestr(text
, words
[i
]) == NULL
)
92 search(int num_words
, char **words
)
96 for (i
= 0; i
< num_errnos
; ++i
) {
97 if (matches(errnos
[i
].code
, num_words
, words
))
98 report(errnos
[i
].name
, errnos
[i
].code
);
104 search_all(int num_words
, char **words
)
108 /* Static buffers are ugly, but they're simple. If anyone has a
109 locale name longer than a kilobyte, they will suffer, and they
110 will complain, and then I will fix this. */
113 f
= popen("locale -a", "r");
115 fprintf(stderr
, "ERROR: Can't execute locale -a: %d: %s\n",
116 errno
, strerror(errno
));
120 while (fgets(line
, sizeof line
, f
) != NULL
) {
121 line
[strcspn(line
, "\n")] = '\0';
122 setlocale(LC_ALL
, line
);
123 search(num_words
, words
);
132 { "help", 0, NULL
, 'h' },
133 { "list", 0, NULL
, 'l' },
134 { "search", 0, NULL
, 's' },
135 { "search-all-locales", 0, NULL
, 'S' },
142 printf("Usage: errno [-lsS] [--list] [--search] [--search-all-locales] "
148 main(int argc
, char **argv
)
158 } mode
= lookup_mode
;
160 setlocale(LC_ALL
, "");
163 int c
= getopt_long(argc
, argv
, "hlsS", options
, &index
);
181 mode
= search_all_mode
;
188 fprintf(stderr
, "getopt returned 0x%02x\n", c
);
193 exit_code
= EXIT_SUCCESS
;
197 for (i
= optind
; i
< argc
; ++i
) {
198 const char *arg
= argv
[i
];
199 if (toupper(arg
[0]) == 'E') {
200 if (!report_from_name(arg
))
201 exit_code
= EXIT_FAILURE
;
202 } else if (isdigit(arg
[0])) {
203 if (!report_from_code(atoi(arg
)))
204 exit_code
= EXIT_FAILURE
;
206 fprintf(stderr
, "ERROR: Not understood: %s\n", arg
);
207 exit_code
= EXIT_FAILURE
;
213 for (i
= 0; i
< num_errnos
; ++i
)
214 report(errnos
[i
].name
, errnos
[i
].code
);
218 search(argc
- optind
, argv
+ optind
);
221 case search_all_mode
:
222 search_all(argc
- optind
, argv
+ optind
);