From: Sami Kerola Date: Tue, 9 May 2017 18:10:51 +0000 (+0100) Subject: switch_root: use getopt_long() to parse options X-Git-Tag: v2.30-rc1~17 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=105bb8572f5eddd3e196363ee18a0d210990905f;p=thirdparty%2Futil-linux.git switch_root: use getopt_long() to parse options Signed-off-by: Sami Kerola --- diff --git a/sys-utils/switch_root.c b/sys-utils/switch_root.c index 2b42ddf852..7bd07783ee 100644 --- a/sys-utils/switch_root.c +++ b/sys-utils/switch_root.c @@ -33,6 +33,7 @@ #include #include #include +#include #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);