From: Christian Weisgerber Subject: getopt: BSD or GNU? To: gameoftrees@openbsd.org Date: Sat, 26 Sep 2020 23:36:56 +0200 Sadly, there is no portable way to reset getopt(3). POSIX is silent on the issue. The BSD way is: optind = 1; optreset = 1; The GNU way is: optind = 0; These are mutually exclusive on the application side. OpenBSD's getopt supports both and tog.c uses a nonsensical mix. We should make up our mind and go with one or the other. I don't really care which one to choose, some platforms will require a patch either way, but the BSD scheme has the minor advantage that optreset will trigger a compile error if unavailable, while lack of support for optind=0 will only manifest at run time. The fact that we mix getopt_long() and getopt() does not affect this, although switching everything to non-POSIX getopt_long() and optind=0 would also take care of it. (Alternatively, do we need getopt_long at all?) Death by a thousand papercuts... diff 1367695b58142d73ad701f34f04fe45c4ad2782b /home/naddy/got blob - d493fceb5e36c0e2b7129cfa0950467e1fba2e30 file + got/got.c --- got/got.c +++ got/got.c @@ -213,7 +213,8 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - optind = 0; + optind = 1; + optreset = 1; if (Vflag) { got_version_print_str(); blob - c85b72cb7d79be804da3d376cc54c65fee5d8dfd file + tog/tog.c --- tog/tog.c +++ tog/tog.c @@ -5652,7 +5652,7 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - optind = 0; + optind = 1; optreset = 1; if (Vflag) { -- Christian "naddy" Weisgerber naddy@mips.inka.de