diff --git a/src/eval.c b/src/eval.c index 071fb1b..59e7506 100644 --- a/src/eval.c +++ b/src/eval.c @@ -953,8 +953,6 @@ evalfun(struct funcnode *func, int argc, char **argv, int flags) INTON; shellparam.nparam = argc - 1; shellparam.p = argv + 1; - shellparam.optind = 1; - shellparam.optoff = -1; pushlocalvars(); evaltree(func->n.ndefun.body, flags & EV_TESTED); poplocalvars(0); diff --git a/src/options.c b/src/options.c index 6f381e6..5b24eeb 100644 --- a/src/options.c +++ b/src/options.c @@ -163,8 +163,8 @@ setarg0: } shellparam.p = xargv; - shellparam.optind = 1; - shellparam.optoff = -1; + shoptind = 1; + shoptoff = -1; /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */ while (*xargv) { shellparam.nparam++; @@ -316,8 +316,6 @@ setparam(char **argv) shellparam.malloc = 1; shellparam.nparam = nparam; shellparam.p = newparam; - shellparam.optind = 1; - shellparam.optoff = -1; } @@ -362,8 +360,6 @@ shiftcmd(int argc, char **argv) } ap2 = shellparam.p; while ((*ap2++ = *ap1++) != NULL); - shellparam.optind = 1; - shellparam.optoff = -1; INTON; return 0; } @@ -394,8 +390,8 @@ void getoptsreset(value) const char *value; { - shellparam.optind = number(value) ?: 1; - shellparam.optoff = -1; + shoptind = number(value) ?: 1; + shoptoff = -1; } /* @@ -412,20 +408,10 @@ getoptscmd(int argc, char **argv) if (argc < 3) sh_error("Usage: getopts optstring var [arg]"); - else if (argc == 3) { + else if (argc == 3) optbase = shellparam.p; - if ((unsigned)shellparam.optind > shellparam.nparam + 1) { - shellparam.optind = 1; - shellparam.optoff = -1; - } - } - else { + else optbase = &argv[3]; - if ((unsigned)shellparam.optind > argc - 2) { - shellparam.optind = 1; - shellparam.optoff = -1; - } - } return getopts(argv[1], argv[2], optbase); } @@ -438,10 +424,10 @@ getopts(char *optstr, char *optvar, char **optfirst) int done = 0; char s[2]; char **optnext; - int ind = shellparam.optind; - int off = shellparam.optoff; + int ind = shoptind; + int off = shoptoff; - shellparam.optind = -1; + shoptind = -1; optnext = optfirst + ind - 1; if (ind <= 1 || off < 0 || strlen(optnext[-1]) < off) @@ -509,8 +495,8 @@ out: s[1] = '\0'; setvar(optvar, s, 0); - shellparam.optoff = p ? p - *(optnext - 1) : -1; - shellparam.optind = ind; + shoptoff = p ? p - *(optnext - 1) : -1; + shoptind = ind; return done; } diff --git a/src/options.h b/src/options.h index 975fe33..8295eb9 100644 --- a/src/options.h +++ b/src/options.h @@ -38,9 +38,9 @@ struct shparam { int nparam; /* # of positional parameters (without $0) */ unsigned char malloc; /* if parameter list dynamically allocated */ char **p; /* parameter list */ - int optind; /* next parameter to be processed by getopts */ - int optoff; /* used by getopts */ }; +int shoptind; /* next parameter to be processed by getopts */ +int shoptoff; /* used by getopts */