the C standard doesn't prohibit a zero argc, from C89 5.1.2.2:
> * The value of argc shall be nonnegative.
> * argv[argc] shall be a null pointer.
> * If the value of argc is greater than zero, the string pointed to by
> argv[0] represents the program name (...)
this situation can be triggered by calling exec with NULL argv[0] and
tested by applying the following patch to test.c:
if (argc > 1) {
+ execv(argv[0], (char *[]){NULL});
+ } else if (argc == 0) {
return manual_test(argc, argv);
} else {
return testsuite();
not sure how likely it is for such situation to occurs in practice,
but deal with it in any case by setting optind to 0 if argv[0] happens
to be null.
---
optparse.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/optparse.h b/optparse.h
index f96184a..8d6c0a9 100644
--- a/optparse.h
+++ b/optparse.h
@@ -143,7 +143,7 @@ optparse_init(struct optparse *options, char **argv)
{
options->argv = argv;
options->permute = 1;
- options->optind = 1;
+ options->optind = argv[0] != 0;
options->subopt = 0;
options->optarg = 0;
options->errmsg[0] = '\0';
--
2.35.1
Thanks, NRK! Applied and pushed. I thought I had fixed this already, but
that was a different project.