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>
char *optarg;
......@@ -16,32 +17,44 @@ int getopt(int argc, char * const argv[], const char *optstring) {
static int nextchar = 0;
char *arg;
const char *theopt;
// first time initialization
if (nextargc == 0) {
nextargc = 1;
optind = 1;
nextchar = 0;
}
again:
optarg = 0;
if (nextargc >= argc) {
nextargc = 0;
optind = nextargc;
// last arg
if (nextargc >= argc)
return -1;
}
arg = argv[nextargc];
if (!arg[nextchar]) {
nextargc++;
nextchar = 0;
goto again;
}
if (!nextchar) {
arg = argv[nextargc];
if (arg[0] != '-') {
nextargc = 0;
// not an option
if (arg[0] != '-')
return -1;
// end of options "--"
if (arg[1] == '-') {
nextargc++;
optind = nextargc;
return -1;
}
nextchar = 1;
}
// try to match the arg with the options
theopt = match(arg[nextchar++], optstring);
if (!theopt) {
nextargc++;
nextchar = 0;
goto again;
}
if (!theopt)
return '?';
if (theopt[1] == ':') {
if (arg[nextchar]) {
optarg = &arg[nextchar];
......@@ -57,7 +70,6 @@ again:
optarg = argv[nextargc++];
}
}
optind = nextargc;
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