From: Mark J. <ma...@fr...> - 2019-07-26 20:35:57
|
This marginally simplifies the code. Writing argc--; argv++; after the option parsing loop is a common idiom anyway. --- contrib/elftoolchain/elfcopy/main.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/contrib/elftoolchain/elfcopy/main.c b/contrib/elftoolchain/elfcopy/main.c index 919fbf193bc4..f9a6cb612fab 100644 --- a/contrib/elftoolchain/elfcopy/main.c +++ b/contrib/elftoolchain/elfcopy/main.c @@ -1016,14 +1016,16 @@ elfcopy_main(struct elfcopy *ecp, int argc, char **argv) elfcopy_usage(); } } + argc -= optind; + argv += optind; - if (optind == argc || optind + 2 < argc) + if (argc == 0 || argc > 2) elfcopy_usage(); - infile = argv[optind]; + infile = argv[0]; outfile = NULL; - if (optind + 1 < argc) - outfile = argv[optind + 1]; + if (argc > 1) + outfile = argv[1]; create_file(ecp, infile, outfile); } @@ -1066,8 +1068,10 @@ mcs_main(struct elfcopy *ecp, int argc, char **argv) mcs_usage(); } } + argc -= optind; + argv += optind; - if (optind == argc) + if (argc == 0) mcs_usage(); /* Must specify one operation at least. */ @@ -1104,7 +1108,7 @@ mcs_main(struct elfcopy *ecp, int argc, char **argv) sac->string = string; } - for (i = optind; i < argc; i++) { + for (i = 0; i < argc; i++) { /* If only -p is specified, output to /dev/null */ if (print && !append && !compress && !delete) create_file(ecp, argv[i], "/dev/null"); @@ -1179,16 +1183,18 @@ strip_main(struct elfcopy *ecp, int argc, char **argv) strip_usage(); } } + argc -= optind; + argv += optind; if (ecp->strip == 0 && ((ecp->flags & DISCARD_LOCAL) == 0) && ((ecp->flags & DISCARD_LLABEL) == 0) && lookup_symop_list(ecp, NULL, SYMOP_STRIP) == NULL) ecp->strip = STRIP_ALL; - if (optind == argc) + if (argc == 0) strip_usage(); - for (i = optind; i < argc; i++) + for (i = 0; i < argc; i++) create_file(ecp, argv[i], outfile); } -- 2.22.0 |
From: Joseph K. <jk...@us...> - 2019-07-30 08:08:56
|
Mark, On Fri, Jul 26, 2019 at 9:36 PM Mark Johnston <ma...@fr...> wrote: > > This marginally simplifies the code. Writing argc--; argv++; after the > option parsing loop is a common idiom anyway. The option handling code in elfcopy(1) seems patterned after the getopt(3) usage example in the POSIX standard: https://pubs.opengroup.org/onlinepubs/9699919799/functions/getopt.html. I'm curious -- would there be a drawback to using 'optind' to index the argv[] array? Regards, Joseph Koshy |
From: Ed M. <em...@fr...> - 2019-07-30 17:00:16
|
On Tue, 30 Jul 2019 at 04:09, Joseph Koshy via Elftoolchain-developers <elf...@li...> wrote: > > The option handling code in elfcopy(1) seems > patterned after the getopt(3) usage example in > the POSIX standard: > > https://pubs.opengroup.org/onlinepubs/9699919799/functions/getopt.html. The author of that example may have chosen to use optind to demonstrate its existence and use though. It doesn't much matter, but I find the "discard processed options" form somewhat more clear. |
From: Mark J. <ma...@fr...> - 2019-07-30 14:44:38
|
On Tue, Jul 30, 2019 at 09:08:38AM +0100, Joseph Koshy wrote: > Mark, > > On Fri, Jul 26, 2019 at 9:36 PM Mark Johnston <ma...@fr...> wrote: > > > > This marginally simplifies the code. Writing argc--; argv++; after the > > option parsing loop is a common idiom anyway. > > The option handling code in elfcopy(1) seems > patterned after the getopt(3) usage example in > the POSIX standard: > > https://pubs.opengroup.org/onlinepubs/9699919799/functions/getopt.html. I noted that most of the other ELFToolchain utilities adjust argc and argv by optind immediately after the getopt loop: addr2line, ar, cxxfilt, elfdump, readelf, size, and strings. > I'm curious -- would there be a drawback to > using 'optind' to index the argv[] array? There is no drawback, it just seemed odd to me to use it. (And for Capsicum I will need to add some more uses.) If you prefer to keep the existing code I will adjust my patches accordingly. |
From: Joseph K. <jk...@us...> - 2019-08-05 19:04:20
|
On Tue, Jul 30, 2019 at 3:44 PM Mark Johnston <ma...@fr...> wrote: > I noted that most of the other ELFToolchain utilities adjust argc and > argv by optind immediately after the getopt loop: addr2line, ar, > cxxfilt, elfdump, readelf, size, and strings. Added in [r3780], thanks! Regards, Joseph Koshy |