int bin_getopts(char *name, char **argv, char *ops, int func) { int lenstr, lenoptstr, i; char *optstr = unmetafy(*argv++, &lenoptstr), *var = *argv++; char **args = (*argv) ? argv : pparams; static int optcind = 1, quiet; char *str, optbuf[2], *opch = optbuf + 1; /* zoptind keeps count of the current argument number */ if (zoptind < 1) /* first call */ zoptind = 1; if (zoptind == 1) { /*!! added*/ quiet = 0; optbuf[0] = '+'; zsfree(zoptarg); zoptarg = ztrdup(""); setsparam(var, ztrdup("")); if (*optstr == ':') { quiet = 1; optstr++; } } /*!! added*/ if (zoptind > arrlen(args)) return 1; str = unmetafy(args[zoptind - 1], &lenstr); if ((*str != '+' && *str != '-') || optcind >= lenstr || (lenstr == 2 && str[0] == '-' && str[1] == '-')) { /* current argument doesn't contain options, or optcind is impossibly large */ if (*str == '+' || *str == '-') zoptind++; optcind = 0; return 1; } /* Get the option character. optcind records the current position within the argument. */ if (!optcind) optcind = 1; *opch = str[optcind++]; if (optcind == lenstr) { if(args[zoptind++]) str = unmetafy(args[zoptind - 1], &lenstr); optcind = 0; } /* look for option in the provided optstr */ for (i = 0; i != lenoptstr; i++) if (*opch == optstr[i]) break; if (i == lenoptstr) { /* not a valid option */ setsparam(var, ztrdup("?")); if (quiet) { zsfree(zoptarg); zoptarg = metafy(opch, 1, META_DUP); return 0; } zerr("bad option: -%c", NULL, *opch); errflag = 0; return 0; } /* copy option into specified parameter, with + if required */ setsparam(var, metafy(opch - (*str == '+'), 1 + (*str == '+'), META_DUP)); /* handle case of an expected extra argument */ if (optstr[i + 1] == ':') { /*!! changed 1 to i + 1 */ if (!args[zoptind - 1]) { /* no extra argument was provided */ if (quiet) { zsfree(zoptarg); zoptarg = metafy(opch, 1, META_DUP); setsparam(var, ztrdup(":")); return 0; } setsparam(var, ztrdup("?")); zerr("argument expected after -%c option", NULL, *opch); errflag = 0; return 0; } /* skip over the extra argument */ zsfree(zoptarg); zoptarg = metafy(str + optcind, lenstr - optcind, META_DUP); zoptind++; optcind = 0; } return 0; }