From: Sami Kerola Date: Sun, 12 Jun 2011 17:41:31 +0000 (+0200) Subject: look: add long options X-Git-Tag: v2.20-rc1~138^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a35f7505808d5b37ff45625390f330bf1c76941c;p=thirdparty%2Futil-linux.git look: add long options Including new help screen. Additionally unused includes are removed. Signed-off-by: Sami Kerola --- diff --git a/misc-utils/look.c b/misc-utils/look.c index 85a8cc1b2a..b05c22e408 100644 --- a/misc-utils/look.c +++ b/misc-utils/look.c @@ -46,17 +46,14 @@ * the manual page. */ -#include #include #include - -#include #include #include #include #include +#include #include -#include #include #include @@ -80,7 +77,7 @@ static int compare (char *, char *); static char *linear_search (char *, char *); static int look (char *, char *); static void print_from (char *, char *); -static void usage (void); +static void __attribute__ ((__noreturn__)) usage(FILE * out); int main(int argc, char *argv[]) @@ -89,6 +86,16 @@ main(int argc, char *argv[]) int ch, fd, termchar; char *back, *file, *front, *p; + static const struct option longopts[] = { + {"alternative", no_argument, NULL, 'a'}, + {"alphanum", no_argument, NULL, 'd'}, + {"ignore-case", no_argument, NULL, 'f'}, + {"terminate", required_argument, NULL, 't'}, + {"version", no_argument, NULL, 'V'}, + {"help", no_argument, NULL, 'h'}, + {NULL, 0, NULL, 0} + }; + setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); @@ -99,11 +106,11 @@ main(int argc, char *argv[]) termchar = '\0'; string = NULL; /* just for gcc */ - while ((ch = getopt(argc, argv, "adft:")) != -1) + while ((ch = getopt_long(argc, argv, "adft:Vh", longopts, NULL)) != -1) switch(ch) { case 'a': - file = _PATH_WORDS_ALT; - break; + file = _PATH_WORDS_ALT; + break; case 'd': dflag = 1; break; @@ -113,9 +120,16 @@ main(int argc, char *argv[]) case 't': termchar = *optarg; break; + case 'V': + printf(_("%s from %s\n"), + program_invocation_short_name, + PACKAGE_STRING); + return EXIT_SUCCESS; + case 'h': + usage(stdout); case '?': default: - usage(); + usage(stderr); } argc -= optind; argv += optind; @@ -130,7 +144,7 @@ main(int argc, char *argv[]) string = *argv; break; default: - usage(); + usage(stderr); } if (termchar != '\0' && (p = strchr(string, termchar)) != NULL) @@ -347,9 +361,18 @@ compare(char *s2, char *s2end) { return ((i > 0) ? LESS : (i < 0) ? GREATER : EQUAL); } -static void -usage() +static void __attribute__ ((__noreturn__)) usage(FILE * out) { - (void)fprintf(stderr, _("usage: look [-dfa] [-t char] string [file]\n")); - exit(2); + fprintf(out, _("Usage: %s [options] string [file]\n"), + program_invocation_short_name); + + fprintf(out, _("\nOptions:\n" + " -a, --alternative use alternate dictionary\n" + " -d, --alphanum compare only alpha numeric characters\n" + " -f, --ignore-case ignore when comparing\n" + " -t, --terminate=C define string termination character\n" + " -V, --version output version information and exit\n" + " -h, --help display this help and exit\n\n")); + + exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); }