From: Sami Kerola Date: Sun, 10 Apr 2011 14:24:21 +0000 (+0200) Subject: mesg: use long options, also --help and --version added X-Git-Tag: v2.20-rc1~332 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fe16e1251327902bfd18ae287328d4ef9eeb9c24;p=thirdparty%2Futil-linux.git mesg: use long options, also --help and --version added Signed-off-by: Sami Kerola --- diff --git a/term-utils/mesg.c b/term-utils/mesg.c index 6176aabfbe..33f19413ca 100644 --- a/term-utils/mesg.c +++ b/term-utils/mesg.c @@ -53,6 +53,7 @@ #include #include #include +#include #include "nls.h" #include "c.h" @@ -62,6 +63,18 @@ #define IS_NOT_ALLOWED 1 /* Receiving messages is not allowed. */ #define MESG_EXIT_FAILURE 2 /* An error occurred. */ +static void __attribute__ ((__noreturn__)) usage(FILE * out) +{ + fprintf(out, _("\nUsage:\n" + " %s [options] [y | n]\n"), program_invocation_short_name); + fprintf(out, + _("\nOptions:\n" + " -V, --version output version information and exit\n" + " -h, --help output help screen and exit\n")); + + exit(out == stderr ? MESG_EXIT_FAILURE : EXIT_SUCCESS); +} + int main(int argc, char *argv[]) { struct stat sb; @@ -72,11 +85,22 @@ int main(int argc, char *argv[]) bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); - 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: - goto usage; + usage(stderr); } argc -= optind; @@ -110,9 +134,8 @@ int main(int argc, char *argv[]) if (chmod(tty, sb.st_mode & ~(S_IWGRP|S_IWOTH)) < 0) err(MESG_EXIT_FAILURE, _("change %s mode failed"), tty); return IS_NOT_ALLOWED; + default: + warnx(_("invalid argument: %c"), *argv[0]); + usage(stderr); } - -usage: - fprintf(stderr, _("Usage: %s [y | n]"), program_invocation_short_name); - return MESG_EXIT_FAILURE; }