]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
switch_root: use getopt_long() to parse options
authorSami Kerola <kerolasa@iki.fi>
Tue, 9 May 2017 18:10:51 +0000 (19:10 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 10 May 2017 08:53:28 +0000 (10:53 +0200)
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
sys-utils/switch_root.c

index 2b42ddf852fef999ef1d046970e87e302d6cc3dc..7bd07783ee3a7ec07bfeb6f1c048ca4d239c7cdd 100644 (file)
@@ -33,6 +33,7 @@
 #include <errno.h>
 #include <ctype.h>
 #include <dirent.h>
+#include <getopt.h>
 
 #include "c.h"
 #include "nls.h"
@@ -217,14 +218,25 @@ static void __attribute__((__noreturn__)) usage(FILE *output)
 int main(int argc, char *argv[])
 {
        char *newroot, *init, **initargs;
+       int c;
+       static const struct option longopts[] = {
+               {"version", no_argument, NULL, 'V'},
+               {"help", no_argument, NULL, 'h'},
+               {NULL, 0, NULL, 0}
+       };
+
        atexit(close_stdout);
 
-       if (argv[1] && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")))
-               usage(stdout);
-       if (argv[1] && (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-V"))) {
-               printf(UTIL_LINUX_VERSION);
-               return EXIT_SUCCESS;
-       }
+       while ((c = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
+               switch (c) {
+               case 'V':
+                       printf(UTIL_LINUX_VERSION);
+                       return EXIT_SUCCESS;
+               case 'h':
+                       usage(stdout);
+               default:
+                       errtryhelp(EXIT_FAILURE);
+               }
        if (argc < 3)
                usage(stderr);