[Replicant] [libsamsung-ipc] [PATCH 10/26] tools: ipc-modem: getopt_long: shorten nested ifs

Denis 'GNUtoo' Carikli GNUtoo at cyberdimension.org
Mon Mar 28 20:20:24 UTC 2022


This makes the code more readable. It should contain no functional
changes.

Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo at cyberdimension.org>
---
 tools/ipc-modem.c | 65 +++++++++++++++++++++--------------------------
 1 file changed, 29 insertions(+), 36 deletions(-)

diff --git a/tools/ipc-modem.c b/tools/ipc-modem.c
index 7acd6eb..1fbc778 100644
--- a/tools/ipc-modem.c
+++ b/tools/ipc-modem.c
@@ -626,50 +626,43 @@ int main(int argc, char *argv[])
 		exit(1);
 	}
 
-	while (c >= 0) {
+	while (true) {
 		c = getopt_long(argc, argv, "", opt_l, &opt_i);
-		if (c < 0)
+		if (c != 0)
 			break;
 
-		switch (c) {
-		case 0:
-			if (strcmp(opt_l[opt_i].name, "call") == 0) {
-				if (optarg) {
-					if (strlen(optarg) < 14) {
-						assert(strlen(optarg) <
-						       sizeof(call_number));
-						printf("[I] "
-						       "Got call number!\n");
-						strcpy(call_number, optarg);
-					} else {
-						printf("[E] "
-						       "Call number is too long!"
-						       "\n");
-						return 1;
-					}
-				}
-			} else if (strcmp(opt_l[opt_i].name, "debug") == 0) {
-				cmdline_opts.debug = true;
-			} else if (strcmp(opt_l[opt_i].name, "dry-run") == 0) {
-				cmdline_opts.dry_run = true;
-			} else if (strncmp(opt_l[opt_i].name, "help", 4) == 0) {
-				print_help();
-				exit(1);
-			} else if ((strcmp(opt_l[opt_i].name, "pin") == 0) &&
-				   (optarg)) {
-				if (strlen(optarg) < 8) {
+		if (strcmp(opt_l[opt_i].name, "call") == 0) {
+			if (optarg) {
+				if (strlen(optarg) < 14) {
 					assert(strlen(optarg) <
-					       sizeof(sim_pin));
-
-					printf("[I] Got SIM PIN!\n");
-					strcpy(sim_pin, optarg);
+					       sizeof(call_number));
+					printf("[I] Got call number!\n");
+					strcpy(call_number, optarg);
 				} else {
-					printf("[E] SIM PIN is too long!\n");
+					printf("[E] "
+					       "Call number is too long!\n");
 					return 1;
 				}
 			}
-
-			break;
+		} else if (strcmp(opt_l[opt_i].name, "debug") == 0) {
+			cmdline_opts.debug = true;
+		} else if (strcmp(opt_l[opt_i].name, "dry-run") == 0) {
+			cmdline_opts.dry_run = true;
+		} else if (strncmp(opt_l[opt_i].name, "help", 4) == 0) {
+			print_help();
+			exit(1);
+		} else if ((strcmp(opt_l[opt_i].name, "pin") == 0) &&
+			   (optarg)) {
+			if (strlen(optarg) < 8) {
+				assert(strlen(optarg) <
+				       sizeof(sim_pin));
+
+				printf("[I] Got SIM PIN!\n");
+				strcpy(sim_pin, optarg);
+			} else {
+				printf("[E] SIM PIN is too long!\n");
+				return 1;
+			}
 		}
 	}
 
-- 
2.35.1



More information about the Replicant mailing list