Commit 4030b18b authored by Rich Prohaska's avatar Rich Prohaska Committed by Yoni Fogel

fix optind in the windows getopt. addresses #1747

git-svn-id: file:///svn/toku/tokudb@11784 c7de825b-a66e-492c-adef-691d508d4ae1
parent 0bd72715
#include <stdio.h>
#include <unistd.h> #include <unistd.h>
char *optarg; char *optarg;
...@@ -16,32 +17,44 @@ int getopt(int argc, char * const argv[], const char *optstring) { ...@@ -16,32 +17,44 @@ int getopt(int argc, char * const argv[], const char *optstring) {
static int nextchar = 0; static int nextchar = 0;
char *arg; char *arg;
const char *theopt; const char *theopt;
// first time initialization
if (nextargc == 0) { if (nextargc == 0) {
nextargc = 1; nextargc = 1;
optind = 1; optind = 1;
nextchar = 0; nextchar = 0;
} }
again: again:
optarg = 0; optarg = 0;
if (nextargc >= argc) { optind = nextargc;
nextargc = 0; // last arg
if (nextargc >= argc)
return -1; return -1;
}
arg = argv[nextargc]; arg = argv[nextargc];
if (!arg[nextchar]) {
nextargc++;
nextchar = 0;
goto again;
}
if (!nextchar) { if (!nextchar) {
arg = argv[nextargc]; // not an option
if (arg[0] != '-') { if (arg[0] != '-')
nextargc = 0; return -1;
// end of options "--"
if (arg[1] == '-') {
nextargc++;
optind = nextargc;
return -1; return -1;
} }
nextchar = 1; nextchar = 1;
} }
// try to match the arg with the options
theopt = match(arg[nextchar++], optstring); theopt = match(arg[nextchar++], optstring);
if (!theopt) { if (!theopt)
nextargc++; return '?';
nextchar = 0;
goto again;
}
if (theopt[1] == ':') { if (theopt[1] == ':') {
if (arg[nextchar]) { if (arg[nextchar]) {
optarg = &arg[nextchar]; optarg = &arg[nextchar];
...@@ -57,7 +70,6 @@ int getopt(int argc, char * const argv[], const char *optstring) { ...@@ -57,7 +70,6 @@ int getopt(int argc, char * const argv[], const char *optstring) {
optarg = argv[nextargc++]; optarg = argv[nextargc++];
} }
} }
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