[hackers] [slock][PATCH] Improve option parsing routine

From: Klemens Nanni <kl3_AT_posteo.org>
Date: Sat, 29 Oct 2016 00:32:43 +0200

This reduces the amount of strcmp() calls and comparisons in general to
a minimum.
---
 slock.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
diff --git a/slock.c b/slock.c
index 283b04e..e9b050a 100644
--- a/slock.c
+++ b/slock.c
_AT_@ -298,15 +298,14 @@ main(int argc, char *argv[]) {
 	Display *dpy;
 	int s, nlocks, nscreens;
 
-	if (argc > 1 && !strncmp("-", argv[1], 1)) {
-		if ((argc == 2 && !strcmp("-v", argv[1])) ||
-		    (argc == 3 && !strcmp("-v", argv[1]) && !strcmp("--", argv[2]))) {
+	if (argv[1] && argv[1][0] == '-') {
+		if (argv[1][1] == 'v' && argv[1][2] == '\0' &&
+		    (argc == 2 || (argc == 3 && !strcmp(argv[2], "--")))) {
 			fputs("slock-"VERSION"\n", stderr);
 			return 0;
-		} else if (!strcmp("--", argv[1])) {
-			--argc;
-			++argv;
-		} else
+		} else if (argv[1][1] == '-' && argv[1][2] == '\0')
+			--argc, ++argv;
+		else
 			die("usage: slock [-v] [cmd [arg ...]]\n");
 	}
 
-- 
2.8.3
Received on Sat Oct 29 2016 - 00:32:43 CEST

This archive was generated by hypermail 2.3.0 : Sat Oct 29 2016 - 00:36:15 CEST