Return-Path: Received: from mx3-rdu2.redhat.com ([66.187.233.73]:47082 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751679AbeCERSM (ORCPT ); Mon, 5 Mar 2018 12:18:12 -0500 From: Kenneth Dsouza To: linux-nfs@vger.kernel.org Cc: bfields@fieldses.org Subject: [PATCH] Switch to getopt instead of parsing the flags by hand. Date: Mon, 5 Mar 2018 22:48:06 +0530 Message-Id: <20180305171806.9576-1-kdsouza@redhat.com> Sender: linux-nfs-owner@vger.kernel.org List-ID: Signed-off-by: Kenneth D'souza --- nfs4_getfacl/nfs4_getfacl.c | 62 ++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/nfs4_getfacl/nfs4_getfacl.c b/nfs4_getfacl/nfs4_getfacl.c index 1705d35..5a9c911 100644 --- a/nfs4_getfacl/nfs4_getfacl.c +++ b/nfs4_getfacl/nfs4_getfacl.c @@ -40,6 +40,7 @@ #include #include "libacl_nfs4.h" #include +#include static void usage(int); static void more_help(); @@ -52,10 +53,17 @@ static int recursive(const char *fpath, const struct stat *sb, int tflag, struct return 0; } +static struct option long_options[] = { + {"more-help", 0, 0, 'H' }, + {"help", 0, 0, 'h' }, + {"recursive", 0, 0, 'R' }, + { NULL, 0, 0, 0, }, +}; + int main(int argc, char **argv) { - int res = 1; - int counter = 1; + int opt, res = 1; + int do_recursive = 0; execname = basename(argv[0]); @@ -63,27 +71,39 @@ int main(int argc, char **argv) fprintf(stderr, "%s: you must specify a path.\n", execname); usage(0); goto out; - } else if (!strcmp(argv[1], "-?") || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { - usage(1); - res = 0; - goto out; - } else if (!strcmp(argv[1], "-H") || !strcmp(argv[1], "--more-help")) { - more_help(); - res = 0; - goto out; - } else if (!strcmp(argv[1], "-R") || !strcmp(argv[1], "--recursive")) { - if (argc < 3) - usage(1); - for(counter = 2; counter < argc; counter++) { - if (nftw(argv[counter], recursive, 20, 0) == -1) { - printf("Invalid filename: %s\n", argv[counter]); - } + } + + while ((opt = getopt_long(argc, argv, "HR?h", long_options, NULL)) != -1) { + switch(opt) { + case 'H': + more_help(); + res = 0; + goto out; + + case 'R': + do_recursive = 1; + break; + + default: + usage(1); + res = 0; + goto out; } - res = 0; + } + + if (optind >= argc) { + fprintf(stderr, "%s: you must specify a path after options.\n", execname); + usage(0); goto out; } - for (counter = 1; counter < argc; counter++) { - print_acl_from_path(argv[counter]); + + for(; optind < argc; optind++) { + if(do_recursive) { + if (nftw(argv[optind], recursive, 20, 0) == -1) + printf("Invalid filename: %s\n", argv[optind]); + } + else + print_acl_from_path(argv[optind]); res = 0; } out: @@ -105,7 +125,7 @@ static void usage(int label) { if (label) fprintf(stderr, "%s %s -- get NFSv4 file or directory access control lists.\n", execname, VERSION); - fprintf(stderr, "Usage: %s file\n -H, --more-help\tdisplay ACL format information\n -?, -h, --help\tdisplay this help text\n -R --recursive\trecurse into subdirectories\n", execname); + fprintf(stderr, "Usage: %s [-R] file ...\n -H, --more-help\tdisplay ACL format information\n -?, -h, --help\tdisplay this help text\n -R --recursive\trecurse into subdirectories\n", execname); } static void more_help() -- 2.9.5