diff options
author | Lars Wirzenius <liw@liw.fi> | 2012-06-05 21:42:47 +0100 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2012-06-05 21:42:47 +0100 |
commit | 99153f44efacda0ebc3cd2eb42db0a36ef2145ad (patch) | |
tree | a4971d69eb649a758ee3144fafeb482870b7f0af | |
parent | 212ab4ba7c7d5ed1d4da75069fcaebfa74582180 (diff) | |
download | moreutils-99153f44efacda0ebc3cd2eb42db0a36ef2145ad.tar.gz |
Add errno --search-all-locale option
-rw-r--r-- | errno.c | 48 |
1 files changed, 45 insertions, 3 deletions
@@ -100,18 +100,47 @@ search(int num_words, char **words) } +static void +search_all(int num_words, char **words) +{ + FILE *f; + + /* Static buffers are ugly, but they're simple. If anyone has a + locale name longer than a kilobyte, they will suffer, and they + will complain, and then I will fix this. */ + char line[1024]; + + f = popen("locale -a", "r"); + if (f == NULL) { + fprintf(stderr, "ERROR: Can't execute locale -a: %d: %s\n", + errno, strerror(errno)); + exit(EXIT_FAILURE); + } + + while (fgets(line, sizeof line, f) != NULL) { + line[strcspn(line, "\n")] = '\0'; + setlocale(LC_ALL, line); + search(num_words, words); + } + + fclose(f); +} + + static struct option options[] = { { "help", 0, NULL, 'h' }, { "list", 0, NULL, 'l' }, { "search", 0, NULL, 's' }, + { "search-all-locales", 0, NULL, 'S' }, }; static void usage(void) { - printf("Usage: errno [-ls] [--list] [--search] [keyword]\n"); + printf("Usage: errno [-lsS] [--list] [--search] [--search-all-locales] " + "[keyword]\n"); } @@ -121,12 +150,17 @@ main(int argc, char **argv) int i; int exit_code; int index = 0; - enum { lookup_mode, list_mode, search_mode } mode = lookup_mode; + enum { + lookup_mode, + list_mode, + search_mode, + search_all_mode + } mode = lookup_mode; setlocale(LC_ALL, ""); for (;;) { - int c = getopt_long(argc, argv, "hls", options, &index); + int c = getopt_long(argc, argv, "hlsS", options, &index); if (c == -1) break; @@ -142,6 +176,10 @@ main(int argc, char **argv) case 's': mode = search_mode; break; + + case 'S': + mode = search_all_mode; + break; case '?': break; @@ -179,6 +217,10 @@ main(int argc, char **argv) case search_mode: search(argc - optind, argv + optind); break; + + case search_all_mode: + search_all(argc - optind, argv + optind); + break; } return exit_code; |