diff options
author | tissoire | 2007-12-20 13:33:47 +0000 |
---|---|---|
committer | tissoire | 2007-12-20 13:33:47 +0000 |
commit | 98f62bc78586332320db02686c2453d3dd3a6f31 (patch) | |
tree | 438bcaaf3851236dfa1859c1e982853c8c911175 /src/getopt.c | |
download | ivypointer-98f62bc78586332320db02686c2453d3dd3a6f31.zip ivypointer-98f62bc78586332320db02686c2453d3dd3a6f31.tar.gz ivypointer-98f62bc78586332320db02686c2453d3dd3a6f31.tar.bz2 ivypointer-98f62bc78586332320db02686c2453d3dd3a6f31.tar.xz |
initial release of ivypointer
Diffstat (limited to 'src/getopt.c')
-rw-r--r-- | src/getopt.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/getopt.c b/src/getopt.c new file mode 100644 index 0000000..36a9bc1 --- /dev/null +++ b/src/getopt.c @@ -0,0 +1,76 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +int optind = 1; +char *optarg = (char *)NULL; + +int getopt (int argc, char **argv, char *optstring) +{ + int cur_option; /* Current option */ + char *cp; /* Character pointer */ + static int GetOptionPosition = 1; + + if (GetOptionPosition == 1) + { + +/* Check for out of range, correct start character and not single */ + + if ((optind >= argc) || (*argv[optind] != '-') || !argv[optind][1]) + return EOF; + + if (!strcmp (argv[optind], "--")) + return EOF; + } + +/* Get the current character from the current argument vector */ + + cur_option = argv[optind][GetOptionPosition]; + +/* Validate it */ + + if ((cur_option == ':') || + ((cp = strchr (optstring, cur_option)) == (char *)NULL)) + { + +/* Move to the next offset */ + + if (!argv[optind][++GetOptionPosition]) + { + optind++; + GetOptionPosition = 1; + } + + return '?'; + } + +/* Parameters following ? */ + + optarg = (char *)NULL; + + if (*(++cp) == ':') + { + if (argv[optind][GetOptionPosition + 1]) + optarg = &argv[optind++][GetOptionPosition + 1]; + + else if (++optind >= argc) + { + optarg = (char *)NULL; + GetOptionPosition = 1; + return '?'; + } + + else + optarg = argv[optind++]; + + GetOptionPosition = 1; + } + + else if (!argv[optind][++GetOptionPosition]) + { + GetOptionPosition = 1; + optind++; + } + + return cur_option; +} |