]> git.pld-linux.org Git - packages/ash.git/blame - ash-getopt.patch
- removed uclibc stuff
[packages/ash.git] / ash-getopt.patch
CommitLineData
cf7098dd
MM
1diff -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
4@@ -79,7 +79,7 @@
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 *));
10
11
12 /*
13@@ -118,7 +118,8 @@
14 arg0 = *argptr++;
15
16 shellparam.p = argptr;
17- shellparam.reset = 1;
18+ shellparam.optind = 1;
19+ shellparam.optoff = -1;
20 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
21 while (*argptr) {
22 shellparam.nparam++;
23@@ -282,7 +283,8 @@
24 shellparam.malloc = 1;
25 shellparam.nparam = nparam;
26 shellparam.p = newparam;
27- shellparam.optnext = NULL;
28+ shellparam.optind = 1;
29+ shellparam.optoff = -1;
30 }
31
32
33@@ -330,7 +332,8 @@
34 }
35 ap2 = shellparam.p;
36 while ((*ap2++ = *ap1++) != NULL);
37- shellparam.optnext = NULL;
38+ shellparam.optind = 1;
39+ shellparam.optoff = -1;
40 INTON;
41 return 0;
42 }
43@@ -363,10 +366,8 @@
44 getoptsreset(value)
45 const char *value;
46 {
47- if (number(value) == 1) {
48- shellparam.optnext = NULL;
49- shellparam.reset = 1;
50- }
51+ shellparam.optind = number(value);
52+ shellparam.optoff = -1;
53 }
54
55 /*
56@@ -385,50 +386,58 @@
57
58 if (argc < 3)
59 error("Usage: getopts optstring var [arg]");
60- else if (argc == 3)
61+ else if (argc == 3) {
62 optbase = shellparam.p;
63- else
64+ if (shellparam.optind > shellparam.nparam + 1) {
65+ shellparam.optind = 1;
66+ shellparam.optoff = -1;
67+ }
68+ }
69+ else {
70 optbase = &argv[3];
71-
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;
79+ }
80 }
81
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);
86 }
87
88 STATIC int
89-getopts(optstr, optvar, optfirst, optnext, optpptr)
90+getopts(optstr, optvar, optfirst, optind, optoff)
91 char *optstr;
92 char *optvar;
93 char **optfirst;
94- char ***optnext;
95- char **optpptr;
96+ int *optind;
97+ int *optoff;
98 {
99 char *p, *q;
100 char c = '?';
101 int done = 0;
102- int ind = 0;
103 int err = 0;
104 char s[10];
105+ char **optnext = optfirst + *optind - 1;
106
107- if ((p = *optpptr) == NULL || *p == '\0') {
108+ if (*optind <= 1 || *optoff < 0 || !(*(optnext - 1)) ||
109+ strlen(*(optnext - 1)) < *optoff)
110+ p = NULL;
111+ else
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)
117 return 1;
118- p = **optnext;
119+ p = *optnext;
120 if (p == NULL || *p != '-' || *++p == '\0') {
121 atend:
122- ind = *optnext - optfirst + 1;
123- *optnext = NULL;
124+ *optind = optnext - optfirst + 1;
125 p = NULL;
126 done = 1;
127 goto out;
128 }
129- (*optnext)++;
130+ optnext++;
131 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
132 goto atend;
133 }
134@@ -453,7 +462,7 @@
135 }
136
137 if (*++q == ':') {
138- if (*p == '\0' && (p = **optnext) == NULL) {
139+ if (*p == '\0' && (p = *optnext) == NULL) {
140 if (optstr[0] == ':') {
141 s[0] = c;
142 s[1] = '\0';
143@@ -468,30 +477,29 @@
144 goto bad;
145 }
146
147- if (p == **optnext)
148- (*optnext)++;
149+ if (p == *optnext)
150+ optnext++;
151 setvarsafe("OPTARG", p, 0);
152 p = NULL;
153 }
154 else
155 setvarsafe("OPTARG", "", 0);
156- ind = *optnext - optfirst + 1;
157+ *optind = optnext - optfirst + 1;
158 goto out;
159
160 bad:
161- ind = 1;
162- *optnext = NULL;
163+ *optind = 1;
164 p = NULL;
165 out:
166- *optpptr = p;
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);
171 s[0] = c;
172 s[1] = '\0';
173 err |= setvarsafe(optvar, s, 0);
174 if (err) {
175- *optnext = NULL;
176- *optpptr = NULL;
177+ *optind = 1;
178+ *optoff = -1;
179 flushall();
180 exraise(EXERROR);
181 }
182diff -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
185@@ -41,10 +41,9 @@
186 struct shparam {
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 */
195 };
196
197
198
This page took 1.418731 seconds and 4 git commands to generate.