Skip to content

Commit

Permalink
refactor: optimize getopt() usage (#147)
Browse files Browse the repository at this point in the history
* optind will be the index of the first mismatched argument, so use it
  directly.
* use while instead of for

Signed-off-by: Celeste Liu <[email protected]>
  • Loading branch information
CoelacanthusHex committed Aug 20, 2024
1 parent 0499a3a commit 1304ca1
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 21 deletions.
11 changes: 3 additions & 8 deletions src/applet/notification/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand All @@ -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;

Expand Down
11 changes: 3 additions & 8 deletions src/applet/notification/remove.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand All @@ -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;

Expand Down
3 changes: 1 addition & 2 deletions src/applet/profile/discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
4 changes: 1 addition & 3 deletions src/applet/profile/download.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -90,7 +89,6 @@ static int applet_main(int argc, char **argv)
default:
break;
}
opt = getopt(argc, argv, opt_string);
}

if (activation_code != NULL)
Expand Down

0 comments on commit 1304ca1

Please sign in to comment.