linux-nfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Switch to getopt instead of parsing the flags by hand.
@ 2018-03-05 17:18 Kenneth Dsouza
  2018-03-06 15:35 ` J. Bruce Fields
  0 siblings, 1 reply; 2+ messages in thread
From: Kenneth Dsouza @ 2018-03-05 17:18 UTC (permalink / raw)
  To: linux-nfs; +Cc: bfields

Signed-off-by: Kenneth D'souza <kdsouza@redhat.com>
---
 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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-03-06 15:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-03-05 17:18 [PATCH] Switch to getopt instead of parsing the flags by hand Kenneth Dsouza
2018-03-06 15:35 ` J. Bruce Fields

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).