1 diff -urN netbsd-sh/options.c ash-0.3.7.orig/options.c
2 --- netbsd-sh/options.c Fri Jul 9 13:02:07 1999
3 +++ ash-0.3.7.orig/options.c Mon Apr 23 22:16:46 2001
5 STATIC void options __P((int));
6 STATIC void minus_o __P((char *, int));
7 STATIC void setoption __P((int, int));
8 -STATIC int getopts __P((char *, char *, char **, char ***, char **));
9 +STATIC int getopts __P((char *, char *, char **, int *, int *));
16 shellparam.p = argptr;
17 - shellparam.reset = 1;
18 + shellparam.optind = 1;
19 + shellparam.optoff = -1;
20 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
24 shellparam.malloc = 1;
25 shellparam.nparam = nparam;
26 shellparam.p = newparam;
27 - shellparam.optnext = NULL;
28 + shellparam.optind = 1;
29 + shellparam.optoff = -1;
36 while ((*ap2++ = *ap1++) != NULL);
37 - shellparam.optnext = NULL;
38 + shellparam.optind = 1;
39 + shellparam.optoff = -1;
47 - if (number(value) == 1) {
48 - shellparam.optnext = NULL;
49 - shellparam.reset = 1;
51 + shellparam.optind = number(value);
52 + shellparam.optoff = -1;
59 error("Usage: getopts optstring var [arg]");
61 + else if (argc == 3) {
62 optbase = shellparam.p;
64 + if (shellparam.optind > shellparam.nparam + 1) {
65 + shellparam.optind = 1;
66 + shellparam.optoff = -1;
72 - if (shellparam.reset == 1) {
73 - shellparam.optnext = optbase;
74 - shellparam.optptr = NULL;
75 - shellparam.reset = 0;
76 + if (shellparam.optind > argc - 2) {
77 + shellparam.optind = 1;
78 + shellparam.optoff = -1;
82 - return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
83 - &shellparam.optptr);
84 + return getopts(argv[1], argv[2], optbase, &shellparam.optind,
85 + &shellparam.optoff);
89 -getopts(optstr, optvar, optfirst, optnext, optpptr)
90 +getopts(optstr, optvar, optfirst, optind, optoff)
105 + char **optnext = optfirst + *optind - 1;
107 - if ((p = *optpptr) == NULL || *p == '\0') {
108 + if (*optind <= 1 || *optoff < 0 || !(*(optnext - 1)) ||
109 + strlen(*(optnext - 1)) < *optoff)
112 + p = *(optnext - 1) + *optoff;
113 + if (p == NULL || *p == '\0') {
114 /* Current word is done, advance */
115 - if (*optnext == NULL)
116 + if (optnext == NULL)
120 if (p == NULL || *p != '-' || *++p == '\0') {
122 - ind = *optnext - optfirst + 1;
124 + *optind = optnext - optfirst + 1;
131 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
138 - if (*p == '\0' && (p = **optnext) == NULL) {
139 + if (*p == '\0' && (p = *optnext) == NULL) {
140 if (optstr[0] == ':') {
143 @@ -468,30 +477,29 @@
147 - if (p == **optnext)
151 setvarsafe("OPTARG", p, 0);
155 setvarsafe("OPTARG", "", 0);
156 - ind = *optnext - optfirst + 1;
157 + *optind = optnext - optfirst + 1;
167 - fmtstr(s, sizeof(s), "%d", ind);
168 + *optoff = p ? p - *(optnext - 1) : -1;
169 + fmtstr(s, sizeof(s), "%d", *optind);
170 err |= setvarsafe("OPTIND", s, VNOFUNC);
173 err |= setvarsafe(optvar, s, 0);
182 diff -urN netbsd-sh/options.h ash-0.3.7.orig/options.h
183 --- netbsd-sh/options.h Fri Jul 9 13:02:07 1999
184 +++ ash-0.3.7.orig/options.h Mon Apr 23 22:16:46 2001
187 int nparam; /* # of positional parameters (without $0) */
188 unsigned char malloc; /* if parameter list dynamically allocated */
189 - unsigned char reset; /* if getopts has been reset */
190 char **p; /* parameter list */
191 - char **optnext; /* next parameter to be processed by getopts */
192 - char *optptr; /* used by getopts */
193 + int optind; /* next parameter to be processed by getopts */
194 + int optoff; /* used by getopts */