From bf32106998fd39751c2bec2ae14448e1581b8891 Mon Sep 17 00:00:00 2001 From: Coelacanthus Date: Mon, 19 Aug 2024 21:22:52 +0800 Subject: [PATCH] refactor: optimize getopt() usage * optind will be the index of the first mismatched argument, so use it directly. * use while instead of for Signed-off-by: Celeste Liu --- src/applet/notification/process.c | 11 +++-------- src/applet/notification/remove.c | 11 +++-------- src/applet/profile/discovery.c | 3 +-- src/applet/profile/download.c | 4 +--- 4 files changed, 8 insertions(+), 21 deletions(-) diff --git a/src/applet/notification/process.c b/src/applet/notification/process.c index 580588f..0da6e27 100644 --- a/src/applet/notification/process.c +++ b/src/applet/notification/process.c @@ -67,10 +67,9 @@ static int applet_main(int argc, char **argv) int fret = 0; int all = 0; int autoremove = 0; - int argc_seq_offset = 1; + int opt = 0; - int opt = getopt(argc, argv, opt_string); - for (int i = 0; opt != -1; i++) + while ((opt = getopt(argc, argv, opt_string)) != -1) { switch (opt) { @@ -87,14 +86,10 @@ static int applet_main(int argc, char **argv) printf("\t -r Automatically remove processed notifications\r\n"); return -1; default: - goto run; break; } - argc_seq_offset++; - opt = getopt(argc, argv, opt_string); } -run: if (all) { struct es10b_notification_metadata_list *notifications, *rptr; @@ -121,7 +116,7 @@ static int applet_main(int argc, char **argv) } else { - for (int i = argc_seq_offset; i < argc; i++) + for (int i = optind; i < argc; i++) { unsigned long seqNumber; diff --git a/src/applet/notification/remove.c b/src/applet/notification/remove.c index eb35126..5637640 100644 --- a/src/applet/notification/remove.c +++ b/src/applet/notification/remove.c @@ -41,10 +41,9 @@ static int applet_main(int argc, char **argv) int fret = 0; int all = 0; - int argc_seq_offset = 1; + int opt = 0; - int opt = getopt(argc, argv, opt_string); - for (int i = 0; opt != -1; i++) + while ((opt = getopt(argc, argv, opt_string)) != -1) { switch (opt) { @@ -57,14 +56,10 @@ static int applet_main(int argc, char **argv) printf("\t -a All notifications\r\n"); return -1; default: - goto run; break; } - argc_seq_offset++; - opt = getopt(argc, argv, opt_string); } -run: if (all) { struct es10b_notification_metadata_list *notifications, *rptr; @@ -91,7 +86,7 @@ static int applet_main(int argc, char **argv) } else { - for (int i = argc_seq_offset; i < argc; i++) + for (int i = optind; i < argc; i++) { unsigned long seqNumber; diff --git a/src/applet/profile/discovery.c b/src/applet/profile/discovery.c index 44ad82f..406b89c 100644 --- a/src/applet/profile/discovery.c +++ b/src/applet/profile/discovery.c @@ -24,8 +24,7 @@ static int applet_main(int argc, char **argv) cJSON *jdata = NULL; - opt = getopt(argc, argv, opt_string); - while (opt != -1) + while ((opt = getopt(argc, argv, opt_string)) != -1) { switch (opt) { diff --git a/src/applet/profile/download.c b/src/applet/profile/download.c index b45b290..4dee77b 100644 --- a/src/applet/profile/download.c +++ b/src/applet/profile/download.c @@ -49,8 +49,7 @@ static int applet_main(int argc, char **argv) cJSON *jmetadata = NULL; struct es8p_metadata *profile_metadata = NULL; - opt = getopt(argc, argv, opt_string); - while (opt != -1) + while ((opt = getopt(argc, argv, opt_string)) != -1) { switch (opt) { @@ -90,7 +89,6 @@ static int applet_main(int argc, char **argv) default: break; } - opt = getopt(argc, argv, opt_string); } if (activation_code != NULL)