diff options
author | (no author) | 2006-01-10 13:51:39 +0000 |
---|---|---|
committer | (no author) | 2006-01-10 13:51:39 +0000 |
commit | d7960adb0c52a7abb2af932d1e7af98b980c8642 (patch) | |
tree | 7b995c5f5d12d102735a562364ca1a6ca50a29bc /src/getopt.c | |
parent | deaa6a5ec82c1d158caf360fd12672f72d7dae5b (diff) | |
download | ivy-c-d7960adb0c52a7abb2af932d1e7af98b980c8642.zip ivy-c-d7960adb0c52a7abb2af932d1e7af98b980c8642.tar.gz ivy-c-d7960adb0c52a7abb2af932d1e7af98b980c8642.tar.bz2 ivy-c-d7960adb0c52a7abb2af932d1e7af98b980c8642.tar.xz |
This commit was manufactured by cvs2svn to create branch 'protocol_v3'.
Diffstat (limited to 'src/getopt.c')
-rwxr-xr-x | src/getopt.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/getopt.c b/src/getopt.c new file mode 100755 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; +} |