]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
ctrlaltdel: add version & help options
authorSami Kerola <kerolasa@iki.fi>
Wed, 31 Aug 2011 18:52:01 +0000 (20:52 +0200)
committerSami Kerola <kerolasa@iki.fi>
Sat, 17 Sep 2011 12:25:20 +0000 (14:25 +0200)
Including other necessary changes to usage().

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
sys-utils/ctrlaltdel.c

index d6c83b42f0a22fed4a3e2b23446fb78c6edd8bb9..8805435d4da6ca7a5123a8f9e1fbb2dfb93059ce 100644 (file)
@@ -5,6 +5,7 @@
  * - added Native Language Support
  */
 
+#include <getopt.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "nls.h"
 #include "c.h"
 
-int main(int argc, char *argv[])
+static void __attribute__ ((__noreturn__)) usage(FILE * out)
 {
+       fprintf(out, USAGE_HEADER);
+       fprintf(out, _(" %s <hard|soft>\n"), program_invocation_short_name);
+       fprintf(out, USAGE_OPTIONS);
+       fprintf(out, USAGE_HELP);
+       fprintf(out, USAGE_VERSION);
+       fprintf(out, USAGE_BEGIN_TAIL);
+       fprintf(out, USAGE_MAN_TAIL, "ctrlaltdel(8)");
+       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+}
+
+int main(int argc, char **argv)
+{
+       int ch;
+       static const struct option longopts[] = {
+               {"version", no_argument, NULL, 'V'},
+               {"help", no_argument, NULL, 'h'},
+               {NULL, 0, NULL, 0}
+       };
+
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
 
+       while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
+               switch (ch) {
+               case 'V':
+                       printf(UTIL_LINUX_VERSION);
+                       return EXIT_SUCCESS;
+               case 'h':
+                       usage(stdout);
+               default:
+                       usage(stderr);
+               }
+
        if (geteuid())
                errx(EXIT_FAILURE,
                     _("You must be root to set the Ctrl-Alt-Del behaviour"));
@@ -29,9 +60,9 @@ int main(int argc, char *argv[])
        } else if (argc == 2 && !strcmp("soft", argv[1])) {
                if (my_reboot(LINUX_REBOOT_CMD_CAD_OFF) < 0)
                        err(EXIT_FAILURE, "reboot");
-       } else
-               errx(EXIT_FAILURE, _("Usage: %s hard|soft"),
-                               program_invocation_short_name);
+       } else {
+               usage(stderr);
+       }
 
        return EXIT_SUCCESS;
 }