123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- #include "getopt.h"
- #include <stddef.h>
- #include <string.h>
- const int no_argument = 0;
- const int required_argument = 1;
- const int optional_argument = 2;
- char* optarg;
- int optopt;
- int optind = 1;
- int opterr;
- static char* optcursor = NULL;
- int getopt(int argc, char* const argv[], const char* optstring) {
- int optchar = -1;
- const char* optdecl = NULL;
- optarg = NULL;
- opterr = 0;
- optopt = 0;
-
- if (optind >= argc)
- goto no_more_optchars;
-
- if (argv[optind] == NULL)
- goto no_more_optchars;
-
- if (*argv[optind] != '-')
- goto no_more_optchars;
-
- if (strcmp(argv[optind], "-") == 0)
- goto no_more_optchars;
-
- if (strcmp(argv[optind], "--") == 0) {
- ++optind;
- goto no_more_optchars;
- }
- if (optcursor == NULL || *optcursor == '\0')
- optcursor = argv[optind] + 1;
- optchar = *optcursor;
-
- optopt = optchar;
-
- optdecl = strchr(optstring, optchar);
- if (optdecl) {
-
- if (optdecl[1] == ':') {
- optarg = ++optcursor;
- if (*optarg == '\0') {
-
- if (optdecl[2] != ':') {
-
- if (++optind < argc) {
- optarg = argv[optind];
- } else {
-
- optarg = NULL;
- optchar = (optstring[0] == ':') ? ':' : '?';
- }
- } else {
- optarg = NULL;
- }
- }
- optcursor = NULL;
- }
- } else {
-
- optchar = '?';
- }
- if (optcursor == NULL || *++optcursor == '\0')
- ++optind;
- return optchar;
- no_more_optchars:
- optcursor = NULL;
- return -1;
- }
- int getopt_long(int argc, char* const argv[], const char* optstring,
- const struct option* longopts, int* longindex) {
- const struct option* o = longopts;
- const struct option* match = NULL;
- int num_matches = 0;
- size_t argument_name_length = 0;
- const char* current_argument = NULL;
- int retval = -1;
- optarg = NULL;
- optopt = 0;
- if (optind >= argc)
- return -1;
- if (strlen(argv[optind]) < 3 || strncmp(argv[optind], "--", 2) != 0)
- return getopt(argc, argv, optstring);
-
- current_argument = argv[optind] + 2;
- argument_name_length = strcspn(current_argument, "=");
- for (; o->name; ++o) {
- if (strncmp(o->name, current_argument, argument_name_length) == 0) {
- match = o;
- ++num_matches;
- }
- }
- if (num_matches == 1) {
-
- if (longindex)
- *longindex = (int) (match - longopts);
-
- if (match->flag)
- *(match->flag) = match->val;
- retval = match->flag ? 0 : match->val;
- if (match->has_arg != no_argument) {
- optarg = strchr(argv[optind], '=');
- if (optarg != NULL)
- ++optarg;
- if (match->has_arg == required_argument) {
-
- if (optarg == NULL && ++optind < argc) {
- optarg = argv[optind];
- }
- if (optarg == NULL)
- retval = ':';
- }
- } else if (strchr(argv[optind], '=')) {
-
- retval = '?';
- }
- } else {
-
- retval = '?';
- }
- ++optind;
- return retval;
- }
|