Commit 0bd72715 authored by Rich Prohaska's avatar Rich Prohaska Committed by Yoni Fogel

fix the getopt parser. closes #1747

git-svn-id: file:///svn/toku/tokudb@11758 c7de825b-a66e-492c-adef-691d508d4ae1
parent bb05ac34
...@@ -12,38 +12,53 @@ static const char *match(char c, const char *optstring) { ...@@ -12,38 +12,53 @@ static const char *match(char c, const char *optstring) {
} }
int getopt(int argc, char * const argv[], const char *optstring) { int getopt(int argc, char * const argv[], const char *optstring) {
static int lastargc = 0; static int nextargc = 0;
static int nextchar = 0;
char *arg; char *arg;
const char *theopt; const char *theopt;
if (lastargc == 0) { if (nextargc == 0) {
lastargc = 1; nextargc = 1;
optind = 1; optind = 1;
nextchar = 0;
} }
again:
optarg = 0; optarg = 0;
if (lastargc >= argc) { if (nextargc >= argc) {
lastargc = 0; nextargc = 0;
return -1; return -1;
} }
arg = argv[lastargc++]; arg = argv[nextargc];
if (!nextchar) {
arg = argv[nextargc];
if (arg[0] != '-') { if (arg[0] != '-') {
lastargc = 0; nextargc = 0;
return -1; return -1;
} }
theopt = match(arg[1], optstring); nextchar = 1;
}
theopt = match(arg[nextchar++], optstring);
if (!theopt) { if (!theopt) {
lastargc = 0; nextargc++;
return -1; nextchar = 0;
goto again;
} }
if (theopt[1] == ':') { if (theopt[1] == ':') {
if (arg[2]) if (arg[nextchar]) {
optarg = &arg[2]; optarg = &arg[nextchar];
else if (lastargc >= argc) { nextargc++;
lastargc = 0; nextchar = 0;
} else if (nextargc >= argc) {
nextargc++;
nextchar = 0;
return -1; return -1;
} else } else {
optarg = argv[lastargc++]; nextargc++;
nextchar = 0;
optarg = argv[nextargc++];
} }
optind = lastargc; }
optind = nextargc;
return theopt[0]; return theopt[0];
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment