diff options
author | Sami Kerola | 2011-04-02 18:11:09 +0200 |
---|---|---|
committer | Karel Zak | 2011-04-06 10:48:35 +0200 |
commit | ca96c6ac52d5c25a7f3e8d2b5b433faf1ad47bc0 (patch) | |
tree | b65c9228da060a04815e6a13b2f444caba665a24 /text-utils | |
parent | rev: memory leak fix (diff) | |
download | kernel-qcow2-util-linux-ca96c6ac52d5c25a7f3e8d2b5b433faf1ad47bc0.tar.gz kernel-qcow2-util-linux-ca96c6ac52d5c25a7f3e8d2b5b433faf1ad47bc0.tar.xz kernel-qcow2-util-linux-ca96c6ac52d5c25a7f3e8d2b5b433faf1ad47bc0.zip |
rev: option parsing bug fixed & long options added
The former getopts segment gave impression unknown options will
cause the program to exit with error and help is available with
-h. Neither work quite as designed, all unknown options made the
program to exit with success; and none of the options where
known.
The fix also has support for long options, and new --version
switch.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'text-utils')
-rw-r--r-- | text-utils/rev.c | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/text-utils/rev.c b/text-utils/rev.c index 76a362030..6c1366d23 100644 --- a/text-utils/rev.c +++ b/text-utils/rev.c @@ -56,6 +56,7 @@ #include <string.h> #include <unistd.h> #include <signal.h> +#include <getopt.h> #include "nls.h" #include "xalloc.h" @@ -70,10 +71,14 @@ static void sig_handler(int signo) _exit(EXIT_SUCCESS); } -static void __attribute__((__noreturn__)) usage(FILE *out) +static void __attribute__ ((__noreturn__)) usage(FILE * out) { - fprintf(out, _("Usage: %s [file ...]\n"), - program_invocation_short_name); + fprintf(out, _("Usage: %s [options] [file ...]\n"), + program_invocation_short_name); + + fprintf(out, _("\nOptions:\n" + " -V, --version output version information and exit\n" + " -h, --help display this help and exit\n")); fprintf(out, _("\nFor more information see rev(1).\n")); @@ -95,9 +100,18 @@ int main(int argc, char *argv[]) signal(SIGINT, sig_handler); signal(SIGTERM, sig_handler); - while ((ch = getopt(argc, argv, "")) != -1) + static const struct option longopts[] = { + { "version", no_argument, 0, 'V' }, + { "help", no_argument, 0, 'h' }, + { NULL, 0, 0, 0 } + }; + + while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1) switch(ch) { - case '?': + case 'V': + printf(_("%s from %s\n"), program_invocation_short_name, + PACKAGE_STRING); + exit(EXIT_SUCCESS); case 'h': usage(stdout); default: |