2018-03-05 17:18:12

by Kenneth Dsouza

[permalink] [raw]
Subject: [PATCH] Switch to getopt instead of parsing the flags by hand.

Signed-off-by: Kenneth D'souza <[email protected]>
---
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 <libgen.h>
#include "libacl_nfs4.h"
#include <ftw.h>
+#include <getopt.h>

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



2018-03-06 15:35:03

by J. Bruce Fields

[permalink] [raw]
Subject: Re: [PATCH] Switch to getopt instead of parsing the flags by hand.

Applied, thanks.--b.

On Mon, Mar 05, 2018 at 10:48:06PM +0530, Kenneth Dsouza wrote:
> Signed-off-by: Kenneth D'souza <[email protected]>
> ---
> 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 <libgen.h>
> #include "libacl_nfs4.h"
> #include <ftw.h>
> +#include <getopt.h>
>
> 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