From: Sami Kerola Date: Tue, 9 May 2017 18:10:50 +0000 (+0100) Subject: vipw: use getopt_long() to parse options X-Git-Tag: v2.30-rc1~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6c9f102f3e5806d70da2d33a40d86a10cf0a3354;p=thirdparty%2Futil-linux.git vipw: use getopt_long() to parse options Signed-off-by: Sami Kerola --- diff --git a/login-utils/vipw.c b/login-utils/vipw.c index 4d85924346..7d4a831de4 100644 --- a/login-utils/vipw.c +++ b/login-utils/vipw.c @@ -61,6 +61,7 @@ #include #include #include +#include #include "c.h" #include "fileutils.h" @@ -312,6 +313,13 @@ static void __attribute__((__noreturn__)) usage(FILE *out) int main(int argc, char *argv[]) { + int c; + 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); @@ -325,15 +333,16 @@ int main(int argc, char *argv[]) xstrncpy(orig_file, PASSWD_FILE, sizeof(orig_file)); } - if (1 < argc) { - if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) { + while ((c = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1) + switch (c) { + case 'V': printf(UTIL_LINUX_VERSION); - exit(EXIT_SUCCESS); - } - if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) + return EXIT_SUCCESS; + case 'h': usage(stdout); - usage(stderr); - } + default: + errtryhelp(EXIT_FAILURE); + } edit_file(0);