From patchwork Mon Mar 5 17:18:06 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kenneth Dsouza X-Patchwork-Id: 10259369 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D068360134 for ; Mon, 5 Mar 2018 17:18:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C0320288A8 for ; Mon, 5 Mar 2018 17:18:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B44F228947; Mon, 5 Mar 2018 17:18:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CC449288A8 for ; Mon, 5 Mar 2018 17:18:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751852AbeCERSN (ORCPT ); Mon, 5 Mar 2018 12:18:13 -0500 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 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F3B81405CC01; Mon, 5 Mar 2018 17:18:11 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.76.0.202]) by smtp.corp.redhat.com (Postfix) with ESMTP id E6CFA2144B20; Mon, 5 Mar 2018 17:18:10 +0000 (UTC) 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> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Mon, 05 Mar 2018 17:18:12 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Mon, 05 Mar 2018 17:18:12 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'kdsouza@redhat.com' RCPT:'' Sender: linux-nfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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()