52 int getopt(
int argc,
char*
const argv[],
const char* optstring) {
54 const char* optdecl =
NULL;
62 goto no_more_optchars;
67 goto no_more_optchars;
72 goto no_more_optchars;
76 if (strcmp(argv[
optind],
"-") == 0)
77 goto no_more_optchars;
81 if (strcmp(argv[optind],
"--") == 0) {
83 goto no_more_optchars;
98 optdecl = strchr(optstring, optchar);
102 if (optdecl[1] ==
':') {
110 if (optdecl[2] !=
':') {
121 if (++optind < argc) {
129 optchar = (optstring[0] ==
':') ?
':' :
'?';
158 int getopt_long(
int argc,
char*
const argv[],
const char* optstring,
163 size_t argument_name_length = 0;
164 const char* current_argument =
NULL;
173 if (strlen(argv[
optind]) < 3 || strncmp(argv[optind],
"--", 2) != 0)
174 return getopt(argc, argv, optstring);
177 current_argument = argv[
optind] + 2;
178 argument_name_length = strcspn(current_argument,
"=");
179 for (; o->
name; ++o) {
180 if (strncmp(o->
name, current_argument, argument_name_length) == 0) {
186 if (num_matches == 1) {
190 *longindex = (int) (match - longopts);
199 retval = match->
flag ? 0 : match->
val;
202 optarg = strchr(argv[optind],
'=');
216 }
else if (strchr(argv[optind],
'=')) {
const int optional_argument
int getopt(int argc, char *const argv[], const char *optstring)
const int required_argument
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)